-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (67 loc) · 1.44 KB
/
main.cpp
File metadata and controls
80 lines (67 loc) · 1.44 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
76
77
78
79
80
#ifndef _definition_h_
#include "defs.h"
#define _definition_h_
#endif
//read data from input file to corresponding variables
//return 1 if successfully done, otherwise return 0
int readFile(FILE* f, eventList*& pEvent)
{
char c = fgetc(f);
int num, minus;
eventList* tail = NULL;
while (c != -1 && !feof(f))
{
minus = 0;
while (c < '0' || c > '9')
{
if (c == '-') minus = 1;
c = fgetc(f);
}
num = c - '0';
c = fgetc(f);
while (c >= '0' && c <= '9')
{
num = num * 10 + c - '0';
c = fgetc(f);
}
if (minus) num = -num;
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;
}
return 1;
}
void display(KnightTree* pTree, int n)
{
cout.flush();
if (pTree)
{
cout << setw(3) << setfill('0') << pTree->key;
cout << pTree->level;
display(pTree->pLeftChild,++n);
display(pTree->pRightChild,++n);
}
}
int main(int argc, char* argv[])
{
if (argc < 2) return 1;
const char* filename = argv[1];
FILE* f = fopen(filename, "r");
if (f == NULL) //file not found or cannot read
{
printf("File not found \n");
return 0;
}
eventList* pEvent = NULL;
KnightTree* pFinalTree = NULL;
ringsignList* pSarumanList = NULL;
readFile(f, pEvent);
pFinalTree = siege(pEvent,pSarumanList);
display(pFinalTree,0);
return 0;
}