C primer plus第7章(C控制语句:分支和跳转)习题

1.

#include 
#include 
#include 
int main()
{
    int con1,con2,con3;
    con1 = con2 = con3 = 0;
    char c;
    while((c = getchar()) != '#') {
        if(c == '\n') {
            con1++;
        }else if(isblank(c)) {
            con2++;
        } else {
            con3++;
        }
    }
    printf("c1 = %d;c2 = %d;c3 = %d", con1,con2,con3);
    return 0;
}

2.

#include 
#include 
int main()
{
    int con1;
    con1 = 0;
    char c;
    while((c = getchar()) != '#')
    {
        printf("%c %d,",c,c);
        con1++;
        if(con1%8 == 0)
        {
            putchar('\n');
        }

    }
    return 0;
}

3.

#include 
int main (void)
{
    int inpu,evenCon,oddCon;
    double evenSum,oddSum;
    evenCon = oddCon = 0;
    evenSum = oddSum = 0.00;
    while(scanf("%d", &inpu) == 1 && inpu != 0)
    {
        if(inpu % 2 == 0)
        {
            evenCon++;
            evenSum += (double)inpu;
        }
        else
        {
            oddCon++;
            oddSum += (double)inpu;
        }
    }
    printf("偶数个数:%d\n偶数平均值%lf\n奇数个数%d\n奇数平均值%lf\n",evenCon,evenSum/evenCon,oddCon,oddSum/oddCon);
    return 0;
}


4

#include 
#include 
int main()
{
    char c;
    int c1,c2;
    c1 = c2 = 0;
    while((c = getchar()) != '#')
    {
        if(c == '.') {
            putchar('!');
            c1++;
        }
        else if(c == '!')
        {
            putchar('!!');
            c2++;
        }
        else
        {
            putchar('c');
        }
    }
    printf("\n%d,%d",c1,c2);
    return 0;
}


5.

#include 
int main (void)
{
    int inpu,evenCon,oddCon;
    double evenSum,oddSum;
    evenCon = oddCon = 0;
    evenSum = oddSum = 0.00;
    while(scanf("%d", &inpu) == 1 && inpu != 0)
    {
        switch(inpu % 2)
        {
        case 0:
            evenCon++;
            evenSum += (double)inpu;
            break;
        default:
            oddCon++;
            oddSum += (double)inpu;
            break;
        }
    }
    printf("偶数个数:%d\n偶数平均值%lf\n奇数个数%d\n奇数平均值%lf\n",evenCon,evenSum/evenCon,oddCon,oddSum/oddCon);
    return 0;
}

6.

#include 
int main (void)
{
    int con = 0;
    char perch,ch;
    while((ch = getchar()) != '#')
    {
        if(perch == 'e' && ch == 'i')
        {
            con++;
        }
        perch = ch;
    }
    printf("ei序列共出现%d次\n",con);
    return 0;
}


7.

#include 
#define SPH 10.00
#define SF40 10.00 * 40
#define SO40PH 10.00 * 1.5
#define TAXNE300 0.85
#define TAX300 300 * TAXNE300
#define TAX450 TAX300 + 150* TAXO300
#define TAXO300 0.8
#define TAXO450 0.75
int main (void)
{
    int hour = 0;
    double pay = 0.00;
    do
    {
        printf("请输入一周的工作时间1-168小时之间的整数:");
    }
    while((scanf("%d", &hour)) != 1 || hour < 1 || hour > 168);
    //不计税金应得
    if(hour < 40)
    {
        pay = hour * SPH;
    }
    else if(hour >= 40)
    {
        pay = SF40 + (hour - 40) * SO40PH;
    }
    printf("本周应得报酬税前%lf元\n",pay);
    //计算税金
    if(pay < 300)
    {
        pay = pay * TAXNE300;
    }
    else if(pay >= 300 && pay <450)
    {
        pay = TAX300 + (pay-300) * TAXO300;
    }
    else if(pay >= 450)
    {
        pay = TAX450 + (pay-450) * TAXO450;
    }
    printf("本周应得报酬税后%lf元\n",pay);
    return 0;
}

8.

#include 
#define L1 8.75
#define L2 9.33
#define L3 10.00
#define L4 11.20
#define TAXNE300 0.85
#define TAX300 300 * TAXNE300
#define TAX450 TAX300 + 150* TAXO300
#define TAXO300 0.8
#define TAXO450 0.75
int main (void)
{
    int hour = 0;
    int select;
    double pay = 0.00;
    double sph;
    do
    {
        printf("请选择工资级别\n");
        printf("1:8.75/h    2:9.33/h\n");
        printf("3:10.00/h   4:11.20/h\n");
        printf("5:quit\n");
    }
    while(scanf("%d",&select) != 1);
    switch(select)
    {
    case 1:
        sph = L1;
        break;
    case 2:
        sph = L2;
        break;
    case 3:
        sph = L3;
        break;
    case 4:
        sph = L4;
        break;
    case 5:
        return;
        break;
    }
    do
    {
        printf("请输入一周的工作时间1-168小时之间的整数:");
    }
    while((scanf("%d", &hour)) != 1 || hour < 1 || hour > 168);
    //不计税金应得
    if(hour < 40)
    {
        pay = hour * sph;
    }
    else if(hour >= 40)
    {
        pay = sph * 40 + (hour - 40) * sph * 1.5;
    }
    printf("本周应得报酬税前%lf元\n",pay);
    //计算税金
    if(pay < 300)
    {
        pay = pay * TAXNE300;
    }
    else if(pay >= 300 && pay <450)
    {
        pay = TAX300 + (pay-300) * TAXO300;
    }
    else if(pay >= 450)
    {
        pay = TAX450 + (pay-450) * TAXO450;
    }
    printf("本周应得报酬税后%lf元\n",pay);
    return 0;
}

9.

#include 
#include 
bool isPrime(int num);
int main (void)
{
    int num;
    do
    {
        printf("请输入一个整数:");
    }
    while(scanf("%d", &num) != 1);
    printf("小于此数的所有素数为:\n")
    for(; num>=1; num--)
    {
        if(isPrime(num))
        {
            printf("%d\n",num);
        }
    }
    return 0;
}

bool isPrime(int num)
{
    int i;
    for(i = num -1; i > 1; i--)
    {
        if(num % i == 0)
        {
            return false;
        }
    }
    return true;
}

10.

#include 
#define TAX 0.85
#define TAXOVE 0.72
//单身
#define SINMON 17850.00
#define SINTAX SINMON * TAX
//户主
#define HHMON 23900.00
#define HHTAX HHMON * TAX
//共有 inCommon
#define ICMON 29750.00
#define ICTAX ICMON * TAX
//离异 divorce
#define DIVMON 14875.00
#define DIVTAX DIVMON * TAX
double getPay(double pay, int type);
int main (void)
{
    int type;
    double pay;
    while(1)
    {
        do
        {
            printf("请正确选择类型:\n");
            printf("1:单身            2:户主\n");
            printf("3:已婚,共有       4:已婚,离异\n");
            printf("5:退出\n");
        }
        while(scanf("%d", &type) != 1);
        if(type == 5)
        {
            break;
        }
        do
        {
            printf("请正确输入税前收入:\n");
        }
        while(scanf("%lf", &pay) != 1);
        printf("税后收入为%lf\n",getPay(pay,type));
    }
    return 0;
}

double getPay(double pay, int type)
{
    double taxmon,basetax;
    switch(type)
    {
    case 1:
        taxmon = SINMON;
        basetax = SINTAX;
        break;
    case 2:
        taxmon = HHMON;
        basetax = HHTAX;
        break;
    case 3:
        taxmon = ICMON;
        basetax = ICTAX;
        break;
    case 4:
        taxmon = DIVMON;
        basetax = DIVTAX;
        break;
    }
    if(pay < taxmon)
    {
        pay = pay * TAX;
    }
    else if(pay >= taxmon)
    {
        pay = basetax + (pay - taxmon) * TAXOVE;
    }
    return pay;
}

11.

#include 
//朝鲜菜
#define S1 1.25
//甜菜
#define S2 0.65
//胡萝卜
#define S3 0.89
//sale
#define SALE 0.95
#define SL 100
//freight
#define L0M 3.50
#define L1P 5
#define L1M 10.00
#define L2P 20
#define L2M 8.00
#define L2O 0.1
double getPay(double pon, char type);
char get_first(void);
int main (void)
{
    char type;
    double pon;
    while(1)
    {
        do
        {
            printf("请正确选择菜的种类:\n");
            printf("a:朝鲜菜          b:甜菜\n");
            printf("c:胡萝卜          q:退出\n");
        }
        while((type = get_first()) != 'a' && type != 'b' && type != 'c' && type != 'd' && type != 'q');
        if(type == 'q')
        {
            break;
        }

        do
        {
            printf("请正确输入重量:\n");
        }
        while(scanf("%lf", &pon) != 1);
        printf("您最终需支付%lf\n",getPay(pon,type));
    }
    return 0;
}

char get_first(void)
{
    int ch;
    ch = getchar();
    while(getchar() != '\n')
    {
        continue;
    }
    return ch;
}
double getPay(double pon, char type)
{
    double pay,per;
    switch(type)
    {
    case 'a':
        per = S1;
        break;
    case 'b':
        per = S2;
        break;
    case 'c':
        per = S3;
        break;
    }
    if((pay = per * pon) >= SL)
    {
        pay *= SALE;
    }

    if(pon < L1P)
    {
        pay += L0M;
    }
    else if(pon >= L1P && pon < L2P)
    {
        pay += L1M;
    }
    else
    {
        pay += L2M + (pon - L2P) * L2O;
    }
    return pay;
}




你可能感兴趣的:(C和C++,CPrimerPlus)