-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressionevaluator.c
185 lines (182 loc) · 3.02 KB
/
expressionevaluator.c
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
183
184
185
#include<stdio.h>
void input();
void addition();
void subtraction();
void multiplication();
void division();
void balancing_symbols();
int checkexpression(char temp1, char temp2);
int *input_array;
char stack[100];
int size,i,top=-1,validity,flag=0,temp;
int result=0,value=1,x,c=1;
int main()
{
int choice;
char exp[100];
while(1)
{
printf("\n---Calculator---\n");
printf("1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. DIVISION\n5. EXPRESSSION MODE\n6. EXIT\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
addition();
break;
}
case 2:
{
subtraction();
break;
}
case 3:
{
multiplication();
break;
}
case 4:
{
division();
break;
}
case 5:
{
printf("\nEXPRESSION MODE ON\n");
balancing_symbols();
//(x==1)?printf("Valid\n"):printf("Not valid\n");
}
case 6:
{
exit(1);
}
default:
{
printf("Enter the valid Choice\n");
break;
}
}
}
}
void input()
{
printf("Enter Number of Operands\t");
scanf("%d",&size);
if(size==1)
{
printf("Can't perform operation with 1 Operand\n");
exit(1);
}
else
{
input_array=(int *)malloc(size*sizeof(int));
printf("Enter the %d operands\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&input_array[i]);
}
}
}
void addition()
{
input();
for(i=0;i<size;i++)
{
result+=input_array[i];
}
free(input_array);
printf("\n\n|The result of addition of %d numbers are %d|\n",size,result);
}
void subtraction()
{
input();
result=input_array[0];
for(i=1;i<size;i++)
{
result-=input_array[i];
}
free(input_array);
printf("\n\n | The result of Subtraction of %d numbers are %d|\n",size,result);
}
void multiplication()
{
input();
for(i=0;i<size;i++)
{
value*=input_array[i];
}
free(input_array);
printf("\n\n|The result of multiplication of %d numbers are %d|\n",size,value);
}
void division()
{
input();
value=input_array[0];
for(i=1;i<size;i++)
{
value/=input_array[i];
}
free(input_array);
printf("\n\n | The result of division of %d numbers are %d|\n",size,value);
}
void balancing_symbols()
{
char exp[100];
printf("Enter your expression\t");
scanf("%s",exp);
for(i=0;exp[i]!='\0';i++)
{
if(exp[i]=='{' || exp[i]=='[' || exp[i]=='(')
{
flag=1;
top++;
stack[top]=exp[i];
}
else if(exp[i]=='}' || exp[i]==')' || exp[i]==']')
{
flag=1;
temp=top;
top--;
validity=checkexpression(exp[temp],exp[i]);
if(validity==1)
{
continue;
}
else
{
printf("Please, Enter the valid expression !");
exit(1);
}
}
else
c=0;
}
if(flag==1)
{
if(top==-1)
{
printf("Valid");
//function call to evaluation
}
else
printf("Please, Enter the valid expression !");
exit(1);
}
else if(c==0 && top==-1)
{
printf("Valid");
//function call to evaluation
}
}
int checkexpression(char temp1, char temp2)
{
if(temp1=='(' && temp2==')')
return 1;
else if(temp1=='[' && temp2==']')
return 1;
else if(temp1 =='{' && temp2=='}')
return 1;
else
return 0;
}