Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************/
- /** 20140506 Exam 09 **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2014/05/26 am.11:51 **/
- /************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int compare(int, int);
- void main()
- {
- printf("-----------------------Start Game !-----------------------\n");
- srand(time(NULL));
- int random = rand() % (100 - 1 + 1) + 1;
- printf("-->Random = %d\n", random);
- int max_limit = 100, min_limit = 1, money = 1000;
- while(1)
- {
- printf("You have %4d dollars. And the range is %d to %d.\n", money, min_limit, max_limit);
- printf("Enter a number of you guess (-1 is exit the game): ");
- int input = 0;
- scanf("%d", &input);
- if(input == -1)
- {
- if(money > 1000)
- {
- printf("\nYou win %4d dollars. Welcome at next time.\n", money - 1000);
- }
- else if(money < 1000)
- {
- printf("\nYou lose %4d dollars. Welcome at next time.\n", 1000 - money);
- }
- else if(money == 1000)
- {
- printf("\nYou do not win and do not lose. Goodbye~\n");
- }
- printf("------------------------End Game !------------------------\n");
- system("pause");
- return;
- }
- switch(compare(input, random))
- {
- case 0:
- printf("The number is smaller.\n\n");
- if(max_limit > input)
- {
- max_limit = input;
- }
- else
- {
- max_limit = max_limit;
- }
- money -= 100;
- break;
- case 1:
- printf("The number is larger.\n\n");
- if(min_limit < input)
- {
- min_limit = input;
- }
- else
- {
- min_limit = min_limit;
- }
- money -= 100;
- break;
- case 2:
- printf("Comgratulations, you get the number !\n\n");
- money += 200;
- random = rand() % (100 - 1 + 1) + 1;
- printf("-->Random = %d\n", random);
- max_limit = 100;
- min_limit = 1;
- }
- if(money == 0)
- {
- printf("Sorry, you do not have money. Welcome at next time.\n");
- printf("------------------------End Game !------------------------\n");
- system("pause");
- return;
- }
- }
- }
- int compare(int input, int random)
- {
- if(input > random)
- {
- return 0;
- }
- else if(input < random)
- {
- return 1;
- }
- else if(input = random)
- {
- return 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment