-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
86 lines (69 loc) · 2 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include "helpers.h"
#include "stack.h"
//Global tree root
Node* ROOT=NULL;
//Wrapper Function takes key and value to insert in the BST.
void put(char *key,float value){
ROOT=insert(ROOT, key, value);
}
void runFile(char *filename)
{
VERBOSE("Reading the file");
FILE *f=fopen(filename, "r");
if(!f)
ERROR("File doesn't exist");
char *Line=malloc(sizeof(char)*100);
lineNUM=0;
while (!feof(f))
{
// Scan Full Line.
fscanf(f, "%[^\n]\n", Line);
Line = removeSpaces(Line);
// Two-sided equation check for the equal sign '='.
char *equalSign=strstr(Line,"=");
if (!equalSign)
ERROR("INVALID EXPRESSION: \"%s\"", Line);
// Tokenizing Left Hand Side (LHS).
if(Line[0]=='=')
ERROR("NO LHS !");
char *LHS=malloc(sizeof(char) * (equalSign-Line)+1);
strcpy(LHS, strtok(Line, "="));
// Single LHS Variable Check. (Valid Variable Name)
for (int i = 0; i < strlen(LHS); ++i)
{
if (!isalpha(LHS[0]) || ispunct(LHS[i]) && LHS[i]!='_')
ERROR("INVALID LHS: \"%s\"", LHS);
}
// Tokenizing Right Hand Side.
char *RHS=strtok(NULL,"");
if(!RHS)
ERROR("NO RHS !");
char *Postfix=infixToPost(format(RHS));
float value = evaluate_postfix(Postfix, ROOT);
put(LHS,value);
printf("Line#%ld:\tLHS[%s]=RHS[%s]\n",lineNUM++ + 1,LHS,RHS);
//COLOR(4);
printf("%s=%.2f\n",LHS,value);
//COLOR(0);
}
puts("------------------------");
//COLOR(-5);
puts("Order BY Variable Name: ");
//COLOR(0);
inOrder(ROOT);
variable heap[count(ROOT)];
fromTreeToHeap(ROOT, heap);
puts("------------------------");
heapSort(count(ROOT), heap);
//COLOR(-5);
puts("Order BY Variable Value:");
//COLOR(0);
printArray(heap,count(ROOT));
}
int main()
{
runFile("src.txt");
return 0;
}