《c primer plus》编程练习回顾-第七章

来源:互联网 发布:网络电视nba直播软件 编辑:程序博客网 时间:2024/04/30 05:32

7.12 编程练习
1.

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

2.

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

3.

#include<stdio.h>int main(void){    int i_even = 0, sum_even = 0, i_odd = 0, sum_odd = 0, num;    printf("Please input numbers (0 to quit):");   /*用恒成立的循环持续获取输入*/    while (1)     {        scanf("%d", &num);        if (num == 0) break;        if (num % 2 == 0) { i_even++; sum_even += num; }        else { i_odd++; sum_odd += num; }    }    printf("even number's count: %d\n", i_even);    printf("even number's sum : %d\n", sum_even);    printf("odd number's count: %d\n", i_odd);    printf("odd number's sum : %d\n", sum_odd);    return 0;}

4.

#include<stdio.h>int main(void){    int a = 0, b = 0;    char ch;    printf("Please input a string end by #:");    while ((ch = getchar()) != '#')    {        if (ch == '.')        {            putchar('!');            a++;        }        else if (ch == '!')         {             putchar('!');             putchar('!');            b++;        }        else putchar(ch);    }    printf("\nthe times of '.' replaced by '!': %d\n", a);    printf("the times of '!' replaced by '!!': %d\n", b);    return 0;}

5.

#include<stdio.h>int main(void){    int num, odds = 0, even = 0;    while (scanf("%d", &num) == 1 && num != 0)    {        switch (num % 2)        {        case 0:odds++; break;        case 1:even++; break;        }    }    printf("odds=%d,even=%d", odds, even);    return 0;}

6.

#include<stdio.h>int main(void){    int num = 0;    char ch1, ch2 = 'a';    while (scanf("%c", &ch1) == 1 && ch1 != '#')    {        if (ch1 == 'i'&&ch2 == 'e')            num++;        else            ch2 = ch1;    }    printf("%d\n", num);    return 0;}

7.

#include<stdio.h>#define BASIC 10.00#define TIME 40 #define ADD 1.5#define LIMIT1 300 #define RATE1 0.15#define LIMIT2 150 #define RATE2 0.20#define RATE3 0.25 int main(void){    double hours, gross, tax;    printf("input the work hours of a week:");    scanf("%lf", &hours);    if (hours > 40) hours = 40 + (hours - 40) * 1.5;    gross = hours * BASIC;    printf("gross income:\t\t%lf\n", gross);    if (gross <= LIMIT1) tax = gross * RATE1;    else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;    else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;    printf("tax:\t\t\t%lf\n", tax);    printf("net income:\t\t%lf\n", gross - tax);    return 0;}

8.

#include<stdio.h>int get_int(void);#define TIME 40#define ADD 1.5#define LIMIT1 300#define RATE1 0.15#define LIMIT2 150#define RATE2 0.20#define RATE3 0.25int main(void){    double basic, hours, gross, tax;    printf("Enter the number corresponding to the desired pay rate or action:\n");    printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");    printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");    printf("5) quit\n");    switch (get_int())    {    case 1: basic = 8.75; break;    case 2: basic = 9.33; break;    case 3: basic = 10.00; break;    case 4: basic = 11.20; break;    default: printf("quit\n"); return 0;    }        printf("you have select $%.2lf\n", basic);        printf("input the work hours of a week:");        scanf("%lf", &hours);        if (hours > 40) hours = 40 + (hours - 40) * 1.5;        gross = hours * basic;        printf("gross income:\t\t%lf\n", gross);        if (gross <= LIMIT1) tax = gross * RATE1;        else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;        else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;        printf("tax:\t\t\t%lf\n", tax);        printf("net income:\t\t%lf\n", gross - tax);        return 0;    }    int get_int(void)    {        int num;        char str[40];        while (scanf("%d", &num) != 1)        {            gets(str);            printf("error! %s is not a number. input again.\n", str);        }        while (getchar() != '\n');        return num;    }

9.

#include<stdio.h>int isprime(int);int main(void){    int num, i;    printf("input a positive number:");    scanf("%d", &num);    printf("all the primes <= %d:\n", num);    for (i = 2; i <= num; i++)    if (isprime(i))        printf("%d\t", i);    printf("\n");    return 0;}int isprime(int n){    int div;    for (div = 2; div * div <= n; div++)    if (n % div == 0)        return 0;    return 1;}

10.

#include<stdio.h>#define SINGLE 17850#define HOST 23900#define MARRIED_SHARE 29750#define MARRIED_DIVORCE 14875#define RATE1 0.15#define RATE2 0.28int main(void){    double type, pay, tax;    char ch;    while (1)    {        printf("Select the type of marriage:\n");        printf("1)SINGLE\n2)HOST\n3)MARRIED_SHARE\n4)MARRIED_DIVORCE\n5)quit\n");        while ((ch = getchar()) == '\n') continue;        switch (ch)        {        case '1': type = SINGLE; break;        case '2': type = HOST; break;        case '3': type = MARRIED_SHARE; break;        case '4': type = MARRIED_DIVORCE; break;        case '5': printf("quit\n"); return 0;        default: printf("input error\n"); continue;        }        printf("you have select %c\n", ch);        printf("input the pay:");        scanf("%lf", &pay);        if (pay <= type) tax = pay * RATE1;        else tax = type * RATE1 + (pay - type) * RATE2;        printf("wax is %.2lf\n", tax);    }    return 0;}

11.

#include <stdio.h>#include <ctype.h>#define ARTICHOKE 1.25#define BEET 0.65#define CARROT 0.89#define DISCOUNT_LIMIT 100#define DISCOUNT_RATE 0.05#define FREIGHT_FEE1 3.50#define FREIGHT_LIMIT1 5#define FREIGHT_FEE2 10.00#define FREIGHT_LIMIT2 20#define FREIGHT_FEE3 8#define FREIGHT_RATE 0.1int main(void){    char ch;    double artichoke = 0, beet = 0, carrot = 0;    double sum, discount, freight;    printf("Please select your vegetable: a,b,c,q\n");    printf("a.artichoke price:$%.2f\n", ARTICHOKE);    printf("b.beet price:$%.2f\n", BEET);    printf("c.carrot price:$%.2f\n", CARROT);    printf("q.end\n");    printf("(price as dollars per pound)\n");    while ((ch = tolower(getchar())) != 'q')    {        switch (ch)        {        case 'a': printf("How many pounds of artichokes do you want?");            scanf("%lf", &artichoke);            printf("Please select your vegetable: a,b,c,q:");            continue;        case 'b': printf("How many pounds of beets do you want?");            scanf("%lf", &beet);            printf("Please select your vegetable: a,b,c,q:");            continue;        case 'c': printf("How many pounds of carrots do you want?");            scanf("%lf", &carrot);            printf("Please select your vegetable: a,b,c,q:");            continue;        default: break;        }    }    printf("%10s%10s%10s%10s\n", " ", "artichoke", "beet", "carrot");    printf("%10s%10.2lf%10.2lf%10.2lf\n", "price", ARTICHOKE, BEET, CARROT);    printf("%10s%10.2lf%10.2lf%10.2lf\n", "pound", artichoke, beet, carrot);    printf("%10s%10.2lf%10.2lf%10.2lf\n", "charge", ARTICHOKE * artichoke, BEET * beet, CARROT        * carrot);    sum = ARTICHOKE * artichoke + BEET * beet + CARROT * carrot;    if (sum > DISCOUNT_LIMIT) discount = sum*DISCOUNT_RATE;    else discount = 0;    printf("discount: %.2f\n", discount);    if (artichoke + beet + carrot <= 5) freight = 3.50;    else if (artichoke + beet + carrot <20) freight = 10;    else freight = 8 + (artichoke + beet + carrot) * 0.1;    printf("freight: %.2f\n", freight);    sum = sum - discount + freight;    printf("sum: %.2f\n", sum);    return 0;}
0 0
原创粉丝点击