《C Primer Plus》(第六版)第七章:C控制语句:分支和跳转|复习题与编程练习答案参考

目录

复习题

1.判断下列表达式是true还是false。

a

100 > 3 && 'a'>'c'

b

100 > 3 || 'a'>'c'

c

!(100>3)

2.根据下列描述的条件,分别构造一个表达式:

a

umber等于或大于90,但是小于100

b

h不是字符q或k

c

umber在1~9之间(包括1和9),但不是5

d

umber不在1~9之间

3.下面的程序关系表达式过于复杂,而且还有些错误,请简化并改正。

#include

int main(void) /* 1 */

{ /* 2 */

int weight, height; /* weight以磅为单位,height以英寸为单位 *//* 4 */

scanf("%d , weight, height); /* 5 */

if (weight < 100 && height > 64) /* 6 */

if (height >= 72) /* 7 */

printf("You are very tall for your weight.\n");

else if (height < 72 &&> 64) /* 9 */

printf("You are tall for your weight.\n");/* 10 */

else if (weight > 300 && !(weight <= 300) /* 11 */

&& height < 48) /* 12 */

if (!(height >= 48)) /* 13 */

printf(" You are quite short for your weight.\n");

else /* 15 */

printf("Your weight is ideal.\n"); /* 16 */

/* 17 */

return 0;

}

4.下列各个表达式的值是多少?

a.5 > 2

b.3 + 4 > 2 && 3 < 2

c.x >= y || y > x

d.d = 5 + ( 6 > 2 )

e.'X' > 'T' ? 10 : 5

f.x > y ? y > x : x > y

5.下面的程序将打印什么?

#include

int main(void)

{

int num;

for (num = 1; num <= 11; num++)

{

if (num % 3 == 0)

putchar('$');

else

putchar('*');

putchar('#');

putchar('%');

}

putchar('\n');

return 0;

}

6.下面的程序将打印什么?

#include

int main(void)

{

int i = 0;

while (i < 3) {

switch (i++) {

case 0: printf("fat ");

case 1: printf("hat ");

case 2: printf("cat ");

default: printf("Oh no!");

}

putchar('\n');

}

return 0;

}

7.下面的程序有哪些错误?

#include

int main(void)

{

char ch;

int lc = 0; /* 统计小写字母

int uc = 0; /* 统计大写字母

int oc = 0; /* 统计其他字母

while ((ch = getchar()) != '#')

{

if ('a' <= ch >= 'z')

lc++;

else if (!(ch < 'A') || !(ch > 'Z')

uc++;

oc++;

}

printf(%d lowercase, %d uppercase, %d other, lc, uc, oc);

return 0;

}

8.下面的程序将打印什么?

/* retire.c */

#include

int main(void)

{

int age = 20;

while (age++ <= 65)

{

if ((age % 20) == 0) /* age是否能被20整除? */

printf("You are %d.Here is a raise.\n", age);

if (age = 65)

printf("You are %d.Here is your gold watch.\n", age);

}

return 0;

}

9.给定下面的输入时,以下程序将打印什么?

q

c

h

b

#include

int main(void)

{

char ch;

while ((ch = getchar()) != '#')

{

if (ch == '\n')

continue;

printf("Step 1\n");

if (ch == 'c')

continue;

else if (ch == 'b')

break;

else if (ch == 'h')

goto laststep;

printf("Step 2\n");

laststep: printf("Step 3\n");

}

printf("Done\n");

return 0;

}

10.重写复习题9,但这次不能使用continue和goto语句。

编程题

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

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

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

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

5.使用switch重写练习4。

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

7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总

额、税金和净收入。做如下假设:

a.基本工资 = 1000美元/小时

b.加班(超过40小时) = 1.5倍的时间

c.税率: 前300美元为15%

续150美元为20%

494余下的为25%

用#define定义符号常量。不用在意是否符合当前的税法。

8.修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。使用switch完成工资等级选择。运行程序后,显示的菜单应该类似这样:*****************************************************************Enter the number corresponding to the desired pay rate or action:1) $8.75/hr   2) $9.33/hr3) $10.00/hr  4) $11.20/hr5) quit*****************************************************************如果选择 1~4 其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入 5。如果输入 1~5 以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。

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

10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类

别,每个类别有两个等级。

下面是该税收计划的摘要(美元数为应征税的收入):

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磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。


复习题

1.判断下列表达式是true还是false

a

100 > 3 && 'a'>'c'

b

100 > 3 || 'a'>'c'

c

!(100>3)

&&与、||或、!非
a. false
b. true
c. false

2.根据下列描述的条件,分别构造一个表达式:

a

umber等于或大于90,但是小于100

b

h不是字符qk

c

umber19之间(包括19),但不是5

d

umber不在19之间

a. umber >= 90 && umber < 100
b. h != 'q' || h != 'k'
c. ( umber >= 1 && umber <= 9 ) && umber != 5
d. ! ( umber >= 1 && umber <= 9 )或 number < 1 || number > 9

3.下面的程序关系表达式过于复杂,而且还有些错误,请简化并改正。

#include <stdio.h>

int main(void) /* 1 */

{ /* 2 */

int weight, height; /* weight以磅为单位,height以英寸为单位 *//* 4 */

scanf("%d , weight, height); /* 5 */

if (weight < 100 && height > 64) /* 6 */

if (height >= 72) /* 7 */

printf("You are very tall for your weight.\n");

else if (height < 72 &&> 64) /* 9 */

printf("You are tall for your weight.\n");/* 10 */

else if (weight > 300 && !(weight <= 300) /* 11 */

&& height < 48) /* 12 */

if (!(height >= 48)) /* 13 */

printf(" You are quite short for your weight.\n");

else /* 15 */

printf("Your weight is ideal.\n"); /* 16 */

/* 17 */

return 0;

}

#include <stdio.h>
int main(void)
{
	int weight, height; 
	printf("Enter your weight in pounds and ");
		printf("your height in inches.\n");
	scanf("%d %d", &weight, &height);
	if (weight < 100 && height > 64)
		if (height >= 72)
			printf("You are very tall for your weight.\n");
		else
			printf("You are tall for your weight.\n");
	else if (weight > 300 && height < 48)
		printf(" You are quite short for your weight.\n");
	else
		printf("Your weight is ideal.\n");
	return 0;
}
5 行:应该是 scanf("%d %d", &weight, &height); 。不要忘记 scanf() 中要用& 。另外,这一行前面应该有提示用户输入的语句。
9 行:测试条件中要表达的意思是 (height < 72 && height > 64) 。根据前面第7 行中的测试条件,能到第 9 行的 height 一定小于 72 ,所以,只需要用表达式(height > 64) 即可。但是,第 6 行中已经包含了 height > 64 这个条件,所以这里完全不必再判断,if else 应改成 else
11 行:条件冗余。第 2 个表达式( weight 不小于或不等于 300 )和第 1个表达式含义相同。只需用一个简单的表达式(weight > 300) 即可。但是,问题不止于此。第 11 行是一个错误的 if ,这行的 else if 与第 6 行的 if 匹配。但是,根据if 最接近规则 ,该 else if 应该与第 9 行的 else if 匹配。因此,在weight小于 100 且小于或等于 64 时到达第 11 行,而此时 weight 不可能超过
300
7 行~第 10 行:应该用花括号括起来。这样第 11 行就确定与第 6 行匹配。但是,如果把第9 行的 else if 替换成简单的 else ,就不需要使用花括号。
13 行:应简化成 if (height > 48) 。实际上,完全可以省略这一行。因为第12 行已经测试过该条件。

4.下列各个表达式的值是多少?

a.5 > 2

b.3 + 4 > 2 && 3 < 2

c.x >= y || y > x

d.d = 5 + ( 6 > 2 )

e.'X' > 'T' ? 10 : 5

f.x > y ? y > x : x > y

a.1
b.0
c.1
d.6
e.10
f.0

5.下面的程序将打印什么?

#include <stdio.h>

int main(void)

{

int num;

for (num = 1; num <= 11; num++)

{

if (num % 3 == 0)

putchar('$');

else

putchar('*');

putchar('#');

putchar('%');

}

putchar('\n');

return 0;

}

*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%  

6.下面的程序将打印什么?

#include <stdio.h>

int main(void)

{

int i = 0;

while (i < 3) {

switch (i++) {

case 0: printf("fat ");

case 1: printf("hat ");

case 2: printf("cat ");

default: printf("Oh no!");

}

putchar('\n');

}

return 0;

}

没有break
fat hat cat Oh no!
hat cat Oh no!
cat Oh no!

7.下面的程序有哪些错误?

#include <stdio.h>

int main(void)

{

char ch;

int lc = 0; /* 统计小写字母

int uc = 0; /* 统计大写字母

int oc = 0; /* 统计其他字母

while ((ch = getchar()) != '#')

{

if ('a' <= ch >= 'z')

lc++;

else if (!(ch < 'A') || !(ch > 'Z')

uc++;

oc++;

}

printf(%d lowercase, %d uppercase, %d other, lc, uc, oc);

return 0;

}

5 行~第 7 行的注释要以 */ 结尾,或者把注释开头的 /* 换成 // 。表达式'a' <= ch >= 'z' 应替换成 ch >= 'a' && ch <= 'z' 。或者,包含 ctype.h 并使用 islower() ,这种方法更简单,而且可移植性更高。顺带一提,虽然从 C 的语法方面看, 'a' <= ch >= 'z' 是有效的表达式,但是它的含义不明。因为关系运算符从左往右结合,该表达式被解释成('a'<= ch) >= 'z'。圆括号中的表达式的值不是 1 就是 0 (真或假),然后判断该值是否大于或等于'z' 的数值码。 1 0 都不满足测试条件,所以整个表达式恒为
0 (假)。
在第 2 个测试表达式中,应该把 || 改成 && 。另外,虽然!(ch< 'A') 是有效的表达式,而且含义也正确,但是用ch >= 'A' 更简单。这一行的 'z' 后面应该有两个圆括号。
更简单的方法是使用isuupper() 。在 uc++; 前面应该加一行else 。否则,每输入一个字符, uc 都会递增 1
另外,在 printf() 语句中的格式化字符串应该用双引号括起来。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int lc = 0; /*统计小写字母*/
int uc = 0; /*统计大写字母*/
int oc = 0; /*统计其他字母*/
while ((ch = getchar()) != '#')
{
if (islower(ch))
lc++;
else if (isupper(ch))
uc++;
else
oc++;
}
printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
return 0;
}

8.下面的程序将打印什么?

/* retire.c */

#include <stdio.h>

int main(void)

{

int age = 20;

while (age++ <= 65)

{

if ((age % 20) == 0) /* age是否能被20整除? */

printf("You are %d.Here is a raise.\n", age);

if (age = 65)

printf("You are %d.Here is your gold watch.\n", age);

}

return 0;

}

会打印

 You are 65.Here is your gold watch.

直到程序停止。(因为第二个条件语句总是真的)

到40、60的时候会打印 

You are 40.Here is a raise.

You are 60.Here is a raise.

9.给定下面的输入时,以下程序将打印什么?

q

c

h

b

#include <stdio.h>

int main(void)

{

char ch;

while ((ch = getchar()) != '#')

{

if (ch == '\n')

continue;

printf("Step 1\n");

if (ch == 'c')

continue;

else if (ch == 'b')

break;

else if (ch == 'h')

goto laststep;

printf("Step 2\n");

laststep: printf("Step 3\n");

}

printf("Done\n");

return 0;

}

q
Step 1
Step 2
Step 3
c
Step 1
h
Step 1
Step 3
b
Step 1
Done

10.重写复习题9,但这次不能使用continuegoto语句。

#include <stdio.h>
int main(void)
{
	char ch;
	while ((ch = getchar()) != '#')
	{
		if (ch != '\n')
		{
			printf("Step 1\n");
			if (ch == 'b')
				break;
			else if (ch != 'c')
			{
				if (ch != 'h')
					printf("Step 2\n");
				printf("Step 3\n");
			}
		}
	}
	printf("Done\n");
	return 0;
}

编程题

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

#include<stdio.h>
int main()
{
	int spaces = 0;
	int lines = 0;
	int other = 0;

	printf("请输入(到#停止读取):\n");
	char ch;
	while ((ch = getchar()) != '#')
	{
		if (ch == ' ')
			spaces++;
		else if (ch == '\n')
			lines++;
		else
		{
			other++;
		}
	}
	printf("有%d个空格,有%d个换行符,有%d个其他字符。\n", spaces, lines, other);
	return 0;
}

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

#include<stdio.h>
int main()
{
	char ch;
	int count = 0;

	printf("请输入(到#停止读取):\n");
	while ((ch = getchar()) != '#')
	{
		printf("%c:%d ", ch, (int)ch);
		count++;
		if (count % 8 == 0)
		{
			printf("\n");
		}
	}

	return 0;
}

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


#include<stdio.h>
int main()
{
	int o_count = 0;//偶数计数
	int j_count = 0;//奇数计数
	int o_sum = 0;//偶数总和
	float j_sum = 0;//奇数总和

	int num;
	printf("请输入整数:(输入0退出)\n");
	scanf_s("%d", &num);
	while (num != 0)
	{
		if (num % 2 == 0)
		{
			o_count++;
			o_sum += num;
		}
		if (num % 2 != 0)
		{
			j_count++;
			j_sum += num;
		}
		printf("请输入整数:(输入0退出)\n");
		scanf_s("%d", &num);
	}
	printf("\n偶数有%d个,平均值为%d\n奇数有%d个,平均值为%.1f\n", o_count, o_sum / o_count, j_count, j_sum / j_count);

	return 0;
}

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


#include<stdio.h>
int main()
{
	int count_1 = 0;
	int count_2 = 0;


	printf("请输入(到#停止读取):\n");
	char ch;
	while ((ch = getchar()) != '#')
	{
		if (ch == '.')
		{
			printf("!");
			count_1++;
		}
		else if (ch == '!')
		{
			printf("!!");
			count_2++;
		}
		else {
			printf("%c", ch);
		}

	}
	printf("\n进行了%d次替换(%d次'.' , %d次'!')\n", count_1 + count_2, count_1, count_2);
	return 0;
}

5.使用switch重写练习4

#include<stdio.h>
int main()
{
	int count_1 = 0;
	int count_2 = 0;


	printf("请输入(到#停止读取):\n");
	char ch;
	while ((ch = getchar()) != '#')
	{
		switch (ch) {
		case '!':printf("!!"); count_2++;
			break;
		case '.':putchar('!'); count_1++;
			break;
		default:printf("%c", ch);
		}
	}
	printf("\n进行了%d次替换(%d次'.' , %d次'!')\n", count_1 + count_2, count_1, count_2);
	return 0;
}

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

#include <stdio.h>

int main() {
    int count = 0;
    char prev_ch = '\0'; // 用于存储上一个读取的字符,初始化为空字符

    printf("请输入(到#停止读取):\n");
    char ch;
    while ((ch = getchar()) != '#') {
        // 检查上一个字符是否是 'e' 且当前字符是否是 'i'
        if (prev_ch == 'e' && ch == 'i') {
            count++;
        }
        // 更新上一个字符为当前字符,以便在下一次循环中使用
        prev_ch = ch;
    }

    printf("\n%d\n", count);
    return 0;
}

7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总

额、税金和净收入。做如下假设:

a.基本工资 = 1000美元/小时

b.加班(超过40小时) = 1.5倍的时间

c.税率: 前300美元为15%

150美元为20%

494余下的为25%

#define定义符号常量。不用在意是否符合当前的税法。

#include <stdio.h>
#define Salary_Per_Hour 10.00 //10.00美元每小时
int main() {
    float worktime;
    float salary;
    float taxe;

    printf("输入一周工作的小时数:\n");
    scanf_s("%f", &worktime);
    if (worktime > 40)
    {
        worktime *= 1.5;
    }
    salary = Salary_Per_Hour * worktime;
    if (salary <= 300)
    {
        taxe = salary * 0.15;
    }
    else if (salary <= 450)
    {
        taxe = salary * 0.2;
    }
    else {
        taxe = salary * 0.25;
    }

    printf("工资总额为:%f$,税金为:%f$,净收入为:%f$", salary, taxe, salary - taxe);

    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>

int main() {
    float worktime, salary, taxe, Salary_Per_Hour;
    int i;
    printf("*****************************************************************\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr\t\t\t2) $9.33/hr\n3) $10.00/hr\t\t\t4) $11.20/hr\n5) quit\n");
    printf("*****************************************************************\n");
    
    scanf_s("%d", &i);
    while(i != 5)
    {
        
        switch (i)
        {
        case 1:Salary_Per_Hour = 8.75; printf("请问您一周要工作的时间(小时):\n");
            break;
        case 2:Salary_Per_Hour = 9.33; printf("请问您一周要工作的时间(小时):\n");
            break;
        case 3:Salary_Per_Hour = 10.00; printf("请问您一周要工作的时间(小时):\n");
            break;
        case 4:Salary_Per_Hour = 11.20; printf("请问您一周要工作的时间(小时):\n");
            break;
        default:printf("请输入1~4之间的数字,输入5退出。\n");
            break;
        }

        scanf_s("%f", &worktime);
        if (worktime > 40)
        {
            worktime *= 1.5;
        }
        salary = Salary_Per_Hour * worktime;
        if (salary <= 300)
        {
            taxe = salary * 0.15;
        }
        else if (salary <= 450)
        {
            taxe = salary * 0.2;
        }
        else {
            taxe = salary * 0.25;
        }

        printf("工资总额为:%f$,税金为:%f$,净收入为:%f$\n", salary, taxe, salary - taxe);

        printf("\n*****************************************************************\n");
        printf("Enter the number corresponding to the desired pay rate or action:\n");
        printf("1) $8.75/hr\t\t\t2) $9.33/hr\n3) $10.00/hr\t\t\t4) $11.20/hr\n5) quit\n");
        printf("*****************************************************************\n");
        scanf_s("%d", &i);

    }
    printf("\n退出!\n");

    return 0;
}

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

#include <stdio.h>
#include <stdbool.h>

bool is_prime(int num) {
    
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int num;

    while (1)
    {
        printf("请输入一个正整数: ");
        if (scanf_s("%d", &num) == 1 && num > 0)
        {  
            break;
        }
        else if (getchar() != '\n' && getchar() != EOF)//跳过换行符和错误输入
        {
            printf("输入无效!");
        }
    }
    for (int i = 2; i <= num; i++)
    {
        if (is_prime(i))
        {
            printf("%d ", i);
        }
    }

    return 0;
}

10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类

别,每个类别有两个等级。

下面是该税收计划的摘要(美元数为应征税的收入):

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

int main() {
    float  salary, taxe;
    int i;

    printf("*****************************************************************\n");
    printf("请选择缴纳税金的种类:\n");
    printf("1) 单身\t\t\t2) 户主\n3) 已婚,共有\t\t4) 已婚,离异\n5) quit\n");
    printf("*****************************************************************\n");

    scanf_s("%d", &i);
    while (i != 5)
    {
        printf("请输入您的应纳税收入:\n");
        scanf_s("%f", &salary);

        switch (i)
        {
        case 1:
            if (salary <= 17850)
            {
                taxe = salary * 0.15;
            }
            else {
                taxe = 17850 * 0.15 + (salary - 17850) * 0.28;
            }
            break;
        case 2:
            if (salary <= 23900)
            {
                taxe = salary * 0.15;
            }
            else {
                taxe = 23900 * 0.15 + (salary - 23900) * 0.28;
            }
            break;
        case 3:
            if (salary <= 29750)
            {
                taxe = salary * 0.15;
            }
            else {
                taxe = 29750 * 0.15 + (salary - 29750) * 0.28;
            }
            break;
        case 4:
            if (salary <= 14875)
            {
                taxe = salary * 0.15;
            }
            else {
                taxe = 14875 * 0.15 + (salary - 14875) * 0.28;
            }
            break;
        default:printf("请输入1~4之间的数字,输入5退出。\n");
            break;
        }

        
        printf("您需要缴纳的税收为%f\n", taxe);

        printf("*****************************************************************\n");
        printf("请选择缴纳税金的种类:\n");
        printf("1) 单身\t\t\t2) 户主\n3) 已婚,共有\t\t4) 已婚,离异\n5) quit\n");
        printf("*****************************************************************\n");
        scanf_s("%d", &i);

    }
    printf("\n退出!\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>

int main() {
    float yang_weight = 0, tian_weight = 0, hu_weight = 0; // 洋蓟、甜菜、胡萝卜的重量
    float yang_cost = 0, tian_cost = 0, hu_cost = 0; // 洋蓟、甜菜、胡萝卜的费用
    float total_weight = 0, total_cost = 0, discount = 0, shipping_cost = 0; // 总重量、总费用、折扣、运费
    char ch;

    while (1) {
        printf("*****************************************************************\n");
        printf("请输入您要订购的物品\n");
        printf("a) 洋蓟(2.05美元/磅)\t\tb) 甜菜(1.15美元/磅)\nc) 胡萝卜(1.09美元/磅)\t\tq) 退出订购\n");
        printf("*****************************************************************\n");

        scanf(" %c", &ch); // 注意 %c 前面的空格,用于消耗可能存在的换行符

        if (ch == 'q') {
            break;
        }

        float weight;
        float price_per_pound;
        float *cost, *weight_ptr;

        switch (ch) {
        case 'a':
            price_per_pound = 2.05;
            weight_ptr = &yang_weight;
            cost = &yang_cost;
            break;
        case 'b':
            price_per_pound = 1.15;
            weight_ptr = &tian_weight;
            cost = &tian_cost;
            break;
        case 'c':
            price_per_pound = 1.09;
            weight_ptr = &hu_weight;
            cost = &hu_cost;
            break;
        default:
            printf("无效输入,请重新输入。\n");
            continue;
        }

        printf("请输入%c的重量:", ch);
        scanf("%f", &weight);
        *weight_ptr += weight;
        *cost = price_per_pound * weight;
        total_weight += weight;
        total_cost += *cost;
    }

    if (total_cost >= 100) {
        discount = total_cost * 0.05;
        total_cost -= discount;
    }

    if (total_weight <= 5) {
        shipping_cost = 6.5;
    } else if (total_weight <= 20) {
        shipping_cost = 14;
    } else {
        shipping_cost = 14 + (total_weight - 20) * 0.5;
    }

    printf("\n购买信息:\n");
    printf("洋蓟: %.2f 磅, 每磅 2.05 美元, 总费用: %.2f 美元\n", yang_weight, yang_cost);
    printf("甜菜: %.2f 磅, 每磅 1.15 美元, 总费用: %.2f 美元\n", tian_weight, tian_cost);
    printf("胡萝卜: %.2f 磅, 每磅 1.09 美元, 总费用: %.2f 美元\n", hu_weight, hu_cost);
    printf("总重量: %.2f 磅\n", total_weight);
    printf("订单总费用 (不含折扣): %.2f 美元\n", yang_cost + tian_cost + hu_cost);
    if (discount > 0) {
        printf("折扣: %.2f 美元\n", discount);
    }
    printf("运费和包装费: %.2f 美元\n", shipping_cost);
    printf("所有费用总额: %.2f 美元\n", total_cost + shipping_cost);

    printf("\n退出成功!\n");

    return 0;
}
这里用到了指针,后续学完指针内容要回来重做一遍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值