第七章 C控制语句:分支和跳转

GitHub地址,欢迎 star

7.1 if 语句

我们首先从一个程序认识 if 。该程序读取一列数据,每个数据都标识每日的最低温度(℃),然后打印统计的总天数和最低温度在 0℃ 以下的天数占总天数的百分比。

#include <stdio.h>
int main(void)
{
    const int FREEZING = 0;
    float temperature;
    int cold_days = 0;
    int all_days = 0;

    printf("Enter the list of daily low temperatures.\n");
    printf("Use Celsius, and enter q to quit.\n");
    while(scanf("%f", &temperature) == 1)
    {
        all_days++;
        if(temperature < FREEZING)
        {
            cold_days++;
        }
    }
    if(all_days != 0)
        {
            printf("%d days total: %.lf%% were below freezing.\n", all_days, 100.0 * (float)cold_days / all_days);
        }
        if(all_days == 0)
        {
            printf("No data entered!\n");
        }
	return 0;
}

输出:
Enter the list of daily low temperatures.
Use Celsius, and enter q to quit.
12 5 -2.5 0 6 8 -3 -10 5 10 q
10 days total: 30% were below freezing.

7.2 if else 语句

简单形式的 if 语句可以让程序选择执行一条语句,或者跳过这条语句。C 还提供了 if else 形式,可以在两条语句之间作选择。我们可以把上面的改一下:

if(all_days != 0)
{
	printf("%d days total: %.lf%% were below freezing.\n", all_days, 100.0 * (float)cold_days / all_days);
} 
else
{
	 printf("No data entered!\n");
}

7.2.1 另一个示例:介绍 getchar() 和 putchar()

到目前为止,我们所学的输入输出有 scanf() 和 printf() 根据转换说明读写字符。

getchar() 函数不带任何参数,它从输入队列中返回下一个字符。例如: ch = getchar();
putchar() 函数打印它的参数。例如打印 ch :putchar(ch);

由于它们只能处理字符,所以它们比 scanf() 和 printf() 更高效、简洁。

接下来,我们编写一个程序来说明这两个函数是如何工作的。该程序把一行输入重新打印出来,但是每个非空格都被替换成原字符在 ASCII 序列中的下一个字符,空格不变。这一过程可描述为“如果字符是空白,原样打印;否则,打印原字符在 ASCII 序列中的下一个字符”。

#include <stdio.h>
#define SPACE ' '
int main(void)
{
    char ch;
    ch = getchar();
    while(ch != '\n')
    {
        if(ch == SPACE)
            putchar(ch);
        else
            putchar(ch + 1);
        ch = getchar();
    }
    putchar(ch);
	return 0;
}

输出:
CALL ME HAL.
DBMM NF IBM/

7.2.2 ctype.h 系列的字符函数

C 有一系列专门处理字符的函数,ctype.h 头文件包含了这些函数的原型。这些函数接收一个字符作为参数,如果该字符是特殊字符,就返回 true;否则,返回 false。如下示例:

#include <stdio.h>
#include <ctype.h>
int main(void)
{
    char ch;
    while((ch = getchar()) != '\n')
    {
        if(isalpha(ch)) // 如果是一个字符
            putchar(ch + 1);
        else
            putchar(ch);
    }
    putchar(ch);
	return 0;
}

程序输出:
Look! It’s a programmer!
Mppl! Ju’t b qsphsbnnfs!

这里需要注意的是,ctype.h 里的函数并不是给变原有字符,而是修改函数返回的字符。ch = getchar() 这种形式的才是修改原有字符

小结:用 if 语句进行选择

关键字: if、else

一般注解:
下面各种形式中,statement 可以是一条简单语句或符合语句。表达式为真说明其值是非零值。
形式1:
if(expression)
statement
如果 expression 为真,执行 statement 部分、
形式2:
if(expression)
statement1
else
statement2
如果 expression 为真,执行 statement1,否则执行 statement2。
形式3:
if(expression1)
statement1
else if(expression2)
statement2
else
statement3
如果 expression1 为真,执行 statement1,如果 expression2 为真,执行 statement2,否则执行 statement3。

7.3 逻辑运算符

逻辑运算符两侧的条件必须都为真,整个表达式才为真。逻辑运算符的优先级比关系运算符低,所以不必再子表达式两侧加圆括号。
假设 exp1 和 exp2 是两个简单的关系表达式,那么:

  • 当且仅当 exp1 和 exp2 都为真时,exp1 && exp2 为真;
  • 如果 exp1 和 exp2 为真,则 exp1 || exp2 为真;
  • 如果 exp1 为假,则 !exp1 为真;如果 exp1 为真,则 !exp1 为假。

7.3.1 优先级

! 运算符的优先级很高,比乘法运算符还高,与递增运算符的优先级相同,只比圆括号的优先级低。&& 运算符的优先级比 || 运算符高,但是两者的优先级都比关系运算符低,比赋值运算符高。因此,表达式 a > b && b > c || b > d 相当于 ((a > b) && (b > c)) || (b > d)。

小结:逻辑运算符和表达式

逻辑运算符:
逻辑运算符的运算对象通常是关系表达式。! 运算符只需要一个运算对象,其他两个逻辑运算符都需要两个运算对象,左侧一个,右侧一个。
逻辑表达式:
当且仅当 expression1 和 expression2 都为真,expression1 && expression2 才为真。如果 expression1 或 expression2 为真,expression1 || expression2 为真。如果 expression为假,!expression则为真,反之亦然。
求值顺序:
逻辑表达式的求值顺序是从左往右。一旦发现有使整个表达式为假的因素,立即停止求值。

7.4 一个统计单词的程序

现在,我们可以编写一个统计单词数量的程序。该程序还可以计算字符数和行数。

#include <stdio.h>
#include <ctype.h> // 为 isspace() 函数提供原型
#include <stdbool.h> // 为 bool、true、false 提供定义
#define STOP '|'
int main(void)
{
    char c; // 读入字符
    char prev; // 读入的前一根字符
    long n_chars = 0L; // 字符数
    int n_lines = 0; // 行数
    int n_words = 0; // 单词数
    int p_lines = 0; // 不完整的行数
    bool inword = false; // 如果 c 再单词中,inword 等于 true

    printf("Enter text to be analyzed (| to terminate) :\n");
    prev = '\n'; // 用于识别完整的行
    while((c = getchar()) != STOP)
    {
        n_chars++; // 统计字符
        if(c == '\n')
        {
            n_lines++; // 统计行
        }
        if(!isspace(c) && !inword)
        {
            inword = true; // 开始一个新的单词
            n_words++; // 统计单词
        }
        if(isspace(c) && inword)
        {
            inword = false; // 达到单词的末尾
        }
        prev = c; // 保存字符的值
    }

    if(prev != '\n')
    {
        p_lines = 1;
    }
    printf("characters = %ld, words = %d, lines = %d, ", n_chars, n_words, n_lines);
    printf("partial lines = %d\n", p_lines);
	return 0;
}

程序运行输出:
Enter text to be analyzed (| to terminate) :
Reason is a
powerful servant but
an inadequate master.
|
characters = 55, words = 9, lines = 3, partial lines = 0

7.5 条件运算符:?:

C 提供 条件表达式(conditional expression) 作为表达式 if else 语句的一种便捷方式,该表达式使用 ?: 条件运算符。该运算符分为两部分,需要3个运算对象。也叫三元运算。条件运算符是C语言中唯一的三元运算符。
条件表达式的通用形式如下:
expression1 ? expression2 : expression3
如果 expression1 为真,那么整个条件表达式的值为 expression2 ;如果 expression1 为假,那么整个表达式的值为 expression3。
示例:
(5 > 3) ? 1 : 2 值为1
(3 > 5) ? 1 : 2 值为2

7.6 循环辅助:continue 和 break

7.6.1 continue 语句

3 种循环都可以使用 continue 语句。执行到该语句时,会跳过本次迭代的剩余部分,并开始下一轮迭代。如果 continue 语句在嵌套循环内,则只会影响包含该语句的内层循环。

7.6.2 break 语句

程序执行到循环中的 break 语句时,会终止包含它的循环,并继续执行下一阶段。如果 break 语句位于嵌套循环内,它只会影响包含它的当前循环。

7.7 多重选择:switch 和 break

使用条件运算符和 if else 语句很容易编写二选一的程序。然而,有时程序需要在多个选项中进行选择。可以用 if else if … else 来完成。但是,大多数情况下使用 switch 语句更方便。我们现在看一下读取一个字母,然后打印出与该字母开头的动物名。

#include <stdio.h>
#include <ctype.h>
int main(void)
{
    char ch;
    printf("Give me a letter of the alphabet, and I will give ");
    printf("an animal name\nbeginning with that letter.\n");
    printf("Please type in a letter; type # to end my act.\n");
    while((ch = getchar()) != '#')
    {
        if('\n' == ch)
            continue;
        if(islower(ch))
            switch(ch)
        {
            case 'a':
                printf("argali, a wild sheep of Asia\n");
                break;
            case 'b':
                printf("babirusa, a wild pig of Malay\n");
                break;
            case 'c':
                printf("coati, racoomlike mammal\n");
                break;
            case 'd':
                printf("desman, aquatic, molelike critter\n");
                break;
            case 'e':
                printf("echidna, the spiny anteater\n");
                break;
            case 'f':
                printf("fisher, brownish marten\n");
                break;
            default:
                printf("That's a stumper!\n");
                break;
        }
        else
            printf("I recognize only lowercase letters.\n");
        while(getchar() != '\n')
            continue;
        printf("Please type another letter or a #.\n");
    }
    printf("Bye!\n");
    return 0;
}

Give me a letter of the alphabet, and I will give an animal name
beginning with that letter.
Please type in a letter; type # to end my act.
a
argali, a wild sheep of Asia
Please type another letter or a #.
dab
desman, aquatic, molelike critter
Please type another letter or a #.
r
That’s a stumper!
Please type another letter or a #.
Q
I recognize only lowercase letters.
Please type another letter or a #.
#
Bye!
该程序的两个主要特点是:使用了 switch 语句和它对输出的处理。。

7.8 关键概念

智能的一方面是,根据情况作出相应的响应。所以,选择语句时开发具有智能行为程序的基础。C 语言通过 if 、if else 和 switch 语句,以及条件运算符(? : )可以实现智能选择。

if 和 if else 语句使用测试条件来判断执行哪些语句。所有非零值都被视为 true ,零被视为 false 。测试通常涉及关系表达式(比较两个值)、逻辑表达式(用逻辑运算符组合或更改其他表达式)。

要记住一个通用原则,如果要测试两个条件,应该使用逻辑运算符把两个完整的测试表达式组合起来。

7.9 本章小结

if 语句使用测试条件控制程序是否执行测试条件后面的一条简单语句或复合语句。如果测试表达式的值是非零值,则执行语句;如果测试表达式的值是零,则不执行语句。if else 语句可用于二选一的情况。如果测试条件是非零,则执行 else 前面的语句;如果测试表达式的值是零,则执行 else 后面的语句。在 else 后面使用另一 if 语句形成 else if 。可构造多选一的结构。

测试条件通常都是关系表达式,即用一个关系运算符的表达式。使用 C 的逻辑运算符,可以把关系表达式组合成更复杂的测试条件。

在多数情况下,用条件运算符写成的表达式比 if else 语句更简洁。

ctype.h 系列的字符函数为创建以分类字符为基础的测试表达式提供了便捷的工具。

switch 语句可以在一系列以整数作为标签的语句中进行选择。如果紧跟在 switch 关键字后的测试条件的整数值与某标签匹配,程序就转至执行匹配的标签语句,然后在遇到 break 之前,继续执行标签语句后面的语句。

break 、continue语句都是跳转语句,使程序流跳转至程序的另一处。break 语句使程序跳转至紧跟在包含 break 语句的循环或 switch 末尾的下一条语句。continue 语句使程序跳出当前循环的剩余部分,并开始下一轮迭代。

7.10 复习题

1、判断下列表达式是 true 还是 false。
a、 100 > 3 && ‘a’ > ‘c’
b、100 > 3 || ‘a’ > ‘c’
c、!(100 > 3)
答案:a、false;b、true;c、false

2、根据下列描述的条件,分别构造一个表达式:
a、number 等于或大于 90 ,但是小于 100
b、ch 不是字符 q 或 k
c、number 在 1 ~ 9 之间(包括 1 和 9),但不是 5
d、number 不在 1 ~ 9 之间
答案:a、number >= 90 && number < 100
b、ch != ‘q’ || ch != ‘k’
c、number >= 1 && number <= 9 && number != 5
d、number < 1 || number > 9

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

#include <stdio.h>
int main(void)
{
    int weight, height;
    scanf("%d , weigth, height); /* weight 以磅为单位, height 以英寸为单位 */
    if(weight < 100 && height > 64)
        if(height >= 72)
            printf("You are very tall for your weight.\n");
        else if(height < 72 &&> 64)
            printf("You are tall for your weight.\n");
        else if(weight > 300 && !(weight <= 300) && height < 48)
        if(!(height >= 48))
            printf(" You are quite short for your weight.\n");
    else
        printf(" You are quite short for your weight.\n");
    return 0;
}

答案:

#include <stdio.h>
int main(void)
{
    int weight, height;
    printf("Enter your weight in pounds and your height in inches.\n");
    scanf("%d %d", &weight, &height); /* 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(" You are quite short for your weight.\n");
    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
答案:a、true;b、false;c、true:d、6;e、10;f、false

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

#include <stdio.h>
int main(void)
{
    int num;
    for(num = 1; num <= 11; num++)
    {
        if(num % 3 == 0)
            putchar('$');
        else
            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;
}

答案:

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;
}

答案:

#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++;
        oc++;
    }
    printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
    return 0;
}

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

#include <stdio.h>
int main(void)
{
    int age = 20;
    while(age++ <= 65)
    {
        if((age % 20) == 0)
            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.
问题出在 if(age = 65)

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 ,但这次不能使用 continue 和 goto 语句。

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

7.11 编程练习

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

#include <stdio.h>
#define SPACE ' '
#define ENTER '\n'
int main(void)
{
    char ch;
    int spaceNum = 0,enterNum = 0,charNum = 0;
    printf("Please input(end with #)");
    while((ch = getchar()) != '#')
    {
        if(ch == SPACE)
        {
            spaceNum++;
        }
        else if(ch == ENTER)
        {
           enterNum++;
        }
        else
        {
            charNum++;
        }
    }
    printf("Space num is %d,Enter num is %d, Char num is %d",spaceNum,enterNum,charNum);
    return 0;
}

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

#include <stdio.h>
#define ENTER '\n'
int main(void)
{
    char ch;
    int charNum = 0;
    printf("Please input(end with #)\n");
    while((ch = getchar()) != '#')
    {
        if(ch != ENTER)
        {
            charNum++;
            printf("%c - %d",ch,ch);
            if(charNum % 8 == 0)
                printf("\n");
        }

    }
    return 0;
}

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

#include <stdio.h>
int main(void)
{
    int ch;
    int num1 = 0,num2 = 0,sum1 = 0,sum2 = 0;
    printf("Please input(end with 0)\n");
    scanf("%d",&ch);
    while(ch != 0)
    {
        if(ch % 2 != 0)
        {
            num1++;
            sum1 += ch;
        }
        else
        {
            num2++;
            sum2 += ch;
        }
        scanf("%d",&ch);

    }
    printf("Odd num average is %d, count is %d\n",sum1 / 2,num1);
    printf("Even num average is %d, count is %d\n",sum2 / 2,num2);
    return 0;
}

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

#include <stdio.h>
int main(void)
{
    char ch;
    int replaceNum = 0;
    printf("Please input(end with #)");
    while((ch = getchar()) != '#')
    {
        if(ch == '.')
        {
            replaceNum++;
            printf("!");
        }
        else if(ch == '!')
        {
           replaceNum++;
           printf("!!");
        }
        else
        {
            printf("%c",ch);
        }
    }
    printf("Replace num is %d",replaceNum);
    return 0;
}

5、使用 switch 重写练习 4。

#include <stdio.h>
int main(void)
{
    char ch;
    int replaceNum = 0;
    printf("Please input(end with #)");
    while((ch = getchar()) != '#')
    {
        switch(ch)
        {
        case '.':
            replaceNum++;
            printf("!");
            break;
        case '!':
            replaceNum++;
            printf("!!");
           break;
        default:
            printf("%c",ch);
            break;
        }
    }
    printf("Replace num is %d",replaceNum);
    return 0;
}

6、编写程序读取输入,读到 # 停止,报告 ei 出现的次数。

注意
该程序要记录前一个字符和当前字符。用 “Receive your eieio award” 这样的输入来测试。

#include <stdio.h>
int main(void)
{
    char ch,preCh;
    int showNum = 0;
    printf("Please input(end with #)");
    while((ch = getchar()) != '#')
    {
        if(preCh == 'e' && ch == 'i')
        {
            showNum++;
        }
        preCh = ch;
    }
    printf("Show num is %d",showNum);
    return 0;
}

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

#include <stdio.h>
#define MONEY_H 1000
#define BASE_M 300
#define LEVEL1_M 450
#define BASE_RATE 0.15
#define LEVEL1_RATE 0.2
#define LEVEL2_RATE 0.25
int main(void)
{
    int hours,wage,taxSum;
    scanf("%d",&hours);
    if(hours <= BASE_M)
    {
        wage = hours * MONEY_H;
        taxSum = wage * BASE_RATE;
    }
    else if(hours <= LEVEL1_M)
    {
        wage = hours * MONEY_H;
        taxSum = BASE_M * BASE_RATE + (wage - BASE_M) * LEVEL1_RATE;
    }
    else
    {
        wage = hours * MONEY_H;
        taxSum = BASE_M * BASE_RATE + LEVEL1_M * LEVEL1_RATE + (wage - BASE_M - LEVEL1_M) * LEVEL2_RATE;
    }
    printf("Wage is %d,tax is %d,income is %d",wage,taxSum,wage - taxSum);
    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 BASE_M 300.0
#define LEVEL1_M 450.0
#define BASE_RATE 0.15
#define LEVEL1_RATE 0.2
#define LEVEL2_RATE 0.25
int main(void)
{
    int select;
    float hours,wage,taxSum,moneyH;
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr\n");
    printf("2) $9.33/hr\n");
    printf("3) $10.00/hr\n");
    printf("4) $11.20/hr\n");
    printf("5) $quit\n");
    scanf("%d",&select);
    while(select != 5)
    {
        switch(select)
        {
        case 1:
            moneyH = 8.75;
            break;
        case 2:
            moneyH = 9.33;
            break;
        case 3:
            moneyH = 10.00;
            break;
        case 4:
            moneyH = 11.20;
            break;
        }
        printf("Enter the number of hours:\n");
        scanf("%f",&hours);
        if(hours <= BASE_M)
        {
            wage = hours * moneyH;
            taxSum = wage * BASE_RATE;
        }
        else if(hours <= LEVEL1_M)
        {
            wage = hours * moneyH;
            taxSum = BASE_M * BASE_RATE + (wage - BASE_M) * LEVEL1_RATE;
        }
        else
        {
            wage = hours * moneyH;
            taxSum = BASE_M * BASE_RATE + LEVEL1_M * LEVEL1_RATE + (wage - BASE_M - LEVEL1_M) * LEVEL2_RATE;
        }
        printf("Wage is %0.2f,tax is %0.2f,income is %0.2f\n",wage,taxSum,wage - taxSum);
        printf("Enter the number corresponding to the desired pay rate or action:\n");
        printf("1) $8.75/hr\n");
        printf("2) $9.33/hr\n");
        printf("3) $10.00/hr\n");
        printf("4) $11.20/hr\n");
        printf("5) $quit\n");
        scanf("%d",&select);
    }
    return 0;
}

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

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

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值