编译环境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. VictoriaMetrics 中文教程(10)集群版介绍

    VictoriaMetrics 中文教程系列文章: VictoriaMetrics 中文教程(01)简介 VictoriaMetrics 中文教程(02)安装 VictoriaMetrics 中文教程 ...

  2. DashText-快速开始

    快速开始 DashText,是向量检索服务DashVector推荐使用的稀疏向量编码器(Sparse Vector Encoder),DashText可通过BM25算法将原始文本转换为稀疏向量(Spa ...

  3. 矩阵怪 - 2024全新矩阵产品,一键分发抖音,快手,视频号,B站,小红书!

    本方案面向谁,解决了什么问题 本方案主要面向C端客户,特别是那些在各大短视频平台(如小红书.抖音.视频号.快手.B站等)上进行内容创作和分发的个人用户.自由职业者.小型团队或企业.这些用户通常面临着在 ...

  4. ABC370 E - Avoid K Partition

    ABC370 E - Avoid K Partition 求一个序列的合法划分方案数.一种划分合法当且仅当没有一个子串的和是 \(k\). 由于是否存在子串和为 \(k\) 很重要,因此考虑将它加入状 ...

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

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

  6. 题解:CF1537E2 Erase and Extend (Hard Version)

    CF1537E2 Erase and Extend 题解 分析 通过观察题目,可以证明结果一定是由多次前缀复制得来的. 题目要求你进行删和复制的操作,与其交替着操作,不如直接先删到最优的前缀再进行复制 ...

  7. 一条语句查看web日志排名前十的IP/URL页面及总数

    查看 Nginx web 访问日志访问量前十的ip 以及访问的网站页面地址 ,可以分析网站哪些页面受欢迎,以及访问量大的ip在干什么! 标签: <无> 代码片段(3)[全屏查看所有代码] ...

  8. Lucene 源代码剖析-2 Lucene是什么

    转载自 http://download.csdn.net/source/858994 源地址下是 Word 文档,这里转换成HTML 格式 1           Lucene是什么 Apache L ...

  9. vue之slot和slot-scope

    插槽分两类,默认插槽和具名插槽:通俗理解就是默认插槽是没有名称的插槽,具名插槽是有名称的插槽. 何时使用插槽?简单的举个栗子:有2个组件,父组件father,子组件son. 父组件 father &l ...

  10. 进程管理工具之supervisor(完整版)*

    Supervisor 介绍 Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.它是通过fork/ex ...