C Primer Plus 编程练习(第7章)

1

#include<stdio.h>
#include<ctype.h>

int main(void)
{
    char ch;
    int s_num = 0;
    int e_num = 0;
    int o_num = 0;

    while ((ch = getchar()) != '#')
    {
        if(isspace(ch))
            s_num++;
        else if (ch =='\n')
            e_num++;
        else
            o_num++;
    }
    printf("空格数为%d, 换行数为%d, 所有其他字符的数为%d.\n",
           s_num, e_num, o_num);

    return 0;
}

2

#include<stdio.h>
#include<ctype.h>

int main(void)
{
    char ch;
    int num = 0;

    while ((ch = getchar()) != '#')
    {
        printf("%c-%d ", ch, ch);
        num++;
        if (num % 8 == 0)
            printf("\n");
    }

    return 0;
}

3

#include<stdio.h>

int main(void)
{
    int a;
    int all_num = 0;
    int o_num = 0;
    int j_num = 0;
    double o_avg = 0.0;
    double j_avg = 0.0;
    while (scanf("%d", &a) == 1 && a != 0)
    {
        all_num++;
        if (all_num % 2 == 0)
         {
            o_avg += (double)a;
            o_num++;
         }
        else
        {
            j_avg += (double)a;
            j_num++;
        }
    }
    o_avg /= o_num;
    j_avg /= j_num;
    printf("输入的偶数的个数为%d,", o_num);
    if (o_num > 0)
        printf(" 这些偶数的平均值为%.1lf.\n", o_avg);
    printf("输入的奇数的个数为%d,", j_num);
    if (j_num > 0)
        printf("这些奇数的平均值为%.1lf.\n", j_avg)

    return 0;
}

4

#include<stdio.h>
#include<ctype.h>

int main(void)
{
    char ch;
    int i = 0;
    int j = 0;

    printf("Please enter something to test this program (# to quit): ");
    while ((ch = getchar()) != '#')
    {
        if ('.' == ch)
        {
            putchar('!');
            i++;
        }
        else if ('!' == ch)
        {
            putchar('!');
            putchar('!');
            j++;
        }
        else
            putchar(ch);
    }
    printf("\n");
    printf("%d %s.\n", i + j, i + j == 1? "time" : "times");
    return 0;
}

5

#include<stdio.h>
#include<ctype.h>

int main(void)
{
    char ch;
    int i = 0;
    int j = 0;

    printf("Please enter something to test this program (# to quit): ");
    while ((ch = getchar()) != '#')
    {
        switch(ch)
        {
            case '.': putchar('!');
                      i++;
                      break;
            case '!': putchar('!');
                      putchar('!');
                      j++;
                      break;
            default: putchar(ch);
        }
    }
    printf("\n");
    printf("%d %s.\n", i + j, i + j == 1 ? "time" : "times");

    return 0;
}

6

#include<stdio.h>
#include<ctype.h>

int main(void)
{
    char ch;
    char prev;
    int i = 0;

    printf("Please enter something to test this program (# to quit): ");
    while ((ch = getchar()) != '#')
    {
        if ('e' == ch)
            prev = ch;
        if ('i' == ch && 'e' == prev)
            i++;
    }
    printf("%d %s.\n", i, i == 1 ? "time" : "times");

    return 0;
}

7

#include<stdio.h>
#define money_per_hour 10.00
#define add_time 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define ADD_TIME 40

int main(void)
{
    int time;
    double all_money, speed_money, make_money;

    printf("Please enter your work time: ");
    scanf("%d", &time);
    if (time <= ADD_TIME)
    {
        all_money = time * money_per_hour;
        if (all_money <= BREAK1)
            speed_money = RATE1 * all_money;
        else if (all_money <= BREAK2)
            speed_money = BREAK1 * RATE1 + (all_money - BREAK1) * RATE2;
        make_money = all_money - speed_money;
    }
    else
    {
        all_money = (ADD_TIME + (time - ADD_TIME) * add_time) * money_per_hour;
        if (all_money <= BREAK2)
            speed_money = BREAK1 * RATE1 + (all_money - BREAK1) * RATE2;
        else
            speed_money = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2 + RATE3 * (all_money - BREAK2);
        make_money = all_money - speed_money;
    }
    printf("总工资为%.2lf, 税金为%.2lf, 净收入为%.2lf.\n",all_money, speed_money, make_money);

    return 0;
}

8

#include<stdio.h>
#define add_time 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define ADD_TIME 40
#define CLASS1 8.75
#define CLASS2 9.33
#define CLASS3 10.00
#define CLASS4 11.20

int main(void)
{
    int num, time;
    double all_money, speed_money, make_money, money_per_hour;

    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");
    scanf("%d", &num);
    while (num != 5)
    {
        if (num >= 1 && num <= 4)
        {
            if (num == 1)
                money_per_hour = CLASS1;
            if (num == 2)
                money_per_hour = CLASS2;
            if (num == 3)
                money_per_hour = CLASS3;
            if (num == 4)
                money_per_hour = CLASS4;
            printf("Please enter your work time: ");
            scanf("%d", &time);
            if (time <= ADD_TIME)
            {
                all_money = time * money_per_hour;
                if (all_money <= BREAK1)
                    speed_money = RATE1 * all_money;
                else if (all_money <= BREAK2)
                    speed_money = BREAK1 * RATE1 + (all_money - BREAK1) * RATE2;
                make_money = all_money - speed_money;
            }
            else
            {
                all_money = (ADD_TIME + (time - ADD_TIME) * add_time) * money_per_hour;
                if (all_money <= BREAK2)
                    speed_money = BREAK1 * RATE1 + (all_money - BREAK1) * RATE2;
                else
                    speed_money = BREAK1 * RATE1 + (BREAK2 - BREAK1) * RATE2 + RATE3 * (all_money - BREAK2);
                make_money = all_money - speed_money;
            }
        printf("总工资为%.2lf, 税金为%.2lf, 净收入为%.2lf.\n",all_money, speed_money, make_money);
        }
        else if (num < 1 && num > 5)
        {
            printf("Please enter the correct item.\n");
        }
        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");
        scanf("%d", &num);
    }
    return 0;
}

9

#include<stdio.h>
#include<stdbool.h>

int main(void)
{
    unsigned long num , i, j;
    bool isPrime;

    printf("Enter int number:\n");
    scanf("%lu", &num);
    for (i = 2; i <= num; i++)
    {
        for (j = 2, isPrime = true; (j * j) <= i; j++)
        {
            if (i % j == 0)
            {
                isPrime = false;
            }
        }
        if (isPrime)
        {
            printf("%lu | ", i);
        }
    }
    return 0;
}

10

#include<stdio.h>
#define RATE1 0.15
#define RATE2 0.28
#define SINGLE 17850
#define HOUSE 23900
#define MARRY_ALL 29750
#define MARRY_ONLY 14875

int main(void)
{
    int num;
    double all_money, speed_money, label;

    printf("***********************************\n");
    printf("1) 单身               2)户主\n");
    printf("3) 已婚,共有         4)已婚,离异\n");
    printf("5) quit\n");
    printf("***********************************\n");
    printf("Please enter a number for your choice: ");
    scanf("%d", &num);
    while (num != 5)
    {
        if (num >= 1 && num <= 4)
        {
            if (num == 1)
                label = SINGLE;
            if (num == 2)
                label = HOUSE;
            if (num == 3)
                label = MARRY_ALL;
            if (num == 4)
                label = MARRY_ONLY;
            printf("Please enter your salary: ");
            scanf("%lf", &all_money);
            if (all_money <= label)
                speed_money = RATE1 * all_money;
            else
                speed_money = RATE1 * label + (all_money - label) * RATE2;
            printf("您应该缴纳的税金为%.2lf\n", speed_money);
        }
        else
            printf("You must enter the correct number!\n");
        printf("***********************************\n");
        printf("1) 单身               2)户主\n");
        printf("3) 已婚,共有         4)已婚,离异\n");
        printf("5) quit\n");
        printf("***********************************\n");
        printf("Please enter a number for your choice: ");
        scanf("%d", &num);
    }
    printf("Done!\n");

    return 0;
}

11

#include<stdio.h>
#include<ctype.h>
#define YANGJI 2.05
#define TIANCAI 1.15
#define HULUOBU 1.09
#define BREAK1 5
#define BREAK2 20
#define RATE1 6.5
#define RATE2 14
#define MONEY_100 100
#define MT_20 0.15
#define COUNT 0.05

int main(void)
{
    char ch;
    int i, j, k, num;
    double veg_cost,  all_cost, count, freight;
    printf("***********************************************\n");
    printf("a) 洋蓟 2.05美元/磅      b) 甜菜 1.15美元/磅\n");
    printf("c) 胡萝卜 1.09美元/磅    q) 退出订购\n");
    printf("***********************************************\n");
    printf("请输入对应字母进行购买: ");
    while((ch = getchar()) != 'q')
    {
        switch(ch)
        {
            case 'a': printf("请输入需要购买的洋蓟数量: ");
                      scanf("%d", &i);
                      break;
            case 'b': printf("请输入需要购买的甜菜数量: ");
                      scanf("%d", &j);
                      break;
            case 'c': printf("请输入需要购买的胡萝卜数量: ");
                      scanf("%d", &k);
        }
        printf("请继续点购: ");
    }
    num = i + j + k;
    veg_cost = YANGJI * i + TIANCAI * j + HULUOBU * k;
    if (num <= BREAK1 )
    {
        freight = RATE1;
        all_cost = veg_cost + freight;
    }
    else if (num <= BREAK2)
    {
        freight = RATE2;
        all_cost = veg_cost + freight;
    }
    else
    {
        freight = RATE2 + (num - BREAK2) * MT_20;
        if (veg_cost >= MONEY_100)
            count = ((int)veg_cost / MONEY_100) * COUNT * MONEY_100;
        else
            count = 0.0;
        all_cost = freight + veg_cost - count;
    }
    printf("订购的蔬菜重量为%d, 订单的蔬菜费用为%.2lf.\n", num, veg_cost);
    printf("订单的总费用为%.2lf, 折扣为%.2lf, 运费和包装费为%.2lf.\n",
           all_cost, count, freight);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值