-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnum_4.cpp
More file actions
115 lines (109 loc) · 2.06 KB
/
num_4.cpp
File metadata and controls
115 lines (109 loc) · 2.06 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
#include<iostream>
#include<string>
#include<vector>
#include<istream>
#include<map>
#include<sstream>
using namespace std;
typedef pair<int, int> precedence;
map<char,precedence> order=
{
{'(',make_pair(1,6)},
{')',make_pair(6,1)},
{'+',make_pair(3,2)},
{'-',make_pair(3,2)},
{'*',make_pair(5,4)},
{'/',make_pair(5,4)},///second->out
{'#',make_pair(0,0)},///first->in
};
int main()
{
string tempt;
getline(cin, tempt);
istringstream target(tempt +'#'+'$');
vector<char> symbols;
symbols.push_back('#');
bool if_opper = true;
double number_to_calcu = 0;
do
{
char next_sym = target.peek();
if (next_sym == ' ')
{
target.get();
continue;
}
if (next_sym > '0'&& next_sym < '9')
{
target >> number_to_calcu;
cout << number_to_calcu << " ";
if_opper = false;
}
else if (next_sym == '+' || next_sym == '-')
{
if ((symbols.back() == '#' || symbols.back() == '(' )&&if_opper)
{
target >> number_to_calcu;
cout << number_to_calcu << " ";
if_opper = false;
}
else
{
if (order[next_sym].second > order[symbols.back()].first)
{
symbols.push_back(target.get());
}
else
{
while (order[next_sym].second < order[symbols.back()].first)
{
char out_put = symbols.back();
cout << out_put << " ";
symbols.pop_back();
}
}
}
}
else
{
if (order[next_sym].second > order[symbols.back()].first)
{
if (next_sym == '(')
{
if_opper = true;
}
symbols.push_back(target.get());
}
else
{
while (order[next_sym].second <= order[symbols.back()].first)
{
if (order[next_sym].second == order[symbols.back()].first)
{
symbols.pop_back();
break;
}
else
{
char out_put = symbols.back();
cout << out_put;
if (symbols.size() != 2)
{
cout << " ";
}
symbols.pop_back();
}
}
if (next_sym != ')' && next_sym != '#')
{
symbols.push_back(target.get());
}
else
{
target.get();
}
}
}
} while (target.peek()!='$');
return 0;
}