-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.cpp
More file actions
121 lines (109 loc) · 2.78 KB
/
Result.cpp
File metadata and controls
121 lines (109 loc) · 2.78 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
#include "Header.h"
symbol resultOfEquation(queue* data, stack* operatorStack, VarVector* resultVar, VarVector* variablesContainer, int* operationCount, int maxOperation)
{
int count = operatorStack->size();
for (int i = 0; i < count; i++)
{
data->push(operatorStack->top());
operatorStack->pop();
}
stack result;
while ((data->size() != 0) && (*operationCount <= maxOperation))
{
symbol buffer = data->front();
data->pop();
if (buffer._type == digitS || buffer._type == variableS)
{
if (buffer._type == variableS)
{
if (variablesContainer->at(buffer.index).isNull)
{
buffer.isNull = true;
buffer.value = 0;
}
else
{
buffer.value = variablesContainer->at(buffer.index).value;
buffer.isNull = false;
}
}
result.push(buffer);
}
else if (buffer._type == negationS)
{
symbol firstVatiable = result.top();
result.pop();
firstVatiable._type = digitS;
firstVatiable.isNull = !firstVatiable.isNull;
if (firstVatiable.isNull)
{
firstVatiable.value = 0;
}
*operationCount = *operationCount + 1;
result.push(firstVatiable);
}
else if (buffer._type == minusVarS)
{
symbol firstVatiable = result.top();
result.pop();
firstVatiable._type = digitS;
firstVatiable.value = -firstVatiable.value;
*operationCount = *operationCount + 1;
result.push(firstVatiable);
}
else
{
symbol secondVariable = result.top();
result.pop();
symbol firstVatiable = result.top();
result.pop();
symbol newDigitSymbol = resultOfMathEquation(firstVatiable, buffer, secondVariable, variablesContainer);
*operationCount = *operationCount + 1;
result.push(newDigitSymbol);
}
}
for (int i = resultVar->size() - 1; i >= 0; i--)
{
var newValueVar = resultVar->at(i);
int index = variablesContainer->isInitalizedVariable(newValueVar.name);
if (index != -1)
{
variablesContainer->at(index).value = result.top().value;
variablesContainer->at(index).isNull = result.top().isNull;
*operationCount = *operationCount + 1;
}
if (*operationCount >= maxOperation)
break;
}
if (result.size() == 0)
{
symbol nullVal;
nullVal._type = digitS;
nullVal.value = 0;
nullVal.isNull = true;
result.push(nullVal);
}
return result.top();
}
void showResult(VarVector* variablesContainer, int numberOfOperation)
{
if (variablesContainer->size() != 0)
{
cout << numberOfOperation << endl;
for (int i = 0; i < variablesContainer->size(); i++)
{
if (variablesContainer->at(i).isInitalized)
{
if (variablesContainer->at(i).isNull)
{
cout << variablesContainer->at(i).name << " " << "Nul" << endl;
}
else
{
cout << variablesContainer->at(i).name << " " << variablesContainer->at(i).value << endl;
}
}
variablesContainer->empty();
}
}
}