c语言编程练习都为本人学习时的编写代码记录或学习笔记,若有不当之处欢迎指正,感激不尽(其中题型当然可能有其他编程设计方案,比如利用自定义函数等,因为我在操练基础,所以就不涉及,仅作记录)。
1.编写一个程序。该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。
#include
int main(void)
{
char ch;
int sp, li, t_or; //space ,line , total_others
t_or = li = sp = 0;
printf("Please input a string end by '#':\n");
while((ch = getchar()) != '#') {
if(ch == ' ')
sp++;
else if(ch == '\n')
li++;
else
t_or++;
}
printf("space=%d,line=%d,total_other=%d\n", sp, li, t_or);
return 0;
}
2.编写一个程序,该程序读取输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCII 码。每行打印 8 个字符,编码对。建议:利用字符计数和模运算符(%)在每 8 个循环周期时打印一个换行符。
//for 循环
#include
int main(void)
{
char ch;
int co = 0; //count
printf("Please input a string end by '#':\n");
for(co = 1;(ch = getchar()) != '#'; co++) {
printf("%c,%d|| ", ch, ch);
if(co % 8 == 0)
printf("\n");
}
printf("\n");
return 0;
}
//while 循环
#include
int main(void)
{
char ch;
int co = 0;
printf("Please input a string end by '#':\n");
while((ch = getchar()) != '#') {
co++;
printf("%c,%d|| ", ch, ch);
if(co % 8 == 0)
printf("\n");
}
printf("\n");
return 0;
}
3.编写一个程序。该程序读取整数,直到输入 0。输入终止后,程序应该报告输入的偶数(不包括0)总个数、偶数的平均值,输入的奇数总个数以及奇数的平均值。
#include
int main(void)
{
int e, s_e, o, s_o, num;
s_o = s_e = o = e = 0;
//e-even(偶),o-odd(奇),s_e-sum_even,s_o-sum_odd
printf("Please input numbers(0 to quit!):\n");
while(scanf("%d", &num) == 1) {
if(num == 0)
break;
if(num % 2 == 0) {
e++;
s_e += num;
}
else {
o++;
s_o += num;
}
}
printf("e =%d,", e);
printf("s_e / e=%.1f\n",1.0 * s_e / e);
printf("o =%d,", o);
printf("s_o / o=%.1f\n",1.0 * s_o / o);
return 0;
}
4.利用 if else 语句编写程序读取输入,直到#。用一个感叹号代替每个句号,将原有的每个感叹号用两个感叹号代替,最后报告进行了多少次替代。
#include
int main(void)
{
int i = 0, j = 0;
char ch;
printf("Please input a string end by '#':\n");
while((ch = getchar()) != '#') {
if(ch == '.') {
putchar('!');
i++;
}
else if(ch == '!') {
putchar('!');
putchar('!');
j++;
}
else
putchar(ch);
}
printf("\n");
printf("'.' times=%d\n", i);
printf("'!' times=%d\n", j);
return 0;
}
5.用 switch 重做练习 3。
#include
int main(void)
{
int e, s_e, o, s_o, num;
s_o = s_e = o = e = 0;
//e-even(偶),o-odd(奇),s_e-sum_even,s_o-sum_odd
printf("Please input numbers(0 to quit!):\n");
while(scanf("%d", &num) == 1) {
if(num == 0)
break;
switch(num % 2) {
case 0: e++;
s_e += num;
break;
case 1: o++;
s_o += num;
}
}
printf("e =%d,", e);
printf("s_e / e=%.1f\n",1.0 * s_e / e);
printf("o =%d,", o);
printf("s_o / o=%.1f\n",1.0 * s_o / o);
return 0;
}
6.编写一个程序读取输入,直到#,并报告序列 ei 出现的次数。此程序必须要记住前一个字符和当前的字符。用诸如“Receive your eieio award.”的输入测试它。
#include
int main(void)
{
char f, p;
int c = 0;
f = 0;
//f-former,p-present,c-count
printf("Please input some characters: \n");
while((p = getchar()) != '#') {
if((f == 'e') && (p == 'i'))
c++;
f = p;
}
printf("ei has appeared %d times\n", c);
return 0;
}
7.编写程序,要求输入一周中的工作小时数,然后打印工资总额、税金以及净工资。作如下假设:
a.基本工资等级=10.00 美元//J,时
b.加班(超过 40 小时)=1.5 倍的时间
c.税率前 300 美元为 15%
下一个 150 美元为 20%
余下的为 25%
用#define 定义常量,不必关心本例是否符合当前的税法。
#include
#define BASIC 10.00 //基本工资等级
#define TIME 40 //正常工作时间
#define ADD 1.5 //加班的折算时间倍数
#define L1 300 //工资额度1
#define RATE1 0.15 //L1的税率
#define L2 150 //工资额度2
#define RATE2 0.20 //L2的税率
#define RATE3 0.25 //余下工资额度的税率
int main(void)
{
double h, g, t;
//h-hours,g-gross,t-tax
printf("input the work hours of a week:\n");
scanf("%lf", &h);
if(h > 40)
h = 40 + (h - 40) * ADD;
g = h * BASIC;
if(g <= L1)
t = g * RATE1;
else if(g <= L2)
t = g * RATE1 + (g - L1) * RATE2;
else
t = g * RATE1 + (g - L1) * RATE2 + (g - L1 - L2) * RATE3;
printf("gross income = %lf\n", g);
printf("tax = %lf\n", t);
printf("net income = %lf\n", g - t);
return 0;
}
8.修改练习 7 中的假设 a,使程序提供一个选择工资等级的菜单。用 switch 选择工资等级。程序运行的开头应该像这样:
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4)$11.20/hr
5)quit
如果选择 l 到 4.那么程序应该请求输入工作小时数。程序应该一直循环运行,直到输入 5。如果输入 l 到 5 以外的选项,那么程序应该提醒用户合适的选项是哪些,然后再循环。用#define 为各种工资等级和税率定义常量。
#include
#define TIME 40 //正常工作时间
#define ADD 1.5 //加班的折算时间倍数
#define L1 300 //工资额度1
#define RATE1 0.15 //L1的税率
#define L2 150 //工资额度2
#define RATE2 0.20 //L2的税率
#define RATE3 0.25 //余下工资额度的税率
int main(void)
{
double h, g, t, BASIC;
int n, d;
//h-hours,g-gross,t-tax,d-data
for(d = 0; d < 69; d++)
printf("*");
printf("\n\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n\n");
for(d = 0; d < 69; d++)
printf("*");
printf("\n");
while(1) {
d = scanf("%d", &n);
while(d != 1 || n < 1 || n > 5) {
getchar(); //输入非数字字符时,将其从输入流里取出,避免无法进行下一次读取
printf("error,it's wrong number,please input again!(1-5):\n");
d = scanf("%d", &n);
}
switch(n) {
case 1: BASIC = 8.75;
break;
case 2: BASIC = 9.33;
break;
case 3: BASIC = 10.00;
break;
case 4: BASIC = 11.20;
break;
case 5: printf("quit!\n");
return 0;
}
printf("input the work hours of a week:\n");
scanf("%lf", &h);
if(h > 40)
h = 40 + (h - 40) * ADD;
g = h * BASIC;
if(g <= L1)
t = g * RATE1;
else if(g <= L2)
t = g * RATE1 + (g - L1) * RATE2;
else
t = g * RATE1 + (g - L1) * RATE2 + (g - L1 - L2) * RATE3;
printf("gross income = %lf\n", g);
printf("tax = %lf\n", t);
printf("net income = %lf\n", g - t);
printf("Please input your selection(1~4) again!(type 5 to quit).\n");
}
return 0;
}
在做这题时,出现了一个问题如下:
问题:while循环中,当scanf函数控制输入格式为%d类型,当输入如字符类型等非数字字符,会出现死循环。
原因:因为你要读取的是数字,输入一个字符时, scanf函数会失败,你输入的字符进入了输入流,但是这个字符还在流里面没有被读出来,所以就死循环动不了了;scanf如果输入不匹配的话,是不会把那个字符取走的,所以下次scanf还会遇到那个字符,因为getchar()函数是读字符的,getchar的作用就是把它取走;所以加个getchar()函数就ok了。
我也看了一下是不是要清除输入缓存,结果发现在GCC上不行,原因如下:
fflush(stdin);这个是未定义行为,C标准没有规定stdin能够用fflush清空。VC上可以、TC上可以,那个是编译器扩展,GCC就不行。
标准的清空输入缓存的方法是:
int c;
while ( (c = getchar()) != ‘\n’ && c != EOF ) ;
或者
scanf(” %c”,&a);
把这个放在scanf前面就行了。
9.编写一个程序,接受一个整数输入,然后显示所有小于或等于该数的素数。
#include
int main(void)
{
int n, i, j, f;
printf("Please input a integer:\n");
scanf("%d", &n);
printf("all the primes <= %d as follow:\n", n);
for( i = 1; i <= n; i++) {
f = 1; //标记
for(j = 2; j < i; j++) {
if(i % j == 0)
f = 0;
}
if(f == 1 && i != 1) {
printf("%d\t", i);
}
}
printf("\n");
return 0;
}
10. 1988 年 United States Federal Tax Schedule 是近期最基本的。它分为 4 类,每类有两个等级。下面是其摘要;美元数为应征税的收入。
例如,有 20 000 美元应征税收入的单身雇佣劳动者应缴税金 0.15×17 850 美元+0.28×(20 000美元-17 850 美元)。编写一个程序,让用户指定税金种类和应征税收入,然后计算税金。使用循环以便用户可以多次输入。
#include
#define SINGLE 17850 //单身限额
#define HOST 23900 //户主限额
#define M_S 29750 //MARRIED_SHARE,已婚共有的限额
#define M_D 14875 //MARRIED_DIVORCE,已婚离异的限额
#define RATE1 0.15 //税率1
#define RATE2 0.28 //税率2
int main(void)
{
double ty, p, ta;
char ch;
//ty-type,p-pay,ta-tax;分别为类型、收入、征税
while(1) {
printf("Please Select the type of marriage:\n");
printf("1) SINGLE\t\t\t2) HOST\t\t\t3) MARRIED_SHARE\n");
printf("4) MARRIED_DOVORCE\t\t5) quit\n");
while((ch = getchar()) =='\n') continue; //跳过回车
switch(ch) {
case '1' : ty = SINGLE;
break;
case '2' : ty = HOST;
break;
case '3' : ty = M_S;
break;
case '4' : ty = M_D;
break;
case '5' : printf("quit!\n");
return 0; //退出
default : printf("error,please input again!\n");
continue;
}
printf("you have select %c\n", ch);
printf("Please input the pay:\n");
scanf("%lf", &p);
if(p <= ty)
ta = p * RATE1;
else
ta = ty * RATE1 + (p - ty) * RATE2;
printf("tax payalbe = %.2lf\n", ta);
}
return 0;
}
11. ABC Mail Order Grocery 朝鲜蓟的售价是 1.25 美元/磅,甜菜的售价是 0.65 美元/磅,胡萝卜的售价是 0.89 美元/磅。在添加运输费用之前,他们为 100 美元的订单提供 5%的打折优惠。对 5 磅或以下的定单收取 3.50 美元的运输和装卸费用;超过 5 磅而不足 20 磅的定单收取 1O.OO 美元的运输和装卸费用:加磅或以上的运输,在 8 美元基础上每磅加收 0.1 美元。编写程序,在循环中使用 switch 语句,以便对输入 a 的响应是让用户输入所需的朝鲜蓟磅数,b 为甜菜的磅数,c 为胡萝卜的磅数,而 q 允许用户退出订购过程。然后程序计算总费用、折扣和运输费用(如果有运输费的话),以及总数。随后程序应该显示所有的购买信息:每磅的费用、订购的磅数、该订单每种蔬菜的费用、订单的总费用、折扣,如果有的话加上运输费用,以及所有费用的总数。
#include
#include //需从其中调用tolower函数(将给定字母转换为小写)
#define ART_E 1.25 //ARTICHOKE
#define BEET 0.65
#define CARROT 0.89
#define DIS_L 100 //DISCOUNT_LIMIT
#define DIS_R 0.05 //DISCOUNT_RATE
#define FRE_F1 3.50 //FREIGHT_FEE1
#define FRE_L1 1.5 //FREIGHT_LIMIT1
#define FRE_F2 10.00 //FREIGHT_FEE2
#define FRE_L2 20 //FREIGHT_LIMIT2
#define FRE_F3 8 //FREIGHT_FEE3
#define FRE_R 0.1 //FREIGHT_RATE
int main(void)
{
char ch;
double art_e = 0, beet = 0, carrot = 0; //磅数, ar_e=artichoke
double sum, dis_ct, fre_t;
printf("Please select your vegetable(a, b, c , q(quit!):\n");
printf("a.artichoke price:$%.2f\n",ART_E);
printf("b.beet price:$%.2f\n",BEET);
printf("c.carrot price:$%.2f\n",CARROT);
printf("q.quit\n");
printf("(price as dollars per pound):\n");
while((ch = tolower(getchar())) != 'q') {
switch(ch) {
case 'a': printf("How many pounds of artichokes do you want?\n");
scanf("%lf",&art_e);
printf("Please select your vegetable: a,b,c,q:\n");
continue;
case 'b': printf("How many pounds of beets do you want?\n");
scanf("%lf",&beet);
printf("Please select your vegetable: a,b,c,q:\n");
continue;
case 'c': printf("How many pounds of carrots do you want?\n");
scanf("%lf",&carrot);
printf("Please select your vegetable: a,b,c,q:\n");
continue;
default: break;
}
}
printf("%10s%10s%10s%10s\n"," ","artichoke","beet","carrot");
printf("%10s%10.2lf%10.2lf%10.2lf\n","price",ART_E,BEET,CARROT);
printf("%10s%10.2lf%10.2lf%10.2lf\n","pound",art_e,beet,carrot);
printf("%10s%10.2lf%10.2lf%10.2lf\n","charge",ART_E * art_e,BEET * beet,CARROT * carrot);
sum = ART_E * art_e + BEET * beet + CARROT * carrot;
if (sum > DIS_L)
dis_ct = sum*DIS_R;
else
dis_ct = 0;
printf("discount: %.2f\n",dis_ct);
if (art_e + beet + carrot <= 5)
fre_t = 3.50;
else if (art_e + beet + carrot <20)
fre_t = 10;
else
fre_t = 8 + (art_e + beet + carrot) * 0.1;
printf("freight: %.2f\n",fre_t);
sum = sum - dis_ct + fre_t;
printf("sum: %.2f\n",sum);
return 0;
}