-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEquation parser.c
376 lines (365 loc) · 9.55 KB
/
Equation parser.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define input_size 200
#define PI 3.14159
char token_eqn[200][201];
int find_target(char str[],int start,int serial,int mode)
{
// mode 1 finds the end of brackets.
// mode 2 finds the starting of brackets.
int pos=-1,i,len,serial_temp=serial;
len=strlen(str);
for(i=start;i<len && mode==1;i++)
{
if(str[i]==')' && serial_temp==serial)
{
pos=i-1;
break;
}
else if(str[i]=='(')
serial_temp++;
else if(str[i]==')')
serial_temp--;
}
for(i=start;i<len && mode==2;i++)
{
if(str[i]=='(')
{
pos=i-2;
break;
}
}
return pos;
}
void n_strcpy(char from_str[],char to_str[],int pos1,int pos2)
{
int i,j;
for(i=pos1,j=0;i<=pos2;i++,j++)
to_str[j]=from_str[i];
to_str[j]='\0';
}
void leftShift_num(double array[],int len,int start_pos)
{
int i;
for(i=start_pos;i<len-1;i++)//the element of the starting pos will be erased
array[i]=array[i+1];
array[len-1]=0;
//return len-1;
}
int leftShift_str(char array[],int len,int start_pos)
{
int i;
for(i=start_pos;i<len-1;i++)//the element of the starting pos will be erased
array[i]=array[i+1];
array[len-1]='\0';
return len-1;
}
double ultimate_value(char ch, double val1,double val2)
{
if(ch=='+')
return val1+val2;
if(ch=='-')
return val1-val2;
if(ch=='*')
return val1*val2;
if(ch=='/')
return val1/val2;
if(ch=='^')
return pow(val1,val2);
}
int count_sign(char str1[],char sign[])
{
int i,count=0,len,depth=0;
len=strlen(str1);
for(i=0;i<len;i++)
{
if(str1[i]=='(')
depth++;
else if(str1[i]==')')
depth--;
else if((str1[i]=='+' || str1[i]=='-' || str1[i]=='*' || str1[i]=='/' || str1[i]=='^') && depth==0)
{
sign[count]=str1[i];
count++;
}
}
return count;
}
double ff(char str[],double x, double y)
{
if(strcmp(str,"x")==0)
return x;
if(strcmp(str,"y")==0)
return y;
return atof(str);
}
int strTok(char str1[],char str2[])//returns how many substrings are there
{
int i,j,len1,len2,len3=0,flag1=0,count=0;
len1=strlen(str1);
len2=strlen(str2);
for(i=0;i<=len1;i++)
{
flag1=0;
for(j=0;j<len2;j++)
{
if(str1[i]==str2[j])
flag1=1;
}
if(flag1==0)
{
token_eqn[count][len3]=str1[i];
len3++;
}
else
{
token_eqn[count][len3]='\0';
count++;
len3=0;
}
}
return count+1;
}
double f2(double temp[],char sign[],int num_signs)
{
int found_pow=1,found_mult=1,found_div=1,i=0;
while(found_mult==1 || found_div==1 || found_pow==1)
{
for(i=0;i<num_signs;i++)
{
if(sign[i]=='^')
{
found_pow=1;
temp[i]=ultimate_value('^',temp[i],temp[i+1]);
leftShift_num(temp,num_signs+1,i+1);
leftShift_str(sign,num_signs,i);
num_signs--;
}
}
for(i=0;i<num_signs;i++)
{
if(sign[i]=='*')
{
found_mult=1;
temp[i]=ultimate_value('*',temp[i],temp[i+1]);
leftShift_num(temp,num_signs+1,i+1);
leftShift_str(sign,num_signs,i);
num_signs--;
}
}
for(i=0;i<num_signs;i++)
{
if(sign[i]=='/')
{
found_div=1;
temp[i]=ultimate_value('/',temp[i],temp[i+1]);
leftShift_num(temp,num_signs+1,i+1);
leftShift_str(sign,num_signs,i);
num_signs--;
}
}
found_mult=0;
found_div=0;
found_pow=0;
for(i=0;i<num_signs;i++)
{
if(sign[i]=='^')
{
found_pow=1;
break;
}
if(sign[i]=='*')
{
found_mult=1;
break;
}
if(sign[i]=='/')
{
found_div=1;
break;
}
}
}
while(num_signs>0)
{
temp[0]=ultimate_value(sign[0],temp[0],temp[1]);
leftShift_num(temp,num_signs+1,1);
leftShift_str(sign,num_signs,0);
num_signs--;
}
return temp[0];
}
/*double f1(char str[],double x,double y)
{
double temp[input_size];
int num_signs,i;
char sign_temp[input_size];
num_signs=count_sign(str,sign_temp);
strTok(str,"+-/*^");
for(i=0;i<=num_signs;i++)
temp[i]=ff(token_eqn[i],x,y);
return f2(temp,sign_temp,num_signs);
}
*/
//#define PI 3.14159265
double f3(char str[],double x,double y)
{
int len,i,start=-1,end=-2,count1=0,count2=0,sign_num,k;
double temp[input_size],argument=0;
char sign[input_size],str_temp[input_size];
len=strlen(str);
// sign_num=count_sign(str,)
for(i=0;i<len;i++)
{
if(str[i]=='(')
{
start=i+1;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
temp[count1]=f3(str_temp,x,y);
count1++;
i=end+1;
}
else if(str[i]=='+' ||str[i]=='-' ||str[i]=='*' ||str[i]=='/' ||str[i]=='^')
{
sign[count2]=str[i];
count2++;
}
else if(str[i]=='s' && str[i+1]=='i' && str[i+2]=='n')
{
start=i+4;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=sin(argument*PI/180.0);
count1++;
i=end+1;
}
else if(str[i]=='c' && str[i+1]=='o' && str[i+2]=='s' && str[i+3]!='e')
{
start=i+4;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=cos(argument*PI/180.0);
count1++;
i=end+1;
}
else if(str[i]=='t' && str[i+1]=='a' && str[i+2]=='n')
{
start=i+4;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=tan(argument*PI/180.0);
count1++;
i=end+1;
}
else if(str[i]=='c' && str[i+1]=='o' && str[i+2]=='t')
{
start=i+4;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=(cos(argument*PI/180.0)/sin(argument*PI/180.0));
count1++;
i=end+1;
}
else if(str[i]=='s' && str[i+1]=='e' && str[i+2]=='c')
{
start=i+4;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=(1.0/cos(argument*PI/180.0));
count1++;
i=end+1;
}
else if(str[i]=='c' && str[i+1]=='o' && str[i+2]=='s' && str[i+3]=='e' && str[i+4]=='c')
{
start=i+6;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=(1.0/sin(argument*PI/180.0));
count1++;
i=end+1;
}
else if(str[i]=='l' && str[i+1]=='o' && str[i+2]=='g')
{
start=i+4;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=log10(argument);
count1++;
i=end+1;
}
else if(str[i]=='l' && str[i+1]=='n')
{
start=i+3;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=log(argument);
count1++;
i=end+1;
}
else if(str[i]=='e' && str[i+1]=='^')
{
start=i+3;
end=find_target(str,start,0,1);
n_strcpy(str,str_temp,start,end);
argument=f3(str_temp,x,y);
temp[count1]=exp(argument);
count1++;
i=end+1;
}
else
{
k=0;
while(str[i]!='(' && str[i]!=')' && str[i]!='+' && str[i]!='-' && str[i]!='*' && str[i]!='/' && str[i]!='^' && i<len)
{
str_temp[k]=str[i];
k++;
i++;
}
str_temp[k]='\0';
temp[count1]=ff(str_temp,x,y);
count1++;
i--;
}
}
sign[count2]='\0';
return f2(temp,sign,count2);
}
int main()
{
char str1[input_size], str[input_size];
printf("Enter the expression : ");
scanf("%s",str1);
int i;
if(str1[0] == '-')
{
str[0] = '0';
for(i=0;str1[i]!='\0';i++)
str[i+1] = str1[i];
str[i+1] = '\0';
}
else
strcpy(str, str1);
double x, y;
printf("Set the value of x and y in f(x, y). (0 if they are absent).\n");
printf("Enter the value of x : ");
scanf("%lf", &x);
printf("Enter the value of y : ");
scanf("%lf", &y);
double ans;
ans=f3(str,x,y); //set value of x, y in f(x, y).
printf("Value : %lf\n",ans);
return 0;
}
//(sin(x)-x^5)*x-tan(x/x^2);
//(x^2+(x^5*cos(3*x+x)^2))^.5)/(2*sin(x))