编译环境VS Code+WSL GCC 源码请到文末下载 。 注意:本章部分题目有些超纲(回用到函数调用),不理解的同学可以先去看一下后面的知识点和注释。

/*第1题*************************/
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
int space,new,others;
space = new = others = 0;
while ((ch = getchar()) != '#')
{
if(ch == ' ')
space++;
else if(ch == '\n')
new++;
else
others++;
}
printf("空格有:%d个\n换行有:%d个\n其他有:%d个\n",space,new,others);
return 0;
}
/*第2题*************************/
#include<stdio.h>
#include<ctype.h>
int main()
{
char arry[999]={0};
char ch;
int cnt = 0,i = 0;
while ((ch = getchar()) != '#'){
arry[cnt++] = ch;
}
for(i=1;i<cnt+1;i++){
if(isalnum(arry[i-1])||ispunct(arry[i-1]))
printf("\'%c\'-%-3d ",arry[i-1],arry[i-1]);
else
printf("\'?\'-%-3d ",arry[i-1]);
if(i != 0 && i%8 == 0)
putchar('\n');
}
putchar('\n');
return 0;
}
/*第3题*************************/
#include<stdio.h>
int main()
{
int s_num_sum,d_num_sum,s_num_cnt,d_num_cnt,input_num;
double s_num_avg,d_num_avg;
s_num_sum = d_num_sum = s_num_cnt = d_num_cnt = input_num = 0;
printf("请输入整数,输入0退出:");
do{
if(scanf("%d",&input_num) == 1 && input_num != 0){
//如果输入的是一个不为0的整数,将继续下面的操作(排除其他情况)
if(input_num%2 == 0){ //如果输入是双数
d_num_sum += input_num; //双数总和累加
d_num_cnt++; //双数个数++
}
else{
s_num_sum += input_num;
s_num_cnt++;
}
}
} while (input_num != 0);
s_num_avg = 1.0 * s_num_sum / s_num_cnt;
d_num_avg = 1.0 * d_num_sum / d_num_cnt;
printf("输入的偶数个数:%d\n输入的偶数平均值:%g\n输入的奇数个数:%d\n输入的奇数平均值:%g\n",
d_num_cnt,d_num_avg,s_num_cnt,s_num_avg);
return 0;
}
/*第4题*************************/
#include<stdio.h>
int main()
{
char arry[999]={0};
char ch;
int i=0,cnt=0;
printf("请输入字符,直到#停止:");
while ((ch = getchar()) != '#')
{
if(ch == '.'){
arry[i++]='!';
cnt++;
}
else if(ch == '!'){
arry[i++]='!';
arry[i++]='!';
cnt += 2;
}
else{
arry[i++]=ch;
}
}
arry[i]='\0';
printf("替换后为:\n%s\n",arry);
printf("进行了%d次替换\n",cnt);
return 0;
}
/*第5题*************************/
#include<stdio.h>
int main()
{
char arry[999]={0};
char ch;
int i=0,cnt=0;
printf("请输入字符,直到#停止:");
while ((ch = getchar()) != '#')
{
switch (ch)
{
case '.':
arry[i++]='!';
cnt++;
break; case '!':
arry[i++]='!';
arry[i++]='!';
cnt += 2;
break; default:
arry[i++]=ch;
break;
}
}
arry[i]='\0';
printf("替换后为:\n%s\n",arry);
printf("进行了%d次替换\n",cnt);
return 0;
}
/*第6题*************************/
#include<stdio.h>
int main()
{
char arry[999]={0};
char ch;
int i=0,cnt=0;
printf("请输入字符,直到#停止:");
while ((ch = getchar()) != '#')
{
if(ch == '.'){
arry[i++]='!';
cnt++;
}
else if(ch == '!'){
arry[i++]='!';
arry[i++]='!';
cnt += 2;
}
else{
arry[i++]=ch;
}
}
arry[i]='\0';
printf("替换后为:\n%s\n",arry);
printf("进行了%d次替换\n",cnt);
return 0;
}
/*第7题*************************/
#include<stdio.h>
#define BASE_TIME 10
#define OVER_TIME (BASE_TIME * 1.5)
#define BASE_TAX 0.15
#define ADD_TAX 0.2
#define OTHER_TAX 0.25
int main()
{ //总工作时间,总工资,净工资,总税收
float work_times,all_salary,clear_salary,all_tax;
printf("您一周工作了多少小时?\n");
scanf("%f",&work_times);
if(work_times > 40)
{
all_salary = (work_times * BASE_TIME) + (work_times - 40) * OVER_TIME;
}
else
{
all_salary = work_times * BASE_TIME;
} if(all_salary <= 300)
{
all_tax = all_salary * BASE_TAX;
}
else if(all_salary <= 450 && all_salary >300)
{
all_tax = 300 * BASE_TAX + (all_salary - 300) * ADD_TAX;
}
else
{
all_tax = 300 * BASE_TAX + 150 * ADD_TAX + (all_salary - 450) * OTHER_TAX;
}
clear_salary =all_salary - all_tax;
printf("总工作时间:%g\n总工资:%g\n净工资:%g\n总税收:%g\n",work_times,all_salary,clear_salary,all_tax);
return 0;
}
/*第8题*************************/
#include<stdio.h>
//#define BASE_TIME 10
#define OVER_TIME (BASE_TIME * 1.5)
#define BASE_TAX 0.15
#define ADD_TAX 0.2
#define OTHER_TAX 0.25
int main()
{ //总工作时间,总工资,净工资,总税收
float work_times,all_salary,clear_salary,all_tax,BASE_TIME;
int case_num; while (1)
{
printf("*************************************\n\n");
printf("请输入数子对应的工资等级,输入5退出程序:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.0/hr 4) $11.2/hr\n");
printf("5) exit\n");
printf("*************************************\n"); if((scanf("%d",&case_num) != 1) || !(case_num <= 5 && case_num >= 0))
//排除非数字和不在范围内
{
printf("输入错误,请重新输入!\n");
continue;//跳过下面的内容,重新开始循环
}
switch (case_num)
{
case 1:
BASE_TIME =8.75;
break;
case 2:
BASE_TIME =9.33;
break;
case 3:
BASE_TIME =10;
break;
case 4:
BASE_TIME =11.2;
break;
case 5:
default:
printf("程序退出!\n");
goto end;//跳出两重循环直接到程序结尾
break;
}
break;//结束while循环
}
printf("您一周工作了多少小时?\n");
scanf("%f",&work_times);
if(work_times > 40)
{
all_salary = (work_times * BASE_TIME) + (work_times - 40) * OVER_TIME;
}
else
{
all_salary = work_times * BASE_TIME;
} if(all_salary <= 300)
{
all_tax = all_salary * BASE_TAX;
}
else if(all_salary <= 450 && all_salary >300)
{
all_tax = 300 * BASE_TAX + (all_salary - 300) * ADD_TAX;
}
else
{
all_tax = 300 * BASE_TAX + 150 * ADD_TAX + (all_salary - 450) * OTHER_TAX;
}
clear_salary =all_salary - all_tax;
printf("总工作时间:%g\n总工资:%g\n净工资:%g\n总税收:%g\n",work_times,all_salary,clear_salary,all_tax);
end:return 0;
}
/*第9题*************************/
#include<stdio.h>
#include<math.h>
int main()
{
int ipt,i,j;
printf("请输入一个大于0的数:");
while ((scanf("%d",&ipt)!=1) && (ipt <= 0))
{
printf("输入错误!\n");
printf("请输入一个大于0的数:");
}
if(ipt<4)
{
for(i=1;i<=ipt;i++)
printf("%-4d",i);
}
else
{
for(i=1;i<4;i++)
printf("%-4d",i); for(i=4;i<ipt+1;i++){
for(j=2;j<i;j++){
if(i%j == 0){
break;
}
}
if(j == i){
printf("%-4d",i);
}
}
}
putchar(10);
return 0;
}
/*第10题*************************/
#include<stdio.h>
//#define class 10
#define CLASS_1 17850
#define CLASS_2 23900
#define CLASS_3 29750
#define CLASS_4 14875
#define BASE_TAX 0.15
#define OVER_TAX 0.28
int main()
{ //类别,总工资,总税收
float base,all_salary,all_tax;
int case_num; while (1)
{
printf("------------------------------------------------\n");
printf("请输入数子对应的类别,输入5退出程序:\n");
printf("------------------------------------------------\n");
printf("1) 单身 17850$按15%%计,超出部分按28%%计算\n");
printf("2) 户主 23900$按15%%计,超出部分按28%%计算\n");
printf("3) 已婚,共有 29750$按15%%计,超出部分按28%%计算\n");
printf("4) 单身 14875$按15%%计,超出部分按28%%计算\n");
printf("5) exit\n");
printf("------------------------------------------------\n"); if((scanf("%d",&case_num) != 1) || !(case_num <= 5 && case_num >= 0))
//排除非数字和不在范围内
{
printf("输入错误,请重新输入!\n");
while (getchar() != '\n');//清空输入缓存
continue;//跳过下面的内容,重新开始循环
}
switch (case_num)
{
case 1:
base = CLASS_1;
break;
case 2:
base = CLASS_2;
break;
case 3:
base = CLASS_3;
break;
case 4:
base = CLASS_4;
break;
case 5:
default:
printf("程序退出!\n");
goto end;//跳出两重循环直接到程序结尾
break;
}
break;//结束while循环
}
while (1)
{
printf("您的收入是多少?输入Q退出:\n");
while(scanf("%f",&all_salary) != 1){
printf("程序退出!\n");
goto end;//跳出两重循环直接到程序结尾
}
if(all_salary <= base)
{
all_tax = all_salary * BASE_TAX;
}
else
{
all_tax = base * BASE_TAX +(all_salary - base) * OVER_TAX;
}
printf("您的总税收为:%g$\n",all_tax);
}
end:return 0;
}
/*第11题*************************/
#include<stdio.h>
//#define class 10
#define YANG_LI 2.05
#define TIAN_CAI 1.05
#define HUOLUO_BO 1.19 double yl_weight = 0,tc_weight = 0,hlb_weight = 0;
double yl_prices = 0,tc_prices = 0,hlb_prices = 0,all_prices = 0; double get_weight(int case_class);
void show_info();
int user_ui(); int main()
{
user_ui(); return 0;
} double get_weight(int case_class)//传入需要购买的品种
{
double weight=0;
printf("您要购买多少");
switch (case_class)
{
case 1:
printf("洋蓟");
break;
case 2:
printf("甜菜");
break;
case 3:
printf("胡萝卜");
break;
default:
printf("???");
break;
}
printf("(单位/磅)?输入0退回:\n"); while(scanf("%lf",&weight) != 1||weight < 0){//排除非数字和不在范围内
while (getchar() != '\n');//清空输入缓存
printf("输入错误,请重新输入!\n");
}
return weight;
} void show_info()
{
double all_weight,weight_prices,free_prices = 0;
yl_prices = yl_weight * YANG_LI;
tc_prices = tc_weight * TIAN_CAI;
hlb_prices = hlb_weight * HUOLUO_BO;
all_prices = yl_prices + tc_prices + hlb_prices;
all_weight = yl_weight + tc_weight + hlb_weight;
if(yl_weight > 0){printf("您已购买洋蓟:%g 磅,%g 美元\n",yl_weight,yl_prices);}
if(tc_prices > 0){printf("您已购买甜菜:%g 磅,%g 美元\n",tc_weight,tc_prices);}
if(hlb_prices > 0){printf("您已购买胡萝卜:%g 磅,%g 美元\n",hlb_weight,hlb_prices);}
if(all_prices > 0){printf("您已购买%g 美元\n",all_prices);} if(all_weight <= 5){
weight_prices = 6.5;
}
else if(all_weight > 5 && all_weight <= 20){
weight_prices = 14;
}
else
{
weight_prices = 14 + (all_weight-20)*0.5;
} if(all_prices >= 100){
free_prices = all_prices * 0.05;
printf("已为您优惠%g 美元\n",free_prices);
}
printf("合计重量:%g 磅,共需运费:%g 美元,合计支付价格:%g 美元\n",
all_weight,weight_prices,all_prices - free_prices + weight_prices);
} int user_ui()
{
int case_num;
while (1)
{
printf("------------------------------------------------\n");
printf("欢迎光临ABC邮购杂货店!\n");
printf("------------------------------------------------\n");
printf("1) 洋蓟 2.05美元/磅\n");
printf("2) 甜菜 1.15美元/磅\n");
printf("3) 胡萝卜 1.09美元/磅\n");
printf("4) exit\n");
printf("------------------------------------------------\n");
printf("您要购买什么?输入4退出程序:\n");
printf("------------------------------------------------\n");
if((scanf("%d",&case_num) != 1) || !(case_num <= 4 && case_num >= 0))
//排除非数字和不在范围内
{
printf("输入错误,请重新输入!\n");
while (getchar() != '\n');//清空输入缓存
continue;//跳过下面的内容,重新开始循环
}
switch (case_num)
{
case 1:
yl_weight += get_weight(case_num);
//debug printf("TEST:%g\n",yl_weight);
show_info();
break;
case 2:
tc_weight += get_weight(case_num);
show_info();
break;
case 3:
hlb_weight += get_weight(case_num);
show_info();
break;
case 4:
default:
printf("程序退出!\n");
goto end;//跳出两重循环直接到程序结尾
break;
}
}
end:return 0;
}

C Primer Plus 第6版 第七章 编程练习参考答案的更多相关文章

  1. 《C++ Primer》 第四版 第7章 函数

    <C++ Primer> 第四版 第7章 函数 思维导图笔记 超级具体.很具体,图片版,有利于复习查看 http://download.csdn.net/detail/onlyshi/94 ...

  2. c primer plus(五版)编程练习-第七章编程练习

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  3. C primer plus 第五版十二章习题

    看完C prime plus(第五版)第十二章,随带完成了后面的习题. 1.不使用全局变量,重写程序清单12.4的程序. 先贴出12.4的程序,方便对照: /* global.c --- 使用外部变量 ...

  4. Java编程思想第四版*第七章*个人练习

    欢迎加群:239063848 成团的笔记:该组仅用于技术共享和交流,问题和答案公布 潘基聊天.禁止广告.禁止招聘-- 练习1:(2)创建一个简单的类.第二个类中,将一个引用定义为第一个类的对象.运用惰 ...

  5. java编程思想第四版第七章习题

    (略) (略) (略) (略) 创建两个带有默认构造器(空参数列表)的类A和类B.从A中继承产生一个名为C的新,并在C内创建一个B类的成员.不要给C编写构造器.创建一个C类的对象并观察其结果. pac ...

  6. java编程思想第四版第七章总结

    1. 实现类的复用通常有两种方式 组合:在新的类中产生现有类的对象 继承:按照现有类的类型来创造新类 2. 一个特殊的方法toString() 在非基本类型的对象中, 都有toString()方法 当 ...

  7. C++ Primer Plus(第6版)习题(第二章)

    1..编写一个C++程序,它显示您的姓名和地址. #include<iostream> using namespace std; int main() { string name,addr ...

  8. C#高级编程第11版 - 第七章 索引

    [1]7.1 相同类型的多个对象 1.假如你需要处理同一类型的多个对象,你可以使用集合或者数组. 2.如果你想使用不同类型的不同对象,你最好将它们组合成class.struct或者元组. [2]7.2 ...

  9. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂

    C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...

  10. C Primer Plus(第五版)1

    这是C Primer Plus(第五版)的第一章,上传上来主要是方便我进行做笔记,写注释,还有我会删掉一些“废话”等. 1.1 C语言的起源 贝尔实验室的 Dennis Ritchie 在1972年开 ...

随机推荐

  1. CentOS8安装RabbitMQ3.8.16

    之前安装过旧版的RabbitMQ和Erlang,先卸载. ①:卸载RabbitMQ /sbin/service rabbitmq-server stop yum list | grep rabbitm ...

  2. 同步完善Docker常用操作命令

    镜像 images_name 表示镜像名 con_name表示容器名 #获取镜像 docker pull images_name #查看已有的docker镜像 docker images #删除镜像 ...

  3. 函数(C语言)

    目录 1. 函数的概念 2. 库函数 2.1 标准库和头文件 2.2 库函数的使用方法 3. 自定义函数 3.1 函数的语法形式 3.2 函数的举例 4. 形参和实参 4.1 实参 4.2 形参 4. ...

  4. 一个整合性、功能丰富的.NET网络通信框架

    前言 最近有不少同学问:.NET网络通信框架有什么好推荐的吗?今天大姚给大家分享一款基于Apache License开源的一个整合性.功能丰富的.NET(包括 C# .VB.Net.F#)网络通信框架 ...

  5. OpenGL常用函数整理

    常用函数 颜色设置 glClear(GL_COLOR_BUFFER_BIT); //清空颜色,GL_COLOR_BUFFER_BIT是颜色缓冲区 glClearColor(R,G,B,A); //设置 ...

  6. NES 名词解释

    本文介绍了 NES(FC.红白机.小霸王)中一些名词或者术语,主要与 PPU 有关. Tile 8x8 像素图像.每像素 2 比特, 共 16 字节大小.每个像素可以使用 4 种颜色. Sprite ...

  7. 周六晚11实习生上线数据观测突发问题(涉及MYSQL,Hive等)

    前提 有点恐怖,上次需求上线后,部分线上数据观测要留到11月初,上次是一个税收相关的需求,已有功能的线上数据观察已经完成,还剩下部分只有在十一月初才可以观察 简单提一嘴(非技术相关) 之前把hive ...

  8. ubuntu系统下安装 steam 游戏平台

    方法1:安装命令: sudo snap install steam 方法2:下载安装: 地址: https://store.steampowered.com/about/

  9. 基于Java+SpringBoot+Mysql实现的古诗词平台功能设计与实现六

    一.前言介绍: 1.1 项目摘要 随着信息技术的迅猛发展和数字化时代的到来,传统文化与现代科技的融合已成为一种趋势.古诗词作为中华民族的文化瑰宝,具有深厚的历史底蕴和独特的艺术魅力.然而,在现代社会中 ...

  10. .NET 创建动态方法方案及 Natasha V9

    前言 本篇文章前面客观评估了 .NET 创建动态方方案多个方面的优劣,后半部分是 Natasha V9 的新版特性. .NET 中创建动态方法的方案 创建动态方法的不同选择 以下陈列了几种创建动态方法 ...