c primer plus(五版)编程练习-第七章编程练习

2018-06-18 04:02:56来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

#include<stdio.h>
#include<ctype.h>
int main(void){
    char ch;
    int count1,count2,count3;
    count1 = count2 = count3 = 0;
   printf("Enter text to be analyzed(#to terminate):\n");
while((ch = getchar()) != '#'){ if ( ch == '\n'){ count1++; } else if(isspace(ch)){ count2++; } else if (!isspace(ch)){ count3++; } } printf("换行数量%d,空格数量%d,其他字符数量%d",count1,count2,count3); return 0; }

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

#include<stdio.h>
#define STOP '#'
int main(void){
    char ch;
    int length = 1;
    printf("Enter text to be analyzed(#to terminate):\n");
    while((ch = getchar()) != STOP){
        if(length%8==0){
            printf("\n");
        }
        printf("%c/%d ",ch,ch);
        length++;
    }
    return 0;
}

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

#include<stdio.h>
int main(void){
    int num,even_num,odd_num;
    double even_count,odd_count;//必需定义为double类型,否则打印平均值会出错
    even_num = odd_num = 0;
    even_count = odd_count = 0.0 ;
    printf("Enter int to be analyzed(0 to terminate):\n");
    while (scanf("%d", &num) == 1 && num != 0){
            if (num%2 == 0){
                even_num++;
                even_count +=num;
            } else {
                odd_num++;
                odd_count +=num;
            }
    }
    if (even_count > 0)
        printf("Even have %d, even average is %.2f\n",even_num,even_count / even_num);
    if (odd_num > 0)
        printf("Odd have %d,odd average is %.2f",odd_num,odd_count/odd_num);
    return 0;
}

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

#include<stdio.h>
#define STOP '#'
int main(void){
    char ch;
    int i;
    i = 0;
    while((ch = getchar()) != STOP){
        if (ch == '.'){
            putchar('!');
            i++;
        } else if (ch == '!'){
            putchar(ch);
            putchar(ch);
            i++;
        } else {
            putchar(ch);
        }
    }
    printf("\n");
    printf("count %d",i);
    return 0;
}

5.用switch 重做练习3。

#include<stdio.h>
int main(void){
    int num,even_num,odd_num;
    double even_count,odd_count;//必需定义为double类型,否则打印平均值会出错
    even_num = odd_num = 0;
    even_count = odd_count = 0.0 ;
    printf("Enter int to be analyzed(0 to terminate):\n");
    while (scanf("%d", &num) == 1 && num != 0){
            switch (num%2){
                case 0:
                    even_num++;
                    even_count +=num;
                    break;
                case 1:
                    odd_num++;
                    odd_count +=num;
                    break;
            }
    }
    if (even_count > 0)
        printf("Even have %d, even average is %.2f\n",even_num,even_count / even_num);
    if (odd_num > 0)
        printf("Odd have %d,odd average is %.2f",odd_num,odd_count/odd_num);
    return 0;
}

 6.编写一个程序读取输入,直到#,并报告序列ei 出现的次数。说明此程序必须要记住前一个字符和当前的字符。用诸如“Receive your eieio award.”的输入测试它。

#include<stdio.h>
#define STOP '#'
#define PREV_CHAR 'e'
#define CURRENT_CHAR 'i'
int main(void){
    char ch, prev_char;
    int i;
    i = 0;
    prev_char = 0;
    printf("Enter text to be analyzed(#to terminate):\n");
    while((ch = getchar()) != STOP){
        if (ch == PREV_CHAR){
            prev_char = ch;
        }
        if (ch == CURRENT_CHAR && prev_char == PREV_CHAR){
            i++;
        }
    }
    printf("Enter of 'ei' %d ",i);
    return 0;
}

7.编写程序,要求输入一周中的工作小时数,然后打印工资总额、税金以及净工资。作如下假设:
a.基本工资等级=10.00 美元/小时
b.加班(超过40 小时)=1.5 倍的时间
c.税率前300 美元为15%
下一个150 美元为20%余下的为25%。

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

#include<stdio.h>
#define BASE 10.00
#define TAX_LEVEL1 300
#define TAX_LEVEL2 150
#define TAX_LEVEL1_RATE 0.15
#define TAX_LEVEL2_RATE 0.2
#define TAX_LEVEL3_RATE 0.25
#define BASE_HOUR 40
#define OVER_RATE 1.5
int main(void){
    float num,wage,tax,real_wage,temp,real_hour;
    wage = tax = real_wage = real_hour = 0.0;
    printf("Please enter the number of hours you work a week:\n");
    scanf("%f",&num);

    if (num > BASE_HOUR){
        real_hour = num + (num - BASE_HOUR)*OVER_RATE;
    } else {
        real_hour = num;
    }
    wage = real_hour*BASE;
    temp = wage - TAX_LEVEL1;
    if (temp <= 0){
        tax += wage*TAX_LEVEL1_RATE;
    } else {
        tax +=TAX_LEVEL1*TAX_LEVEL1_RATE;
        if (temp<= TAX_LEVEL2){
            tax += temp*TAX_LEVEL2_RATE;
        } else {
            tax += TAX_LEVEL2*TAX_LEVEL2_RATE;
            tax += (temp - TAX_LEVEL2) * TAX_LEVEL3_RATE;
        }
    }
    real_wage = wage - tax;
    printf("hour:%.2f wage:%.2f tax:%.2f real wage :%.2f",num,real_hour,wage,tax,real_wage);
    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 LEVEL1 8.75
#define LEVEL2 9.33
#define LEVEL3 10.00
#define LEVEL4 11.20
#define TAX_LEVEL1 300
#define TAX_LEVEL2 150
#define TAX_LEVEL1_RATE 0.15
#define TAX_LEVEL2_RATE 0.2
#define TAX_LEVEL3_RATE 0.25
#define BASE_HOUR 40
#define OVER_RATE 1.5
int main(void){
    int level;
    float fee,num,wage,tax,real_wage,temp,real_hour;
    wage = tax = real_wage = real_hour = fee = 0.0;

    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");
    while(scanf("%d",&level) == 1 && level != 5){

      switch(level){
        case 1:
            fee = LEVEL1;
            break;
        case 2:
            fee = LEVEL2;
            break;
        case 3:
            fee = LEVEL3;
            break;
        case 4:
            fee = LEVEL4;
            break;
        default:
             printf("You should enter the number between 1 to  4 (5 to quit).\n");
             printf("Please enter the right number: \n");
             continue;
        }
        printf("Please enter the number of hours you work a week:\n");
        scanf("%f",&num);
        if (num > BASE_HOUR){
            real_hour = num + (num - BASE_HOUR)*OVER_RATE;
        } else {
            real_hour = num;
        }
        wage = real_hour*fee;
        temp = wage - TAX_LEVEL1;
        if (temp <= 0){
            tax += wage*TAX_LEVEL1_RATE;
        } else {
            tax +=TAX_LEVEL1*TAX_LEVEL1_RATE;
            if (temp<= TAX_LEVEL2){
                tax += temp*TAX_LEVEL2_RATE;
            } else {
                tax += TAX_LEVEL2*TAX_LEVEL2_RATE;
                tax += (temp - TAX_LEVEL2) * TAX_LEVEL3_RATE;
            }
        }
        real_wage = wage - tax;
        printf("hour:%.2f wage:%.2f tax:%.2f real wage :%.2f\n",real_hour,wage,tax,real_wage);
        printf("Please enter next number:\n");
    }
    printf("exit!");
    return 0;
}

没有学透getchar和scanf的区别,一开始用getchar获取输入,结果出错了。这也告诉自己,对学习要有敬畏之心,看都教材不是看小说,要了解的是细节不是大概,马虎不得。

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

#include<stdio.h>
#include<stdbool.h>
int main(void){
    int num,i,k;
    bool isPrime;
    printf("Enter int number:\n");
    scanf("%d",&num);

    for(i=2;i<=num;i++){
        for(k=2,isPrime = true;(k*k)<=i;k++){
            if (i%k==0){

                 isPrime = false;
            }
        }
        if (isPrime){
            printf("%lu ",i);
        }
    }
    return 0;
}

10.1988 年United States Federal Tax Schedule 是近期最基本的。它分为4 类,每类有两个等级。下面是其摘要;美元数为应征税的收入。

种类 税金
单身 前17,850 美元按15%,超出部分按28%
户主 前23,900 美元按15%,超出部分按28%
已婚,共有 前29,750 美元按15%,超出部分按28%
已婚,离异 前14,875 美元按15%,超出部分按28%

 

 

 

 

 

例如,有20 000 美元应征税收入的单身雇佣劳动者应缴税金0.15×17 850 美元+0.28×(20 000美元–17 850 美元)。编写一个程序,让用户指定税金种类和应征税收入,然后计算税金。使用循环以便用户可以多次输入。

#include<stdio.h>
#define TYPE1 1
#define TYPE2 2
#define TYPE3 3
#define TYPE4 4
#define TAX_RATE1 0.15
#define TAX_RATE2 0.28
#define TYPE1_BASE 17850
#define TYPE2_BASE 23900
#define TYPE3_BASE 29750
#define TYPE4_BASE 14875
int main(void){
    double tax,income,base;
    int type;
    printf("请选择税金种类:\n");
    printf("1)单身   2)户主  3)已婚,共有 4)已婚,离异 5)退出\n");
    while(scanf("%d",&type) == 1 && type != 5){
        switch(type){
        case TYPE1:
            base = TYPE1_BASE;
            break;
        case TYPE2:
            base = TYPE2_BASE;
            break;
        case TYPE3:
            base = TYPE3_BASE;
            break;
        case TYPE4:
            base = TYPE4_BASE;
            break;
        default:
            printf("请输入1到4之间的数字,或者输入5为退出\n");
            continue;
        }
        printf("请输入您的应征税收入:\n");
        scanf("%lf",&income);
        if(income>base){
            tax = base*TAX_RATE1+(income-base)*TAX_RATE2;
        } else {
            tax = income*TAX_RATE1;
        }
        printf("您共要缴纳%.2lf美元税金\n",tax);
        printf("请输入您税金种类代码:\n");
    }
    printf("Exit.");
    return 0;
}

感觉和第七、八题一样,而且更没有难度。

11.ABC Mail Order Grocery 朝鲜蓟的售价是1.25 美元/磅,甜菜的售价是0.65 美元/磅,胡萝卜的售价是0.89 美元/磅。在添加运输费用之前,他们为100 美元的订单提供5%的打折优惠。对5磅或以下的定单收取3.50 美元的运输和装卸费用;超过5 磅而不足20 磅的定单收取10.00 美元的运输和装卸费用;20 磅或以上的运输,在8 美元基础上每磅加收0.1 美元。编写程序,在循环中使用switch 语句,以便对输入a 的响应是让用户输入所需的朝鲜蓟磅数,b 为甜菜的磅数,c 为胡萝卜的磅数,而q 允许用户退出订购过程。然后程序计算总费用、折扣和运输费用(如果有运输费的话),以及总数。随后程序应该显示所有的购买信息:每磅的费用、订购的磅数、该订单每种蔬菜的费用、订单的总费用、折扣,如果有的话加上运输费用,以及所有费用的总数。

#include<stdio.h>
#define ARTICHOKE 'a'
#define BEET 'b'
#define CARROT 'c'
#define ARTICHOKE_PRICE 1.25
#define BEET_PRICE 0.65
#define CARROT_PRICE 0.89
#define SHIP_FEE_LEVEL1 3.5
#define SHIP_FEE_LEVEL2 10.00
#define SHIP_FEE_LEVEL3 0.1
#define SHIP_FEE_LEVEL3_BASE 8.0
#define WEIGHT_LEVEL1 5
#define WEIGHT_LEVEL2 20
#define DISCOUNT 0.05
#define DISCOUNT_BASE 100
int main(void){
    double fee_count,ship_fee,price,order_count,discount_count,
    pound_count,pound,artichoke_pound,beet_pound,carrot_pound,artichoke_fee,beet_fee,carrot_fee;
    char type;
    pound_count = discount_count = fee_count = ship_fee = order_count = artichoke_pound = beet_pound = carrot_pound = 0.0;
    artichoke_fee = beet_fee = carrot_fee = 0.0;
    printf("请选择要订购的蔬菜:\n");
    printf("a)朝鲜蓟   b)甜菜  c)胡萝卜 q)退出\n");
    while(scanf("%c",&type) == 1 && type != 'q'){
        if( type != ARTICHOKE && type != BEET && type != CARROT ){
            printf("请选择输入a到c之间的字母,或者输入q退出:\n");
            continue;
        }
        printf("您输入你要订购的磅数:");
        scanf("%lf",&pound);
        pound_count += pound;
        switch(type){
        case ARTICHOKE:
            artichoke_pound = pound;
            artichoke_fee = pound*ARTICHOKE_PRICE;
            break;
        case BEET:
            beet_pound = pound;
            beet_fee = pound*BEET_PRICE;
            break;
        case CARROT:
            carrot_pound = pound;
            carrot_fee = pound*CARROT_PRICE;
            break;
        }

    }
    if (pound_count <= WEIGHT_LEVEL1){
        ship_fee = SHIP_FEE_LEVEL1;
    } else if(pound_count > WEIGHT_LEVEL1 && pound_count < WEIGHT_LEVEL2){
        ship_fee = SHIP_FEE_LEVEL2;
    } else {
        ship_fee = SHIP_FEE_LEVEL3_BASE+ (pound_count*0.1);
    }

    fee_count = ship_fee+artichoke_fee+beet_fee+carrot_fee;
    if (fee_count>DISCOUNT_BASE){
        discount_count = DISCOUNT_BASE*DISCOUNT;
    }
    order_count = fee_count-discount_count+ship_fee;

    printf("你共订购了%.2f磅蔬菜,平均每磅%.2f美元 \n",pound_count,order_count/pound_count);
    if (artichoke_fee>0){
        printf("订购了朝鲜蓟%.2f磅 共%.2f美元\n",artichoke_pound,artichoke_fee);
    }
    if (beet_fee>0){
        printf("订购了甜菜%.2f磅 共%.2f美元\n",beet_pound,beet_fee);
    }
    if (carrot_fee>0){
        printf("订购了胡萝卜%.2f磅 共%.2f美元\n",carrot_pound,carrot_fee);
    }
    printf("共%.2f美元 折扣优惠%.2f \n",fee_count,discount_count);
    if (ship_fee>0){
        printf("运费共%.2f \n",ship_fee);
    }

    printf("订单金额%.2f\n",order_count);
    return 0;
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:第三章--Win32程序的执行单元(部分概念及代码讲解)(中-线程同

下一篇:窝上课不听,how to learn C language easily(1)