-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab6_Q3.cpp
More file actions
183 lines (176 loc) · 4.26 KB
/
Lab6_Q3.cpp
File metadata and controls
183 lines (176 loc) · 4.26 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
173
174
175
176
177
178
179
180
181
182
#include<iostream>
#include<cmath>
#include<string>
#include<sstream>
using namespace std;
class Node {
public:
string data;
Node* next;
Node(string d) {
data = d;
next = NULL;
}
};
class stack {
private:
Node* top;
public:
stack() {
top = NULL;
}
void push(string val) {
Node* temp = new Node(val);
if (top == NULL) {
top = temp;
} else {
temp->next = top;
top = temp;
}
}
string pop() {
if (top == NULL) {
return "";
}
Node* temp = top;
string val = temp->data;
top = top->next;
delete temp;
return val;
}
void display() {
Node* temp = top;
while (temp) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
bool isEmpty() {
return top == NULL;
}
string peek() {
if (top == NULL) {
return "";
} else {
return top->data;
}
}
};
double string_to_double(const string& s) {
stringstream s_(s);
double result;
s_ >>result;
return result;
}
string double_to_string(double num) {
stringstream s;
s << num;
return s.str();
}
int precedence(char c) {
if (c == '^') {
return 3;
}
else if (c == '*' || c == '/') {
return 2;
}
else if (c == '+' || c == '-') {
return 1;
}
else {
return -1;
}
}
bool isOperator(char c) {
return c == '+' || c == '-' || c == '/' || c == '*' || c == '^';
}
string infixtopostfix(const string& infix) {
string postfix = " ";
stack s1;
for (int i = 0; i < infix.length(); i++) {
char c = infix[i];
if (isalnum(c) || c == '.') {
postfix += c;
} else if (c == '(') {
char str[2];
str[0] = c;
str[1] = '\0';
s1.push(str);
postfix += ' ';
} else if (c == ')') {
postfix += ' ';
while (!s1.isEmpty() && s1.peek() != "(") {
postfix += s1.pop();
}
s1.pop();
} else if (isOperator(c)) {
postfix += ' ';
while (!s1.isEmpty() && precedence(s1.peek()[0]) >= precedence(c)) {
postfix += s1.pop();
}
char str[2];
str[0] = c;
str[1] = '\0';
s1.push(str);
}
}
while (!s1.isEmpty()) {
postfix += ' ';
postfix += s1.pop();
}
return postfix;
}
double calculation(const string& postfix) {
stack s1;
string ans = " ";
for (int i = 0; i < postfix.length(); i++) {
char c = postfix[i];
if (isdigit(c) || c == '.') {
ans += c;
}
else if (c == ' ' && ans != " ") {
double ans_ = string_to_double(ans);
s1.push(double_to_string(ans_));
ans = " ";
} else if (isOperator(c)) {
double val1 = string_to_double(s1.pop());
double val2 = string_to_double(s1.pop());
switch (c) {
case '+':
s1.push(double_to_string(val2 + val1));
break;
case '-':
s1.push(double_to_string(val2 - val1));
break;
case '*':
s1.push(double_to_string(val2 * val1));
break;
case '/':
s1.push(double_to_string(val2 / val1));
break;
case '^':
s1.push(double_to_string(pow(val2, val1)));
break;
default:
cout << "Invalid operator" << endl;
}
}
}
return string_to_double(s1.pop());
}
int main() {
string equation = "12+13-5*(0.5+0.5)+1";
stack s1;
s1.push("x");
s1.push("=");
s1.push("12");
s1.push("+");
s1.pop();
s1.pop();
string postfix = infixtopostfix(equation);
double result = calculation(postfix);
s1.push(double_to_string(result));
s1.display();
cout << "Answer is: " << result << endl;
}