-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_calculation.cpp
More file actions
137 lines (127 loc) · 3.22 KB
/
Copy pathexpression_calculation.cpp
File metadata and controls
137 lines (127 loc) · 3.22 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
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
int periority(char &symbol)
{
switch (symbol)
{
case '-':
case '+':
return 1;
case '/':
case '*':
return 2;
case '^':
return 3;
default:
return -1;
}
}
string exp_to_postfix(string expression)
{
stack<char> symbols_stack;
string postfixed = "";
// cout << "expression size " << expression.size() << endl;
for (int i = 0; i < expression.size(); i++)
{
char current = expression[i];
// cout << "current char " << current << endl;
if (current >= '0' && current <= '9')
{
postfixed += (current);
postfixed += ' ';
}
else if (current == '+' || current == '-' || current == '/' || current == '*' || current == '^')
{
if (!symbols_stack.empty() && periority(current) <= periority(symbols_stack.top()))
{
postfixed += symbols_stack.top();
postfixed += ' ';
symbols_stack.pop();
}
symbols_stack.push(current);
}
else if (current == '(')
{
symbols_stack.push('(');
}
else if (current == ')')
{
while (!symbols_stack.empty() && symbols_stack.top() != '(')
{
postfixed += symbols_stack.top();
postfixed += ' ';
symbols_stack.pop();
}
symbols_stack.pop();
}
}
while (!symbols_stack.empty())
{
postfixed += symbols_stack.top();
postfixed += ' ';
symbols_stack.pop();
}
return postfixed;
}
int calc(int num_1, int num_2, char op)
{
switch (op)
{
case '+':
return num_1 + num_2;
case '-':
return num_1 - num_2;
case '*':
return num_1 * num_2;
case '/':
return num_1 / num_2;
case '^':
return pow(num_1, num_2);
default:
return -1;
}
}
int calc_exp(string expression)
{
string postfix = exp_to_postfix(expression);
stack<int> operataions;
string current_num = "";
for (int i = 0; i < postfix.size(); i++)
{
char current_char = postfix[i];
cout << current_char << endl;
if (current_char == ' ')
{
if (current_num != "")
{
operataions.push(stoi(current_num));
current_num = "";
}
}
else if (current_char >= '0' && current_char <= '9')
{
current_num += current_char;
}
else if (current_char == '+' || current_char == '-' || current_char == '/' || current_char == '*' || current_char == '^')
{
int num_1 = operataions.top();
operataions.pop();
int num_2 = operataions.top();
operataions.pop();
operataions.push(calc(num_1, num_2, current_char));
}
}
return operataions.top();
}
int main()
{
// cout << "Enter your expression" << endl;
string expression;
cin >> expression;
// cout << expression;
cout << calc_exp(expression) << endl;
cout << exp_to_postfix(expression) << endl;
return 0;
}