-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathguess.cpp
More file actions
78 lines (74 loc) · 1.52 KB
/
guess.cpp
File metadata and controls
78 lines (74 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//MingkuanPang
// This program can read a letter, and
// it will try to guess the letter.After each
//guess, the user should tell this program whether its
//guess was too high or too low.With this information, the
//program should be able to guess the letter within five
//tries.
#include<iostream>
#include<stdio.h>
using namespace std;
char guess(int, int);
bool if_upper(char);
int main()
{
char letter, guess_letter, size, answer;
int max, min;
cout << "Please enter a letter: ";
letter = cin.get();
cin.ignore(999, '\n');
if (if_upper(letter))
{
min = 'A';
max = 'Z';
}
else
{
min = 'a';
max = 'z';
}
while (1)
{
guess_letter = guess(max, min);
cout << "If it is " << guess_letter << " ?" << endl;
answer = cin.get();
cin.ignore(999, '\n');
answer = toupper(answer);
if (answer == 'Y')
{
cout << "Hey! I am better than you!" << endl;
break;
}
else
{
cout << "Does it come before " << guess_letter << " or after?" << endl;
size = cin.get();
cin.ignore(999, '\n');
size = toupper(size);
if (size == 'B')
{
max = guess_letter - 1;
}
else
{
min = guess_letter + 1;
}
}
}
return 0;
}
char guess(int max, int min)//This function can return a guessing letter within the range between min to max.
{
char guess_letter, size;
guess_letter = (max + min) / 2;
return guess_letter;
}
bool if_upper(char letter)//This function can judge if a letter is capital or not.
{
bool real;
if (letter >= 'A' and letter <= 'Z')
real = 1;
else
real = 0;
return real;
}