主頁 > 後端開發 > C Primer Plus (7.12) 編程練習

C Primer Plus (7.12) 編程練習

2023-02-03 06:24:45 後端開發

/*C Primer Plus (7.11) 3*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     double weight,height;
 5     printf("Please enter your weight and height.\n");
 6     printf("Weight (pound):");
 7     scanf("%lf",&weight);
 8     printf("Height (inch):");
 9     scanf("%lf",&height);
10 //加入建立比較友好的人機互動
11     if (weight < 100 && height > 64)
12         if (height >= 72)
13         printf("You are very tall for your weight.\n");
14     else
15         printf("You are tall for your weight.\n");
16     else if (weight > 300 && height < 48)
17         printf("You are quite short for your weight.\n");
18     else
19         printf("Your weight is ideal.\n");
20 //減少沒有用的if判斷條件
21     return 0;
22 }
23 /*
24 輸出樣例
25 
26 Please enter your weight and height.
27 Weight (pound):99
28 Height (inch):65
29 You are tall for your weight.
30 
31 Please enter your weight and height.
32 Weight (pound):98
33 Height (inch):72
34 You are very tall for your weight.
35 
36 Please enter your weight and height.
37 Weight (pound):301
38 Height (inch):46
39 You are quite short for your weight.
40 
41 Please enter your weight and height.
42 Weight (pound):200
43 Height (inch):50
44 Your weight is ideal.
45 
46 */

/*C Primer Plus (7.11) 10*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char ch;
 5 
 6     while ((ch=getchar()) != '#')
 7     {
 8         if (ch != '\n')
 9         {
10             printf("Step 1\n");
11             if (ch == 'b')
12                 break;
13             else if (ch !='c')
14             {
15                 if (ch != 'h')
16                     printf("Step 2\n");
17                     printf("Step 3\n");
18             }
19         }
20     }
21     printf("Done.\n");
22     return 0;
23 }
24 /*
25 輸出樣例
26 
27 q
28 Step 1
29 Step 2
30 Step 3
31 c
32 Step 1
33 h
34 Step 1
35 Step 3
36 b
37 Step 1
38 Done.
39 
40 */

/*C Primer Plus (7.12) 1*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int space, linebreak, others;
 5     int realothers = 0;
 6     char ch;
 7     space = linebreak = others = 0;
 8 
 9     printf("Please enter some characters (# to quit).\n");
10     while ((ch = getchar()) != '#')
11     {
12         if (ch == ' ' ? space++ : others++ && ch == '\n' ? linebreak++ : others++);
13     }
14     realothers = others / 2;
15     printf("These are the number of characters required for statistics.\n");
16     printf("Space : %d" ,space);
17     printf("\nLinebreak : %d" ,linebreak);
18     printf("\nOthers: %d" ,realothers);
19 
20     return 0;
21 }
22 /*
23 輸出樣例
24 
25 Please enter some characters (# to quit).
26 Hello,My name is Coco.
27 Hello. My name is Mike !#
28 These are the number of characters required for statistics.
29 Space : 8
30 Linebreak : 1
31 Others: 38
32 
33 */

/*C Primer Plus (7.12) 2*/

 1 #include<stdio.h>
 2 int main(void)
 3 {
 4     int i = 0;
 5     char ch;
 6 
 7     printf("Please enter some characters (# to quit):");
 8     while ((ch = getchar()) != '#')
 9     {
10         if (i++ % 8 == 0)
11         {
12             putchar('\n');                     //每輸出8個字符的資訊就進行一次換行操作
13         }
14         if (ch == '\n')
15         {
16             printf("\'\\n\' -> %2d ",ch);
17         }
18         else if (ch == '\t')
19         {
20             printf("\'\\t\' -> %2d ",ch);
21         }
22         else
23         {
24             printf("\'%c\' -> %2d ",ch,ch);
25         }
26     }
27     printf("\nDone.");
28 
29     return 0;
30 }
31 /*
32 輸出樣例
33 
34 Please enter some characters (# to quit):KurokiTomoko#
35 
36 'K' -> 75 'u' -> 117 'r' -> 114 'o' -> 111 'k' -> 107 'i' -> 105 'T' -> 84 'o' -> 111
37 'm' -> 109 'o' -> 111 'k' -> 107 'o' -> 111
38 Done.
39 
40 */

/*C Primer Plus (7.12) 3*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int num;
 5     int even,odd;                    //偶數的個數,奇數的個數
 6     int e_sum,o_sum;
 7     double e_value,o_value;          //偶數和的平均值,奇數和的平均值
 8     even = odd = num = e_sum = o_sum = 0;
 9     e_value = https://www.cnblogs.com/NoldorFromMiddleEarth/p/o_value =0.0;
10     printf("Please enter some integer numbers.\n");
11     printf("The result your entered (0 to quit) : ");
12     while (scanf("%d",&num) == 1 && num)
13     {
14         (num % 2 == 0 ? (even++, e_sum += num) : (odd++, o_sum += num));
15         printf("Now you can enter again (0 to quit) : ");
16     }
17     printf("There are %d even numbers.\n",even);
18     if (even > 0)
19     {
20         e_value = https://www.cnblogs.com/NoldorFromMiddleEarth/p/e_sum / (double)even;
21         printf("The average of even numbers is : %.3lf\n",e_value);
22     }
23     printf("There are %d odd numbers.\n",odd);
24     if (odd > 0)
25     {
26         o_value = https://www.cnblogs.com/NoldorFromMiddleEarth/p/o_sum / (double)odd;
27         printf("The average of odd numbers is : %.3lf",o_value);
28     }
29     printf("\nDone.");
30     return 0;
31 }
32 /*
33 輸出樣例
34 
35 Please enter some integer numbers.
36 The result your entered (0 to quit) : 1
37 Now you can enter again (0 to quit) : 2
38 Now you can enter again (0 to quit) : 3
39 Now you can enter again (0 to quit) : 4
40 Now you can enter again (0 to quit) : 5
41 Now you can enter again (0 to quit) : 6
42 Now you can enter again (0 to quit) : 7
43 Now you can enter again (0 to quit) : 8
44 Now you can enter again (0 to quit) : 9
45 Now you can enter again (0 to quit) : 0
46 There are 4 even numbers.
47 The average of even numbers is : 5.000
48 There are 5 odd numbers.
49 The average of odd numbers is : 5.000
50 Done.
51 
52 */

/*C Primer Plus (7.12) 4*/

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char ch;
 5     int count1 = 0;
 6     int count2 = 0;
 7     printf("Please enter the text you want (enter '#' to quit).");
 8     printf("\nNow please enter : ");
 9     while ((ch = getchar()) != '#')
10     {
11         if (ch == '.')
12         {
13             putchar('!');
14             count1++;
15         }
16         else if (ch == '!')
17         {
18             printf("!!");
19             count2++;
20         }
21         else
22         {
23             putchar(ch);
24         }
25     }
26     printf("The number of times an exclamation mark "
27            "has been replaced with a period is : %d",count1);
28     printf("\nThe number of times an exclamation mark "
29            "is replaced by two exclamations is : %d",count2);
30     printf("\nDone.");
31 
32     return 0;
33 }
34 /*
35 輸出樣例
36 
37 Please enter the text you want (enter '#' to quit).
38 Now please enter : !!!!!.....
39 !!!!!!!!!!!!!!!
40 #
41 The number of times an exclamation mark has been replaced with a period is : 5
42 The number of times an exclamation mark is replaced by two exclamations is : 5
43 Done.
44 
45 */

/*C Primer Plus (7.12) 5*/

#include<stdio.h>
int main()
{
    char ch;
    int count1 = 0;
    int count2 = 0;
    printf("Please enter the text you want (enter '#' to quit).");
    printf("\nNow please enter : ");
    while ((ch = getchar()) != '#')
    {
        switch(ch)
        {
        case '.':
            {
                putchar('!');
                count1++;
                break;
            }
        case '!':
            {
                printf("!!");
                count2++;
                break;
            }
        default:
            {
                putchar(ch);
            }
        }
    }
    printf("The number of times an exclamation mark "
           "has been replaced with a period is : %d",count1);
    printf("\nThe number of times an exclamation mark "
           "is replaced by two exclamations is : %d",count2);
    printf("\nDone.");

    return 0;
}
/*
輸出樣例

Please enter the text you want (enter '#' to quit).
Now please enter : Hello, This is Coconut !
Hello, This is Coconut !!
My name is Coconut.
My name is Coconut!
#
The number of times an exclamation mark has been replaced with a period is : 1
The number of times an exclamation mark is replaced by two exclamations is : 1
Done.

*/

/*C Primer Plus (7.12) 6*/

#include<stdio.h>
int main()
{
    int count = 0;
    char ch;
    char prev;            //讀取的前一個字符
    printf("Please enter some characters ('#' to quit):");
    prev = '#';          //前一個字符為“#”的時候會停止(用於識別結束符號)
    while ((ch = getchar()) != '#')
    {
        if(prev == 'e' && ch == 'i')
            count++;
        prev = ch;
    }
    printf("There %d ei in this sentence.",count);

    return 0;
}
/*
輸出樣例

Please enter some characters ('#' to quit):Receive your eieio award.#
There 3 ei in this sentence.

*/

/*C Primer Plus (7.12) 7*/

#include<stdio.h>
#define BASIC_SALARY 10.00
#define EXTRA_WORK 1.5
#define NORMAL_TAX 0.15
#define EXTRA_TAX 0.20
#define OTHER_TAX 0.25
int main()
{
    double worktime = 0.0;
    double salary,tax,netincome;
    salary = tax = netincome = 0.0;
    printf("Please enter your "
           "working hours in a week : ");
    while (scanf("%lf",&worktime) != 1 || worktime <= 0)
    {
        while (getchar() != '\n') continue;
        printf("Please enter a right number( >= 0 ).");
    }
    salary = worktime > 40 ? (40.00 * BASIC_SALARY) + (1.5 * (worktime - 40)) * BASIC_SALARY : worktime * BASIC_SALARY;
    if (salary <= 300)
    {
        tax = 300.00 * NORMAL_TAX;
        netincome = salary - tax;
    }
    else if (salary <= 450)
    {
        tax = 300.00 * NORMAL_TAX + (salary - 300.00) * EXTRA_TAX;
        netincome = salary - tax;
    }
    else
    {
        tax = 300.00 * NORMAL_TAX + 150.00 * EXTRA_TAX + (salary - 450.00) * OTHER_TAX;
        netincome = salary - tax;
    }
    printf("There is your salary, tax and net income information.\n");
    printf("Salary : %.3lf",salary);
    printf("\nTax : %.3lf",tax);
    printf("\nNet income : %.3lf",netincome);

    return 0;
}
/*
輸出樣例

Please enter your working hours in a week : 300
There is your salary, tax and net income information.
Salary : 4300.000
Tax : 1037.500
Net income : 3262.500

Please enter your working hours in a week : 450
There is your salary, tax and net income information.
Salary : 6550.000
Tax : 1600.000
Net income : 4950.000

Please enter your working hours in a week : 521.73
There is your salary, tax and net income information.
Salary : 7625.950
Tax : 1868.988
Net income : 5756.963
*/

/*C Primer Plus (7.12) 8*/

#include<stdio.h>
#include<stdbool.h>
#define EXTRA_WORK 1.5
#define NORMAL_TAX 0.15
#define EXTRA_TAX 0.20
#define OTHER_TAX 0.25
void quit ();
void menu ();
void Salary (double Bsalary , double worktime);

int choice = 0;
double worktime = 0.0;

int main()
{
    while (true)
    {
         menu ();

         switch(choice)
     {
        case 1 :
           {
               Salary(8.75,worktime);
               break;
           }
        case 2 :
           {
               Salary(9.33,worktime);
               break;
           }
        case 3 :
           {
               Salary(10.00,worktime);
               break;
           }
        case 4 :
           {
               Salary(11.20,worktime);
               break;
           }
        case 5 :
           {
               quit();
               printf("Done.");
               return 0;
           }
     }
    }
}

void quit()
{
    printf("\t\t\n************************************************\t\t\n");
    printf("||                                            ||");
    printf("\n||                                            ||");
    printf("\n||      Thank you to use this programme!      ||");
    printf("\n||                                            ||");
    printf("\n||                                            ||");
    printf("\t\t\n************************************************\t\t\n");
}

void menu()
{
    printf("\t\t\n*****************************************************************\t\t\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr                          2) $9.33/hr");
    printf("\n3) $10.00/hr                         4) $11.20/hr\n");
    printf("5) quit");
    printf("\t\t\n*****************************************************************\t\t\n");
    printf("Please enter your options: ");
    scanf("%d",&choice);
    while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
    {
        printf("Please enter the right choice:");
        scanf("%d",&choice);
    }
}

void Salary(double Bsalary , double worktime)
{
  double tax,netincome,salary;
  salary = tax = netincome = 0.0;
  printf("Please enter your working hours in a week : ");

    while (scanf("%lf",&worktime) != 1 || worktime <= 0)
    {
        while (getchar() != '\n') continue;
        printf("Please enter a right number( >= 0 ).");
    }
    salary = worktime > 40 ? (40.00 * Bsalary) + (1.5 * (worktime - 40)) * Bsalary : worktime * Bsalary;
    if (salary <= 300)
    {
        tax = 300.00 * NORMAL_TAX;
        netincome = salary - tax;
    }
    else if (salary <= 450)
    {
        tax = 300.00 * NORMAL_TAX + (salary - 300.00) * EXTRA_TAX;
        netincome = salary - tax;
    }
    else
    {
        tax = 300.00 * NORMAL_TAX + 150.00 * EXTRA_TAX + (salary - 450.00) * OTHER_TAX;
        netincome = salary - tax;
    }
    printf("There is your salary, tax and net income information.\n");
    printf("Salary : %.3lf",salary);
    printf("\nTax : %.3lf",tax);
    printf("\nNet income : %.3lf",netincome);
  }
/*
輸出樣例

*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr                          2) $9.33/hr
3) $10.00/hr                         4) $11.20/hr
5) quit
*****************************************************************
Please enter your options: 2
Please enter your working hours in a week : 600
There is your salary, tax and net income information.
Salary : 8210.400
Tax : 2015.100
Net income : 6195.300
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr                          2) $9.33/hr
3) $10.00/hr                         4) $11.20/hr
5) quit
*****************************************************************
Please enter your options: 1
Please enter your working hours in a week : 325.44
There is your salary, tax and net income information.
Salary : 4096.400
Tax : 986.600
Net income : 3109.800
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr                          2) $9.33/hr
3) $10.00/hr                         4) $11.20/hr
5) quit
*****************************************************************
Please enter your options: 5

************************************************
||                                            ||
||                                            ||
||      Thank you to use this programme!      ||
||                                            ||
||                                            ||
************************************************
Done.

*/

/*C Primer Plus (7.12) 9*/

 1 #include<stdio.h>
 2 int Prime (int x)
 3 {
 4     int index = 0;
 5     for (index = 2; index < x; index++)
 6     {
 7         if (x % index == 0)
 8             return 0;
 9     }
10     return 1;
11 }
12 
13 int main()
14 {
15     int num;
16     printf("Please enter a number (<= 0 to quit) : ");
17     while (scanf("%d",&num) ==1 && num > 0)
18     {
19         if (num == 1)
20         {
21             printf("This number is not a prime.");
22         }
23         else
24         {
25             printf("These are prime numbers less than %d : ",num);
26             for (int index = 2; index < num; index++)
27                 if (Prime(index))
28                 {
29                     printf("%-5d",index);
30                 }
31         }
32         printf("\nNow you can enter again : ");
33     }
34     printf("That's all.");
35 
36     return 0;
37 }
38 /*
39 輸出樣例
40 
41 Please enter a number (<= 0 to quit) : 15
42 These are prime numbers less than 15 : 2    3    5    7    11   13
43 Now you can enter again : 40
44 These are prime numbers less than 40 : 2    3    5    7    11   13   17   19   23   29   31   37
45 Now you can enter again : 0
46 That's all.
47 
48 */

/*C Primer Plus (7.12) 10*/

  1  #include<stdio.h>
  2  #include<stdbool.h>
  3  #define Choice1 17850
  4  #define Choice2 23900
  5  #define Choice3 29750
  6  #define Choice4 14875
  7  #define TAX1 0.15
  8  #define TAX2 0.28
  9  void MENU();
 10  void QUIT();
 11  double TAX_PLAN1 (double money, double Ctax);
 12  double TAX_PLAN2 (double money1, double Ctax1);
 13  double TAX_PLAN3 (double money2, double Ctax2);
 14  double TAX_PLAN4 (double money3, double Ctax3);
 15  int choice = 0;
 16  double income = 0.0;
 17  double tax = 0.0;
 18  double tax1 = 0.0;
 19  double tax2 = 0.0;
 20  double tax3 = 0.0;
 21 
 22  int main()
 23  {
 24      while (true)
 25      {
 26          MENU();
 27 
 28          switch (choice)
 29          {
 30          case 1 :
 31             {
 32                 TAX_PLAN1 (income, tax);
 33                 break;
 34             }
 35          case 2 :
 36             {
 37                 TAX_PLAN2 (income, tax1);
 38                 break;
 39             }
 40          case 3 :
 41             {
 42                 TAX_PLAN3 (income, tax2);
 43                 break;
 44             }
 45          case 4 :
 46             {
 47                 TAX_PLAN4 (income, tax3);
 48                 break;
 49             }
 50          case 5 :
 51             {
 52                 QUIT();
 53                 return 0;
 54             }
 55          }
 56      }
 57 
 58  }
 59  void MENU()
 60  {
 61      printf("************************************************************");
 62      printf("\nPlease enter your choice.\n");
 63      printf("(1) Single                     (2) Head of household\n");
 64      printf("(3) Married, jointly owned     (4) Married, divorced\n");
 65      printf("(5) Quit\n");
 66      printf("************************************************************\n");
 67      printf("Please enter your choice : ");
 68      scanf("%d",&choice);
 69      while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
 70     {
 71         printf("Please enter the right choice:");
 72         scanf("%d",&choice);
 73     }
 74  }
 75 
 76  void QUIT()
 77  {
 78     printf("\t\t\n************************************************\t\t\n");
 79     printf("||                                            ||");
 80     printf("\n||                                            ||");
 81     printf("\n||      Thank you to use this programme!      ||");
 82     printf("\n||                                            ||");
 83     printf("\n||                                            ||");
 84     printf("\t\t\n************************************************\t\t\n");
 85  }
 86 
 87  double TAX_PLAN1 (double money, double Ctax)
 88  {
 89      double income, tax;
 90      income = tax = 0;
 91      printf("Please enter your income (<= 0 to quit) : ");
 92      while (scanf("%lf",&income) == 1 && income > 0)
 93      {
 94          if (income <= Choice1)
 95          {
 96              tax = income * TAX1;
 97              printf("This is your tax : %g$\n",tax); break;
 98          }
 99          else
100          {
101              tax = (Choice1 * TAX1)+(income - Choice1) * TAX2;
102              printf("This is your tax : %g$\n",tax); break;
103          }
104       }
105   }
106 
107   double TAX_PLAN2 (double money1, double Ctax1)
108   {
109      double income, tax1;
110      income = tax1 = 0;
111      printf("Please enter your income (<= 0 to quit) : ");
112      while (scanf("%lf",&income) == 1 && income > 0)
113      {
114          if (income <= Choice2)
115          {
116              tax1 = income * TAX1;
117              printf("This is your tax : %g$\n",tax1); break;
118          }
119          else
120          {
121              tax1 = (Choice2 * TAX1) + (income - Choice2) * TAX2;
122              printf("This is your tax : %g$\n",tax1); break;
123          }
124      }
125   }
126 
127   double TAX_PLAN3 (double money2, double Ctax2)
128   {
129      double income, tax2;
130      income = tax2 = 0;
131      printf("Please enter your income (<= 0 to quit) : ");
132      while (scanf("%lf",&income) == 1 && income > 0)
133      {
134          if (income <= Choice3)
135          {
136              tax2 = income * TAX1;
137              printf("This is your tax : %g$\n",tax2); break;
138          }
139          else
140          {
141              tax2 = (Choice3 * TAX1)+(income - Choice3) * TAX2;
142              printf("This is your tax : %g$\n",tax2); break;
143          }
144      }
145   }
146 
147   double TAX_PLAN4 (double money3, double Ctax3)
148   {
149      double income, tax3;
150      income = tax3 = 0;
151      printf("Please enter your income (<= 0 to quit) : ");
152       while (scanf("%lf",&income) == 1 && income > 0)
153       {
154          if (income <= Choice4)
155          {
156              tax3 = income * TAX1;
157              printf("This is your tax : %g$\n",tax3); break;
158          }
159          else
160          {
161              tax3 = (Choice4 * TAX1)+(income - Choice4) * TAX2;
162              printf("This is your tax : %g$\n",tax3); break;
163          }
164       }
165   }
166 /*
167 輸出樣例
168 
169 ************************************************************
170 Please enter your choice.
171 (1) Single                     (2) Head of household
172 (3) Married, jointly owned     (4) Married, divorced
173 (5) Quit
174 ************************************************************
175 Please enter your choice : 1
176 Please enter your income (<= 0 to quit) : 20000
177 This is your tax : 3279.5$
178 ************************************************************
179 Please enter your choice.
180 (1) Single                     (2) Head of household
181 (3) Married, jointly owned     (4) Married, divorced
182 (5) Quit
183 ************************************************************
184 Please enter your choice : 6
185 Please enter the right choice:2
186 Please enter your income (<= 0 to quit) : 35415
187 This is your tax : 6809.2$
188 ************************************************************
189 Please enter your choice.
190 (1) Single                     (2) Head of household
191 (3) Married, jointly owned     (4) Married, divorced
192 (5) Quit
193 ************************************************************
194 Please enter your choice : 5
195 
196 ************************************************
197 ||                                            ||
198 ||                                            ||
199 ||      Thank you to use this programme!      ||
200 ||                                            ||
201 ||                                            ||
202 ************************************************
203 
204 */

/*C Primer Plus (7.12) 11*/

  1 #include<stdio.h>
  2 #include<ctype.h>
  3 #define Pr_artichoke 2.05           //洋薊的價格(美元/磅)
  4 #define Pr_beet 1.15                //甜菜的價格(美元/磅)
  5 #define Pr_carrot 1.09              //胡蘿蔔的價格(美元/磅)
  6 #define Discount 0.05               //折扣
  7 #define Under5 6.5                  //低於或等於5磅的運輸和包裝費
  8 #define Under20 14.00               //5-20磅的運輸和包裝費
  9 #define Basic20 14.00               //20磅的運輸和包裝費
 10 #define Extratax 0.50               //超過20磅每重一磅就增加0.5$
 11 #define Bweight 20.00               //20磅
 12 
 13 void MENU();
 14 
 15 char choice;
 16 
 17 int main()
 18 {
 19     double P_artichoke = 0.0;       //洋薊的磅數
 20     double P_beet = 0.0;            //甜菜的磅數
 21     double P_carrot = 0.0;          //胡蘿蔔的磅數
 22     double weight = 0.0;            //重量統計
 23     double T_weight = 0.0;          //總重量
 24     double M_artichoke = 0.0;       //洋薊的總價格
 25     double M_beet = 0.0;            //甜菜的總價格
 26     double M_carrot = 0.0;          //胡蘿蔔的總價格
 27     double P_total = 0.0;           //三種蔬菜花費的錢
 28     double R_total = 0.0;           //算過折扣和包裝費之後的錢
 29     double P_discount = 0.0;        //折扣
 30     double P_trans_and_package = 0.0;   //運輸和包裝費的錢
 31 
 32         MENU();
 33 
 34         while ((choice = tolower(getchar())) != 'q')          //ctype函數(tolower 大寫字符回傳小寫字符)
 35         {
 36             if (isspace(choice))                              //檢測空白符
 37             {
 38                 continue;
 39             }
 40             while (getchar() != '\n')
 41             {
 42                 continue;
 43             }
 44         switch (choice)
 45         {
 46         case 'a' :
 47             {
 48                 printf("Enter pounds of artichokes : ");
 49                 scanf("%lf",&weight);
 50                 P_artichoke += weight;
 51                 break;
 52             }
 53         case 'b' :
 54             {
 55                 printf("Enter pounds of beets : ");
 56                 scanf("%lf",&weight);
 57                 P_beet += weight;
 58                 break;
 59             }
 60         case 'c' :
 61             {
 62                 printf("Enter pounds of carrots : ");
 63                 scanf("%lf",&weight);
 64                 P_carrot += weight;
 65                 break;
 66             }
 67         default :
 68             {
 69                 printf("%c is not a valid choice.\n",choice);
 70                 printf("Please enter a positive choice : \n");
 71             }
 72         }
 73         MENU();
 74         }
 75 
 76     M_artichoke = Pr_artichoke * P_artichoke;           //洋薊的購買的總價錢
 77     M_beet = Pr_beet * P_beet;                          //甜菜購買的總價錢
 78     M_carrot = Pr_carrot * P_carrot;                    //胡蘿蔔購買的總價錢
 79     P_total = M_artichoke + M_beet + M_carrot;          //三種蔬菜在一起的總價錢
 80     T_weight = P_artichoke + P_beet + P_carrot;         //三種蔬菜在一起的總重量
 81 
 82     if (P_total >= 100.0)                               //打折的費用
 83     {
 84         P_discount = P_total * Discount;
 85     }
 86     else
 87     {
 88         P_discount = 0.0;
 89     }
 90 
 91     if (T_weight <= 0.0)
 92     {
 93         P_trans_and_package = 0.0;
 94     }
 95     else if (T_weight < 5.0)                                  //計算包裝和運輸費的費用
 96     {
 97         P_trans_and_package = Under5;
 98     }
 99     else if (T_weight < 20.0)
100     {
101         P_trans_and_package = Under20;
102     }
103     else
104     {
105         P_trans_and_package = Basic20 + Extratax * (T_weight - Bweight);
106     }
107     R_total = P_total + P_trans_and_package - P_discount;
108 
109     printf("\n\n**********************************************************************\n");
110     //蔬菜的報價(美元/磅)
111     printf("The price of artichokes is : $%.2lf per pound.\n",Pr_artichoke);
112     printf("The price of beets is : $%.2lf per pound.\n",Pr_beet);
113     printf("The price of carrots is : $%.2lf per pound.\n",Pr_carrot);
114     //購買蔬菜分開的價格以及一共的價格和購買的磅數
115     printf("You bought %.2lf pounds artichokes. "
116            "And the price is : %.2lf$\n",P_artichoke,M_artichoke);
117     printf("You bought %.2lf pounds beets. "
118            "And the price is : %.2lf$\n",P_beet,M_beet);
119     printf("You bought %.2lf pounds carrots. "
120            "And the price is : %.2lf$\n",P_carrot,M_carrot);
121     printf("The total weight is : %.2lf pound.\n",T_weight);
122     printf("The total cost of vegetables : %.2lf$\n",P_total);
123     printf("Discount : %.2lf$\n",P_discount);
124     printf("The price of transport and package : %.2lf$\n",P_trans_and_package);
125     printf("The last money spent is : %.2lf$\n",R_total);
126     printf("**********************************************************************\n");
127 
128     return 0;
129 }
130 
131 void MENU()
132 {
133     printf("**********************************************************************\n");
134     printf("Enter letters to complete the response.\n");
135     printf("(a) Buy artichokes                           (b) Buy beets\n");
136     printf("(c) Buy carrots\n");
137     printf("    Enter q to quit.\n");
138     printf("**********************************************************************\n");
139     printf("Please enter your choice : ");
140 }
141 /*
142 輸出樣例
143 
144 **********************************************************************
145 Enter letters to complete the response.
146 (a) Buy artichokes                           (b) Buy beets
147 (c) Buy carrots
148     Enter q to quit.
149 **********************************************************************
150 Please enter your choice : a
151 Enter pounds of artichokes : 96.5
152 **********************************************************************
153 Enter letters to complete the response.
154 (a) Buy artichokes                           (b) Buy beets
155 (c) Buy carrots
156     Enter q to quit.
157 **********************************************************************
158 Please enter your choice : v
159 v is not a valid choice.
160 Please enter a positive choice :
161 **********************************************************************
162 Enter letters to complete the response.
163 (a) Buy artichokes                           (b) Buy beets
164 (c) Buy carrots
165     Enter q to quit.
166 **********************************************************************
167 Please enter your choice : b
168 Enter pounds of beets : 66.7
169 **********************************************************************
170 Enter letters to complete the response.
171 (a) Buy artichokes                           (b) Buy beets
172 (c) Buy carrots
173     Enter q to quit.
174 **********************************************************************
175 Please enter your choice : c
176 Enter pounds of carrots : 123.64
177 **********************************************************************
178 Enter letters to complete the response.
179 (a) Buy artichokes                           (b) Buy beets
180 (c) Buy carrots
181     Enter q to quit.
182 **********************************************************************
183 Please enter your choice : q
184 
185 
186 **********************************************************************
187 The price of artichokes is : $2.05 per pound.
188 The price of beets is : $1.15 per pound.
189 The price of carrots is : $1.09 per pound.
190 You bought 96.50 pounds artichokes. And the price is : 197.82$
191 You bought 66.70 pounds beets. And the price is : 76.70$
192 You bought 123.64 pounds carrots. And the price is : 134.77$
193 The total weight is : 286.84 pound.
194 The total cost of vegetables : 409.30$
195 Discount : 20.46$
196 The price of transport and package : 147.42$
197 The last money spent is : 536.25$
198 **********************************************************************
199 
200 */

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542835.html

標籤:C

上一篇:單調堆疊

下一篇:讀Java8函式式編程筆記08_測驗、除錯和重構

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more