Holey_yan

20140519_KUAS_VisualC++_Classwork_0430_Question_2_Answer

May 18th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. /***********************************************/
  2. /** 20140519 Classwork_0430 Question 2 Answer **/
  3. /** Maker  : Yan                              **/
  4. /** E-Mail : [email protected]            **/
  5. /** Date   : 2014/05/19 am.01:19              **/
  6. /***********************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. int process_input(int, int);
  13. void exit_program();
  14.  
  15. void main()
  16. {
  17.     srand(time(NULL));
  18.     int random = rand() % (100 - 1 + 1) + 1;
  19.     int money = 1000, counter = 0;
  20.  
  21.     while(1)
  22.     {
  23.         printf("Now you have %4d dollars.\n", money);
  24.         printf("Just play the game to earn more money.\n");
  25.         printf("Please guess a number: ");
  26.         int input = 0;
  27.         scanf("%d", &input);
  28.         if(input == 0)
  29.         {
  30.             exit_program();
  31.         }
  32.         else if(input > 0)
  33.         {
  34.             switch(process_input(input, random))
  35.             {
  36.                 case 0:
  37.                     printf("My answer is smaller than %d.\n", input);
  38.                     break;
  39.                 case 1:
  40.                     printf("My answer is larger than %d.\n", input);
  41.                     break;
  42.                 case 2:
  43.                     printf("Bingo!! You got it!!\n\n");
  44.                     switch(counter)
  45.                     {
  46.                         case 1:
  47.                         case 2:
  48.                             money += 800;
  49.                             break;
  50.  
  51.                         case 3:
  52.                         case 4:
  53.                             money += 400;
  54.                             break;
  55.  
  56.                         case 5:
  57.                         case 6:
  58.                             money += 100;
  59.                             break;
  60.  
  61.                         case 7:
  62.                         case 8:
  63.                         case 9:
  64.                             money -= 200;
  65.                             break;
  66.  
  67.                         default:
  68.                             money -= 600;
  69.                             break;
  70.                     }
  71.                     if(money < 0)
  72.                     {
  73.                         exit_program();
  74.                     }
  75.                 break;
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. int process_input(int input, int random)
  82. {
  83.     if(input > random)
  84.     {
  85.         return 0;
  86.     }
  87.     else if(input < random)
  88.     {
  89.         return 1;
  90.     }
  91.     else if(input == random)
  92.     {
  93.         return 2;
  94.     }
  95. }
  96.  
  97. void exit_program()
  98. {
  99.     printf("You are exiting now ..\n");
  100.     exit(1);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment