-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
100 lines (76 loc) · 2.46 KB
/
Copy pathapp.cpp
File metadata and controls
100 lines (76 loc) · 2.46 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
#include <iostream>
#include "stack.h"
#include "token.h"
#include "number.h"
#include "function.h"
using namespace std;
int main()
{
Stack postfix;
Stack operations;
Stack numbers;
char buffer[11];
int n;
cin >> n;
while (n)
{
cin >> buffer;
do
{
if (Token::IsNumber(buffer)) postfix.push(buffer);
else if (Token::IsFunction(buffer)) operations.push(buffer);
else if (Token::IsOperator(buffer))
{
while (!operations.empty() && operations.top().GetType() != TokenType::LEFT_PARENTHESES && operations.top().GetPrecedence() >= Token::GetPrecedence(buffer))
Stack::movetop(postfix, operations);
operations.push(buffer);
}
else if (buffer[0] == ',')
{
while (!operations.empty() && operations.top().GetType() != TokenType::LEFT_PARENTHESES)
Stack::movetop(postfix, operations);
Token* tmptop = operations.popptr();
operations.top().IncrementElements();
operations.push(tmptop);
}
else if (buffer[0] == '(') operations.push(buffer);
else if (buffer[0] == ')')
{
while (!operations.empty() && operations.top().GetType() != TokenType::LEFT_PARENTHESES)
Stack::movetop(postfix, operations);
operations.pop();
if (!operations.empty() && operations.top().GetType() == TokenType::FUNCTION)
Stack::movetop(postfix, operations);
}
cin >> buffer;
} while (*buffer != '.');
while (!operations.empty())
Stack::movetop(postfix, operations);
postfix.reverse();
cout << postfix << "\n";
bool wasError = false;
while (!postfix.empty())
{
Token* top = postfix.popptr();
if (top->GetType() == TokenType::NUMBER)
{
numbers.push(top);
continue;
}
cout << *top << " " << numbers;
if (!top->Apply(numbers))
{
wasError = true;
delete top;
break;
}
cout << "\n";
delete top;
}
if(!wasError) cout << numbers.top() << "\n\n";
postfix.clear();
numbers.clear();
n--;
}
return 0;
}