c primer plus 第六版 第七单元
前提:在ubuntu(17.0.0)中使用gcc(11.4.0)编译,以伪代码形式展示。
//所写的代码仅为阅读者提供参考;
//若有不足之处请提出,本人会尽所能修改;
7.12 编程练习
1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、
换行符数和所有其他字符的数量。
void readChar(void)
{
char ch;
int ret,space = 0,other = 0,breaks = 0;
while((ret = scanf("%c",&ch)) != 0)
{
if(' '== ch)
{
space++;
}
else if(10 == ch)
{
breaks++;
}
else if('#' == ch)
{
break;
}
else
{
other++;
}
}
printf("space %d, breaks %d ,other %d.\n",space,breaks,other);
}
结果:
arere
eargtear aergytegtrs#
space 1, breaks 1 ,other 24.
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字
符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数
和求模运算符(%)在每8个循环周期时打印一个换行符。
void readChar1(void)
{
char ch;
int ret,flag = 0;
while((ret = scanf("%c",&ch)) != 0)
{
if(flag % 8 == 0)
{
printf("\n");
}
flag++;
if('#' == ch)
{
break;
}
printf("ch %c ,%d",ch,ch);
}
printf("bye\n");
}
结果:
klsjfoirhfje klrijve
ch k ,107ch l ,108ch s ,115ch j ,106ch f ,102ch o ,111ch i ,105ch r ,114
ch h ,104ch f ,102ch j ,106ch e ,101ch ,32ch k ,107ch l ,108ch r ,114
ch i ,105ch j ,106ch v ,118ch e ,101ch
,10vmeklihfveakl
ch v ,118ch m ,109ch e ,101
ch k ,107ch l ,108ch i ,105ch h ,104ch f ,102ch v ,118ch e ,101ch a ,97
ch k ,107ch l ,108ch ,32ch
,10adekfvlkeijviowahfcw efrlekaijovoiera fvceklvjiiesv
ch a ,97ch d ,100ch e ,101ch k ,107
ch f ,102ch v ,118ch l ,108ch k ,107ch e ,101ch i ,105ch j ,106ch v ,118
ch i ,105ch o ,111ch w ,119ch a ,97ch h ,104ch f ,102ch c ,99ch w ,119
ch ,32ch e ,101ch f ,102ch r ,114ch l ,108ch e ,101ch k ,107ch a ,97
ch i ,105ch j ,106ch o ,111ch v ,118ch o ,111ch i ,105ch e ,101ch r ,114
ch a ,97ch ,32ch f ,102ch v ,118ch c ,99ch e ,101ch k ,107ch l ,108
ch v ,118ch j ,106ch i ,105ch i ,105ch e ,101ch s ,115ch v ,118ch
,10#
bye
3.编写一个程序,读取整数直到用户输入 0。输入结束后,程序应报告
用户输入的偶数(不包括 0)个数、这些偶数的平均值、输入的奇数个数及
其奇数的平均值。
void count(void)
{
int odd = 0, even = 0,num,ret;
float Oave = 0,Eave = 0;
while((ret = scanf("%d",&num)) != 0)
{
if(num == 0)
{
Eave /= even;
Oave /= odd;
break;
}
else if(num % 2 == 0)
{
even++;
Eave += num;
}
else
{
odd++;
Oave += num;
}
}
printf("odd %d ,odd average %.3f,even %d, even average %.3f\n",odd,Oave,even,Eave);
}
结果:
1 2 3 3 0
odd 3 ,odd average 2.333,even 1, even average 2.000
4.使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句
号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
void replace(void)
{
int time = 0,ret;
char ch;
while((ret = scanf("%c",&ch)) != 0)
{
if(ch == '#')
break;
if(ch == '.')
{
ch = '!';
time++;
}
if(ch == '!')
{
time++;
}
}
printf("time = %d\n",time);
}
结果:
kjhuidhvcjs.jkdsnhcvueiwv.fklwejhvcuei.#
time = 6
5.使用switch重写练习4。
void replace1(void)
{
int time = 0,ch;
while((ch = getchar()) != '#')
{
switch(ch)
{
case '.':putchar('!');
time++;
break;
case '!':printf("!!");
time++;
break;
default :putchar(ch);
}
}
printf("time = %d\n",time);
}
结果:
jmjkdjvcio
jmjkdjvcio
mklcw.
mklcw!
!
!!
time = 2
6.编写程序读取输入,读到#停止,报告ei出现的次数。
注意
该程序要记录前一个字符和当前字符。用“Receive your eieio award”这
样的输入来测试。
void test(void)
{
int ret,time = 0,ch,tmp;
while((ch = getchar()) != '#')
{
if((ch == 'i') && (tmp == 'e'))
time++;
tmp = ch;
}
printf("time = %d\n",time);
}
结果:
receive your eieio award
time = 3
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总
额、税金和净收入。做如下假设:
a.基本工资 = 1000美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15%
续150美元为20%
494
余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法。
define INCOME(x) (x > 40?(1000 * x+(x-40) 1.51000):1000*x)
define TAX(x) (300 * 0.15 + (INCOME(x)-300)*0.2)
define NET_INCOME(x) (INCOME(x)-TAX(x))
void wages(void)
{
int hour,ret;
printf("please enter week worktime(h) 😊;
ret = scanf("%d",&hour);
if(ret == 0)
printf("scanf() input error.\n");
printf("hour %d, %f ,%f, %f\n",hour,INCOME(hour),TAX(hour),NET_INCOME(hour));
}
结果:
please enter week worktime(h) :12
hour 12, 12000.000000 ,2385.000000, 9615.000000
please enter week worktime(h) :41
hour 41, 42500.000000 ,8485.000000, 34015.000000
8.修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。使
用switch完成工资等级选择。运行程序后,显示的菜单应该类似这样:
Enter the number corresponding to the desired pay rate or action:
- $8.75/hr 2) $9.33/hr
- $10.00/hr 4) $11.20/hr
- quit
如果选择 1~4 其中的一个数字,程序应该询问用户工作的小时数。程
序要通过循环运行,除非用户输入 5。如果输入 1~5 以外的数字,程序应
提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define
创建符号常量表示各工资等级和税率。
define TYPE1 8.75
define TYPE2 9.33
define TYPE3 10.00
define TYPE4 11.20
define INCOME(x,y) (x > 40?(y * x+(x-40) 1.5y):y*x)
define TAX(x,y) ((INCOME(x,y)>300)?(300 * 0.15 + (INCOME(x,y)-300)0.2):(INCOME(x,y)0.15))
define NET_INCOME(x,y) (INCOME(x,y)-TAX(x,y))
void wages(float hr)
{
int hour,ret;
printf("please enter your work time(h) 😊;
ret = scanf("%d",&hour);
if(ret == 0)
printf("scanf() input error.\n");
printf("hour %d,income %f , tax %f , net income %f\n",hour,INCOME(hour,hr),TAX(hour,hr),NET_INCOME(hour,hr));
}
void wages1(void)
{
int ret,type;
while(1)
{
printf("\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1)$8.75/hr\t2)$9.33/hr\n3)$10.00/hr\t4)$11.20\n5)quit\n");
ret = scanf("%d",&type);
if(ret == 0)
{
printf("scanf() input error.\n");
break;
}
switch(type)
{
case 1: wages(TYPE1);
break;
case 2: wages(TYPE2);
break;
case 3: wages(TYPE3);
break;
case 4: wages(TYPE4);
break;
case 5: return ;
default:printf("please enter correct type (1~5).\n");
}
printf("\n");
}
}
结果:
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
5)quit
3
please enter your work time(h) :40
hour 40,income 400.000000 , tax 65.000000 , net income 335.000000
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
5)quit
5
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的
素数。
void prime(void)
{
int number,ret,i,flag = 0,count = 2;
printf("please enter positive integer:");
ret = scanf("%d",&number);
if(ret == 0)
{
printf("scanf() input error.\n");
}
while(count <= number)
{
for(i = 2;i <= count/2 ;i++)
{
if(count % i == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("%d\n",count);
}
flag = 0;
count++;
}
}
结果:
please enter positive integer:19
2
3
5
7
11
13
17
19
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类
别,每个类别有两个等级。
下面是该税收计划的摘要(美元数为应征税的收入):
例如,一位工资为20000美元的单身纳税人,应缴纳税费
0.15×17850+0.28×(20000−17850)美元。编写一个程序,让用户指定缴纳
税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次
输入。
define TYPE1 17850
define TYPE2 23900
define TYPE3 29750
define TYPE4 14875
define TAX(x,y) ((x>y)?(y0.15+(x-y)0.28):x*0.15)
void tax(float limit)
{
float money,ret;
printf("please enter your work time(h) 😊;
ret = scanf("%f",&money);
if(ret == 0)
printf("scanf() input error.\n");
printf("income %f , tax %f\n",money,TAX(money,limit));
}
void tax1(void)
{
int ret,type;
while(1)
{
printf("\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1)single\t2)$household\n3)$merried\t4)$divorce\n5)quit\n");
ret = scanf("%d",&type);
if(ret == 0)
{
printf("scanf() input error.\n");
break;
}
switch(type)
{
case 1: tax(TYPE1);
break;
case 2: tax(TYPE2);
break;
case 3: tax(TYPE3);
break;
case 4: tax(TYPE4);
break;
case 5: return ;
default:printf("please enter correct type (1~5).\n");
}
printf("\n");
}
}
结果:
Enter the number corresponding to the desired pay rate or action:
1)single 2)$household
3)$merried 4)$divorce
5)quit
3
please enter your work time(h) :300000
income 300000.000000 , tax 80132.500000
Enter the number corresponding to the desired pay rate or action:
1)single 2)$household
3)$merried 4)$divorce
5)quit
5
11.ABC 邮购杂货店出售的洋蓟售价为 2.05 美元/磅,甜菜售价为 1.15
美元/磅,胡萝卜售价为 1.09美元/磅。在添加运费之前,100美元的订单有
5%的打折优惠。少于或等于5磅的订单收取6.5美元的运费和包装费,5磅~
20磅的订单收取14美元的运费和包装费,超过20磅的订单在14美元的基础上
每续重1磅增加0.5美元。编写一个程序,在循环中用switch语句实现用户输
入不同的字母时有不同的响应,即输入a的响应是让用户输入洋蓟的磅数,b
是甜菜的磅数,c是胡萝卜的磅数,q 是退出订购。程序要记录累计的重
量。即,如果用户输入 4 磅的甜菜,然后输入 5磅的甜菜,程序应报告9磅
的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装
费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:
磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装
费,以及所有的费用总额。
define ARTICHOKES 2.05
define BEETS 1.15
define CARROTS 1.09
struct fee_t
{
int Aweight;
int Bweight;
int Cweight;
int Gweight;/good weight/
float Aprice;
float Bprice;
float Cprice;
float Gprice;/good price/
float discount;
float freight;
};
void goods(void)
{
int ret,count;
char ch;
struct fee_t fee;
fee.Aweight = 0;
fee.Bweight = 0;
fee.Cweight = 0;
while(1)
{
printf("**************************************************************************************\n");
printf("enter 'a' for ARTICHOKES, 'b' for beets, 'c' for carrots ,'q' for quit:\n");
ret = scanf("%c",&ch);
if(ret == 0)
{
printf("scanf() input error.\n");
break;
}
switch(ch)
{
case 'a': printf("please enter count : \n");
ret = scanf("%d",&count);
if(ret == 0)
printf("scanf() input error.\n");
fee.Aweight += count;
fee.Aprice = fee.Aweight *ARTICHOKES;
break;
case 'b': printf("please enter count : \n");
ret = scanf("%d",&count);
if(ret == 0)
printf("scanf() input error.\n");
fee.Bweight += count;
fee.Bprice = fee.Bweight *BEETS;
break;
case 'c':printf("please enter count:\n");
ret = scanf("%d",&count);
if(ret == 0)
printf("scanf() input error.\n");
fee.Cweight += count;
fee.Cprice = fee.Cweight *CARROTS;
break;
case 'q': return ;
case 10 : break;
default : printf("please enter correct number.\n");
break;
}
fee.Gweight = fee.Aweight + fee.Bweight + fee.Cweight;
fee.Gprice = fee.Aprice + fee.Bprice + fee.Cprice;
if(fee.Gprice >= 100)
fee.discount = fee.Gprice * 0.05;
else
fee.discount = 0;
if((fee.Gweight > 0) && (fee.Gweight <= 5))
fee.freight = 6.5;
else if((fee.Gweight > 5) &&(fee.Gweight <=20))
fee.freight = 14;
else if(fee.Gweight > 20)
fee.freight = (fee.Gweight - 20)*0.5+14;
printf("artichokes $%.2f , weight %d, price %.2f\n",ARTICHOKES,fee.Aweight,fee.Aprice);
printf("beets $%.2f , weight %d, price %.2f\n",BEETS,fee.Bweight,fee.Cprice);
printf("carrots $%.2f , weight %d, price %.2f\n",CARROTS,fee.Cweight,fee.Cprice);
printf("good price %.2f , discount %.2f , freight %.2f,total price %.2f\n",fee.Gprice,fee.discount,fee.freight,fee.Gprice - fee.discount+fee.freight);
}
}
结果:
enter 'a' for ARTICHOKES, 'b' for beets, 'c' for carrots ,'q' for quit:
a
please enter count :
10
artichokes $2.05 , weight 10, price 20.50
beets $1.15 , weight 0, price 0.00
carrots $1.09 , weight 0, price 0.00
good price 20.50 , discount 0.00 , freight 14.00,total price 34.50
enter 'a' for ARTICHOKES, 'b' for beets, 'c' for carrots ,'q' for quit:
artichokes $2.05 , weight 10, price 20.50
beets $1.15 , weight 0, price 0.00
carrots $1.09 , weight 0, price 0.00
good price 20.50 , discount 0.00 , freight 14.00,total price 34.50
enter 'a' for ARTICHOKES, 'b' for beets, 'c' for carrots ,'q' for quit:
q

浙公网安备 33010602011771号