-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp02_0712209.cpp
187 lines (173 loc) · 4.35 KB
/
p02_0712209.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//File Name:p02_0712209.cpp
//Author:¾G¤Èºa
//Email Address:[email protected]
//Assignment Number:02
//Description:Program that imitates the system which can know who log in and out of system
//Last Change:3/15
#include <iostream>
#include <string>
using namespace std;
typedef string* strp;
void show(strp *, int , int *);
bool input(string str,char c)
{
cout << str;
char in;
cin >> in;
return c == in || c == in + 26;
}
int main()
{
int lablen, y, z;
string x;
cout << "Welcome NCTU computer system. This program imitates the system which can know who log in and out of system.\n";
cout << "How many labs are there in NCTU?";
cin >> lablen;
strp *a = new strp[lablen];
int *list = new int[lablen];
char ans;
cout << "Enter a list with " << lablen << " numbers, which means the amount of computers in each lab.";
for (int i = 0; i < lablen; i++)
cin >> list[i];
for (int i = 0; i < lablen; i++)
a[i] = new string[list[i]];
for (int i = 0; i < lablen; i++)
for (int j = 0; j < list[i]; j++)
a[i][j] = "";
show(a, lablen, list);
do
{
system("cls");
cout << "Who the hell are you?(s for system, u for user)";
cin >> ans;
//if(input("who the hell are you?(s for system, u for user)", 's') {}
if (ans == 's' || ans == 'S')
{
do
{
cout << "\nWho logged in or logged out the computer station?(1 for log in, 2 for log out)";
cin >> ans;
cout << endl;
if (ans == '1')
{
do
{
cout << "Enter the ID, lab number, and the computer station of the student that just logged in:";
cin >> x >> y >> z;
if (a[y - 1][z - 1] == "")
{
a[y - 1][z - 1] = x;
cout << "\nAny other account logged in?(y/n)";
cin >> ans;
}
else
{
cout << "Someone is using this computer station. Enter another one.\n";
ans = 'y';
}
} while (ans == 'y' || ans == 'Y');
show(a, lablen, list);
cout << "\nGo back to log in or out?(y/n)";
cin >> ans;
system("cls");
}
else if (ans == '2')
{
do
{
cout << "Enter the ID, lab number, and the computer station of the student that just logged out:";
cin >> x >> y >> z;
if (a[y - 1][z - 1] == x)
{
a[y - 1][z - 1] = "";
cout << "\nAny other account logged out?(y/n)";
cin >> ans;
}
else
{
cout << "This ID isn't using this computer station. Enter another one.\n";
ans = 'y';
}
} while (ans == 'y' || ans == 'Y');
show(a, lablen, list);
cout << "\nGo back to log in or out?(y/n)";
cin >> ans;
system("cls");
}
else
{
cout << "\nInvalid input.";
ans = 'y';
}
} while (ans == 'y' || ans == 'Y');
}
else if (ans == 'u' || ans == 'U')
{
do
{
cout << "Search for the ID or the computer station?(1 for the former, 2 for the latter)";
cin >> ans;
if (ans == '1')
{
cout << "\nWhich ID do you wanna search?";
cin >> x;
y = -1;
for (int i = 0; i < lablen; i++)
{
for (int j = 0; j < list[i]; j++)
{
if (a[i][j] == x)
{
y = i;
z = j;
break;
}
}
}
if (y < 0)
{
cout << "This ID isn't using any computer.\n";
}
else
{
cout << "This ID is using lab " << y + 1 << " station " << z + 1 << endl;
}
ans = 'n';
}
else if (ans == '2')
{
cout << "Enter the lab and station number you wanna search?";
cin >> y >> z;
if (a[y - 1][z - 1] == "")
cout << "No one is using this computer.\n";
else
cout << "ID " << a[y - 1][z - 1] << " is using this computer.\n";
ans = 'n';
}
else
{
cout << "Wrong ans. Type again.\n";
ans = 'y';
}
} while (ans == 'y' || ans == 'Y');
}
}while (input("Go back to choose user or system?(y/n):", 'y'));
cin.get();
return 0;
}
void show(strp *a, int lablen, int *list)
{
cout << "Lab number\tComputer station\n";
for (int i = 0; i < lablen; i++)
{
cout << i + 1 << "\t\t";
for (int j = 0; j < list[i]; j++)
{
if (a[i][j] == "")
cout << j + 1 << ": empty ";
else
cout << j + 1 << ": " << a[i][j] << " ";
}
cout << endl;
}
}