Holey_yan

20140519_KUAS_VisualC++_Classwork_0430_Question_1_Answer

May 18th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. /***********************************************/
  2. /** 20140519 Classwork_0430 Question 1 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.  
  20.     while(1)
  21.     {
  22.         printf("Please guess a number: ");
  23.         int input = 0;
  24.         scanf("%d", &input);
  25.         if(input == 0)
  26.         {
  27.             exit_program();
  28.         }
  29.         else if(input > 0)
  30.         {
  31.             switch(process_input(input, random))
  32.             {
  33.                 case 0:
  34.                     printf("My answer is smaller than %d.\n", input);
  35.                     break;
  36.                 case 1:
  37.                     printf("My answer is larger than %d.\n", input);
  38.                     break;
  39.                 case 2:
  40.                     printf("Bingo!! You got it!!\n\n");
  41.                     exit_program();
  42.                     break;
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. int process_input(int input, int random)
  49. {
  50.     if(input > random)
  51.     {
  52.         return 0;
  53.     }
  54.     else if(input < random)
  55.     {
  56.         return 1;
  57.     }
  58.     else if(input == random)
  59.     {
  60.         return 2;
  61.     }
  62. }
  63.  
  64. void exit_program()
  65. {
  66.     printf("You are exiting now ..\n");
  67.     exit(1);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment