C Primer Plus 第7章 C控制语句:分支和跳转 编程练习

来源:互联网 发布:wind mac版 excel插件 编辑:程序博客网 时间:2024/06/05 06:03

作业练习

1.#include <stdio.h>int main(void) {    char ch;    int spare, other, n; //空格,其他字符,换行    spare = other = n = 0;    while ((ch = getchar()) != '#') {        if (ch == ' ')            spare++;        else if (ch == '\n')            n++;        else            other++;    }    printf("spare:%d\nn:%d\nother:%d", spare, n, other);    return 0;}
2.#include <stdio.h>int main(void) {    char ch;    int count;    while ((ch = getchar()) != '#'){        printf("%3d ",ch);        count++;        if (count % 8 == 0)            printf("\n");    }    return 0;}
3.#include <stdio.h>int main(void) {    int value, count_o, count_e;    float odd, even;    odd = even = count_o = count_e = 0;    while ((scanf("%d", &value)) == 1 && value != 0) {        if (value % 2 == 0) {            odd = odd + value;            count_o++;        } else {            even = even + value;            count_e++;        }    }    if (count_o > 0)        printf("odd average:%.2f count:%2d\n", odd / count_o, count_o);    if (count_e > 0)    printf("even average:%.2f count:%2d\n", even / count_e, count_e);    return 0;}
4.#include <stdio.h>int main(void) {    char ch;    int count = 0;    while ((ch = getchar()) != '#'){        if(ch == '.'){            ch = '!';            count++;        } else if (ch == '!'){            printf("%c");            count++;        }        printf("%c",ch);    }    printf("Number:%d",count);    return 0;}
5.#include <stdio.h>int main(void) {    char ch;    int count = 0;    while ((ch = getchar()) != '#'){        switch (ch){            case '.':ch='!';count++;                break;            case '!':printf("%c",ch);count++;                break;        }        printf("%c",ch);    }    printf("Number:%d",count);//一共替换了多少次。    return 0;}
6.#include <stdio.h>int main(void) {    char ch,ch2,ch3;    //ch3为前一个字符,ch为当前字符,我真没想到。。    int count = 0;    while ((ch=getchar()) != '#'){        ch3=ch2;ch2=ch;        if (ch3 == 'e' && ch == 'i')            count++;    }    printf("%d",count);    return 0;}
7.#include <stdio.h>#define WORK_OVERTIME 40#define RATE1 0.15#define RATE2 0.20#define RATE3 0.25#define WAGE 10.0#define WAGE_OVERTIME 15.0#define BREAK1 300.0#define BREAK2 450.0#define BASE1 (BREAK1 * RATE1)#define BASE2 (BASE1 + ((BREAK2 - BREAK1) * RATE2))int main(void) {    int hr;    double wage,income,tax;    printf("Please enter the hr (q to quit) :");    while ((scanf("%d",&hr)) == 1){        if (hr <= WORK_OVERTIME)        wage = hr * WAGE;        else wage = WORK_OVERTIME * WAGE +((hr - WORK_OVERTIME) * WAGE_OVERTIME);        if (wage <= BREAK1){            tax = wage * RATE1;        }        else if (wage > 300 && wage <= 450 ){            tax = BASE1 + (wage - BREAK1) * RATE2;        }        else tax = BASE2 + (wage - BREAK2) * RATE3;        income = wage - tax;        printf("The wage: %.2f income:%.2lf tax:%.2lf\n", wage, income, tax);        printf("Please enter the hours (q to quit) :");    }    return 0;}
8.#include <stdio.h>#define WORK_OVERTIME 40#define RATE1 0.15#define RATE2 0.20#define RATE3 0.25#define BREAK1 300.0#define BREAK2 450.0#define BASE1 (BREAK1 * RATE1)#define BASE2 (BASE1 + ((BREAK2 - BREAK1) * RATE2))int main(void) {    int hr,number;    double wage,income,tax,wage2;    printf("------------------------------------------------------------------------\n");    printf("Enter the number number corresponding to the desired pay rate or action:\n");    printf("1) $8.15/hr                       2) $9.33/hr\n");    printf("3) $10.00/hr                      4) $11.20/hr\n");    printf("5) quit\n");    printf("------------------------------------------------------------------------\n");    printf("Please enter your choise:");    while ((scanf("%d", &number)) == 1){        switch (number){            case 1: wage2 = 8.75;                break;            case 2: wage2 = 9.33;                break;            case 3: wage2 = 10.00;                break;            case 4: wage2 = 11.20;                break;            default:                printf("Please enter the : 1 2 3 4 5 \n");                printf("Please enter your choise:");                continue; //continue使得程序跳过该循环的其余部分        }        if (number == 5) break;        printf("Please enter the hours:");        scanf("%d",&hr);        if (hr <= WORK_OVERTIME)        wage = hr * wage2;        else wage = WORK_OVERTIME * wage2 +((hr - WORK_OVERTIME) * wage2 * 1.5);        if (wage <= BREAK1){            tax = wage * RATE1;        }        else if (wage > 300 && wage <= 450 ){            tax = BASE1 + (wage - BREAK1) * RATE2;        }        else tax = BASE2 + (wage - BREAK2) * RATE3;        income = wage - tax;        printf("The wage: %.2f income:%.2lf tax:%.2lf\n", wage, income, tax);        printf("------------------------------------------------------------------------\n");        printf("Enter the number number corresponding to the desired pay rate or action:\n");        printf("1) $8.15/hr                       2) $9.33/hr\n");        printf("3) $10.00/hr                      4) $11.20/hr\n");        printf("5) quit\n");        printf("------------------------------------------------------------------------\n");        printf("Please enter your choise:");    }    return 0;}
9.#include <stdio.h>//写这题的时候我是懵逼的,只好看了下答案。2333//解决方式是能被div整除的就不是素数int main(void) {    int limit;    int num;    int div;    int numisprime; //当其为1的时候num为素数,如果num不是素数时,其变为0    printf("Please enter a positive integer:");    while ((scanf("%d", &limit)) == 1 && limit > 0) {        if (limit > 1)            printf("Here are the prime numbers up through %d\n", limit);        else            printf("No primes.\n");        for (num = 2; num <= limit; num++)        {            for (numisprime = 1, div = 2; (div * div) <= num; div++)                if (num % div == 0)                    numisprime = 0;            if (numisprime == 1)                printf("%d is prime.\n", num);        }        printf("Please enter a positive integer (q to quit) :");    }    return 0;}
10.#include <stdio.h>#define RATE1 0.15#define RATE2 0.28int main(void) {    int number;    double wage,tax,wage2; //wage工资,tax税收,wage2税金分界点    printf("------------------------------------------------------------------------\n");    printf("Enter the number number corresponding to the desired pay rate or action:\n");    printf("1) Single tax                     2) Household tax\n");    //1.单身 2.户主 3.共有 4.离异    printf("3) Married Total tax              4) Married divorced tax\n");    printf("5) quit\n");    printf("------------------------------------------------------------------------\n");    printf("Please enter your choise:");    while ((scanf("%d", &number)) == 1 && number != 5){        switch (number){            case 1: wage2 = 17850; //单身                break;            case 2: wage2 = 23900; //户主                break;            case 3: wage2 = 29750; //已婚,共有                break;            case 4: wage2 = 14875; //已婚,离异                break;            default:                printf("Please enter the : 1 2 3 4 5 \n");                printf("Please enter your choise:");                continue; //continue使得程序跳过该循环的其余部分        }        printf("Please enter the wage:");        scanf("%lf",&wage);        if (wage <= wage2)            tax = wage *RATE1;        else tax = wage2 * RATE1 + ((wage - wage2) * RATE2);        printf("Your tax is :%.2lf\n", tax);        printf("Please enter your choise:");    }    return 0;}
11.#include <stdio.h>#define GOODS1 2.05  //货物1#define GOODS2 1.15  //货物2#define GOODS3 1.09  //货物3#define BREAK1 5#define BREAK2 20#define BASE1 6.5#define BASE2 14#define DISCOUNT_MONEY 100#define DISCOUNT 0.05int main(void) {    int number;    double weight,cost,discount,cost2,goods;    double all;    double a,b,c,w_a,w_b,w_c;    printf("请选择需要购买的货物:\n");    printf("1)洋蓟:2.05/lb\n");    printf("2)甜菜:1.15/lb\n");    printf("3)胡萝卜:1.09/lb\n");    printf("q)退出选购\n");    printf("请输入你的选择:");    while (scanf("%d", &number) == 1 ){        switch (number){            case 1:printf("请输入需要洋蓟的重量:");                scanf("%lf", &a);                break;            case 2:printf("请输入需要甜菜的重量:");                scanf("%lf", &b);                break;            case 3:printf("请输入需要胡萝卜的重量:");                scanf("%lf", &c);                break;            default:printf("输入错误,请输入合适的选项(1,2,3,q):");                continue;        }        w_a = w_a + a;        w_b = w_b + b;        w_c = w_c + c;        a = b = c = 0;        printf("请输入你的选择:");    }    cost = w_a * GOODS1 + w_b * GOODS2 + w_c * GOODS3;    weight = w_a + w_b + w_c;    if (cost >= DISCOUNT_MONEY){        discount = cost * DISCOUNT;        cost = cost * (1 - DISCOUNT);    }    if (weight <= BREAK1)        cost2 = BASE1;    else if (weight > BREAK1 && weight <= BREAK2)        cost2 = BASE2;    else cost2 = BASE2 + ((weight - BREAK2) * 0.5);    printf("所需洋蓟的磅数:%.2lf\n", w_a);    printf("所需甜菜的磅数:%.2lf\n", w_b);    printf("所需胡萝卜的磅数:%.2lf\n", w_c);    printf("订购的总磅数:%.2lf\n", weight);    printf("所需洋蓟的费用:%.2lf\n", w_a * GOODS1);    printf("所需甜菜的费用:%.2lf\n", w_b * GOODS2);    printf("所需胡萝卜的费用:%.2lf\n", w_c * GOODS3);    printf("订单的运输和装卸费用:%.2f\n", cost2);    printf("订单的折扣:%.2f\n", discount);    printf("订单的总费用(包括运输费用,而且还要减去折扣):%.2f\n",cost + cost2);    return 0;}
0 0
原创粉丝点击