代码

5.2 broker.c
计算股票经纪人的佣金
当股票通过经纪人进行买卖时,经纪人的佣金往往根据股票交易额采用某种变化的比例进行计算。下面的表格显示了实际支付给经纪人的费用数量。
交易额范围佣金费用
低于2 500美元 330美元 + 1.7%
2 500~6 250美元 356美元 + 0.66%
6 250~20 000美元 376美元 + 0.34%
20 000~50 000美元 100美元 + 0.22%
50 000~500 000美元 155美元 + 0.11%
超过500 000美元 255美元 + 0.09%
最低收费是39美元。下面的程序要求用户录入交易额,然后显示出佣金的数额:
Enter value of trade: 30000
Commission: $166.00
此程序的重点是用级联式if 语句来确定交易额所在的范围。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	float value, commission;

	printf("Enter value of trade: ");
	scanf("%f", &value);

	if (value < 2500.00f)
	{
		commission = 30.00f + value * 0.017f;
	}
	else if (value < 6250.00f)
	{
		commission = 56.00f + value * 0.0066f;
	}
	else if (value < 20000.00f)
	{
		commission = 76.00f + value * 0.0034f;
	}
	else if (value < 50000.00f)
	{
		commission = 100.00f + value * 0.0022f;
	}
	else
	{
		commission = 255.00f + value * 0.0009f;
	}
	if (commission < 39.00f)
	{
		commission = 39.00f;
	}

	printf("Commission: $%.2f\n", commission);

	return 0;
}

级联式if 语句也可以写成下面这样(改变用粗体表示):

if (value < 2500.00f)
	commission = 30.00f + .017f * value;
else if (value >= 2500.00f && value < 6250.00f)
	commission = 56.00f + .0066f * value;
else if (value >= 6250.00f && value < 20000.00f)
	commission = 76.00f + .0034f * value;
...

程序仍能正确运行,但新增的这些条件是多余的。例如,第一个if子句测试value 的值是否小于2500,如果小于2500则计算佣金。当到达第二个if 测试(value >= 2500.00f && value <6250.00f )时,value 不可能小于2500,所以一定大于等于2500。条件value >= 2500.00f 总是为真,因此加上该测试没有意义。

5.3 date.c
程序  显示法定格式的日期
合同和其他法律文档中经常使用下列日期格式:
Dated this ________ day of ___________, 20 __.
下面编写程序,用这种格式来显示日期。用户以月/日/年的格式录入日期,然后计算机显示出“法定”格式的日期:
Enter date (mm/dd/yy): 7/19/1
Dated this 19th day of July, 2014.
可以使用printf 函数实现格式化的大部分工作。然而,还有两个问题:如何为日添加“th”(或者“st”、“nd”、“rd”),以及如何用单词而不是数字来显示月份。幸运的是,switch 语句可以很好地解决这两个问题:我们用一个switch 语句显示日期的后缀,再用另一个switch 语句显示出月份名。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int month, day, year;

	printf("Enter date (mm/dd/yy) : ");
	scanf("%d /%d /%d", &month, &day, &year);

	printf("Dated this %d", day);
	switch (day) {
		case 1: case 21: case 31:
			printf("st"); break;
		case 2: case 22:
			printf("nd"); break;
		case 3: case 23:
			printf("rd"); break;
		default: printf("th"); break;
	}
	printf(" day of ");

	switch (month) {
		case 1:  printf("January");   break;
		case 2:  printf("February");  break;
		case 3:  printf("March");     break;
		case 4:  printf("April");	  break;
		case 5:  printf("May");       break;
		case 6:  printf("June");      break;
		case 7:  printf("July");      break;
		case 8:  printf("August");    break;	
		case 9:  printf("September"); break;
		case 10: printf("October");   break;
		case 11: printf("November");  break;
		case 12: printf("December");  break;
	}

	printf(", 20%.2d.\n", year);

	return 0;
}

注意,%.2d 用于显示年份的最后两位数字。如果用%d 代替的话,那么将错误地显示倒数第二位为零的年份(将会把2005显示成205)。

练习题

  1. 下列代码片段给出了关系运算符和判等运算符的示例。假设i、j 和k 都是int 型变量,请给出每道题的输出结果。
  (a)
i = 2; j = 3;
k = i * j == 6;
printf("%d", k);
  (b)
i = 5; j = 10; k = 1;
printf("%d", k > i < j);
  (c)
i = 3; j = 2; k = 1;
printf("%d", i < j == j < k);
  (d)
i = 3; j = 4; k = 5;
printf("%d", i % j + i < k);
Solution

(a) 1  
(b) 1  
(c) 1  
(d) 0
  1. 下列代码片段给出了逻辑运算符的示例。假设i 、j 和k 都是int 型变量,请给出每道题的输出结果。
  (a)
i = 10; j = 5;
printf("%d", !i < j);
  (b)
i = 2; j = 1;
printf("%d", !!i + !j);
  (c)
i = 5; j = 0; k = -5;
printf("%d", i && j || k);
  (d)
i = 1; j = 2; k = 3;
printf("%d", i < j || k);
Solution

(a) 1  
(b) 1  
(c) 1  
(d) 1

  1. 下列代码片段给出了逻辑表达式的短路行为的示例。假设i 、j 和k 都是int 型变量,请给出每道题的输出结果。
  (a)
i = 3; j = 4; k = 5;
printf("%d", i < j || ++j < k);
printf("%d %d %d", i, j, k);
  (b)
i = 7; j = 8; k = 9;
printf("%d", i - 7 && j++ < k);
printf("%d %d %d", i, j, k);
  (c)
i = 7; j = 8; k = 9;
printf("%d", (i = j) || (j = k));
printf("%d %d %d", i, j, k);
  (d)
i = 1; j = 1; k = 1;
printf("%d", ++i || ++j && ++k);
printf("%d %d %d", i, j, k);
Solution

(a) 1 3 4 5  
(b) 0 7 8 9  
(c) 1 8 8 9  
(d) 1 2 1 1

  1. 编写一个表达式,要求这个表达式根据i 是否小于、等于或大于j ,分别取值为-1、0或+1。
Solution
(i > j) - (i < j);

  1. 下面的if 语句在C语言中是否合法?
    if (n >= 1 <= 10)
    printf (“n is between 1 and 10\n”);
      如果合法,那么当n 等于0时语句做些什么?
    Solution
    The statement is legal, but confusing. When n equals 0, the printf statement will still be run. This is because the left-associativity of the equality test operators: n >= 1 returns 0, and 0 <= 10 returns 1 (a true value).

  2. 下面的if 语句在C语言中是否合法?
    if (n == 1 - 10)
    printf (“n is between 1 and 10\n”);
      如果合法,那么当n 等于5时语句做些什么?
    Solution
    The statement is legal, but confusing. When n is equal to anything besides -9, the expression returns 0 (a false value), and the printf statement is never run. This is because the 1-10 expression is run first, yielding -9.

  3. 如果i 的值为17,下面的语句显示的结果是什么?如果i 的值为-17,下面的语句显示的结果又是什么?
    printf (“%d\n”, i >= 0 ? i : -i);
    17 17

  4. 下面的if 语句不需要这么复杂,请尽可能地加以简化。

if (age >= 13)
	if (age <= 19)
		teenager = true;
	else
		teenager = false;
else if (age < 13)
		teenager = false;
Solution
teenager = age >= 13 && age <= 19;

9. 下面两个if 语句是否等价?如果不等价,为什么?

if (score >= 90) 					if (score < 60)
	printf("A"); 						printf("F");
else if (score >= 80) 				else if (score < 70)
	printf("B"); 						printf("D");
else if (score >= 70) 				else if (score < 80)
	printf("C"); 						printf("C");
else if (score >= 60)				 else if (score < 90)
	printf("D");						printf("B");
else								 else
	printf("F"); 						printf("A");

Solution
The statements are equivalent.

  1. 下面的代码片段的输出结果是什么?(假设i 是整型变量。)
i = 1;
switch (i % 3) {
	case 0: printf("zero");
	case 1: printf("one");
	case 2: printf("two");
}

Solution
The program will printf zeroonetwo if i % 3 is 0, onetwo if i % 3 is 1, and two if i % 3 is 2, because of the missing break statements. In this example, onetwo will be printed, since i % 3 is 1.

11. 下面的表格给出了美国佐治亚州的电话区号以及每个地区最大的城市。
区号 主要城市
229 Albany
404 Atlanta
470 Atlanta
478 Macon
678 Atlanta
706 Columbus
762 Columbus
770 Atlanta
912 Savannah
  编写一个switch 语句,其控制表达式是变量area_code 。如果area_code 的值在表中,switch 语句打印出相应的城市名;否则switch 语句显示消息“Area code not recognized ”。使用5.3节讨论的方法,使switch 语句尽可能简单。
Solution

switch (area_code) {
    case 229: 
        printf("Albany\n"); 
        break;
    case 404: case 470: case 678: case 770:
        printf("Atlanta\n"); 
        break;
    case 478:
        printf("Macon\n");
        break;
    case 706: case 762:
        printf("Columbus\n");
        break;
    case 912:
        printf("Savannah");
        break;
    default:
        printf("Area code not recognized\n");
}

编程题

  1. 编写一个程序,确定一个数的位数:
    Enter a number: 374
    The number 374 has 3 digits
      假设输入的数最多不超过4位。提示 :利用if 语句进行数的判定。例如,如果数在0到9之间,那么位数为1;如果数在10到99之间,那么位数为2。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {

    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if (number >= 0 && number <= 9) {
        printf("The number %d has %d digits\n", number, 1);
    }
    else if (number <= 99) {
        printf("The number %d has %d digits\n", number, 2);
    }
    else if (number <= 999) {
        printf("The number %d has %d digits\n", number, 3);
    }
    else if (number <= 9999) {
        printf("The number %d has %d digits\n", number, 4);
    }
    else {
        printf("Enter error\n");
    }

    return 0;
}

#include <stdio.h>

int main(void) {

    int n, d;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n >= 0 && n <= 9)
        d = 1;
    else if (n >= 10 && n <= 99)
        d = 2;
    else if (n >= 100 && n <= 999)
        d = 3;
    else if (n >= 1000 && n <= 9999)
        d = 4;

    printf("The number %d has %d digits\n", n, d);

    return 0;
}
  1. 编写一个程序,要求用户输入24小时制的时间,然后显示12小时制的格式:
    Enter a 24-hour time: 21:11
    Equivalent 12-hour time: 9:11 PM
      注意不要把12:00显示成0:00。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {

    int hour, minute;

    printf("Enter a 24-hour time: ");
    scanf("%d :%d", &hour, &minute);

    if (hour > 12) {
        hour -= 12;
        printf("Equivalent 12-hour time: %d:%d PM", hour, minute);
    }
    else {
        printf("Equivalent 12-hour time: %.2d:%.2d AM", hour, minute);
    }

    return 0;
}

存在问题:
应该显示小时:1-12
没有考虑 00:00

#include <stdio.h>

int main(void) {

    int hour, minute;

    printf("Enter a 24-hour time: ");
    scanf("%d :%d", &hour, &minute);

    if (hour < 11)
        printf("Equivalent 12-hour time: %d:%d AM\n", 
                hour == 0 ? 12 : hour, minute);
    else 
        printf("Equivalent 12-hour time: %d:%d PM\n", 
               hour == 12 ? 12 : hour - 12, minute);

    return 0;
}

  1. 修改5.2节的broker.c 程序,同时进行下面两种改变。
      (a) 不再直接输入交易额,而是要求用户输入股票的数量和每股的价格。
      (b) 增加语句用来计算经纪人竞争对手的佣金(少于2000股时佣金为每股33美元+3美分,2000股或更多股时佣金为每股33美元+2美分)。在显示原有经纪人佣金的同时,也显示出竞争对手的佣金。
/* Calculates a broker's commission */

#include <stdio.h>

int main(void) {

    float commission, value, share_price, rival;
    int shares;

    printf("Enter number of shares: ");
    scanf("%d", &shares);
    printf("Enter price per share: ");
    scanf("%f", &share_price);

    value = shares * share_price;

    if (value < 2500.00f)
        commission = 30.00f + .017f * value;
    else if (value < 6250.00f)
        commission = 56.00f + .0066f * value;
    else if (value < 20000.00f)
        commission = 76.00f + .0034f * value;
    else if (value < 50000.00f)
        commission = 100.00f + .0022f * value;
    else if (value < 500000.00f) 
        commission = 155.00f + .0011f * value;
    else
        commission = 255.00f + .0009f * value;

    if (commission < 39.00f)
        commission = 39.00f;

    printf("Commission: $%.2f\n", commission);

    if (shares < 2000)
        rival = 33.00f + .03f * shares;
    else 
        rival = 33.00f + .02f * shares;

    printf("Rival comission: $%.2f\n", rival);

    return 0;
}

  1. 下面是用于测量风力的蒲福风力等级的简化版本。
    速率 (海里/小时) 描述
    小于1 Calm(无风)
    1~3 Light air(轻风)
    4~27 Breeze(微风)
    28~47 Gale(大风)
    48~63 Storm(暴风)
    大于63 Hurricane(飓风)
    编写一个程序,要求用户输入风速(海里/小时),然后显示相应的描述。
#include <stdio.h>

int main(void) {

    int speed;

    printf("Enter a wind speed in knots: ");
    scanf("%d", &speed);
    printf("Wind description: ");

    if (speed < 1)
        printf("Calm\n");
    else if (speed <= 3) 
        printf("Light air\n");
    else if (speed <= 27)
        printf("Breeze\n");
    else if (speed <= 47)
        printf("Gale\n");
    else if (speed <= 63)
        printf("Storm\n");
    else
        printf("Hurricane\n");

    return 0;
}

  1. 在美国的某个州,单身居民需要担负下面表格列出的所得税。
    收入(美元) 税金
    未超过750 收入的1%
    750~2250 7.50美元加上超出750美元部分的2%
    2250~3750 37.50美元加上超出2 250美元部分的3%
    3750~5250 82.50美元加上超出3 750美元部分的4%
    5250~7000 142.50美元加上超出5 250美元部分的5%
    超过7 000 230.00美元加上超出7 000美元部分的6%
    编写一个程序,要求用户输入需纳税的收入,然后显示税金。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	float income, tax;

	printf("Enter your income: ");
	scanf("%f", &income);

	if (income <= 750) {
		tax = income * 0.001f;
	}
	else if (income <= 2250) {
		tax = 7.50f + (income - 750) * 0.002f;
	}
	else if (income <= 3750) {
		tax = 37.50f + (income - 2250) * 0.003f;
	}
	else if (income <= 5250) {
		tax = 82.50f + (income - 3750) * 0.004f;
	}
	else if (income <= 7000) {
		tax = 142.50f + (income - 5250) * 0.005f;
	}
	else {
		tax = 230.000f + (income - 7000) * 0.006f;
	}

	printf("tax: %.2f\n", tax);

	return 0;
}
#include <stdio.h>

int main(void) {

    float income, tax;

    printf("Enter taxable income: ");
    scanf("%f", &income);

    if (income <= 750.00f)
        tax = .01f * income;
    else if (income <= 2250.00f)
        tax = 7.50f + (.02f * (income - 750.00f));
    else if (income <= 3750.00f)
        tax = 37.50f + (.03f * (income - 2250.00f));
    else if (income <= 5250.00f)
        tax = 82.50f + (.04f * (income - 3750.00f));
    else if (income <= 7000)
        tax = 142.50f + (.05f * (income - 5250.00f));
    else
        tax = 230.00f + (.06f * (income - 7000.00f));

    printf("Tax due: $%.2f\n", tax);

    return 0;
}
  1. 修改4.1节的upc.c 程序,使其可以检测UPC的有效性。在用户输入UPC后,程序将显示VALID 或NOT VALID 。
#include <stdio.h>

int main(void) {

    int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, sum1, sum2, total;

    printf("Enter a complete UPC: ");
    scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",
          &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10, &n11, &n12);


    sum1 = n1 + n3 + n5 + n7 + n9 + n11;
    sum2 = n2 + n4 + n6 + n8 + n10;
    total = 3 * sum1 + sum2;

    if (n12 == 9 - ((total - 1) % 10))
        printf("VALID\n");
    else 
        printf("NOT VALID\n");

    return 0;
}

  1. 编写一个程序,从用户输入的4个整数中找出最大值和最小值:
    Enter four integers: 21 43 10 35
    Largest: 43
    Smallest: 10
      要求尽可能少用if 语句。提示 :4条if 语句就足够了。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int num1, num2, num3, num4, max1, max2, min1, min2;

	printf("Enter four integers: ");
	scanf("%d%d%d%d", &num1, &num2, &num3, &num4);

	if (num1 > num2) {
		max1 = num1;
		min1 = num2;
	}
	else {
		max1 = num2;
		min1 = num1;
	}

	if (num3 > num4) {
		max2 = num3;
		min2 = num4;
	}
	else {
		max2 = num4;
		min2 = num3;
	}

	if (max1 > max2) {
		printf("Largest: %d\n", max1);
	}
	else {
		printf("Largest: %d\n", max2);
	}

	if (min1 > min2) {
		printf("Smallest: %d\n", min2);
	}
	else {
		printf("Smallest: %d\n", min1);
	}
	

	return 0;
}

存在问题:一次打印,降低操作系统开销。

#include <stdio.h>

int main(void) {

    int n1, n2, n3, n4, max, min, min1, min2;

    printf("Enter four integers: ");
    scanf("%d%d%d%d", &n1, &n2, &n3, &n4);

    if (n1 >= n2) {
        max1 = n1;
        min1 = n2;
    } else { 
        max1 = n2;
        min1 = n1;
    }
    
    if (n3 >= n4) {
        max2 = n3;
        min2 = n4;
    } else {
        max2 = n4;
        min2 = n3;
    }

    if (max2 > max1)
        max = max2;
    else
        max = max1;
    if (min1 < min2)
        min = min1;
    else
        min = min2;

    printf("Largest: %d\nSmallest: %d\n", max, min);

    return 0;
}

  1. 下面的表格给出了一个城市到另一个城市的每日航班信息。
    起飞时间 抵达时间
    8:00 a.m. 10:16 a.m.
    9:43 a.m. 11:52 a.m.
    11:19 a.m. 1:31 p.m.
    12.47 p.m. 3:00 p.m.
    2:00 p.m. 4:08 p.m.
    3:45 p.m. 5:55 p.m.
    7:00 p.m. 9:20 p.m.
    9:45 p.m. 11:58 p.m.
      编写一个程序,要求用户输入一个时间(用24小时制的时分表示)。程序选择起飞时间与用户输入最接近的航班,显示出相应的起飞时间和抵达时间。
    Enter a 24-hour time: 13:15
    Closest departure time is 12:47 p.m., arriving at 3:00 p.m.
      提示 :把输入用从午夜开始的分钟数表示。将这个时间与表格里也用从午夜开始的分钟数表示的起飞时间相比。例如,13:15从午夜开始是13×60+15 = 795分钟,与下午12:47(从午夜开始是767分钟)最接近。
#include <stdio.h>

int main(void) {

    int user_time,
        hour,
        minute,
        d1 = 480,
        d2 = 583,
        d3 = 679,
        d4 = 767,
        d5 = 840,
        d6 = 945,
        d7 = 1140,
        d8 = 1305;

    printf("Enter a 24-hour time: ");
    scanf("%d :%d", &hour, &minute);
    user_time = hour * 60 + minute;

    printf("Closest departure time is ");

    if (user_time <= d1 + (d2 - d1) / 2)
        printf("8:00 a.m., arriving at 10:16 a.m.\n");
    else if (user_time < d2 + (d3 - d2) / 2)
        printf("9:43 a.m., arriving at 11:52 a.m.\n");
    else if (user_time < d3 + (d4 - d3) / 2)
        printf("11:19 a.m., arriving at 1:31 p.m.\n");
    else if (user_time < d4 + (d5 - d4) / 2)
        printf("12:47 p.m., arriving at 3:00 p.m.\n");
    else if (user_time < d5 + (d6 - d5) / 2)
        printf("2:00 p.m., arriving at 4:08 p.m.\n");
    else if (user_time < d6 + (d7 - d6) / 2)
        printf("3:45 p.m., arriving at 5:55 p.m.\n");
    else if (user_time < d7 + (d8 - d7) / 2)
        printf("7:00 p.m., arriving at 9:20 p.m.\n");
    else
        printf("9:45 p.m., arriving at 11:58 p.m.\n");

    return 0;
}

  1. 编写一个程序,提示用户输入两个日期,然后显示哪一个日期更
    早:
    Enter first date (mm/dd/yy): 3/6/08
    Enter second date (mm/dd/yy): 5/17/07
    5/17/07 is earlier than 3/6/08
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int m1, d1, y1, m2, d2, y2;

	printf("Enter first date (mm/dd/yyyy): ");
	scanf("%d /%d /%d", &m1, &d1, &y1);
	printf("Enter first date (mm/dd/yyyy): ");
	scanf("%d /%d /%d", &m2, &d2, &y2);

	if (y2 > y1) {
		printf("%d/%d/%d is earlier than %d/%d/%d\n", m1, d1, y1, m2, d2, y2);
	}
	else if (y2 == y1 && m2 > m1) {
		printf("%d/%d/%d is earlier than %d/%d/%d\n", m1, d1, y1, m2, d2, y2);
	}
	else if (y1 == y2 && m1 == m2 && d2 > d1) {
		printf("%d/%d/%d is earlier than %d/%d/%d\n", m1, d1, y1, m2, d2, y2);
	}
	else {
		printf("%.2d/%.2d/%.2d is earlier than %.2d/%.2d/%.2d\n",  m2, d2, y2, m1, d1, y1 );

	}

	return 0;
}

有点问题

#include <stdio.h>

int main(void) {

    int d1, d2, m1, m2, y1, y2;

    printf("Enter first date (mm/dd/yy): ");
    scanf("%d /%d /%d", &m1, &d1, &y1);
    printf("Enter second date (mm/dd/yy): ");
    scanf("%d /%d /%d", &m2, &d2, &y2);

    if (y2 > y1)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n", m1, d1, y1, m2, d2, y2);
    else if (y1 > y2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n", m2, d2, y2, m1, d1, y1);
    else if (m2 > m1)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n", m1, d1, y1, m2, d2, y2);
    else if (m1 > m2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n", m2, d2, y2, m1, d1, y1);
    else if (d2 > d1)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n", m1, d1, y1, m2, d2, y2);
    else if (d1 > d2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n", m2, d2, y2, m1, d1, y1);
    else
         printf("%d/%d/%.2d is equal to %d/%d/%.2d\n", m1, d1, y1, m2, d2, y2);

    return 0;
}

  1. 利用switch 语句编写一个程序,把用数字表示的成绩转化为字母表示的等级。
    Enter numerical grade: 84
    Letter grade: B
      使用下面的等级评定规则:A为90~100,B为80~89,C为70~79,D为60~69,F为0~59。如果成绩高于100或低于0显示出错消息。提示 :把成绩拆分成2个数字,然后使用switch 语句判定十位上的数字。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int grade, shiwei, gewei;

	printf("Enter numerical grade: ");
	scanf("%d", &grade);

	if (grade < 0 || grade > 100) {
		printf("error\n");
	}

	shiwei = grade / 10;
	gewei = grade % 10;

	switch (shiwei) {
	case 6:
		printf("Letter grade: D\n");
		break;
	case 7:
		printf("Letter grade: C\n");
		break;
	case 8:
		printf("Letter grade: B\n");
		break;
	case 9: case 10:
		printf("Letter grade: A\n");
		break;
	default:
		printf("Letter grade: F\n");
		break;
	}

	return 0;
}
#include <stdio.h>

int main(void) {

    int grade;

    printf("Enter numerical grade: ");
    scanf("%d", &grade);

    if (grade > 100 || grade < 0)
        grade = -11; /* To be properly caught in default case */

    switch (grade/10) {
        case 0: case 1: case 2: case 3: case 4: case 5:
            printf("Letter grade: F\n");
            break;
        case 6:
            printf("Letter grade: D\n");
            break;
        case 7:
            printf("Letter grade: C\n");
            break;
        case 8:
            printf("Letter grade: B\n");
            break;
        case 9: case 10:
            printf("Letter grade: A\n");
            break;
        default:
            printf("Error: numerical grade out of range 0-100\n");
            break;
    }

    return 0;
}
  1. 编写一个程序,要求用户输入一个两位数,然后显示该数的英文单词:
    Enter a two-digit number: 45
    You entered the number forty-five.
      提示 :把数分解为两个数字。用一个switch 语句显示第一位数字对应的单词(“twenty”、“thirty”等),用第二个switch语句显示第二位数字对应的单词。不要忘记11~19需要特殊处理。
#include <stdio.h>

int main(void) {

    int n;

    printf("Enter a two-digit number: ");
    scanf("%d", &n);

    switch (n / 10) {
        case 1:
            switch (n % 10) {
                case 0:
                    printf("You entered the number ten\n");
                    break;
                case 1:
                    printf("You entered the number eleven\n");
                    break;
                case 2:
                    printf("You entered the number twelve\n");
                    break;
                case 3:
                    printf("You entered the number thirteen\n");
                    break;
                case 4:
                    printf("You entered the number fourteen\n");
                    break;
                case 5:
                    printf("You entered the number fifteen\n");
                    break;
                case 6:
                    printf("You entered the number sixteen\n");
                    break;
                case 7:
                    printf("You entered the number seventeen\n");
                    break;
                case 8:
                    printf("You entered the number eighteen\n");
                    break;
                case 9:
                    printf("You entered the number nineteen\n");
                    break;
                default:
                    break;
            }
            break;
        case 2:
            printf("You entered the number twenty");
            break;
        case 3:
            printf("You entered the number thirty");
            break;
        case 4:
            printf("You entered the number forty");
            break;
        case 5:
            printf("You entered the number fifty");
            break;
        case 6:
            printf("You entered the number sixty");
            break;
        case 7:
            printf("You entered the number seventy");
            break;
        case 8:
            printf("You entered the number eighty");
            break;
        case 9:
            printf("You entered the number ninety");
            break;
        default:
            printf("Your number is out of range 10-99\n");
            return 0;
    }

    if (n / 10 != 1) {
        switch (n % 10) {
            case 0:
                printf("\n");
                break;
            case 1:
                printf("-one\n");
                break;
            case 2:
                printf("-two\n");
                break;
            case 3:
                printf("-three\n");
                break;
            case 4:
                printf("-four\n");
                break;
            case 5:
                printf("-five\n");
                break;
            case 6:
                printf("-six\n");
                break;
            case 7:
                printf("-seven\n");
                break;
            case 8:
                printf("-eight\n");
                break;
            case 9:
                printf("-nine\n");
                break;
            default:
                break;
        }
    }

    return 0;
}