-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDikjstra two stack algorithm.cpp
More file actions
167 lines (140 loc) · 4.9 KB
/
Copy pathDikjstra two stack algorithm.cpp
File metadata and controls
167 lines (140 loc) · 4.9 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
/*
Author : Mahmoud Kassem
Email : m_m_kassem@hotmail.com
Dijkstra two stack algorithm C++ implementaion
*/
#include <iostream>
#include "stack.h"
#include<string>
#include<stdio.h>
#include <cstdlib>
#define Invalid "Invalid Expression"
using namespace std;
stack<double>numbers ; stack<char>operators ;
bool is_digit(char x){ // returns true if the result is digit .
return (x >= '0' && x <= '9') ;
}
bool is_operation(char x){ // checks if the char is a valid operation character .
return (x == '+' || x == '-' || x == '/' || x == 'x') ;
}
double evaluate_expression(double op1 , double op2 , char operator_ch){ // returns the result of the expression
if(operator_ch == '/' && op2 == 0){
cout << Invalid << endl ;
exit(0) ;
}
if(operator_ch == '+')
return op1 + op2 ;
if(operator_ch == '-')
return op1 - op2 ;
if(operator_ch == 'x')
return op1 * op2 ;
if(operator_ch == '/'){ // make sure op2 is not zero before going in the function
return op1 / op2 ;
}
}
double operation(){ // makes an operation
double op2 = numbers.top() ; numbers.pop() ;
double op1 = numbers.top() ; numbers.pop() ;
char operator_ch = operators.top() ; operators.pop() ;
return evaluate_expression(op1 , op2 , operator_ch);
}
int main(int argc , char * argv[])
{
// The input of the program is given using the cmd .
if(argc == 1){
double ans = 0 ;
printf("%.2f\n" , ans) ;
return 0 ;
}
if(argc != 2){
cout << Invalid << endl ;
return 0 ;
}
string exp = argv[1] ; bool sign = 0 ;
for(int i = 0 ; i < exp.size() ; i++){ // loop on the whole expression
if(is_digit(exp[i])){ // if the char is digit then store it in the digits stack
int val = 0 ;
while(i != exp.size() && is_digit(exp[i]))
val = val * 10 + (exp[i] - '0') , i++ ;
i-- ;
numbers.push(val - 2 * (sign) * val) ; // if sign = 1 , make the digit to be -ve
sign = 0 ;
}
else if(exp[i] == '('){
operators.push(exp[i]) ;
}
else if(exp[i] == ')'){ // clear till the open bracket .
while(!operators.empty() && numbers.size() >= 2){
if(operators.top() == '('){
///open_fnd = 1 ;
break ;
}
numbers.push(operation()) ;
}
if(operators.empty() || operators.top() != '('){ // if a opening bracked is not found then the expression must be invalid
cout << Invalid << endl ;
return 0 ;
}
operators.pop() ;
}
else if(exp[i] == '-' || exp[i] == '+'){
if(exp[i] == '-' && (i == 0 || !is_digit(exp[i - 1]) && exp[i - 1] != ')')){
if(sign){ // double signs are not allowed for example --2 is considered to invalid .
cout << Invalid << endl ;
return 0 ;
}
sign ^= 1 ;
continue;
}
while(!operators.empty()){ // make all previous operations wit higher or equal preceduance .
if(is_operation(operators.top())){
if(numbers.size() < 2){
cout << Invalid << endl ;
return 0 ;
}
numbers.push(operation()) ;
}
else
break;
}
operators.push(exp[i]) ;
}
else if(exp[i] == 'x' || exp[i] == '/'){
if(i == 0 || (exp[i - 1] == '(')){
cout << Invalid << endl ;
return 0 ;
}
while(!operators.empty()){
if(operators.top() == 'x' || operators.top() == '/'){ // make all previous operations with higher or equal preceduace .
if(numbers.size() >= 2){
numbers.push(operation()) ;
}
else
return cout << Invalid << endl , 0 ;
}
else {
break ;
}
}
operators.push(exp[i]) ;
}
else {
cout << Invalid << endl ;
return 0 ;
}
}
// clear till the last element
while(!operators.empty()){
if(numbers.size() < 2){
cout << Invalid << endl ;
return 0 ;
}
numbers.push(operation()) ;
}
if(numbers.size() != 1 || !operators.empty()){
cout << Invalid << endl ;
return 0 ;
}
printf("%.2f\n" , numbers.top()) ; // print the result with 2 decimal values .
return 0 ;
}