Holey_yan

20140318_KUAS_VisualC++_CalculationOfResults

Mar 18th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. /*************************************/
  2. /** 20140318 Calculation of Results **/
  3. /** Maker  : Yan                    **/
  4. /** E-Mail : [email protected]  **/
  5. /** Date   : 2014/03/11 pm.11:56    **/
  6. /*************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. void main()
  12. {
  13.         /*定義變數, 當select 輸入為1 時代表及格, 2 則為不及格
  14.           pass 為及格人數,total 為成績總數                   */
  15.         int select = 0, pass = 0, total = 0;
  16.         float pass_percentage = 0;
  17.  
  18.         printf("Enter score, -1 is exit program.\n");
  19.  
  20.         //當輸入不等於-1 時執行迴圈
  21.         while(1)
  22.         {
  23.             printf("Enter Number: ");
  24.             scanf("%d", &select);
  25.             if(select != -1)
  26.             {
  27.                 total += 1;
  28.                 if(select == 1)
  29.                 {
  30.                     pass += 1;
  31.                 }
  32.             }
  33.             else if(select == -1)
  34.             {
  35.                 break;
  36.             }
  37.         }
  38.  
  39.         //已跳出迴圈, 開始計算及格百分率
  40.         //當成績總數等於零, 代表無資料可計算
  41.         if(total == 0)
  42.         {
  43.             printf("No data can calculate");
  44.         }
  45.         else
  46.         {
  47.             //輸出及格人數
  48.             printf("Pass: %d\n", pass);
  49.             //輸出不及格人數(總人數 減去 及格人數)
  50.             printf("Fail: %d\n", total - pass);
  51.  
  52.             //及格百分率, 利用float(浮點數) 進行運算
  53.             pass_percentage = ((float)pass / total) * 100;
  54.             printf("Pass percentage: %2.2f%%.\n", pass_percentage);
  55.         }
  56.     system("pause");
  57. }
Advertisement
Add Comment
Please, Sign In to add comment