-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_browser.cpp
More file actions
56 lines (47 loc) · 1.63 KB
/
test_browser.cpp
File metadata and controls
56 lines (47 loc) · 1.63 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
#include<stdio.h>
#include<string.h>
#include<ctype.h>
using namespace std;
// A utility function to check if a given character is operand
bool isOperand(char c) { return (c >= '0' && c <= '9'); }
// utility function to find value of and operand i.e '1' = 49 so 49-48 = 1(value)
int value(char c) { return (c - '0'); }
// This function evaluates simple expressions. It returns -1 if the
// given expression is invalid.
int evaluate(char *exp)
{
int res;
// Base Case: Given expression is empty
if (*exp == '\0') return -1;
// The first character must be an operand, find its value
res = value(exp[0]);
// Traverse the remaining characters in pairs
for (int i = 1; exp[i]; i += 2)
{
// The next character must be an operator, and
// next to next an operand
char opr = exp[i], opd = exp[i+1];
// If next to next character is not an operand
if (!isOperand(opd)) return -1;
// If next to next character is not an operand
if (!isOperand(opd)) return -1;
// Update result according to the operator
if (opr == '+') res += value(opd);
else if (opr == '-') res -= value(opd);
else if (opr == '*') res *= value(opd);
else if (opr == '/') res /= value(opd);
// If not a valid operator
else
return -1;
}
return res;
}
int main(int argc , char *argv[])
{
char expr1[50];
strcpy(expr1,argv[1]);
int res = evaluate(expr1);
(res == -1)? printf("%s is Invalid\n",expr1):
printf("Value of %d is\n",res);
return 0;
}