课后习题第七章

牧云@^-^@ 提交于 2020-12-31 09:39:45

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


#include<stdio.h>
int main(void)
{
    int space = 0, line_break = 0, other = 0;
    char ch;
    while((ch = getchar()) != '#')
    {
        if(' ' == ch)
        {
            space++;
        }
        else if('\n' == ch)
        {
            line_break++;
        }
        else
        {
            other++;
        }
    }
    printf("space = %d\nline_break = %d\nother = %d\n", space, line_break, other);
    return 0;
}

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


#include<stdio.h>
int main()
{
    char ch;
    int count = 0;
    printf("Please enter text(# to terminate):\n");
    while((ch = getchar()) != '#')
    {
        printf("%c:%d ", ch, ch);
        count++;
        if(count%8 == 0 && count != 0)
        {
            printf("\n");
        }
    }
    return 0;
}

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

#include<stdio.h>
int main()
{
    int number = 1;
    int odd_number = 0, even_number = 0;
    int count_odd = 0, count_even = 0;
 
    printf("Please enter integer to be analyzed(0 to terminate):\n");
 
    while(scanf("%d", &number) == 1 && number != 0)
    {
        if(number%2 != 0)
        {
            odd_number += number;
            count_odd++;
        }
 
        if(number%2 == 0)
        {
           even_number += number;
            count_even++;
        }
 
    }
    odd_number /= count_odd ;
    even_number /= count_even;
    printf("奇数有%d个,平均值 = %d\n", count_odd, odd_number);
    printf("偶数有%d个,平均值 = %d\n", count_even, even_number);
    return 0;
}

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


#include<stdio.h>
int main()
{
    char ch = 0;
    int count1 = 0, count2 = 0;
    printf("Please enter text to be analyzed:(# to terminate):\n");
 
    while ((ch = getchar()) != '#')
    {
        if ('.' == ch)
        {
            putchar('!');
            count1++;
        }
        else if ('!' == ch)
        {
            putchar('!');
            putchar('!');
            count2++;
        }
        else
        {
            putchar(ch);
        }
    }
    printf("\n!代替.一共%d次, !!代替!一共%d次\n", count1, count2);
 
 
    return 0;
}

5.用switch重写练习4。


#include<stdio.h>
int main()
{
    char ch = 0;
    int count1 = 0, count2 = 0;
    printf("Please enter text to be analyzed:(# to terminate):\n");
 
    while ((ch = getchar()) != '#')
    {
 
        switch(ch)
        {
            case '.':  putchar('!'); count1++; break;
            case '!':  putchar('!'); putchar('!'); count2++; break;
            default:   putchar(ch); break;
 
        }
 
    }
    printf("\n!代替.一共%d次, !!代替!一共%d次\n", count1, count2);
 
    return 0;
}

6.编写一个程序读取输入,直接#,并报告序列ei出现的次数。

说明: 此程序必须要记住前一个字符和当前的字符。用诸如"Receive your eieio award."的输入测试它。


#include<stdio.h>
int main()
{
    char ch = ' ', pre_ch = ' ';
    int count = 0;
    while((ch = getchar()) != '#')
    {
        if(ch == 'e')
        {
            pre_ch = 'e';
        }
        if(pre_ch == 'e' && ch == 'i')
        {
            count++;
            pre_ch = ' ';
        }
    }
    printf("ei has appeared %d", count);
 
    return 0;
}

7. 编写程序,要求输入一周中的工作小时数,然后打印工资总额,税金以及净工资。作如下假设:

a. 基本工资等级 = 10.00美元/小时

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

c. 税率 前300美元为15%

下一个150美元为20%

余下的为25%

用#define定义常量,不必关心本例是否符合当前的税法。

#include<stdio.h>
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
 
int main()
{
    double work_time;
    double sum_sanary, tax, after_tax_sanary;
    scanf("%lf", &work_time);
    if(work_time > 40)
    {
        work_time += (work_time - 40)*1.5;
    }
    sum_sanary = work_time * 10;
    if(sum_sanary < 300)
    {
        tax = sum_sanary*RATE1;
    }
    else if(sum_sanary <= 450)
    {
        tax = 300*RATE1+ (sum_sanary - 300)*RATE2;
    }
    else
    {
        tax = 300*RATE1 + 150*RATE2 + (sum_sanary -450)*RATE3;
    }
    after_tax_sanary = sum_sanary - tax;
    printf("sum_sanary = %.2lf\ntax = %.2lf\nafter_tax_sanary"
            "= %.2lf\n", sum_sanary, tax, after_tax_sanary);
 
    return 0;
}

8. 修改练习7中的假设a,使程序提供一个选择等级的菜单。用switch选择工资等级。程序运行的开头应该像这样:

*************************************************************************************************

Enter the number corresponding to 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 sanary1 8.75
#define sanary2 9.33
#define sanary3 10.00
#define sanary4 11.20
 
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
 
int main()
{
    double work_time;
    double sum_sanary, tax, after_tax_sanary;
    int number;
    printf("*******************************************************************************\n");
    printf("Enter the number corresponding to 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");
    while(scanf("%d", &number) && number != 5)
    {
        if(number < 1 && number > 5)
        {
            printf("Please input correct command!\n");
        }
        printf("Please input your work_time:\n");
        scanf("%lf", &work_time);
        if(work_time > 40)
        {
            work_time += (work_time - 40)*1.5;
        }
        switch(number)
        {
            case 1: sum_sanary = work_time * sanary1; break;
            case 2: sum_sanary = work_time * sanary2; break;
            case 3: sum_sanary = work_time * sanary3; break;
            case 4: sum_sanary = work_time * sanary4; break;
        }
        if(sum_sanary < 300)
        {
            tax = sum_sanary*RATE1;
        }
        else if(sum_sanary <= 450)
        {
            tax = 300*RATE1+ (sum_sanary - 300)*RATE2;
        }
        else
        {
            tax = 300*RATE1 + 150*RATE2 + (sum_sanary -450)*RATE3;
        }
        after_tax_sanary = sum_sanary - tax;
        printf("sum_sanary = %.2lf\ntax = %.2lf\nafter_tax_sanary"
           "= %.2lf\n", sum_sanary, tax, after_tax_sanary);
    }
    printf("quit!\n");
 
    return 0;
}

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


#include<stdio.h>
#include<math.h>
int main()
{
    int i;
    int m;
    printf("please input the upper number:");
    scanf("%d", &m);
 
    if(m == 0)
        printf("0不是素数!\n");
    if(m==1)
        printf("1不是素数!\n");
    while(m > 1)
    {
        for(i=2; i <= sqrt(m); i++)//有等号
        {
            if( m%i ==0)
                break;
        }
        if(i > sqrt(m))
        printf("%d\n", m);//是素数
        m--;
    }
 
    return 0;
}

10.

#include<stdio.h>
 
#define RATE1 0.15
#define RATE2 0.28
 
int main()
{
    double sum_sanary, tax;
    int number = 0;
    printf("种类\t\t税金\n");
    printf("单身\t\t前17850美金按15%%,超出部分按28%%\t请按1\n");
    printf("户主\t\t前23900美金按15%%,超出部分按28%%\t请按2\n");
    printf("已婚,共有\t前29750美金按15%%,超出部分按28%%\t请按3\n");
    printf("已婚,离异\t前14875美金按15%%,超出部分按28%%\t请按4\n");
    printf("退出请按5\n\n");
    while(number != 5)
    {
        printf("请选择税金种类:\n");
        scanf("%d", &number);
        printf("Please input your sum_sanary:\n");
        scanf("%lf", &sum_sanary);
        switch(number)
        {
        case 1:
            if(sum_sanary <= 17850)
                tax = sum_sanary*RATE1;
            else
                tax = 17850*RATE1 + (sum_sanary-17850)*RATE2;
                break;
        case 2:
            if(sum_sanary <= 23900)
                tax = sum_sanary*RATE1;
            else
                tax = 23900*RATE1 + (sum_sanary-23900)*RATE2;
                break;
        case 3:
            if(sum_sanary <= 29750)
                tax = sum_sanary*RATE1;
            else
                tax = 29750*RATE1 + (sum_sanary-29750)*RATE2;
                break;
        case 4:
            if(sum_sanary <= 14875)
                tax = sum_sanary*RATE1;
            else
                tax = 14875*RATE1 + (sum_sanary-14875)*RATE2;
                break;
        }
         printf("tax = %.2lf\n\n", tax);
 
    }
 
    return 0;
}

11.


#include<stdio.h>
 
int main()
{
    double n_artichoke = 0.0;
    double n_beet = 0.0;
    double n_carrot = 0.0;
    double freight = 0.0;
    double n_pound = 0.0;
    char choice = 0;
    double total_cost = 0.0;
    double discount = 0.0;
    double total_weight = 0.0;
 
    while ('q' != choice)
    {
        printf("Please choose the item you want to buy:\n");
        printf("%-20s%-20s\n%-20s%-20s\n", "a) Artichoke", "b) Beet", "c) Carrot", "q) Quit");
        printf("Now enter you choose:");
        choice = getchar();
 
        switch (choice)
        {
        case 'a':
            printf("How many pounds of Artichoke do you want to buy:");
            scanf("%lf", &n_pound);
            n_artichoke += n_pound;
            break;
 
        case 'b':
            printf("How many pounds of Beet do you want to buy:");
            scanf("%lf", &n_pound);
            n_beet += n_pound;
            break;
 
        case 'c':
            printf("How many pounds of Carrot do you want to buy:");
            scanf("%lf", &n_pound);
            n_carrot += n_pound;
            break;
 
        case 'q':
            continue;
 
        default:
            printf("Your choice is invalid! Please choose again.\n");
            break;
        }
        while (getchar() != '\n');
    }
 
    printf("%-20s%-20s%-20s%-20s\n", "Category", "Price", "Pounds", "Total price");
    printf("%-20s%-20s%-20.2lf%-20.2lf\n", "Artichoke", "$2.05/pound", n_artichoke, (n_artichoke * 2.05));
    printf("%-20s%-20s%-20.2lf%-20.2lf\n", "Beet", "$1.15/pound", n_beet, (n_beet * 1.15));
    printf("%-20s%-20s%-20.2lf%-20.2lf\n", "Carrot", "$1.09/pound", n_carrot, (n_carrot * 1.09));
 
    total_cost = n_artichoke * 2.05 + n_beet * 1.15 + n_carrot * 1.09;
    printf("Total_cost:%.2lf\n", total_cost);
    if (total_cost > 100)
    {
        discount = total_cost * 0.05;
        printf(" Discount:%2lf\n", discount);
        total_cost -= discount;
    }
 
    total_weight = n_artichoke + n_beet + n_carrot;
    if (0 < total_weight && total_weight <= 5.0)
    {
        freight = 6.5;
    }
    else if (5.0 < total_weight && total_weight <= 20)
    {
        freight = 14.0;
    }
    else if (20 < total_weight )
    {
        freight = 14 + (total_weight - 20) * 0.5;
    }
    printf(" Total_freight:%.2lf\n", freight);
 
    printf(" Final cost:%.2lf\n", (total_cost  + freight));
 
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!