C primer Plus作业第七章

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/************************************/

/* practice 1   第七章作业         */

/***********************************/

void p7_1(void)

{

    char ch;

    int n_space = 0;

    int n_break = 0;

    int n_number = 0;

    printf("输入一段字符串:");

    while((ch=getchar()) != '#')

    {

        if(ch == '\n')

        {

            n_break++;

        }

        else if(ch == ' ')

        {

            n_space++;

        }

        else

        {

            n_number++;

        }

    }

    printf("一共有空格:%d个\n一共有换行符:%d个\n共有字符:%d个",n_space,n_break,n_number);

}

/************************************/

/* practice 2   第七章作业         */

/***********************************/

void p7_2(void)

{

     char ch;

     printf("输入一段字符串:");

     int count=0;

     while((ch=getchar()) != '#')

     {

         count++;

         printf("%c:%d\t",ch,ch);

         if(count%8==0)

         {

             putchar('\n');     //printf("\n")相同

         }

     }

}

/************************************/

/* practice 3   第七章作业         */

/***********************************/

void p7_3(void)

{

    int input;

    int even=0;

    double sum_even=0;

    int odd=0;

    double sum_odd=0;

    printf("请输入整数:");

    while(scanf("%d",&input)==1 && input !=0)

    {

        if(input %2 ==0)

        {

            even++;

            sum_even += input;

        }

        else

        {

            odd++;

            sum_odd += input;

        }

        printf("请输入整数:");

    }

    printf("输入偶数的个数是:%d,平均值是:%.2lf \n输入奇数的个数是:%d,平均值是:%.2lf",even,sum_even/even,odd,sum_odd/odd);

}

/************************************/

/* practice 4   第七章作业         */

/***********************************/

void p7_4(void)

{

    char ch;

    int count1=0,count2=0;

    while((ch=getchar()) != '#')

    {

        if(ch=='.')

        {

            count1++;

            putchar('!');

        }

        else if(ch == '!')

        {

            count2++;

            putchar('!');

            putchar('!');

        }

        else

        {

            putchar(ch);

        }

    }

    printf("用感叹号替换句号替换了%d次\n",count1);

    printf("用两个感叹号替换原来感叹号替换了%d次",count2);

}

/************************************/

/* practice 5   第七章作业         */

/***********************************/

void p7_5(void)

{

    char ch;

    int count1=0,count2=0;

    while((ch=getchar()) != '#')

    {

        switch(ch)

        {

        case '.':

            putchar('!');

            count1++;

            break;

        case '!':

            putchar('!');

            putchar('!');

            count2++;

            break;

        default:

            putchar(ch);

        }

    }

    printf("用感叹号替换句号替换了%d次\n",count1);

    printf("用两个感叹号替换原来感叹号替换了%d次",count2);

}

/************************************/

/* practice 6   第七章作业         */

/***********************************/

void p7_6(void)

{

    char ch,ch_pre;

    int count=0;

    while((ch=getchar())!='#')

    {

        if(ch == 'i')           //新的一轮输入字符,若ch=='i'

        {

            if(ch_pre == 'e')   //而且上一轮输入ch_pre=='e'的话

            {

                count++;        //计数+1

            }

        }

        ch_pre =ch;             //把当前输入字符作为上一次输入字符

    }

    printf("ei出现的次数是%d次",count);

}

/************************************/

/* practice 7   第七章作业         */

/***********************************/

#define SALARY1 10.0

#define RATE1 0.15

#define RATE2 0.2

#define RATE3 0.25

void p7_7(void)

{

    int hour;

    double total;       //工资总额

    double tax;         //税金

    double salary;      //净收入

    printf("a.基本工作 = 10.00美元/小时\n");

    printf("b.加班(超过40小时) = 1.5倍时间\n");

    printf("c.税率:\t前300美元为15%%\n\t续150美元为20%%\n\t余下的为25%%\n");

    printf("请输入你一周工作的小时数:");

    scanf("%d",&hour);

    if(hour>40)

    {

        total = (hour*1.5) * SALARY1;

    }

    else

    {

        total = hour * SALARY1;

    }

    if(total<300)

    {

        tax = total * RATE1;

    }

    else if(total>300 && total<=450)

    {

        tax = 300*RATE1 + (total-300)*RATE2;

    }

    else

    {

        tax = 300*RATE1 + 150*RATE2 + (total - 450)*RATE3;

    }

    salary = total - tax;

    printf("你这周的的工资总额:%.2lf,税金:%.2lf,净收入:%.2lf",total,tax,salary);

}

/************************************/

/* practice 8   第七章作业         */

/***********************************/

void p7_8(void)

{

    int hour;

    double total;       //工资总额

    double tax;         //税金

    double income;      //净收入

    int choice;         //输入选择项

    double salary;

    printf("*****************************************************************\n");

    printf("Enter the number corresponding to the desired pay rate or action:\n");

    printf("1)%-10s\t2)%-10s\n3)%-10s\t4)%-10s\n5)%-10s\n","$8.75/hr","$9.33/hr","$10.00/hr","$11.20/hr","quit");

    printf("*****************************************************************\n");

    printf("Enter the number corresponding to the desired pay rate or action:\n");

    while((scanf("%d",&choice)==1) &&choice !=5)

    {

        switch(choice)

        {

        case 1:

            salary = 8.75;

            break;

        case 2:

            salary = 9.33;

            break;

        case 3:

            salary = 10.00;

            break;

        case 4:

            salary = 11.20;

            break;

        default:

            printf("请输入1~5以内的数字!\n");

            break;

        }

        printf("请输入你一周工作的小时数:");

        scanf("%d",&hour);

        if(hour>40)

        {

            total = (hour*1.5) * salary;

        }

        else

        {

            total = hour * salary;

        }

        if(total<300)

        {

            tax = total * RATE1;

        }

        else if(total>300 && total<=450)

        {

            tax = 300*RATE1 + (total-300)*RATE2;

        }

        else

        {

            tax = 300*RATE1 + 150*RATE2 + (total - 450)*RATE3;

        }

        income = total - tax;

        printf("你这周的的工资总额:%.2lf,税金:%.2lf,净收入:%.2lf\n",total,tax,income);

        printf("Enter the number corresponding to the desired pay rate or action:\n");

    }

    printf("Done");

}

/************************************/

/* practice 10   第七章作业         */

/***********************************/

void p7_10(void)

{

    int choice = 0;

    double income = 0;

    double threshold = 0;

    double tax = 0;

    while (1)

    {

        printf("请选择你的类型:\n");

        printf("1) 单身\n2) 户主\n3) 已婚,共有\n4) 已婚,离异\n请输入你的选择:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            threshold = 17850.0;

            break;

        case 2:

            threshold = 23900.0;

            break;

        case 3:

            threshold = 29750.0;

            break;

        case 4:

            threshold = 14875.0;

            break;

        default:

            printf("输入有误,请输入1-4的数字!");

            continue;

        }

        printf("请输入你的工资:");

        scanf("%lf", &income);

        if (income < threshold)

        {

            tax = income * 0.15;

        }

        else

        {

            tax = threshold * 0.15 + (income - threshold) * 0.28;

        }

        printf("类别:%d, 净收入:%.2lf, 税金:%.2lf\n", choice, income, tax);

    }

}

/************************************/

/* practice 11   第七章作业         */

/***********************************/

#define PRICE1 2.05         //洋蓟的价钱

#define PRICE2 1.15         //甜菜的价钱

#define PRICE3 1.09         //胡萝卜的价钱

void p7_11(void)

{

   double p_artichoke=0;      //洋蓟的磅数

   double p_beet=0;           //甜菜的磅数

   double p_carrot=0;         //胡萝卜的磅数

   double sum_artichoke=0;  //洋蓟的总数

   double sum_beet=0;       //甜菜的总数

   double sum_carrot=0;     //胡萝卜的总数

   char choice=0;

   double p_total=0;        //订购的重量(单位磅)

   double totalCost=0;      //总费用

   double discount=1;       //折扣价

   double packingCost=0;    //运费及包装费

   double cost=0;

   printf("《订货商店》-共有以下三种食材:\n");

   printf("a)%-6sb)%-6sc)%-6s\n","洋蓟","甜菜","胡萝卜");

   while(choice!='q')

   {

       printf("请输入你要订购的的食材(按q退出):");

       fflush(stdin);

       choice=getchar();

       fflush(stdin);

       switch(choice)

       {

       case 'a':

           printf("请输入洋蓟的磅数:");

           scanf("%lf",&p_artichoke);

           sum_artichoke += p_artichoke;

           break;

       case 'b':

           printf("请输入甜菜的磅数:");

           scanf("%lf",&p_beet);

           sum_beet += p_beet;

           break;

       case 'c':

           printf("请输入胡萝卜的磅数:");

           scanf("%lf",&p_carrot);

           sum_carrot += p_carrot;

           break;

       case 'q':

           continue;

       default:

           printf("输入有误,请重新输入!\n");

           break;

       }

   }

   p_total = p_artichoke + p_beet + p_carrot;       //订购的重量(单位磅)

   if(p_total<5)

   {

        packingCost = 6.5;

   }

   else if(p_total>5 && p_total<=20)

   {

       packingCost = 14;

   }

   else

   {

       packingCost = 14+(p_total-20)*0.5;

   }

   totalCost = sum_artichoke*PRICE1 + sum_beet*PRICE2 + sum_carrot*PRICE3;

   if(totalCost>100)

   {

       discount = 0.95;             //在添加运费之前100美元订单有5%的打折优惠

   }

   if(totalCost==0)

   {

       cost=0;

       packingCost=0;

       discount=0;

   }

   else

   {

       cost = totalCost*discount+packingCost;

   }

   printf("%-10s%-10s%-24s%-12s\n","商品名称","物品售价","订购的重量(单位:磅)","订购蔬菜费用");

   printf("%-10s$%-10.2lf%-24.2lf$%-12.2lf\n","洋蓟",PRICE1,p_artichoke,p_artichoke*PRICE1);

   printf("%-10s$%-10.2lf%-24.2lf$%-12.2lf\n","甜菜",PRICE2,p_beet,p_beet*PRICE2);

   printf("%-10s$%-10.2lf%-24.2lf$%-12.2lf\n","胡萝卜",PRICE3,p_carrot,p_carrot*PRICE3);

   printf("%-12s%-6s%-14s%-12s\n","订单总费用","折扣","运费和包装费","费用总额");

   printf("$%-12.2lf%-6.2lf$%-14.2lf$%-12lf",totalCost,discount,packingCost,cost);

}

int main()

{

    p7_11();

    return 0;

}

猜你喜欢

转载自www.cnblogs.com/xuxubot/p/9812915.html