C Primer Plus (中文版)第7章编程练习 参考答案
🌴 C Primer Plus 这本书是很多学习C语言小白的入门书籍~ 很经典 目前我也正在学习~ 后期也会把写的题上传上来 大家一起交流学习!! 加油加油!🍭
☘️欢迎大家讨论 批评指正~
第1题
🍎1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行符数和所有其他字符的数量。
#include<stdio.h>
int main(void){
int space=0,line_break=0,others=0;
char c;
printf("please input the characters:\n");
while((c=getchar())!='#'){
if(' '==c) space+=1;
else if ('\n'==c) line_break+=1;
else others+=1;
}
printf("done!\n");
printf("the number of spaces are :%d , the line_break are: %d, the others are:%d",space,line_break,others);
return 0;
}
第2题
🍐编写一个程序读取输入, 读到#字符停止。 程序要打印每个输入的字
符以及对应的ASCII码(十进制) 。 一行打印8个字符。 建议:使用字符计数
和求模运算符(%) 在每8个循环周期时打印一个换行符
/*
* @Description: 编写一个程序读取输入, 读到#字符停止。 程序要打印每个输入的字
符以及对应的ASCII码(十进制) 。 一行打印8个字符。 建议:使用字符计数
和求模运算符(%) 在每8个循环周期时打印一个换行符
**关于行缓冲**
在这种情况下,当在输入和输出中遇到换行符时,执行真正的I/O操作。这时,我们输入的字符先存放在
缓冲区,等按下回车键换行时才进行实际的I/O操作。典型代表是标准输入(stdin)和标准输出(stdout)。
* @Author: ~光~~
* @Date: 2023-11-16 20:54:22
* @LastEditTime: 2023-11-16 21:45:53
* @LastEditors:
*/
#include<stdio.h>
int main(void){
int i=0,count=0;
char c;
char s[8];
printf("please enter:\n");
while((c=getchar())!='#'){
count+=1;
s[count-1]= c;
if(count==8){
printf("the characters are :");
for(i=0;i<8;i++){
if(s[i]=='\n') printf("\\n ");
else printf("%c ",s[i]);
}
printf("\n");
printf("the ascii are :");
for(i=0;i<8;i++){
printf("%d ",s[i]);
}
printf("\n");
printf("The end of a cycle\n");
count=0;
}
}
return 0;
}
第3题
🍌编写一个程序, 读取整数直到用户输入 0。 输入结束后, 程序应报告
用户输入的偶数(不包括 0) 个数、 这些偶数的平均值、 输入的奇数个数及其奇数的平均值。
/*
* @Description: 编写一个程序, 读取整数直到用户输入 0。 输入结束后, 程序应报告
用户输入的偶数(不包括 0) 个数、 这些偶数的平均值、 输入的奇数个数及其奇数的平均值。
* @Author: ~光~~
* @Date: 2023-11-16 21:48:05
* @LastEditTime: 2023-11-18 21:29:21
* @LastEditors:
*/
#include<stdio.h>
int main(void){
int even=0,odd=0,sum_even=0,sum_odd=0;
int x;
printf("please enter the number:");
scanf("%d",&x);
while(x!=0){
if(x%2==0){
even+=1;
sum_even+=x;
}else{
odd+=1;
sum_odd+=x;
}
printf("please enter the number:");
scanf("%d",&x);
}
printf("done!\n");
printf("The number of even numbers are %d,The number of odd numbers are %d\n",even,odd);
printf("The sum of those even are %d,The sum of those odd are %d\n",sum_even,sum_odd);
printf("The average of those even are %.2f,The average of those odd are %.2f\n",(float)(sum_even/even),(float)(sum_odd/odd));
return 0;
}
第4题
🍑4.使用if else语句编写一个程序读取输入, 读到#停止。 用感叹号替换句号, 用两个感叹号替换原来的感叹号, 最后报告进行了多少次替换。
/*
* @Description: 4.使用if else语句编写一个程序读取输入, 读到#停止。 用感叹号替换句号, 用两个感叹号替换原来的感叹号, 最后报告进行了多少次替换。
* @Author: ~光~~
* @Date: 2023-11-18 21:31:59
* @LastEditTime: 2023-11-18 21:45:21
* @LastEditors:
*/
#include<stdio.h>
int main(void){
char c;
int count=0;
printf("please enter the letters: "\n);
while((c=getchar())!='#'){
if(c=='.'){
count+=1;
putchar('!');
}else if(c=='!'){
count+=1;
printf("!!");
}else{
putchar(c);
}
}
printf("done!\n");
printf("the actions of repalcements are :%d",count);
return 0;
}
第5题
🌿使用switch重写练习4
/*
* @Description: 4.使用if else语句编写一个程序读取输入, 读到#停止。 用感叹号替换句号, 用两个感叹号替换原来的感叹号, 最后报告进行了多少次替换。
**使用switch重写练习4
* @Author: ~光~~
* @Date: 2023-11-18 21:40:27
* @LastEditTime: 2023-11-18 21:40:33
* @LastEditors:
*/
#include<stdio.h>
int main(void){
char c;
int count=0;
printf("please enter :\n");
while((c=getchar())!='#'){
switch(c){
case '.':{
putchar('!');
count++;
break;
}
case '!':{
printf("!!");
count++;
break;
}
default: {
putchar(c);
break;
}
}
}
printf("done!\n");
printf("ok,the number of the actions of replacements are :%d\n",count);
return 0;
}
第6题
🌺6.编写程序读取输入,读到#停止,报告ei出现的次数。
注意 该程序要记录前一个字符和当前字符。 用“Receive your eieio award”这
样的输入来测试
/*
* @Description: 6.编写程序读取输入,读到#停止,报告ei出现的次数。
**注意
该程序要记录前一个字符和当前字符。 用“Receive your eieio award”这
样的输入来测试
* @Author: ~光~~
* @Date: 2023-11-18 21:45:46
* @LastEditTime: 2023-11-18 21:55:45
* @LastEditors:
*/
#include<stdio.h>
int main(void){
char c,prv;
int count=0,flag=0;
printf("please enter:\n");
while((c=getchar())!='#'){
if(c=='e'){
prv='e';
}else if (c=='i'){
if(prv=='e'){
count+=1;
prv=' ';
}
}else {
prv=' ';
}
}
printf("done!\n");
printf("the number of the charcater of ei are:%d",count);
return 0;
}
第7题
🚀编写一个程序, 提示用户输入一周工作的小时数, 然后打印工资总
额、 税金和净收入。 做如下假设:
a.基本工资 = 1000美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15%
续150美元为20%
余下的为25%
用#define定义符号常量。 不用在意是否符合当前的税法
/*
* @Description: 编写一个程序, 提示用户输入一周工作的小时数, 然后打印工资总
额、 税金和净收入。 做如下假设:
a.基本工资 = 1000美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15%
续150美元为20%
余下的为25%
用#define定义符号常量。 不用在意是否符合当前的税法
* @Author: ~光~~
* @Date: 2023-11-18 21:58:23
* @LastEditTime: 2023-11-19 16:18:23
* @LastEditors:
*/
#include<stdio.h>
#define BASIC_SALARY 1000
#define EXTRA_WORK 1.5
#define RATE_300 0.15
#define RATE_450 0.2
#define RATE_left 0.25
int main(void){
int hour,total,rate,profit;
printf("please enter the work time:\n");
scanf("%d",&hour);
if(hour>40) hour=40+(hour-40)*EXTRA_WORK;
total=hour*BASIC_SALARY;
if(total<=300){
rate=total*RATE_300;
profit=total*0.85;
}else if(total>300 && total <=450){
rate=300*RATE_300+(total-300)*RATE_450;
profit=total-rate;
}
else{
rate=RATE_300*300+150*RATE_450+(total-450)*RATE_left;
profit=total-rate;
}
printf("the total is %.2f,the tax is %.2f,the profit is %.2f\n",(float)total,(float)rate,(float)profit);
return 0;
}
第8题
🐬 8.修改练习7的假设a, 让程序可以给出一个供选择的工资等级菜单。 使
用switch完成工资等级选择。 运行程序后, 显示的菜单应该类似这样:
a.基本工资 = 1000美元/小时
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
如果选择 1~4 其中的一个数字, 程序应该询问用户工作的小时数。 程
序要通过循环运行, 除非用户输入 5。 如果输入 1~5 以外的数字, 程序应
提醒用户输入正确的选项, 然后再重复显示菜单提示用户输入。 使用#define
创建符号常量表示各工资等级和税率。
/*
* @Description: 8.修改练习7的假设a, 让程序可以给出一个供选择的工资等级菜单。 使
用switch完成工资等级选择。 运行程序后, 显示的菜单应该类似这样:
a.基本工资 = 1000美元/小时
*****************************************************************
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
*****************************************************************
如果选择 1~4 其中的一个数字, 程序应该询问用户工作的小时数。 程
序要通过循环运行, 除非用户输入 5。 如果输入 1~5 以外的数字, 程序应
提醒用户输入正确的选项, 然后再重复显示菜单提示用户输入。 使用#define
创建符号常量表示各工资等级和税率。
* @Author: ~光~~
* @Date: 2023-11-19 16:20:36
* @LastEditTime: 2023-11-19 16:20:43
* @LastEditors:
*/
#include<stdio.h>
#define BASIC_SALARY 1000
float t(int n,int hour);//返回tax的值
int main(void){
int n,hour=0,flag=0;
float tax,profit;
char c,d;
printf("***************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("***************************************\n");
printf("please enter the number(1-5): ");
n=scanf("%d",&n);
c=getchar();
while(1){
fflush(stdin);
if(c=='\n'){
if(n>=1 && n<=5){
if(n!=5){
printf("What is your working hours? :");
scanf("%d",&hour);
tax=t(n,hour);
}else{
printf("bye!\n");
break;
}
profit=hour*BASIC_SALARY-tax;
printf("the profit is %.2f,the tax is %.2f\n",profit,tax);
}else{
printf("please enter the correct typeof number!!\n");
}
}else{
fflush(stdin);
printf("please enter the correct typeof number!!\n");
}
fflush(stdin);
printf("\n");
printf("========================================\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("***************************************\n");
printf("\nplease enter the number(1-5): ");
scanf("%d",&n);
c=getchar();
}
return 0;
}
float t(int n,int hour){
float tax;
switch(n){
case 1:{
tax=8.75*hour;
break;
}
case 2 :{
tax=9.33*hour;
break;
}
case 3 :{
tax=10.00*hour;
break;
}
case 4 :{
tax=11.20*hour;
break;
}
default:break;
}
return tax;
}
第9题
🐋9.编写一个程序, 只接受正整数输入, 然后显示所有小于或等于该数的
素数。
/*
* @Description: 9.编写一个程序, 只接受正整数输入, 然后显示所有小于或等于该数的
素数。
* @Author: ~光~~
* @Date: 2023-11-19 19:36:48
* @LastEditTime: 2023-11-19 20:12:50
* @LastEditors:
*/
#include<stdio.h>
int su(int n);
int main(void){
int n,j,i,t,flag=0;
char c;
printf("please enter the number :");
scanf("%d",&n);
c=getchar();
fflush(stdin);
if(c=='\n'){
printf("the prime numbers are: ");
for(i=2;i<=n;i++){
t=su(i);
if(t) {
printf("%d ",i);
t=0;
}
}
}
return 0;
}
int su(int n){
int i,flag=0;
for(i=2;i<n;i++){
if(n%i!=0) flag=1;
else {
return 0;
}
}
return 1;
}
第10题
🚩10.1988年的美国联邦税收计划是近代最简单的税收方案。 它分为4个类别, 每个类别有两个等级。
下面是该税收计划的摘要(美元数为应征税的收入) :
例如, 一位工资为20000美元的单身纳税人, 应缴纳税费
0.15×17850+0.28×(20000-17850) 美元。 编写一个程序, 让用户指定缴纳
税金的种类和应纳税收入, 然后计算税金。 程序应通过循环让用户可以多次
输入
/*
* @Description: 10.1988年的美国联邦税收计划是近代最简单的税收方案。 它分为4个类
别, 每个类别有两个等级。
下面是该税收计划的摘要(美元数为应征税的收入) :
例如, 一位工资为20000美元的单身纳税人, 应缴纳税费
0.15×17850+0.28×(20000-17850) 美元。 编写一个程序, 让用户指定缴纳
税金的种类和应纳税收入, 然后计算税金。 程序应通过循环让用户可以多次
输入
* @Author: ~光~~
* @Date: 2023-11-19 20:13:05
* @LastEditTime: 2023-11-20 10:57:39
* @LastEditors:
//
*/
#include<stdio.h>
#define RANK1 17850
#define RANK2 23900
#define RANK3 29750
#define RANK4 14875
void display(void);
int main(void){
int tax=0,type;
float salary;
do{
display();
printf("please enter the type of you:");
scanf("%d",&type);
fflush(stdin);
printf("please enter your salary:");
// scanf("%lf",&salary); !!一定要注意类型问题 如果类型出问题了 后面就也找不到bug
scanf("%f",&salary);
switch(type){
case 1:{
if(salary>RANK1) tax=(salary-RANK1)*0.28+RANK1*0.15;
else tax=salary*0.15;
break;
}
case 2:{
if(salary>RANK2) tax=(salary-RANK2)*0.28+RANK2*0.15;
else tax=salary*0.15;
break;
}
case 3:{
if(salary>RANK3) tax=(salary-RANK3)*0.28+RANK3*0.15;
else tax=salary*0.15;
break;
}
case 4:{
if(salary>RANK4) tax=(salary-RANK4)*0.28+RANK4*0.15;
else tax=salary*0.15;
break;
}
default: break;
}
printf("the profit is %.2f the tax is %.2f\n",(float)(salary-tax),(float)tax);
}while(salary>0 && (type>0&&type<=4));
printf("bye!\n");
return 0;
}
void display(void){
printf("=================================\n");
printf("编号\t\t类别\t\t\n");
printf("1\t\tsolo\n");
printf("2\t\tHousehold\n");
printf("3\t\tMarried co-owned\n");
printf("4\t\tMarried and divorced\n");
printf("=================================\n");
}
第11题
🌈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磅的甜菜。
然后, 该程序要计算货物总价、 折扣(如果有的话) 、 运费和包装
费。 随后, 程序应显示所有的购买信息: 物品售价、 订购的重量(单位:
磅) 、 订购的蔬菜费用、 订单的总费用、 折扣(如果有的话) 、 运费和包装
费, 以及所有的费用总额
/*
* @Description:
* @Author: 汐汐
* @Date: 2023-11-20 10:43:41
* @LastEditTime: 2023-11-21 10:04:00
* @LastEditors:
*/
/*
* @Description:
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磅
的甜菜。
然后, 该程序要计算货物总价、 折扣(如果有的话) 、 运费和包装
费。 随后, 程序应显示所有的购买信息: 物品售价、 订购的重量(单位:
磅) 、 订购的蔬菜费用、 订单的总费用、 折扣(如果有的话) 、 运费和包装
费, 以及所有的费用总额
* @Author: ~光~~
* @Date: 2023-11-20 10:43:41
* @LastEditTime: 2023-11-20 10:43:42
* @LastEditors:
*/
#include<stdio.h>
#include <stdlib.h>
#include <windows.h> //系统函数
#define Artichokes 2.05 //洋蓟
#define Beets 1.15 //甜菜
#define Carrot 1.09 //胡萝卜
void display_sell(void);
void display_shopping(float a,float b,float c);
void display_total(float a,float b,float c,float total,float sum_pounds);
int main(void){
char d='h';//switch选择的变量,判断是否继续 先设置一个初始值 让他进入循环
float a=0,b=0,c=0,total;//各个菜品对应的英镑数字 总价格;
float sum_a=0,sum_b=0,sum_c=0,sum_pounds=0;//累计英镑
//float total,freight_packaging;//总价 折扣 运费和包装费
while(d!='q'){
display_sell();
Sleep(1000);
//system("pause"); 可用于调试
printf("hi~ please enter the leeter and pouds you want to buy!\n");
printf("\na--Artichokes b--Beets c--Carrot q--quit\n");
printf("ok~ please enter the letter(prensents the vegetables) that you want to buy: \n");
printf("\nattention~ if you want to quit,please enter q, else continue.\n");
printf("\nplease enter:");
fflush(stdin);//清理缓存
while(1){ //防止出现 “aaaa ” 等类似的错误输入
d=getchar();
if((getchar()=='\n')) break;
else{
printf("please enter the correct type!\n");
printf("please enter:");
}
fflush(stdin);
}
switch(d){
case 'a':{
// scanf("%f",a); 麻了,又忘记取地址了!!
while(1){//防止出现 “99 s” 类似的错误输入
printf("ok,this is Artichokes! How many pounds do you want to buy?\n");
printf("Enter: ");
if(scanf("%f",&a)){
if(getchar()=='\n'){
sum_a+=a;
break;
}else {
printf("incorrect enter! come back!\n");
}
}else {
printf("incorrect enter! come back!\n");
}
fflush(stdin);//一定要加这个 不然会无限循环 scanf永远不会读入 一直读入缓冲区的\n
}
break;
}
case 'b':{
while(1){
printf("ok,this is Beets! How many pounds do you want to buy?\n");
printf("Enter: ");
if(scanf("%f",&b)){//防止出现 “99 s” 类似的错误输入
if(getchar()=='\n'){
sum_b+=b;
break;
}else {
printf("incorrect enter! come back!\n");
}
}else {
printf("incorrect enter! come back!\n");
}
fflush(stdin);//一定要加这个 不然会无限循环 scanf永远不会读入 一直读入缓冲区的\n
}
break;
}
case 'c':{
while(1){
printf("ok,this is Carrot! How many pounds do you want to buy?\n");
printf("Enter: ");
if(scanf("%f",&c)){
if(getchar()=='\n'){
sum_c+=c;
break;
}else {
printf("incorrect enter! come back!\n");
}
}else {
printf("incorrect enter! come back!\n");
}
fflush(stdin);
}
break;
}
case 'q':{
printf("ok,bye!\n");
break;
}
default: {
printf("no this type! incorrect enter!\n");
printf("come back!\n");
printf("============================\n");
break;
}
}
if(d=='q') break;
fflush(stdin);
printf("well,this is your current shopping list\n");
display_shopping(sum_a,sum_b,sum_c);
printf("ok,enter any key to be continue\n");
getchar();
printf("========================\n");
Sleep(1000);
}
total=sum_a*Artichokes+sum_b*Beets+sum_c*Carrot;
sum_pounds=sum_a+sum_b+sum_c;
printf("this is your final shopping list\n");
display_total(sum_a,sum_b,sum_c,total,sum_pounds);
printf("ok,done! thank you for your shopping!\n");
printf("\n");
return 0;
}
void display_sell(void){
printf("**************************\n");
printf(" Selling price list \t\n");
printf("==========================\n");
printf("types\t\t price(GBP/USD)\n");
printf("--------------------------\n");
printf("artichokes\t 2.05\n");
printf("beets\t\t 1.15\n");
printf("carrot\t\t 1.09\n");
printf("***************************\n");
}
void display_shopping(float a,float b,float c){
printf("**************************************************************\n");
printf("\t\t\t Shopping list \t\n");
printf("==============================================================\n");
printf("types\t\t price(GBP/USD) pounds(£) part_sum($) \t \n");
printf("--------------------------------------------------------------\n");
printf("artichokes\t 2.05 \t \t%7.2f %16.2f\n",a,a*Artichokes);
printf("beets\t\t 1.15 \t\t%7.2f %16.2f\n",b,Beets*b);
printf("carrot\t\t 1.09 \t \t%7.2f %16.2f\n",c,c*Carrot);
printf("**************************************************************\n");
}
/*在添加运费之前, 100美元的订单有
5%的打折优惠。 少于或等于5磅的订单收取6.5美元的运费和包装费, 5磅~
20磅的订单收取14美元的运费和包装费, 超过20磅的订单在14美元的基础上
每续重1磅增加0.5美元。 */
void display_total(float a,float b,float c,float total,float sum_pounds){
float discout=0,freight_packaging=0,consumption;
float a_price=0,b_price=0,c_price=0;
consumption=total;//净消费
if(total>100) discout=total*0.05;
if(sum_pounds<=5) freight_packaging=6.5;
else if(sum_pounds>5 && sum_pounds<20) freight_packaging=14;
else freight_packaging=14+(sum_pounds-20)*0.5;
total=total-discout+freight_packaging;
printf("**************************************************************\n");
printf("\t\t\t Shopping list \t\n");
printf("==============================================================\n");
printf("types\t\t price(GBP/USD) pounds(£) part_sum($) \t \n");
printf("--------------------------------------------------------------\n");
printf("artichokes\t 2.05 \t \t%7.2f %16.2f\n",a,a*Artichokes);
printf("beets\t\t 1.15 \t\t%7.2f %16.2f\n",b,Beets*b);
printf("carrot\t\t 1.09 \t \t%7.2f %16.2f\n",c,c*Carrot);
printf("Tatal\t\t None \t \t%7.2f %16.2f\n",sum_pounds,total);
printf("==============================================================\n");
printf("\n");
Sleep(1000);
printf("your net consumption is %4.2f$\n",consumption);
printf("your discount is %4.2f$\n",discout);
printf("your consumption of freight_packaging is %4.2f$\n",freight_packaging);
printf("your final consumption is %4.2f$\n",total);
printf("**************************************************************\n");
printf("\n");
}
⛵️完成啦~
☘️如果有其他解法~ 欢迎大家讨论 批评指正~
🌈 此编程练习参考答案为本人所写,如有错误欢迎大家批评指正~~ 如转载请说明来源~
🌈ok,完结~(●’◡’●) 看到这里 点个赞叭 (●’◡’●)