-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.cpp
More file actions
100 lines (88 loc) · 2.51 KB
/
Copy pathevaluator.cpp
File metadata and controls
100 lines (88 loc) · 2.51 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 <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std ;
struct stack {
double operand ;
int opcode ;
};
stack A[100] ;
int top = -1 ;
int getOperand ( char expr[] , int &from ){
int operandValue = 0 ;
while ( isdigit(expr[from]) ){
if ( (expr[from] >= 48 && expr[from] <= 57) ){
operandValue = ( operandValue *10 ) + ( (int)(expr[from]) - 48 ) ;
}
++from ;
}
--from ;
return operandValue ;
}
bool isOperator ( char opr ){
return (( opr == '+' ) || ( opr == '-' ) || ( opr == '*' ) || ( opr == '/' ))?true:false ;
}
int setOpCode ( char opr ){
if ( opr == '+' )
return 1 ;
else if ( opr == '-' )
return 2 ;
else if ( opr == '*' )
return 3 ;
else if ( opr == '/' )
return 4 ;
else
return -1 ;
}
double performOperation ( int operand1 , int operand2 , int opcode ){
double result = 0 ;
if ( opcode == 1 ){
result = operand1 + operand2 ;
} else if ( opcode == 2 ){
result = operand1 - operand2 ;
} else if ( opcode == 3 ){
result = operand1 * operand2 ;
} else if ( opcode == 4 ){
result = operand1 / operand2 ;
}
return result ;
}
extern double evaluator ( char expr[] , int &from )
{
double result = 0 , temp = 0 ;
int i = 0 , opcode = 0 , dec = -1 , j = 0 , ten = 1 ;
bool readOnce = false ;
++from ;
while ( expr[from] != '\0' ){
if ( expr[from] == '(' ){
if ( !opcode ){
result = evaluator ( expr , from ) ;
} else if ( opcode == 1 ){
result += evaluator ( expr , from ) ;
} else if ( opcode == 2 ){
result -= evaluator ( expr , from ) ;
} else if ( opcode == 3 ){
result *= evaluator ( expr , from ) ;
} else if ( opcode == 4 ){
result /= evaluator ( expr , from ) ;
}
} else if ( isdigit(expr[from]) ){
temp = getOperand ( expr , from ) ;
if ( !result && !readOnce ){
result = temp ;
readOnce = true ;
}
if ( opcode ){
readOnce = true ;
result = performOperation ( result , temp , opcode ) ;
}
} else if ( isOperator(expr[from]) ){
opcode = setOpCode ( expr[from] ) ;
} else if ( expr[from] == ')' ){
return result ;
}
++from ;
}
return result ;
}