Holey_yan

20140506_KUAS_VisualC++_Exam09

May 6th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /************************************/
  2. /** 20140506 Exam 09               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/05/26 am.11:51   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. int compare(int, int);
  13.  
  14. void main()
  15. {
  16.     printf("-----------------------Start Game !-----------------------\n");
  17.     srand(time(NULL));
  18.     int random = rand() % (100 - 1 + 1) + 1;
  19.     printf("-->Random = %d\n", random);
  20.     int max_limit = 100, min_limit = 1, money = 1000;
  21.     while(1)
  22.     {
  23.         printf("You have %4d dollars. And the range is %d to %d.\n", money, min_limit, max_limit);
  24.         printf("Enter a number of you guess (-1 is exit the game): ");
  25.         int input = 0;
  26.         scanf("%d", &input);
  27.         if(input == -1)
  28.         {
  29.             if(money > 1000)
  30.             {
  31.                 printf("\nYou win %4d dollars. Welcome at next time.\n", money - 1000);
  32.             }
  33.             else if(money < 1000)
  34.             {
  35.                 printf("\nYou lose %4d dollars. Welcome at next time.\n", 1000 - money);
  36.             }
  37.             else if(money == 1000)
  38.             {
  39.                 printf("\nYou do not win and do not lose. Goodbye~\n");
  40.             }
  41.             printf("------------------------End Game !------------------------\n");
  42.             system("pause");
  43.             return;
  44.         }
  45.         switch(compare(input, random))
  46.         {
  47.             case 0:
  48.                 printf("The number is smaller.\n\n");
  49.                 if(max_limit > input)
  50.                 {
  51.                     max_limit = input; 
  52.                 }
  53.                 else
  54.                 {
  55.                     max_limit = max_limit; 
  56.                 }
  57.                 money -= 100;
  58.                 break;
  59.             case 1:
  60.                 printf("The number is larger.\n\n");
  61.                 if(min_limit < input)
  62.                 {
  63.                     min_limit = input;
  64.                 }
  65.                 else
  66.                 {
  67.                     min_limit = min_limit; 
  68.                 }
  69.                
  70.                 money -= 100;
  71.                 break;
  72.             case 2:
  73.                 printf("Comgratulations, you get the number !\n\n");
  74.                 money += 200;
  75.                 random = rand() % (100 - 1 + 1) + 1;
  76.                 printf("-->Random = %d\n", random);
  77.                 max_limit = 100;
  78.                 min_limit = 1;
  79.         }
  80.         if(money == 0)
  81.         {
  82.             printf("Sorry, you do not have money. Welcome at next time.\n");
  83.             printf("------------------------End Game !------------------------\n");
  84.             system("pause");
  85.             return;
  86.         }
  87.     }
  88. }
  89.  
  90. int compare(int input, int random)
  91. {
  92.     if(input > random)
  93.     {
  94.         return 0;
  95.     }
  96.     else if(input < random)
  97.     {
  98.         return 1;
  99.     }
  100.     else if(input = random)
  101.     {
  102.         return 2;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment