1.编写一个程序读取输入, 读到#字符停止, 然后报告读取的空格数、换行符数和所有其他字符的数量

#include<stdio.h>
int main(void)
{char ch;int space, linebreak, other;space = linebreak = other = 0;printf("Enter something, # to quit\n");while ((ch = getchar()) != '#'){if (ch == ' ')space++;else if (ch == '\n')linebreak++;elseother++;}printf("There are %d blank %s, %d line %s and %d other %s.\n",space, (space>1? "spaces":"space"), linebreak, (linebreak>1? "linebreaks":"linebreak"),other, (other>1? "characters":"character"));return 0;
}

2.编写一个程序读取输入, 读到#字符停止。 程序要打印每个输入的字符以及对应的ASCII码(十进制) 。 一行打印8个字符。 建议:使用字符计数和求模运算符(%) 在每8个循环周期时打印一个换行符。

#include<stdio.h>
int main(void)
{char ch;int sum = 0;printf("Enter some characters, # to quit\n");while ((ch = getchar()) != '#'){sum++;printf("%c-%d\t", ch, ch);if (sum % 8 == 0)printf("\n");}return 0;
}

3.编写一个程序, 读取整数直到用户输入 0。 输入结束后, 程序应报告用户输入的偶数(不包括 0) 个数、 这些偶数的平均值、 输入的奇数个数及其奇数的平均值。

#include<stdio.h>
int main(void)
{int num;int even, odd;float even_sum, odd_sum;even = odd= 0;even_sum = odd_sum = 0.0;printf("Enter some integers, 0 to quit\n");while ((scanf("%d", &num) == 1) && (num != 0)){if (num % 2){odd++;odd_sum += num;}else{even++;even_sum += num;}}printf("even numbers:%d, average:%g, odd numbers:%d, average:%g\n", even, even_sum/even, odd, odd_sum/odd);return 0;
}

4.使用if else语句编写一个程序读取输入, 读到#停止。 用感叹号替换句号, 用两个感叹号替换原来的感叹号, 最后报告进行了多少次替换。

#include<stdio.h>
int main(void)
{char ch;int sub = 0;printf("Enter some sentences, # to quit\n");while ((ch = getchar()) != '#'){if (ch == '.'){putchar('!');sub++;}else if (ch == '!'){putchar('!');putchar('!');sub++;}elseputchar(ch);}printf("\nsubstitution: %d\n", sub);return 0;
}

5.使用switch重写练习4

#include<stdio.h>
int main(void)
{char ch;int sub = 0;printf("Enter some sentences, # to quit\n");while ((ch = getchar()) != '#'){switch (ch){case '.':putchar('!');sub++;break;case '!':putchar('!');putchar('!');sub++;break;default:putchar(ch);}}printf("\nsubstitution: %d\n", sub);return 0;
}

6.编写程序读取输入, 读到#停止, 报告ei出现的次数。
注意
该程序要记录前一个字符和当前字符。 用“Receive your eieio award”这样的输入来测试

#include<stdio.h>
int main(void)
{char ch, pre;pre = 'a';int n = 0;printf("Enter some characters, # to quit\n");while ((ch = getchar()) != '#'){if (pre == 'e' && ch == 'i')n++;pre = ch;}printf("\"ei\" arises %d times.\n", n);return 0;
}

7.编写一个程序, 提示用户输入一周工作的小时数, 然后打印工资总额、 税金和净收入。 做如下假设:
a.基本工资 = 1000美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15%
续150美元为20%
余下的为25%
用#define定义符号常量。 不用在意是否符合当前的税法。

#include<stdio.h>
#define TIME 40
#define BASE 10.00
#define BASE_WAGE (TIME * BASE)
#define ADD 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define TAX1 (BREAK1 * RATE1)
#define TAX2 TAX1 + (RATE2 * (BREAK2 - BREAK1))
int main(void)
{float time, wage, tax, income;printf("Enter the working time (hour)\n");scanf("%f", &time);if (time <= TIME)wage = BASE * time;elsewage = BASE_WAGE + ADD * (time - TIME) * BASE;if (wage <= BREAK1)tax = RATE1 * wage;else if (wage > BREAK1 && wage < BREAK2)tax = TAX1 + RATE2 * (wage - BREAK1);elsetax = TAX2 + RATE3 * (wage - BREAK2);income = wage - tax;printf("The wage is $%.2f, tax is $%.2f, net income is $%.2f\n", wage, tax, income);return 0;
}

8.修改练习7的假设a, 让程序可以给出一个供选择的工资等级菜单。 使用switch完成工资等级选择。 运行程序后, 显示的菜单应该类似这样:


Enter the number corresponding to the desired pay rate or action:

  1. $8.75/hr 2) $9.33/hr
  2. $10.00/hr 4) $11.20/hr
  3. quit

如果选择 1~4 其中的一个数字, 程序应该询问用户工作的小时数。 程序要通过循环运行, 除非用户输入 5。 如果输入 1~5 以外的数字, 程序应提醒用户输入正确的选项, 然后再重复显示菜单提示用户输入。 使用#define创建符号常量表示各工资等级和税率。

#include<stdio.h>
#define TIME 40.0
#define PAY1 8.75
#define PAY2 9.33
#define PAY3 10.00
#define PAY4 11.20
#define ADD 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define TAX1 (BREAK1 * RATE1)
#define TAX2 TAX1 + (RATE2 * (BREAK2 - BREAK1))
int main(void)
{float time, wage, tax, income, pay;int num;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");while (scanf("%d", &num)){if (num == 5)break;else if (num < 1 || num > 5){printf("Enter the correct option\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");continue;}switch (num){case 1:pay = PAY1;break;case 2:pay = PAY2;break;case 3:pay = PAY3;break;case 4:pay = PAY4;break;}printf("Enter the working time (hour)\n");scanf("%f", &time);if (time <= TIME)wage = pay * time;elsewage = pay * (TIME + ADD * (time - TIME));if (wage <= BREAK1)tax = RATE1 * wage;else if (wage > BREAK1 && wage < BREAK2)tax = TAX1 + RATE2 * (wage - BREAK1);elsetax = TAX2 + RATE3 * (wage - BREAK2);income = wage - tax;printf("The wage is $%.2f, tax is $%.2f, net income is $%.2f\n", wage, tax, income);printf("\nEnter the number corresponding to the desired pay rate or action:\n");}return 0;
}

9.编写一个程序, 只接受正整数输入, 然后显示所有小于或等于该数的素数。

#include<stdio.h>
int main(void)
{int num, i, prime, sum;printf("Enter an positive integer\n");while (scanf("%d", &num)){if (num > 0)break;elseprintf("Enter an positive integer\n");}if (num == 1)printf("There is no integer less than or equal to 1.\n");else{printf("There are integers less than or equal to %d: 2  ", num);for (prime = 3; prime <= num; prime++){for (i = 2, sum = 0; i*i <= prime; i++)if (prime % i == 0){sum++;break;}if (!sum)printf("%d  ", prime);}}printf("\n");return 0;
}

10.1988年的美国联邦税收计划是近代最简单的税收方案。 它分为4个类别, 每个类别有两个等级。
下面是该税收计划的摘要(美元数为应征税的收入):

例如, 一位工资为20000美元的单身纳税人, 应缴纳税费0.15×17850+0.28×(20000-17850) 美元。 编写一个程序, 让用户指定缴纳税金的种类和应纳税收入, 然后计算税金。 程序应通过循环让用户可以多次输入

#include<stdio.h>
#define BASE 0.15
#define ADD 0.28
#define BREAK_SINGLE 17850.0
#define BREAK_HOUSEHOLD 23900.0
#define BREAK_MARRIED_JOINT 29750.0
#define BREAK_MARRIED_DIVORCED 14875.0
#define BASE_SINGLE (BASE * BREAK_SINGLE)
#define BASE_HOUSEHOLD (BASE * BREAK_HOUSEHOLD)
#define BASE_MARRIED_JOINT (BASE * BREAK_MARRIED_JOINT)
#define BASE_MARRIED_DIVORCED (BASE * BREAK_MARRIED_DIVORCED)
int main(void)
{int num;float income, tax, bre, base;printf("Enter the number to select your tax class, q to quit\n");printf("1.single   2.household   3.married joint   4.married divorced\n");while (scanf("%d", &num)){if (num < 1 || num > 4){printf("Enter the number to select your tax class, q to quit\n");printf("1.single\t2.household\t3.married joint\t4.married divorced\n");continue;}switch (num){case 1:bre = BREAK_SINGLE;base = BASE_SINGLE;break;case 2:bre = BREAK_HOUSEHOLD;base = BASE_HOUSEHOLD;break;case 3:bre = BREAK_MARRIED_JOINT;base = BASE_MARRIED_JOINT;break;case 4:bre = BREAK_MARRIED_DIVORCED;base = BASE_MARRIED_JOINT;break;}printf("Enter your income to calculate tax\n");scanf("%f", &income);if (income <= bre)tax = BASE * income;elsetax = base + ADD * (income - bre);printf("The tax is $%.2f.\n", tax);printf("Enter the number to select your tax class, q to quit\n");}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>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
#define DISCOUNT 0.05
#define FARE 100.0
#define SHIPPING1 6.50
#define BREAK1 5.0
#define SHIPPING2 14.00
#define BREAK2 20.0
#define PLUS 0.50
int main(void)
{char ch;float pound, pound_sum, artichoke, beet, carrot, discount, cost, shipping, grand;pound_sum = artichoke = beet = carrot = discount = 0.0;printf("Enter the corresponding character to order vegetables, q to quit\n");while ((ch = getchar()) != 'q'){switch (ch){case 'a':printf("Enter the pounds of artichokes desired:\n");scanf("%f", &pound);artichoke += pound;break;case 'b':printf("Enter the pounds of beets desired:\n");scanf("%f", &pound);beet += pound;break;case 'c':printf("Enter the pounds of carrots desired:\n");scanf("%f", &pound);carrot += pound;break;}printf("Enter the corresponding character to order vegetables, q to quit\n");}cost = ARTICHOKE * artichoke + BEET * beet + CARROT * carrot;if (cost >= FARE)discount = DISCOUNT * cost;pound_sum = artichoke + beet + carrot;if (pound_sum <= BREAK1)shipping = SHIPPING1;else if (pound_sum <= BREAK2)shipping = SHIPPING2;elseshipping = SHIPPING2 + PLUS * (pound_sum - BREAK2);grand = cost - discount + shipping;printf("\nthe cost per pound:\n");if (artichoke)printf("artichokes $%g\t", ARTICHOKE);if (beet)printf("beets $%g\t", BEET);if (carrot)printf("carrots $%g\t", CARROT);printf("\nthe pounds ordered:\n");if (artichoke)printf("artichokes %g\t", artichoke);if (beet)printf("beets %g\t", beet);if (carrot)printf("carrots %g\t", carrot);printf("\nthe cost for each vegetable:\n");if (artichoke)printf("artichokes $%g\t", ARTICHOKE * artichoke);if (beet)printf("beets $%g\t", BEET * beet);if (carrot)printf("carrots $%g\t", CARROT * carrot);printf("\nthe total cost: $%g\n", cost);if (discount)printf("the discount: $%g\n", discount);printf("the shipping charge: $%g\n", shipping);printf("the grand total of all the charges: $%g\n", grand);return 0;
}

ps:这个程序有bug,while循环每隔一次不运行switch,但是本菜鸡现在还找不出。

找到问题了!getchar()会在scanf读取数字之后读取换行符。
上面的运行过程是这样的:进入对应的switch语句后,提示输入订购数目,输入数字,enter后会产生一个换行符‘\n’,scanf读取数字,getchar()读取’\n’作为ch,开启新的循环,将ch与各个case逐一对比,没有发现相同的,于是运行 printf(“Enter the corresponding character to order vegetables, q to quit\n”)语句,就出现了上面的结果。

修改后的程序如下:

#include<stdio.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
#define DISCOUNT 0.05
#define FARE 100.0
#define SHIPPING1 6.50
#define BREAK1 5.0
#define SHIPPING2 14.00
#define BREAK2 20.0
#define PLUS 0.50
int main(void)
{char ch;float pound, pound_sum, artichoke, beet, carrot, discount, cost, shipping, grand;pound_sum = artichoke = beet = carrot = discount = 0.0;printf("Enter the corresponding character to order vegetables, q to quit\n");while ((ch = getchar()) != 'q'){if ((ch >= 'a' && ch <= 'z')){switch (ch){case 'a':printf("Enter the pounds of artichokes desired:\n");scanf("%f", &pound);artichoke += pound;break;case 'b':printf("Enter the pounds of beets desired:\n");scanf("%f", &pound);beet += pound;break;case 'c':printf("Enter the pounds of carrots desired:\n");scanf("%f", &pound);carrot += pound;break;}printf("Enter the corresponding character to order vegetables, q to quit\n");}}cost = ARTICHOKE * artichoke + BEET * beet + CARROT * carrot;if (cost >= FARE)discount = DISCOUNT * cost;pound_sum = artichoke + beet + carrot;if (pound_sum <= BREAK1)shipping = SHIPPING1;else if (pound_sum <= BREAK2)shipping = SHIPPING2;elseshipping = SHIPPING2 + PLUS * (pound_sum - BREAK2);grand = cost - discount + shipping;printf("\nthe cost per pound:\n");if (artichoke)printf("artichokes $%g\t", ARTICHOKE);if (beet)printf("beets $%g\t", BEET);if (carrot)printf("carrots $%g\t", CARROT);printf("\nthe pounds ordered:\n");if (artichoke)printf("artichokes %g\t", artichoke);if (beet)printf("beets %g\t", beet);if (carrot)printf("carrots %g\t", carrot);printf("\nthe cost for each vegetable:\n");if (artichoke)printf("artichokes $%g\t", ARTICHOKE * artichoke);if (beet)printf("beets $%g\t", BEET * beet);if (carrot)printf("carrots $%g\t", CARROT * carrot);printf("\nthe total cost: $%g\n", cost);if (discount)printf("the discount: $%g\n", discount);printf("the shipping charge: $%g\n", shipping);printf("the grand total of all the charges: $%g\n", grand);return 0;
}

C primer plus编程练习-第7章相关推荐

  1. C Primer Plus编程题-第五章 运算符、表达式和语句

    第一题: 编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创建一个表示60的符号常量或const变量.通过while循环让用户重复输入值,直到用户输入小于或 ...

  2. C++ Primer Plus课后编程练习第6章参考代码

    (C++ Primer Plus课后编程练习第6章参考代码) 声明: 作者入门小白,将学习过程中的代码做一些分享,仅供大家参考,欢迎大家交流指正.全部编译运行过,水平有限,不喜勿喷. 环境: Wind ...

  3. C++ Primer 第五版 第7章类 7.1——类讲解(成员函数、非成员函数、构造函数)

    习题答案请参考:C++ Primer 第五版 第7章类 7.1--类讲解(成员函数.非成员函数.构造函数)习题答案 目录 7.1 类讲解(成员函数.非成员函数.构造函数) 成员函数 this cons ...

  4. C++ Primer 第五版 第6章 6.3——函数返回类型和return语句习题答案

    理论讲解请参考:C++ Primer 第五版 第6章 6.3--函数返回类型和return语句阅读笔记 目录 6.31 6.32 6.33 6.34 6.35 6.36 6.38 6.31 返回引用无 ...

  5. 整理:C Primer Plus 编程练习

    第一章 无 第二章 C Primer Plus 第6版 第2章 编程练习 第三章C Primer Plus 第6版 第3章 编程练习 第四章C Primer Plus 第6版 第4章 编程练习 第五章 ...

  6. C prime plus 第六版 课后编程练习 第7章

    C prime plus 第六版 课后编程练习 第7章 7.12.1 编写一个程序读取输入,读到#字符停止,然后报告读取的空格数.换行符数和所有其他字符的数量. 7.12.2.编写一个程序读取输入,读 ...

  7. C++ Primer 学习笔记(第四章:表达式)

    2019独角兽企业重金招聘Python工程师标准>>> ##C++ Primer 学习笔记(第四章:表达式) [TOC] ###4.1 基础 左值和右值: 当一个对象被用作右值的时候 ...

  8. IA-32系统编程指南 - 第三章 保护模式的内存管理【1】

    第三章 保护模式的内存管理[1] [作者:lion3875 原创文章 参考文献<Intel 64 and IA-32 system programming guide>] IA-32保护模 ...

  9. 《Python编程快速上手——让繁琐工作自动化》——第一部分 Python编程基础 第1章 Python基础 1.1 在交互式环境中输入表达式...

    本节书摘来自异步社区<Python编程快速上手--让繁琐工作自动化>一书中的第1章,第1.1节,作者[美] Al Sweigart,王海鹏 译,更多章节内容可以访问云栖社区"异步 ...

最新文章

  1. 六数码问题(广搜_队列)
  2. Servlet架构初解析
  3. Linux中如何恢复被误删的数据文件
  4. 计算机视觉与深度学习 | 目标检测综述(RCNN、RPN、YOLOv1 v2 v3、FPN、Mask RCNN、SSD代码类)
  5. 用户 NT AUTHORITY\NETWORK SERVICE 登录失败解决方法
  6. 【转】17.Qt界面布局管理详解
  7. Project查看资源分配情况
  8. 微信小程序 - 实践- 001-实现一个多TAB的菜单框架
  9. php srem,SREM命令_视频讲解_用法示例-redis编程词典-php中文网
  10. 我的世界服务器怎么修改矿物,我的世界怎么设置自定义矿物
  11. SQL Server中的文件流
  12. string的operate+=
  13. 电位器替换遥控器做远程遥控方案,远程控制云台方案
  14. C++ Primer 第三章 标准库类型 笔记
  15. 阿里云域名注册流程(图文)
  16. Java doc或docx转pdf文件预览
  17. 高性能网络编程-反应堆模型(reactor)
  18. VO,DTO,BO,POJO,PO的概念介绍
  19. Android 新版支付宝API开发
  20. golang 万年历的实现代码

热门文章

  1. Planet比Google earth更好用的地图下载神器Basemaps Viewer不用写代码全球高清影像框选下载tif格式
  2. Java人才招聘求职管理系统源码前后端带文字搭建教程
  3. Oracle时间函数 to_char()及常用时间格式
  4. NDM的网络配置文件netmap.cfg
  5. 中国丙泊酚行业竞争与投资前景研究报告(2021版)
  6. 前端JS导出Excel表格 可筛选列 table2excel
  7. freecodecamp_freeCodeCamp如何使我成为最新的训练营毕业生
  8. swift 获取导航栏底部线
  9. java自定义标签库的开发
  10. 苹果手机计算机报不了名,iPhone8无法连接电脑并且不弹出信任对话框怎么办?...