C Primer Plus(第六版)第七章 编程练习答案
Posted 水番正文
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第七章 编程练习答案相关的知识,希望对你有一定的参考价值。
前言:由于都是之前写完的题,已经没有什么感想和特别注意点,所以机器式上传复制,如有幸给小伙伴参考学习,有问题欢迎问,仅供参考,新手勿喷。
CH07 Code answer 1-6:
#include <stdio.h>
#include <ctype.h>
#define SPACE ' '
int main(void)
{
int i,j,x,y,z;
char ch;
int count=0;
printf("7.12.1\\n");
int space_num,n_num,o_num;
space_num = n_num = o_num = 0;
printf("请输入一串字符,将会统计字符:");
while((ch = getchar()) != '#')
{
if(ch == SPACE)
space_num++;
else if(ch == '\\n')
n_num++;
else
o_num++;
}
printf("空格数量为%d 换行数量为%d 其他字符为%d\\n",space_num,n_num,o_num);
printf("\\n");
getchar(); //每次输入就会有个\\n换行,这个是用来接受换行,防止影响下面
printf("7.12.2\\n");
printf("请输入一段字符,会展示字符-ASCII:");
while((ch = getchar()) != '#')
{
if(!isspace(ch))
{
printf("%c-%d ",ch,ch);
count++;
count%8 == 0 ? printf("\\n"),count=0 : count ;
}
if(ch == '\\n') //所以每次的结尾都是个换行,以此来重置程序
{
printf("\\n请输入一段字符,会展示字符-ASCII(输入#退出):");
count = 0 ;
}
}
printf("\\n");
getchar();
printf("7.12.3\\n");
int js_num,os_num;
js_num=os_num=0;
float js,os;
printf("请输入一串数组:");
while((ch = getchar()) != '0')
{
if(ch%2 != 0)
{
js_num++;
js += ch-48;
}
else
{
os_num++;
os += ch-48;
}
}
printf("奇数平均为%.2f 个数为%d 偶数平均为%.2f 个数为%d\\n",js/js_num,js_num,os/os_num,os_num);
printf("\\n");
printf("7.12.4\\n");
printf("请输入字符,我会对你某些字符改变哟(输入#退出):");
while((ch = getchar()) != '#')
{
if(ch == '.')
{
putchar('!'); //注意用法,putchar相当于固定的printf("%c",ch) 而这个ch只能是一个字符
count++; //而且要用单引号是因为单引号代表一个字符实际上是一个整数
} //双引号代表字符串
else if(ch == '!')
{
// putchar('!!'); //所以这无法输出两个!!,只会输出一个
putchar('!');
putchar('!'); //于是这样做
count++;
}
}
printf("替换次数为%d\\n",count);
printf("\\n");
printf("7.12.5\\n");
printf("请输入字符,我会对你某些字符改变哟2.0(输入#退出):");
count = 0;
while((ch = getchar()) != '#')
{
switch (ch)
{
case '.':
putchar('!');
count++;
break;
case '!':
putchar('!');
putchar('!');
count++;
break;
}
}
printf("替换次数为%d\\n",count);
printf("\\n");
printf("7.12.6\\n");
char last;
count = 0;
printf("我会判断你输入了多少个ei(输入#退出):");
while((ch = getchar()) != '#')
{
if(last == 'e' && ch == 'i')
count++;
last = ch;
}
printf("输入次数为%d",count);
printf("\\n");
}
CH07 Code answer 7-8:
#include <stdio.h>
#include <ctype.h>
#define SALARY 10
#define OVERTIME 40
#define Q300 0.15
#define X150 0.2
#define OM 0.25
#define r1 8.75
#define r2 9.33
#define r3 10.00
#define r4 11.20
void Print(void);
int main(void)
{
int i,j,x,y,z;
int count=0;
printf("7.12.7\\n");
float money;
int ho;
printf("请输入你工作了多少小时:");
while(scanf("%d",&ho) == 1)
{
if(ho > 40)
money = (ho-40)*1.5*SALARY + 40*SALARY;
else
money = ho*SALARY;
if(money <= 300)
money = money - Q300 * money;
else if(money <= 450)
money = money - ((money-300)*X150 + 300 * Q300);
else
money = money - ((money-450)*OM + 150*X150 + 300*Q300);
printf("你的工资为%f\\n",money);
printf("请输入你工作了多少小时(输入q退出):");
}
printf("\\n");
getchar(); //消除退出
printf("7.12.8\\n");
int choose;
float sa;
Print();
while(scanf("%d",&choose) == 1)
{
if (choose >= 1 && choose <= 4)
{
switch (choose)
{
case 1:
sa = r1;
break;
case 2:
sa = r2;
break;
case 3:
sa = r3;
break;
case 4:
sa = r4;
break;
}
printf("请输入你工作了多少小时:");
scanf("%d",&ho);
if(ho > 40)
money = (ho-40)*1.5*sa + 40*sa;
else
money = ho*sa;
if(money <= 300)
money = money - Q300 * money;
else if(money <= 450)
money = money - ((money-300)*X150 + 300 * Q300);
else
money = money - ((money-450)*OM + 150*X150 + 300*Q300);
printf("你的工资为%f\\n",money);
Print();
}
else if(choose == 5)
break;
else
{
printf("请输入有效选项\\n");
Print();
}
}
printf("\\n");
}
void Print(void)
{
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/h 4) $11.20/hr\\n");
printf("5) quit\\n");
printf("*****************************************************************\\n");
printf("请选择:");
}
CH07 Code answer 9-10:
#include <stdio.h>
#define SI 17850
#define HO 23900
#define MASH 29750
#define MADI 14875
#define WI 0.15
#define OV 0.28
void Print(void);
int main(void)
{
int i,j,x,y,z;
int count=0;
printf("7.12.9\\n");
int key,flag;
printf("帮你计算包括该数以下的所有素数:");
scanf("%d",&key);
printf("素数为:");
for(i=2;i<=key;i++)
{
for(j=2;j<=i-1;j++)
{
if((i % j) == 0 )
flag++;
}
if(flag == 0 )
printf("%d ",i);
flag = 0 ;
}
printf("\\n\\n");
printf("7.12.10\\n");
float money,rank;
int choose;
Print();
while(scanf("%d",&choose) == 1)
{
if(choose >= 1 && choose <= 4)
{
switch (choose)
{
case 1:
rank = SI;
break;
case 2:
rank = HO;
break;
case 3:
rank = MASH;
break;
case 4:
rank = MADI;
break;
}
printf("请输入你的应纳税收入:");
scanf("%f",&money);
if(money > rank)
{
money = rank * WI + (money - rank)*OV;
}
else
{
money = money * WI;
}
printf("你应交税的金额为%f\\n\\n",money);
Print();
}
else if(choose == 5)
break;
else
{
printf("\\n请输入正确的数字\\n");
Print();
}
}
printf("\\n");
}
void Print(void)
{
printf("********************************************************************\\n");
printf("Enter the number corresponding to the desired pay rate or action2.0:\\n");
printf("1) 单身 2) 户主\\n");
printf("3) 已婚,共友 4) 已婚,离异\\n");
printf("5) 退出\\n");
printf("********************************************************************\\n");
printf("请选择:");
}
CH07 Code answer 11:
#include <stdio.h>
#define CABBAGE 2.05
#define BEET 1.15
#define CARROT 1.09
#define DISCOUNT 0.05
#define SY 6.5
#define YY 14
#define DY YY+0.5*(all_num-20)
void Print(void);
int main(void)
{
float te,cabg_num,beet_num,cart_num,all_num;
float cabg_money,beet_money,cart_money;
float money = 0,sundry_money=0,discount_money=0;
char choose;
Print();
while(scanf("%c",&choose) == 1)
{
if(choose >= 'a' && choose <= 'c')
{
switch (choose)
{
case 'a':
printf("请输入要买的白菜数量:");
scanf("%f",&te);
cabg_num += te;
cabg_money += te * CABBAGE;
break;
case 'b':
printf("请输入要买的甜菜数量:");
scanf("%f",&te);
beet_num += te;
beet_money += te * BEET;
break;
case 'c':
printf("请输入要买的胡萝卜数量:");
scanf("%f",&te);
cart_num += te;
cart_money += te * CARROT;
break;
}
Print();
}
else if(choose == 'q')
break;
else if(choose != '\\n')
{
printf("\\n请输入有效选择\\n");
Print();
}
}
/* if(cabg_num > 0)
{
money += cabg_num * CABBAGE;
}
if(beet_num > 0)
{
money += beet_num * BEET;
}
if(cart_num >0)
{
money += cart_num * CARROT;
}
*/
all_num = cabg_num + beet_num + cart_num;
money = cabg_money + beet_money + cart_money;
if(money > 100)
discount_money = money * DISCOUNT;
if(all_num <= 5)
sundry_money = SY;
else if(all_num <= 20)
sundry_money = YY;
else
sundry_money = DY;
printf("\\n");
printf(" 物品售价 订购的重量 订购的蔬菜费用\\n");
printf("白菜 %-8f %-10f %-14f\\n",CABBAGE,cabg_num,cabg_money); //这里要是想对齐
printf("甜菜 %-8f %-10f %-14f\\n",BEET,beet_num,beet_money); //-8是字宽度
printf("胡萝卜 %-8f %-10f %-14f\\n\\n",CARROT,cart_num,cart_money); //不同参数中间的空是字之间的空
printf("订单的总费用 %f 总折扣 %f 总运费和包装费 %f \\n总额 $%f",money,
discount_money > 0 ? discount_money : 0,
sundry_money,
money + sundry_money - discount_money);
}
void Print(void)
{
printf("\\n***********************************************************\\n");
printf("选择你的蔬菜!\\n");
printf("a) 白菜 b) 甜菜\\n");
printf("c) 胡萝卜 q) 退出\\n");
printf("***********************************************************\\n");
printf("请选择:");
}
以上是关于C Primer Plus(第六版)第七章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章