学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出!
1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、 换行符数和所有其他字符的数量。
#include <stdio.h>
int main(void)
{
char ch = '0';
int blank = 0;
int endline = 0;
int other = 0;
printf("请输入一行字符:");
while ((ch = getchar()) != '#')
{
switch(ch)
{
case ' ':
blank++;
break;
case '\n':
endline++;
break;
default:
other++;
break;
}
}
printf("%d个空白字符,%d个换行符,%d个其他字符\n", blank, endline, other);
return 0;
}
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字 符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数 和求模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
int main(void)
{
char ch = '0';
int count = 0;
printf("请输入一行字符:");
while ((ch = getchar()) != '#')
{
if (count++ % 8 == 0)
printf("\n");
if (ch == '\n')
printf("\\n-%03d. ", ch);
else if (ch == '\t')
printf("\\nt-%03d. ", ch);
else
printf("%c-%03d. ", ch, ch);
}
return 0;
}
3.编写一个程序,读取整数直到用户输入 0。输入结束后,程序应报告 用户输入的偶数(不包括 0)个数、这些偶数的平均值、输入的奇数个数及 其奇数的平均值。
#include <stdio.h>
int main(void)
{
int input = 0;
int even = 0;
int odd = 0;
float even_sum = 0, odd_sum = 0;
float even_ave, odd_ave;
while (scanf("%d", &input))
{
if (input == 0)
break;
if (input != 0 && input % 2 == 0)
{
even++;
even_sum += input;
even_ave = even_sum / even;
}
else
{
odd++;
odd_sum += input;
odd_ave = odd_sum / odd;
}
}
printf("偶数个数为%d,偶数的平均值为%.2f,奇数个数为%d,奇数的平均值为%.2f\n", even, even_ave, odd, odd_ave);
return 0;
}
4.使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句 号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include <stdio.h>
int main(void)
{
char ch = '0';
int count = 0;
printf("请输入一行字符:");
while ((ch = getchar()) != '#')
{
if (ch == '.')
{
printf("!");
count++;
}
else if (ch == '!')
{
printf("!!");
count++;
}
else
printf("%c", ch);
}
printf("\n进行了%d次替换\n",count);
return 0;
}
5.使用switch重写练习4。
#include <stdio.h>
int main(void)
{
char ch = '0';
int count = 0;
printf("请输入一行字符:");
while ((ch = getchar()) != '#')
{
switch (ch)
{
case '.':
printf("!");
count++;
break;
case '!':
printf("!!");
count++;
break;
default:
printf("%c", ch);
}
}
printf("\n进行了%d次替换\n", count);
return 0;
}
6.编写程序读取输入,读到#停止,报告ei出现的次数。 注意 该程序要记录前一个字符和当前字符。用“Receive your eieio award”这 样的输入来测试。
#include <stdio.h>
int main(void)
{
char ch = '0';
int count = 0;
int flag = 0;
printf("Receive your eieio award:");
while ((ch = getchar()) != '#')
{
switch (ch)
{
case 'e':
flag = 1;
break;
case 'i':
if (flag == 1)
{
count++;
}
break;
default:
flag = 0;
break;
}
}
printf("ei出现的次数为%d\n", count);
return 0;
}
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总 额、税金和净收入。做如下假设:
a.基本工资 = 10.00美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15% 续150美元为20%
余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法。
#include <stdio.h>
#define BASE_SALARY 10.00
#define EXTRA_HOUR 1.5
#define BASE_TAX 0.15
#define EXTRA_TAX 0.2
#define EXCEED_TAX 0.25
int main(void)
{
int hours = 0;
float salary = 0.0, tax = 0.0, taxed_salary = 0.0;
while (1)
{
printf("请输入一周工作的小时数:");
scanf("%d", &hours);
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;
tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;
taxed_salary = salary - tax;
}
printf("salary:%.2lf,tax:%.2lf,taxed_salary:%.2lf\n", salary, tax, 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>
#define EXTRA_HOUR 1.5
#define BASE_TAX 0.15
#define EXTRA_TAX 0.2
#define EXCEED_TAX 0.25
void menu(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/hr 4)$11.20/hr\n");
printf("5)quit\n");
printf("*****************************************************************\n");
}
void cal(float base_salary,int hours)
{
float salary = 0.0, tax = 0.0, taxed_salary = 0.0;
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;
tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;
taxed_salary = salary - tax;
}
printf("salary:%.2lf,tax:%.2lf,taxed_salary:%.2lf\n", salary, tax, taxed_salary);
}
int main(void)
{
int hours = 0;
int input = 0;
while (1)
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入工作小时:");
scanf("%d", &hours);
cal(8.75,hours);
break;
case 2:
printf("请输入工作小时:");
scanf("%d", &hours);
cal(9.33, hours);
break;
case 3:
printf("请输入工作小时:");
scanf("%d", &hours);
cal(10.00, hours);
break;
case 4:
printf("请输入工作小时:");
scanf("%d", &hours);
cal(11.20, hours);
break;
case 5:
break;
}
}
return 0;
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的 素数。
#include <stdio.h>
int main(void)
{
int input = 0;
int flag = 1;
printf("请输入一个正整数:");
scanf("%d", &input);
for (int i = 2; i < input; i++)
{
flag = 1;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
{
printf("%d ", i);
}
}
printf("\n");
return 0;
}
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类 别,每个类别有两个等级。 下面是该税收计划的摘要(美元数为应征税的收入):
例如,一位工资为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.28
void eatline()
{
while (getchar() != '\n')
continue;
}
int main(void)
{
int n = 0;
double wage, tax;
while (1)
{
printf("********************************\n");
printf("1) single\n");
printf("2) householder\n");
printf("3) married\n");
printf("4) married but divorced\n");
printf("5) quit\n");
printf("********************************\n");
printf("Please you choose: ");
while (scanf("%d", &n) != 1 || (n > 5 || n < 1))
{
eatline();
printf("Please enter 1, 2, 3, 4 or 5: ");
}
if (n == 5)
{
break;
}
printf("Please enter your wage: ");
while (scanf("%lf", &wage) != 1 || (wage < 0))
{
eatline();
printf("Please enter a number(>= 0): ");
}
eatline();
switch (n)
{
case 1:
{
tax = (wage <= PLAN1 ? wage * RATE1 : PLAN1 * RATE1 + (wage - PLAN1) * RATE2);
break;
}
case 2:
{
tax = (wage <= PLAN2 ? wage * RATE1 : PLAN2 * RATE1 + (wage - PLAN2) * RATE2);
break;
}
case 3:
{
tax = (wage <= PLAN3 ? wage * RATE1 : PLAN3 * RATE1 + (wage - PLAN3) * RATE2);
break;
}
case 4:
{
tax = (wage <= PLAN4 ? wage * RATE1 : PLAN4 * RATE1 + (wage - PLAN4) * RATE2);
break;
}
}
printf("Your tax: %g\n\n", tax);
}
printf("Done.\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>
#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 - base20);
}
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;
}