C primer plus 第六版 第七章 第十一题 编程练习答案

Github 地址:这里这里φ(>ω<*)

/*  本程序应题目建立。  

    题目要求:  太麻烦 --!  略 ——! 祥见 C primer plus(第六版) 216页。 
   Github 地址:
*/

#define _CRT_SECURE_NO_WARNINGS  // 别诧异。我用的是VS2017社区版。貌似VS对于C标准不是很友好。

#include<stdio.h>


#define YJ 2.05 //  洋蓟  美元/磅
#define TC 1.15 //  甜菜  美元/磅 
#define HLB 1.09 // 胡萝卜 美元/磅


#define HUNDRED 100.0  // 100$ 的订单。  
#define YH      0.05 // 100$ 订单的优惠折扣。 


#define FE 6.5   // Five   重量在 5 磅以内的订单包装费。 
#define TY 14.0  // Twenty 重量在 5-20 磅以内的订单包装费。 
#define XZ 0.5   // 续重   重量过 20 磅的订单, 每续重一磅加 0.5 美元。 
#define A " y - 洋蓟的售价是   2.05$ 美元/磅。  \n"
#define B " t - 甜菜的售价是   1.15$ 美元/磅。  \n"
#define C " h - 胡萝卜的售价是 1.09$ 美元/磅。  \n"


  int main(void)
{
   int  i=0;    //循环用。
   int  j=0;    //计数 输入 choice 选项 次数。
  
   int  e_n;    // 清除输入缓冲用。
    
   char n=10;   //换行符 \n。 
   char e= '-'; //菜单分割线。 
   
   char y=0;    // 代表洋蓟。 
   char t=0;    // 代表甜菜。 
   char h=0;    // 代表胡萝卜。 
   char choice[55];   //保存输入选项。
   int  ce=0;     //Choice 选项 保存输入选项分支。  
   
   float w=0;  //保存用户输入重量。(即,物品的磅数。) 
   float w1=0; //累计洋蓟重量。 
   float w2=0; //累计甜菜重量。 
   float w3=0; //累计胡萝卜重量。 
    
   float pay=0;  //保存订单的总费用。 
   float pay1=0; //保存洋蓟的费用。
   float pay2=0; //保存甜菜的费用。 
   float pay3=0; //保存胡萝卜的费用。 
    
   float pc_money=0; //保存   总    包装费和运费 费用。( Packing and freight charges 包装费和运费 缩写为pc. )
   float pc_money1=0; //保存 洋蓟   包装费和运费 费用。( Packing and freight charges 包装费和运费 缩写为pc. ) 
   float pc_money2=0; //保存 甜菜   包装费和运费 费用。( Packing and freight charges 包装费和运费 缩写为pc. )
   float pc_money3=0; //保存 胡萝卜 包装费和运费 费用。( Packing and freight charges 包装费和运费 缩写为pc. )
   
   float total_money=0;  //保存所有费用。 
   
   void Menu(void);      //菜单部分。 
   void Calcation(void); //计算部分。 
   
   Menu:
   {
    //菜单部分。
   for( i=0; i<40; i++ )
   {
        putchar(e);
   }
        putchar(n);
        putchar(n);
    
    printf("Welcome to '百世汇' company .\n");
        printf("We have '洋蓟' 、 '甜菜' 、 '胡萝卜' (Not so much but only your need is that all !)\n");     
    printf("%s%s%s\n",A,B,C);
    printf("Input one letter to buy Or Enter 'q' to quit !\n");
    
        putchar(n);
    for( i=0; i<40; i++ )
    {
     putchar(e);
    }
            putchar(n);
            putchar(n); 
   }

      

      //正文1。 

        for( i=0,j=0; /*  空语句。主要不想用do while。 */ ; i++,j++)     
       {
              printf("Please input one letter for what do you want to buy :");  
              scanf("%c", &choice[i] );
              putchar(n); 
              
              while( ( e_n = getchar() ) != EOF && e_n != '\n')
  {
   /* 空语句。 */ ;
  } 
         
switch( choice[i] )
         {
     case 'y': // 洋蓟 选项分支。 
           printf("Please input weight for how many weight do you want(输入你想要购买的磅数) :"); //输入重量。 
               scanf("%f", &w);
               
               putchar(n);
               while( ( e_n = getchar() ) != EOF && e_n != '\n')
               {
                 /* 空语句。 */ ;
               }
               
               w1 += w; //计数输入重量。
   putchar(n); 
               
   printf("Do you want to buy else things ? or continue input weight ?\n");
               printf("Yes, input 1. No, input 2 to quit !\nPlease input:");  
               scanf("%d", &ce);
               
               putchar(n);
               while( ( e_n = getchar() ) != EOF && e_n != '\n')
               {
                 /* 空语句。 */ ;
               }
       putchar(n); 
               
   //分支选项,为了使交易系统更完美。本程序有选择计数购买别种物品并计算的功能。 
               if( ce == 1)
               {
continue;
               }
               else
               {
                goto Calculation; 
               }  break; 
   
                 case 't': // 甜菜 选项分支。 
           printf("Please input weight for how many weight do you want(输入你想要购买的磅数) :"); //输入重量。 
               scanf("%f", &w);
               
               putchar(n);
               while( ( e_n = getchar() ) != EOF && e_n != '\n')
               {
                 /* 空语句。 */ ;
               }
               
               w2 += w; //计数输入重量。 
               putchar(n);
               
   printf("Do you want to buy else things ? or continue input weight ?\n");
               printf("Yes, input 1. No, input 2 to quit !\nPlease input:");  
               scanf("%d", &ce);
               
               putchar(n);
               while( ( e_n = getchar() ) != EOF && e_n != '\n')
               {
                 /* 空语句。 */ ;
               }
               putchar(n);
               
   //分支选项,为了使交易系统更完美。本程序有选择计数购买别种物品并计算的功能。 
               if( ce == 1)
               {
continue;
               }
               else
               {
                goto Calculation; 
               }  break;   
  
                 case 'h': // 胡萝卜 选项分支。 
           printf("Please input weight for how many weight do you want(输入你想要购买的磅数) :"); //输入重量。 
               scanf("%f", &w);
               
               putchar(n);
               while( ( e_n = getchar() ) != EOF && e_n != '\n')
               {
                 /* 空语句。 */ ;
               }
               
               w3 += w; //计数输入重量。 
               putchar(n);
               
   printf("Do you want to buy else things ? or continue input weight ?\n");
               printf("Yes, input 1. No, input 2 to quit !\nPlease input:");  
               scanf("%d", &ce);
               
               putchar(n);
               while( ( e_n = getchar() ) != EOF && e_n != '\n')
               {
                 /* 空语句。 */ ;
               }
               putchar(n);
               
   //分支选项,为了使交易系统更完美。本程序有选择计数购买别种物品并计算的功能。 
               if( ce == 1)
               {
continue;
               }
               else
               {
                goto Calculation; 
               }  break; 
               
     case 'q': break;          
  
                 default: printf("Worring ! This program failed to run ! It close now !!!\n\n");
          break; 
            }
                      break; //这个 break 跳出 for 循环。 
   }


    //正文2。* 
    Calculation:
{
      //计算 总 价格 部分。
          for( i=0; i<40; i++ )
          {
           putchar(e);
          }
            putchar(n);
            putchar(n);
            
  printf("%s%s%s\n",A,B,C); //输出蔬菜的价格。
  
  for( i=0; i<40; i++) 
  {
   putchar(e);
  }
            putchar(n);


   //计算 价格 部分。   
  pay1 = YJ * w1;//洋蓟的费用。 
  pay2 = TC * w2;//甜菜的费用。 
      pay3 = HLB * w3;//胡萝卜的费用。          
  
          
   //输出 购买蔬菜对应价格 部分。      
      printf("This list shows your shopping list(显示购买蔬菜信息):\n");
   
   //输出洋蓟的总购买信息。(如果有买的话。) 
    for( i=0; i<=j; i++)
   {
         if( choice[i] == 'y')
         {
          printf("You are buy '洋蓟' for %f weight. And it spend %f$ .\n", w1,pay1);
          break;
         }
     else
     {
      continue;
     }
        break;
   }
   //输出甜菜的总购买信息。(如果有买的话。) 
    for( i=0; i<=j; i++)
   {
         if( choice[i] == 't')
         {
          printf("You are buy '甜菜' for %f weight. And it spend %f$ .\n", w2,pay2);
          break;
         }
     else
     {
      continue;
     }
        break;
   }
   //输出胡萝卜的总购买信息。(如果有买的话。)
    for( i=0; i<=j; i++)
   {
         if( choice[i] == 'h')
         {
          printf("You are buy '胡萝卜' for %f weight. And it spend %f$ .\n",w3,pay3);
          break;
         }
     else
     {
      continue;
     }
        break;
   }  
  
   
   //计算 总费用与减去折扣(如果有的话)部分。
    
pay = pay1 + pay2 + pay3;

if( pay< HUNDRED )
{
pay -= pay * YH;
printf("This shopping list have Discount.(有折扣) Minus the discount, This shopping spend your money %f$.\n", pay);

else
{
printf("This shopping list have no Discount.(无折扣) And this shopping spend your money %f$.\n", pay);
}


   //计算 总 包装费和运费 费用。
        
//洋蓟的 包装费和运费 费用。 
if( w1 == 0 )
{
/* 空语句。不计算。不然我也不知道写什么了。*/;
}
else if( w1 <= 5 )
{
pc_money1 += FE;
}
else if( w1 > 5 && w1 <= 20 )
{
pc_money1 += TY;
}
else
{
pc_money1 =(int) ( w1 - 20 ) * XZ + TY;


    //甜菜的 包装费和运费 费用。 
    if( w2 == 0 )
    {
     /* 空语句。不计算。不然我也不知道写什么了。*/;
    }
    else if( w2 <= 5 )
{
pc_money2 += FE;
}
else if( w2 > 5 && w2 <= 20 )
{
pc_money2 += TY;
}
else
{
pc_money2 =(int) ( w2 - 20 ) * XZ + TY;


    // 胡萝卜的 包装费和运费 费用。 
        if( w3 == 0 )
{
/* 空语句。不计算。不然我也不知道写什么了。*/;
}
    else if( w3 <= 5 )
{
pc_money3 += FE;
}
else if( w3 > 5 && w3 <= 20 )
{
pc_money3 += TY;
}
else
{
pc_money3 =(int) ( w1 - 20 ) * XZ + TY;


    //总 包装费和运费 费用。 
    pc_money = pc_money1 + pc_money2 + pc_money3;
printf("Total packing and freight charges(包装费和运费)spend money %f$.\n", pc_money ); 


   //计算 总费用。  
    total_money = pc_money /* 包装费和运费 */ + pay; /* 减去 折扣( 如果有的话 )后的蔬菜总费用*/ 
    printf("This shopping(总价) spend money %f$.\n", total_money );

}

    printf("Welcome to come here again. Wish you have a good day !\n");
printf("Bye !\n"); 
 
getchar(); 
getchar(); 
        return 0;
}

猜你喜欢

转载自blog.csdn.net/Lth_1571138383/article/details/80482489