forked from FantasqueX/Computing_Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (168 loc) · 2.73 KB
/
main.cpp
File metadata and controls
172 lines (168 loc) · 2.73 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
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
#include <iostream>
#include <string>
#include <assert.h>
#include <cmath>
#include <iomanip>
#include "node.h"
#include "graph.h"
#include <iostream>
using namespace std;
int main()
{
//Node* nodes[100];
Graph gra(100);
int n,m,q;
string name,type,tmp;
float value;
string phnames[100];
float phvalues[100];
float outvalue[100];
cin>>n;
for(int i=0; i<n; i++)
{
cin>>name;
cin>>type;
if(type=="C")
{
cin>>value;
gra.push(new Constant(name,value));
}
else if(type=="V")
{
cin>>value;
gra.push(new Variable(name,value));
}
else if(type=="P")
{
gra.push(new Placeholder(name));
}
else assert(0);
}
cin>>m;
for(int i=0; i<m; i++)
{
cin>>name;
cin>>tmp;
assert(tmp=="=");
string p1,p2,p3,p4;
cin>>p1;
if(p1=="COND")
{
cin>>p2;
cin>>p3;
cin>>p4;
gra.push(new Cond(name,gra[p2],gra[p3],gra[p4]));
continue;
}
else if(p1=="PRINT")
{
cin>>p2;
gra.push(new Print(name,gra[p2]));
continue;
}
else if(p1=="SIN")
{
cin>>p2;
gra.push(new Sin(name,gra[p2]));
continue;
}
else if(p1=="EXP")
{
cin>>p2;
gra.push(new Exp(name,gra[p2]));
continue;
}
else if(p1=="LOG")
{
cin>>p2;
gra.push(new Log(name,gra[p2]));
continue;
}
else if(p1=="TANH")
{
cin>>p2;
gra.push(new Tanh(name,gra[p2]));
continue;
}
else if(p1=="SIGMOID")
{
cin>>p2;
gra.push(new Sigmoid(name,gra[p2]));
continue;
}
else
{
cin>>type;
cin>>p2;
if(type=="+")
{
gra.push(new sum(name,gra[p1],gra[p2]));
}
else if(type=="-")
{
gra.push(new subtraction(name,gra[p1],gra[p2]));
}
else if(type=="*")
{
gra.push(new multiply(name,gra[p1],gra[p2]));
}
else if(type=="/")
{
gra.push(new division(name,gra[p1],gra[p2]));
}
else if(type==">")
{
gra.push(new GTR(name,gra[p1],gra[p2]));
}
else if(type=="<")
{
gra.push(new LSS(name,gra[p1],gra[p2]));
}
else if(type==">=")
{
gra.push(new GEQ(name,gra[p1],gra[p2]));
}
else if(type=="<=")
{
gra.push(new LEQ(name,gra[p1],gra[p2]));
}
else if(type=="==")
{
gra.push(new EQU(name,gra[p1],gra[p2]));
}
else assert(0);
}
}
cin>>q;
for(int i=0; i<q; i++)
{
cin>>type;
if(type=="EVAL")
{
cin>>name;
int phcount;
cin>>phcount;
for(int j=0; j<phcount; j++)
{
cin>>phnames[j];
cin>>phvalues[j];
}
outvalue[i]=gra.eval(name,phcount,phnames,phvalues);
if(!isnan(outvalue[i]))cout<<fixed<<setprecision(4)<<outvalue[i]<<endl;
}
else if(type=="SETCONSTANT")
{
cin>>name;
cin>>value;
gra.setvariable(name,value);
}
else if(type=="SETANSWER")
{
cin>>name;
int t;
cin>>t;
gra.setvariable(name,outvalue[t-1]);
}
else assert(0);
}
}