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

#include <stdio.h>
#define STOP '#'
#define SPACE ' ' int main(void)
{int ch, space, enter, others;space = enter = others = 0;printf("请输入信息(输入#结束):\n");while ((ch = getchar()) != STOP)    //字符输入的方式,读到'#'停止{if (ch == SPACE){space++;}else if (ch == '\n'){enter++;}else{others++;}}printf("打印结果是:\n");printf("空格:%10d个\n", space);printf("换行符:%8d个\n", enter);printf("其它字符:%6d个\n", others);return 0;
}

2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符。3.编写一个程序,读取整数直到用户输入0。输入结束后,程序应报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值。

#include <stdio.h>
#define STOP '#'int main(void)
{int ch;int i = 0;printf("请输入信息(输入#结束):\n");while ((ch = getchar()) != STOP){if (i++ % 8 == 0){printf("\n");}if (ch == '\n'){printf("'\\n'-%3d ", ch);}else if (ch == '\t'){printf("'\\t'-%3d ", ch);}else{printf("'%c'-%3d ", ch, ch);}}printf("\nDone!\n");return 0;
}

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

#include <stdio.h>int main(void)
{int n, odd, even;int e_sum, o_sum;odd = even = 0;e_sum = o_sum = 0;printf("请输入一个整数(0退出程序):");while ((scanf("%d", &n) == 1) && (n != 0)){if (n % 2 == 0){even++;e_sum += n;}else{odd++;o_sum += n;}printf("您可以再次输入(0则退出):");}printf("偶数:%d个\n", even);if (even > 0){printf("偶数的平均值是%g\n", (float)e_sum / even);}printf("奇数:%d个\n", odd);if (odd > 0){printf("奇数的平均值是%g\n", (float)o_sum / odd);}printf("本程序完成!\n");return 0;
}

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

#include <stdio.h>
#define STOP '#'int main(void)
{int ch;int n = 0;printf("请输入信息(输入#结束):\n");while ((ch = getchar()) != STOP){if (ch == '.'){putchar('!');n++;}else if (ch == '!'){printf("!!");n++;}else{putchar(ch);}}printf("\n共替代了%d次.\n", n);printf("('.'->'!') or ('!'->'!!').\n");return 0;
}

5.使用switch重写练习4。

#include <stdio.h>
#define STOP '#'int main(void)
{int ch;int n = 0;printf("请输入信息(输入#结束):\n");while ((ch = getchar()) != STOP){switch (ch){case '.':{putchar('!');n++;break;}case '!':{printf("!!");n++;break;}default:{putchar(ch);}}}printf("\n共出现了%d次.\n", n);printf("('.'->'!') or ('!'->'!!').\n");return 0;
}

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

#include <stdio.h>
#define STOP '#'int main(void)
{int ch;int n = 0;int flag = 0;printf("请输入一个整数(0退出程序):\n");while ((ch = getchar()) != STOP){switch (ch){case 'e':{flag = 1; break;}case 'i':{if (1 == flag){n++;}flag = 0;break;}default:{flag = 0;}}}printf("\n共出现了%d次 \'ei\'.\n", n);return 0;
}

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

#include <stdio.h>
#define BASE_SALARY 10.0f
#define EXTRA_HOUR 1.5f
#define BASE_TAX 0.15f
#define EXTRA_TAX 0.2f
#define EXCEED_TAX 0.25fint main(void)
{float hours = 0.0f;float salary, tax, taxed_salary;printf("Enter the working hours a week:");while ((!scanf("%f", &hours)) || (hours < 0)){while (getchar() != '\n')continue;printf("Please enter a positive number:");}if (hours <= 30){salary = hours * BASE_SALARY;tax = salary * BASE_TAX;taxed_salary = salary - tax;}else if (hours <= 40){salary = hours * BASE_SALARY;tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;taxed_salary = salary - tax;}else{salary = (40 + (hours - 40) * EXTRA_HOUR) * BASE_SALARY;if (salary <= 450){tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;}else{tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;}taxed_salary = salary - tax;}printf("salary(before tax):$%g\n", salary);printf("tax:$%g\n", tax);printf("salary(after tax):$%g\n", taxed_salary);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
3)$10.00 /hr
4)$11.20 /hr
5) quit
如果选择1-4其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入5。如果输入1~5以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。

#include <stdio.h>
#include <ctype.h>
#define EXTRA_HOUR 1.5f
#define BASE_TAX 0.15f
#define EXTRA_TAX 0.2f
#define EXCEED_TAX 0.25fint show_menu(void);
void show_salary(float base_salary, float hours);
int get_choice(void);
void eatline(void);int main(void)
{int ch;float n;while ((ch = show_menu()) != 5){printf("Enter the working hours a week:");while ((!scanf("%f", &n)) || (n < 0)){eatline();printf("Enter a positive number:");}eatline();switch (ch){case 1:{show_salary(8.75f, n);break;}case 2:{show_salary(9.33f, n);break;}case 3:{show_salary(10.00f, n);break;}case 4:{show_salary(11.20f, n);break;}}putchar('\n');}printf("Done!\n");return 0;
}int get_choice(void)
{int ch = 0;scanf("%d", &ch);eatline();return ch;
}void eatline(void)
{while (getchar() != '\n')continue;return;
}int show_menu(void)
{int ch;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 you choose:");ch = get_choice();while (ch != 1 && ch != 2 && ch != 3 && ch != 4 && ch != 5){printf("Please enter 1,2,3,4 or 5:");ch = get_choice();}return ch;
}void show_salary(float base_salary, float hours)
{float salary, tax, taxed_salary;if (hours <= 30){salary = hours * base_salary;tax = salary * BASE_TAX;taxed_salary = salary - tax;}else if (hours <= 40){salary = hours * base_salary;tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;taxed_salary = salary - tax;}else{salary = (40 + (hours - 40) * EXTRA_HOUR) * base_salary;if (salary <= 450){tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;}else{tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;}taxed_salary = salary - tax;}printf("salary(before tax):$%g\n", salary);printf("tax:$%g\n", tax);printf("salary(after tax):$%g\n", taxed_salary);return;
}

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

#include <stdio.h>
#include <math.h>int main(void)
{int i, n, number, prime;printf("请您输入一个正整数(<=0退出):");while ((scanf("%d", &number) == 1) && (number > 0)){if (number == 1){printf("1不是素数!\n");printf("您可以再次输入一个正整数(<=0退出):");continue;}printf("小于或等于%d的素数有:\n", number);for (i = 2; i <= number; i++){prime = 1;for (n = 2; n <= sqrt(i); n++){if (i % n == 0){prime = 0;break;}}if (prime){printf("%-3d", i);}}printf("\n您可以再次输入一个正整数(<=0退出):");}printf("本程序完成!\n");return 0;
}

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

类别            税金
单身      17850美元按15%计,超出部分按28%计
户主      23900美元按15%计,超出部分按28%计
已婚,共有   29750美元按15%计,超出部分按28%计
已婚,离异   14875美元按15%计,超出部分按28%计

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

#include <stdio.h>
#define PLAN1 17850
#define PLAN2 23900
#define PLAN3 29750
#define PLAN4 14875
#define RATE1 0.15
#define RATE2 0.28int main(void)
{int n;double wage, tax;while (1){printf("********************************\n");printf("1)单身              2)户主\n");printf("3)已婚,共有        4)已婚,离异\n");printf("5)退出本程序\n");printf("********************************\n");printf("请您选择您的身份(输入5退出本程序):");while ((!scanf("%d", &n)) || (n > 5 || n < 1)){while (getchar() != '\n')continue;printf("请重新输入1,2,3,4或5:");}if (1 == n){printf("请输入您的工资:");scanf("%lf", &wage);if (wage <= PLAN1){tax = wage * RATE1;printf("您需要交$%g税金\n\n", tax);}else{tax = PLAN1 * RATE1 + (wage - PLAN1) * RATE2;printf("您需要交$%g税金\n\n", tax);}}else if (2 == n){printf("请输入您的工资:");scanf("%lf", &wage);if (wage <= PLAN2){tax = wage * RATE1;printf("您需要交$%g税金\n\n", tax);}else{tax = PLAN2 * RATE1 + (wage - PLAN2) * RATE2;printf("您需要交$%g税金\n\n", tax);}}else if (3 == n){printf("请输入您的工资:");scanf("%lf", &wage);if (wage <= PLAN3){tax = wage * RATE1;printf("您需要交$%g税金\n\n", tax);}else{tax = PLAN3 * RATE1 + (wage - PLAN3) * RATE2;printf("您需要交$%g税金\n\n", tax);}}else if (4 == n){printf("请输入您的工资:");scanf("%lf", &wage);if (wage <= PLAN4){tax = wage * RATE1;printf("您需要交$%g税金\n\n", tax);}else{tax = PLAN4 * RATE1 + (wage - PLAN4) * RATE2;printf("您需要交$%g税金\n\n", tax);}}else if (5 == n){break;}}printf("本程序完成!\n");return 0;
}

11.ABC邮购杂货店出售的洋蓟售价为2.05美元/磅,甜菜售价为1.15美元/磅,胡萝卜售价为1.09美元/磅。在添加运费之前,100美元的订单有5%的打折优惠。少于或等于5磅的订单收取6.5美元的运费和包装费,5磅~20磅的订单收取14美元的运费和包装费,超过20磅的订单在14美元的基础上每续重Ⅰ磅增加0.5美元。编写一个程序,在循环中用switch语句实现用户输入不同的字母时有不同的响应,即输入a的响应是让用户输入洋蓟的磅数,b是甜菜的磅数,c是胡萝卜的磅数,q是退出订购。程序要记录累计的重量。即,如果用户输入4磅的甜菜,然后输入5磅的甜菜,程序应报告9磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。

#include <stdio.h>
#include <ctype.h>int main(void)
{const double price_artichokes = 2.05;const double price_beets = 1.15;const double price_carrots = 1.09;const double DISCOUNT_RATE = 0.05;const double under5 = 6.50;const double under20 = 14.00;const double base20 = 14.00;const double extralb = 0.50;int ch;double lb_artichokes = 0;double lb_beets = 0;double lb_carrots = 0;double lb_temp;double lb_total;double cost_artichokes;double cost_beets;double cost_carrots;double cost_total;double final_total;double discount;double shipping;printf("Enter a to buy artichokes, b for beets, ");printf("c for carrots, q to quit: ");while ((ch = tolower(getchar())) != 'q'){if (isspace(ch)){continue;}while (getchar() != '\n')continue;switch (ch){case 'a':{printf("Enter pounds of artichokes: ");scanf("%lf", &lb_temp);lb_artichokes += lb_temp;break;}case 'b':{printf("Enter pounds of beets: ");scanf("%lf", &lb_temp);lb_beets += lb_temp;break;}case 'c':{printf("Enter pounds of carrots: ");scanf("%lf", &lb_temp);lb_carrots += lb_temp;break;}default:{printf("%c is not a valid choice.\n", ch);}}printf("Enter a to buy artichokes, b for beets, ");printf("c for carrots, q to quit: ");}cost_artichokes = price_artichokes * lb_artichokes;cost_beets = price_beets * lb_beets;cost_carrots = price_carrots * lb_carrots;cost_total = cost_artichokes + cost_beets + cost_carrots;lb_total = lb_artichokes + lb_beets + lb_carrots;if (lb_total <= 0){shipping = 0.0;}else if (lb_total < 5.0){shipping = under5;}else if (lb_total < 20.0){shipping = under20;}else{shipping = base20 + extralb * lb_total;}if (cost_total > 100.0){discount = DISCOUNT_RATE * cost_total;}else{discount = 0.0;}final_total = cost_total + shipping - discount;printf("Your order:\n");printf("%.2f lbs of artichokes at $%.2f per pound:$ %.2f\n",lb_artichokes, price_artichokes, cost_artichokes);printf("%.2f lbs of beets at $%.2f per pound: $%.2f\n",lb_beets, price_beets, cost_beets);printf("%.2f lbs of carrots at $%.2f per pound: $%.2f\n",lb_carrots, price_carrots, cost_carrots);printf("Total cost of vegetables: $%.2f\n", cost_total);if (cost_total > 100){printf("Volume discount: $%.2f\n", discount);}printf("Shipping: $%.2f\n", shipping);printf("Total charges: $%.2f\n", final_total);return 0;
}

C Primer Plus第六版第七章编程题目与参考答案⭐相关推荐

  1. C Primer Plus第六版第四章编程题目与参考答案⭐

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. #include <stdio.h>int main() {char firstname[20] ...

  2. C Primer Plus (第六版) 第七章 7.11编程练习 参考答案

    11.ABC 邮购杂货店出售的洋蓟售价为 2.05 美元/磅,甜菜售价为 1.15美元/磅,胡萝卜售价为 1.09美元/磅.在添加运费之前,100美元的订单有5%的打折优惠.少于或等于5磅的订单收取6 ...

  3. c primer plus第六版 第七章

    /*1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格 数.换行符数和所有其他字符的数量.*/ #include<stdio.h> #include<ctype.h> ...

  4. C++primer plus第六版第四章编程题代码

    1. #include "stdafx.h" #include <iostream> #include <string> #include <vect ...

  5. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  6. 数据库系统概念第六版 第七章练习题 15 22

    数据库第七章理论习题 7.15 为医院构造一个包含一组病人和一组医生的E-R图.为每个病人关联一组不同的检查和化验记录. 说明:医生和病人间存在联系,同时病人有特有的检查和化验记录,病人有病床,医生属 ...

  7. 《C Prime Plus》(第六版) 第03章 编程练习 7 英寸转换成厘米

    C Prime Plus 第六版 编译器版本:Mac Xcode 11.6  第03章 数据和C 编程练习 7 7. 1英寸相当于2.54厘米.编写一个程序,提示用户输入身高(/英寸),然后以厘米为单 ...

  8. 《C Prime Plus》(第六版) 第03章 编程练习 8 品脱/盎司/大汤勺/茶勺单位换算

    C Prime Plus 第六版 编译器版本:Mac Xcode 11.6  第03章 数据和C 编程练习 8 在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于 ...

  9. C Primer Plus(第六版)第三章 数据和C

    笔记记录 1.float 类型可以储存带小数的数字. 2.printf()中使用%f来处理浮点值.%.2f中的.2用于精确控制输出,指定输出的浮点数只显示小数点后面两位. 3.scanf()函数用于读 ...

最新文章

  1. 看完陈皓的116篇文章,我给自己定了个5年技术规划
  2. python如何定位路径_selenium_webdriver(python)查看文件路径,鼠标定位
  3. 最耗性能的SQL语句
  4. 商场超市数据备份方案
  5. 不同语言实现两个变量的交换 Python之禅
  6. Jetpack—LiveData组件的缺陷以及应对策略
  7. wifi-sdio接口
  8. 一些好听的纯音乐及下载
  9. 微信公众号(订阅号)文章阅读数监控V0.1
  10. python时间戳转换
  11. 华为云电脑+teamviewer ssh实现手机远程操控服务器
  12. java关闭事件_为Java程序添加退出事件
  13. 电脑没有声音了怎么恢复?恢复声音的6个方法【图解】
  14. Linux的历史背景和基本指令
  15. 16g电脑内存有什么好处_win7系统16g内存和win10系统16g内存有什么区别
  16. 基于javaweb的毕业设计毕业论文管理系统(java+ssm+jsp+tomcat+mysql)
  17. AC-DMIS 5.3 叶片基础知识
  18. 在 Visual Basic .NET 和 Visual C# .NET 中创建控件数组
  19. 老版本NDK下载列表(Android官网)
  20. 【PHM】PHM算法与智能分析技术——数据处理与特征提取方法2

热门文章

  1. 【dubbo-2.5.x】Linux下dubbo-admin监控/管理平台部署详细教程
  2. 2022年度总结——2022我在CSDN的那些事暨2023我的目标展望:Pursue freedom Realize self-worth
  3. 小红书用户画像分析_用户画像分析会成为第一品牌竞争力吗?
  4. linux宝塔原理,linux宝塔是干嘛的
  5. android线程间通信的几种方法_Android进程间和线程间通信方式
  6. 高精度结构光工业3D相机Mech-Eye PRO全面升级:可选蓝光/白光版本,适合中距离应用...
  7. 分布式系统-分片和路由
  8. 毕业论文的前言写什么?
  9. android手机功能创新,Android手机QQ浏览器1.1发布多项创新功能
  10. 科技爱好者周刊(第 213 期):知识孤岛,知识软件