Holey_yan

20140304_KUAS_VisualC++_Exam02

Mar 3rd, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. /************************************/
  2. /** 20140304 Exam 02               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/03/04 pm.01:30   **/
  6. /************************************/
  7.  
  8. //一樣先引入標頭檔案
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. //方式一: 使用if .. else if ..
  13. void method_1()
  14. {
  15.     /*先行定義變數
  16.       number_1 = 第一運算數
  17.       number_2 = 第二運算數
  18.       select = 選擇運算子  */
  19.     int number_1 = 0, number_2 = 0, select = 0;
  20.  
  21.     //輸入第一運算數
  22.     printf("Input the first number: ");
  23.     scanf("%d", &number_1);
  24.  
  25.     //輸入第二運算數
  26.     printf("Input the second number: ");
  27.     scanf("%d", &number_2);
  28.  
  29.     //輸入選擇運算子, 其規定為[1, 2, 3, 4] is [+, -, *, /]
  30.     printf("// Select your action  ");
  31.     printf("[1, 2, 3, 4] is [+, -, *, /] //");
  32.     printf("\nYour choose is: ");
  33.     scanf("%d", &select);
  34.  
  35.     /*開始進行if 條件運算式, 其使用格式為
  36.     --------------------------------------
  37.       if([條件式])
  38.       {
  39.            [敘述式]
  40.       }
  41.       else if([條件式])
  42.       {
  43.            [敘述式]
  44.       }
  45.       else
  46.       {
  47.            [敘述式]
  48.       }
  49.     --------------------------------------*/
  50.     if(select == 1)
  51.     {
  52.         printf("Answer(+) is: %d\n", number_1 + number_2);
  53.         return;
  54.     }
  55.     else if(select == 2)
  56.     {
  57.         printf("Answer(-) is: %d\n", number_1 - number_2);
  58.         return;
  59.     }
  60.     else if(select == 3)
  61.     {
  62.         printf("Answer(*) is: %d\n", number_1 * number_2);
  63.         return;
  64.     }
  65.     else if(select == 4)
  66.     {
  67.         printf("Answer(/) is: %d .. %d\n", number_1 / number_2, number_1 % number_2);
  68.         return;
  69.     }
  70. }
  71.  
  72. //方式二: 使用switch .. case ..
  73. void method_2()
  74. {
  75.     int number_1 = 0, number_2 = 0, select = 0;
  76.  
  77.     printf("Input the first number: ");
  78.     scanf("%d", &number_1);
  79.  
  80.     printf("Input the second number: ");
  81.     scanf("%d", &number_2);
  82.  
  83.     printf("// Select your action  ");
  84.     printf("[1, 2, 3, 4] is [+, -, *, /] //");
  85.     printf("\nYour choose is: ");
  86.     scanf("%d", &select);
  87.  
  88.     /*開始進行switch 條件運算式, 其使用格式為
  89.     ------------------------
  90.       switch([條件式])
  91.       {
  92.            case [值]:
  93.                [運算式]
  94.       }
  95.     ------------------------*/
  96.     switch(select)
  97.     {
  98.         case 1:
  99.             printf("Answer(+) is: %d\n", number_1 + number_2);
  100.             break;
  101.         case 2:
  102.             printf("Answer(-) is: %d\n", number_1 - number_2);
  103.             break;
  104.         case 3:
  105.             printf("Answer(*) is: %d\n", number_1 * number_2);
  106.             break;
  107.         case 4:
  108.             printf("Answer(/) is: %d .. %d\n", number_1 / number_2, number_1 % number_2);
  109.             break;
  110.     }
  111. }
  112.  
  113. void main()
  114. {
  115.     //method_1();
  116.     //method_2();
  117.     system("pause");
  118. }
Advertisement
Add Comment
Please, Sign In to add comment