Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************/
- /** 20140519 Classwork_0430 Question 2 Answer **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2014/05/19 am.01:19 **/
- /***********************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int process_input(int, int);
- void exit_program();
- void main()
- {
- srand(time(NULL));
- int random = rand() % (100 - 1 + 1) + 1;
- int money = 1000, counter = 0;
- while(1)
- {
- printf("Now you have %4d dollars.\n", money);
- printf("Just play the game to earn more money.\n");
- printf("Please guess a number: ");
- int input = 0;
- scanf("%d", &input);
- if(input == 0)
- {
- exit_program();
- }
- else if(input > 0)
- {
- switch(process_input(input, random))
- {
- case 0:
- printf("My answer is smaller than %d.\n", input);
- break;
- case 1:
- printf("My answer is larger than %d.\n", input);
- break;
- case 2:
- printf("Bingo!! You got it!!\n\n");
- switch(counter)
- {
- case 1:
- case 2:
- money += 800;
- break;
- case 3:
- case 4:
- money += 400;
- break;
- case 5:
- case 6:
- money += 100;
- break;
- case 7:
- case 8:
- case 9:
- money -= 200;
- break;
- default:
- money -= 600;
- break;
- }
- if(money < 0)
- {
- exit_program();
- }
- break;
- }
- }
- }
- }
- int process_input(int input, int random)
- {
- if(input > random)
- {
- return 0;
- }
- else if(input < random)
- {
- return 1;
- }
- else if(input == random)
- {
- return 2;
- }
- }
- void exit_program()
- {
- printf("You are exiting now ..\n");
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment