C Primer Plus(第6版) 第七章 编程练习及参考答案
编译环境:Microsoft visual c++ 2010学习版
备注:本文留作作者自用,如有错误敬请指出
1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数,换行符数和所有其他字符的数量。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ char ch;
int space=0;
int linefeed=0;
int other=0;
printf("请输入:");
while((ch=getchar())!='#')
{
if(ch==' ')
space++;
else if(ch=='\n')
linefeed++;
else
other++;
}
printf("读取了%d个空格,%d个换行符和%d个其他字符",space,linefeed,other);
system("pause");
return 0;
}
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。每行打印8个“字符-ASCII码”组合。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ char ch;
int num;
printf("请输入:");
for(num=1;(ch=getchar())!='#';num++)
{
printf("%c-%d ",ch,ch);
if(num%8==0)
printf("\n");
}
system("pause");
return 0;
}
3.编写一个程序,读取整数直到用户输入0。输入结束后,程序应报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num,odd,even;
float sum_odd=0,sum_even=0;
printf("请输入一些整数(以0结尾):");
scanf("%d",&num);
for(odd=0,even=0;num!=0;scanf("%d",&num))
{
if(num%2==0)
{ even++;
sum_even+=num;
}
else
{ odd++;
sum_odd+=num;
}
}
printf("输入%d个偶数,这些偶数的平均值为%f\n输入%d个奇数,这些奇数的平均值为%f",even,sum_even/even,odd,sum_odd/odd);
system("pause");
return 0;
}
4使用if else语句编写一个程序读取输入,读到#停止,用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ int replace=0;
char ch;
printf("请输入(输入#停止):");
while((ch=getchar())!='#')
{
if (ch=='.')
{
printf("!");
replace++;
}
else if(ch=='!')
{
printf("!!");
replace++;
}
else
printf("%c",ch);
}
printf("\n进行了%d次替换",replace);
system("pause");
return 0;
}
5.使用switch重写练习4
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ int replace=0;
char ch;
printf("请输入(输入#停止):");
while((ch=getchar())!='#')
{
switch(ch)
{
case '.': printf("!");
replace++;
break;
case '!': printf("!!");
replace++;
break;
default: printf("%c",ch);
}
}
printf("\n进行了%d次替换",replace);
system("pause");
return 0;
}
6.编写程序读取输入,读到#停止,报告ei出现的次数。
注意
该程序要记录前一个字符和当前字符。用“Receive your eieio award”这样的输入来测试。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ int time=0;
char ch;
char front='a';
printf("请输入(输入#停止):");
while((ch=getchar())!='#')
{
if(ch=='i')
if(front=='e')
time++;
front=ch;
}
printf("\n出现了%d次ei",time);
system("pause");
return 0;
}
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设:
a.基本工资=10.00美元/小时
b.加班(超过40小时)=1.5倍的时间
c.税率:前300美元为15%
续150美元为20%
余下的为25%
用 #define定义符号常量。不用在意是否符合当前的税法。
#include <stdio.h>
#include <stdlib.h>
#define BASE 10.00
#define MORE 40
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
int main(void)
{ float time,wage,tax;
printf("请输入一周工作的小时数:");
scanf("%f",&time);
if(time<=MORE)
wage=time*BASE;
else
wage=(MORE+(time-MORE)*1.5)*BASE;
if(wage<=BREAK1)
tax=RATE1*wage;
else if(wage>BREAK1&&wage<=BREAK2)
tax=BREAK1*RATE1+(wage-BREAK1)*RATE2;
else
tax=BREAK1*RATE1+150*RATE2+(wage-BREAK2)*RATE3;
printf("\n工资总额:%f\n税金:%f\n净收入:%f",wage,tax,wage-tax);
system("pause");
return 0;
}
8.修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单,使用 switch完成工资等级选择。运行程序后,显示的菜单应该类似这样(忽略7):
7***************************************************************************
Enter the number torrespond: to the destred pay rate or action:
1)$8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5)quit
7***************************************************************************
如果选择1~4其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入5。如果输入1到5以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。
#include <stdio.h>
#include <stdlib.h>
#define BASE1 8.75
#define BASE2 9.33
#define BASE3 10.00
#define BASE4 11.20
#define MORE 40
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
int main(void)
{ float time,wage,tax,BASE;
int choice;
printf("***************************************************************");
printf("\nEnter the number torrespond: to the destred pay rate or action:");
printf("\n1)$8.75/hr 2) $9.33/hr");
printf("\n3) $10.00/hr 4) $11.20/hr");
printf("\n5)quit");
printf("\n***************************************************************\n");
scanf("%d",&choice);
ini: while(choice!=5)
{
switch(choice)
{ case 1:BASE=BASE1;
break;
case 2:BASE=BASE2;
break;
case 3:BASE=BASE3;
break;
case 4:BASE=BASE4;
break;
default :{
printf("请输入正确的选项!");
printf("\n***************************************************************");
printf("\nEnter the number torrespond: to the destred pay rate or action:");
printf("\n1)$8.75/hr 2) $9.33/hr");
printf("\n3) $10.00/hr 4) $11.20/hr");
printf("\n5)quit");
printf("\n***************************************************************\n");
scanf("%d",&choice);
goto ini;
}
}
printf("\n请输入一周工作的小时数:");
scanf("%f",&time);
if(time<=MORE)
wage=time*BASE;
else
wage=(MORE+(time-MORE)*1.5)*BASE;
if(wage<=BREAK1)
tax=RATE1*wage;
else if(wage>BREAK1&&wage<=BREAK2)
tax=BREAK1*RATE1+(wage-BREAK1)*RATE2;
else
tax=BREAK1*RATE1+150*RATE2+(wage-BREAK2)*RATE3;
printf("\n工资总额:%f\n税金:%f\n净收入:%f",wage,tax,wage-tax);
}
system("pause");
return 0;
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(void)
{ int num;
int i,j;
int flag=0;
printf("请输入一个正整数:");
while(scanf("%d",&num)==1 && num>0)
{ printf("小于或等于该数的素数有:");
for(i=2;i<=num;i++)
{
for(j=2;j<=sqrt((double)i);j++)
{
if(i%j==0)
flag=1;
}
if(flag==0)
printf("%3d",i);
flag=0;
}
}
system("pause");
return 0;
}
10.1988年的美国联邦税收计划是近代最简单的税收方案,它分为4个类别,每个类别有两个等级。下面是该税收计划的摘要(美元数为应征税的收入):
类别 | 税金 |
---|---|
单身 | 17850美元按15%计,超出部分按28%计 |
户主 | 23900美元按15%计,超出部分按28%计 |
已婚,共有 | 29750美元按15%计,超出部分按28%计 |
已婚,离异 | 14875美元按15%计,超出部分按28%计 |
例如,一位工资为20000美元的单身纳税人,应缴纳税费0.15×17850
+0.28×(2000-17850)美元,编写一个程序,让用户指定缴纳税余的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次输入。
#include <stdio.h>
#include <stdlib.h>
#define TYPE1 17850
#define TYPE2 23900
#define TYPE3 29750
#define TYPE4 14875
#define TAX1 0.15
#define TAX2 0.28
int main(void)
{
int choice,type,wage;
float tax;
printf("请选择缴纳税金的种类:\n");
printf("1.单身 2.户主 3.已婚,共有 4.已婚,离异\n");
scanf("%d",&choice);
do
{
if(choice==1)
type=TYPE1;
else if(choice==2)
type=TYPE2;
else if(choice==3)
type=TYPE3;
else if(choice==4)
type=TYPE4;
printf("请输入应纳税收入(美元):");
scanf("%d",&wage);
if(wage<=type)
tax=wage*TAX1;
else
tax=type*TAX1+(wage-type)*TAX2;
printf("应缴纳税费%f美元",tax);
printf("\n请选择缴纳税金的种类:\n");
printf("1.单身 2.户主 3.已婚,共有 4.已婚,离异\n");
}while(scanf("%d",&choice)==1);
system("pause");
return 0;
}
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磅的甜菜。然后,该程序要计算货物总价,折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价,订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额,。
#include <stdio.h>
#include <stdlib.h>
#define PRICE_ARTICHOKE 2.05
#define PRICE_BEET 1.15
#define PRICE_CARROT 1.09
#define BREAK1 5
#define BREAK2 20
int main(void)
{ char choice;
float weight,weight_all,price1,price2,other,total;
float weight_art=0;
float weight_beet=0;
float weight_car=0;
float discount=0;
printf("请选择你要订购的蔬菜:\n");
printf("a.洋蓟 b.甜菜 c.胡萝卜 q.退出订购\n");
scanf("%c",&choice);
while(choice!='q')
{
switch(choice)
{
case 'a':printf("请输入订购洋蓟的磅数:");
scanf("%f",&weight);
weight_art+=weight;
break;
case 'b':printf("请输入订购甜菜的磅数:");
scanf("%f",&weight);
weight_beet+=weight;
break;
case 'c':printf("请输入订购胡萝卜的磅数:");
scanf("%f",&weight);
weight_car+=weight;
}
if(choice=='\n')
{
scanf("%c",&choice);
continue;
}
printf("\n请选择你要订购的蔬菜:\n");
printf("a.洋蓟 b.甜菜 c.胡萝卜 q.退出订购\n");
scanf("%c",&choice);
}
weight_all=weight_art+weight_beet+weight_car;
price1=PRICE_ARTICHOKE*weight_art+PRICE_BEET*weight_beet+PRICE_CARROT*weight_car;
if(price1>=100)
discount=price1*0.05;
price2=price1-discount;
if(weight_all<=BREAK1)
other=6.5;
else if(weight_all>BREAK1 && weight_all<=BREAK2)
other=14;
else
other=14+0.5*(weight_all-BREAK2);
total=price2+other;
printf("\n洋蓟的售价是%.2f美元/磅,订购了%f磅",PRICE_ARTICHOKE,weight_art);
printf("\n甜菜的售价是%.2f美元/磅,订购了%f磅",PRICE_BEET,weight_beet);
printf("\n胡萝卜的售价是%.2f美元/磅,订购了%f磅",PRICE_CARROT,weight_car);
printf("\n订购的总重量是%f磅",weight_all);
printf("\n订购的蔬菜费用是%f美元",price1);
printf("\n订单的总费用是%f美元",price2);
printf("\n折扣是%f美元",discount);
printf("\n运费和包装费是%f美元",other);
printf("\n订单的总费用是%f美元",total);
system("pause");
return 0;
}