-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
154 lines (133 loc) · 3.07 KB
/
main.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
#ifndef _definition_h_
#include "defs.h"
#define _definition_h_
#endif
const int MAX_CHARACTER_PER_LINE = 250;
//read data from input file to corresponding variables
//return 1 if successfully done, otherwise return 0
int readFile(const char* file_name, knight& theKnight, eventList* &pEvent)
{
FILE* f = 0;
char* str = new char[MAX_CHARACTER_PER_LINE];
int num;
f = fopen(file_name, "r");
if (f == NULL) //file not found || cannot read
return 0;
//----------- read knight information -----------
fgets(str, MAX_CHARACTER_PER_LINE, f);
if (feof(f))
return 0;
int start = 0;
int len = strlen(str);
//read HP
while (start < len && str[start] == ' ')
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start++] - '0';
}
theKnight.HP = num;
if (theKnight.HP < 1 || theKnight.HP > 999)
return 0;
//read level
while (start < len && str[start] == ' ')
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start++] - '0';
}
theKnight.level = num;
if (theKnight.level < 1 || theKnight.level > 10)
return 0;
//read nInitRingsign
while (start < len && str[start] == ' ')
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start++] - '0';
}
theKnight.nInitRingsign = num;
//------------------ read event list ----------------
eventList* tail = NULL;
while (1)
{
str = new char[MAX_CHARACTER_PER_LINE];
if (feof(f))
break;
fgets(str, MAX_CHARACTER_PER_LINE, f);
if (str == NULL)
break;
start = 0;
len = strlen(str);
while (start < len)
{
while (start < len && (str[start] > '9' || str[start] < '0'))
start ++;
if (start >= len)
break;
int num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0')
{
num = num * 10 + str[start++] - '0';
}
eventList* current_event = new eventList;
current_event->nEventCode = num;
current_event->pNext = NULL;
if (pEvent == NULL)
pEvent = current_event;
if (tail != NULL)
tail->pNext = current_event;
tail = current_event;
}
}
fclose(f);
/*
//test for reading
while (pEvent != NULL)
{
cout << pEvent->nEventCode << endl;
pEvent = pEvent->pNext;
}
*/
return 1;
}
void display(knight theKnight, ringsignList* pList)
{
cout.flush();
cout << theKnight.HP;
if (checkPalindrome(pList))
cout << "God saves the Fellowship";
else
{
while (pList)
{
cout << pList->nRingsign;
pList = pList->pNext;
}
}
}
int main(int argc, char* argv[])
{
if (argc < 2) return 1;
const char* filename = argv[1];
knight theKnight;
eventList* pEvent = NULL;
ringsignList* pList = NULL;
readFile(filename, theKnight, pEvent);
pList = combat (theKnight, pEvent);
display(theKnight, pList);
// delete garbage
eventList* p;
while (pEvent != NULL){
p = pEvent;
pEvent = pEvent->pNext;
delete p;
}
ringsignList* q;
while(pList != NULL){
q = pList;
pList = pList->pNext;
delete q;
}
return 0;
}