-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.c
More file actions
370 lines (349 loc) · 8.71 KB
/
calculator.c
File metadata and controls
370 lines (349 loc) · 8.71 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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#define pi 3.148
void itoa(int, char *, int);
void reverse ( char *);
void dtoa ( double, char *);
char * ftoa ( double c );
char * remspace ( void );
char * strch ( char *s, int ch1, int ch2);
char * cal ( char s[] );
char * insert ( char *s , char * p );
double caltrig ( char s[] );
int strptr ( char * s, char * p);
char * insertrig( char *,char *, int);
int charcount(char * , char);
char * cal ( char s[] )
{ //printf("cin\n");
double opr[1]; int i = 0,j = 0; double res; int len = strlen (s); char k[20]; int dup =0 ;double frac[1];
//printf("%s\n",s);
char *p;
p = (char *) malloc(len + 1);
while(s[i]!='\0')
{
if(s[i] != '(' && s[i] != ')' && s[i] !=' ') k[j++] = s[i];i++;
} k[j] = '\0'; i = 0;
strcpy(s,k); j = 0;
while ( s[i] != '\0')
{
if(islower(s[i])) { j = 1; break;}i++;
} //printf("j -- %d\n",j);
if( j )
{
res = caltrig(s);
p = ftoa (res);
strcpy(s,p);//printf("cout\n");
return s;
}i = 0;
while(s[i] != '\0')
{
switch (s[i])
{
case '+': case '*' : case '/': case '%': dup = i; break;
}
if(dup > 0) break;
if(s[i] == '-') dup = i;//printf("dup -- %d\n", dup);
i++;
}//printf("%c",*(s+dup));
opr[0] = atof ( s ); char * first_operator = ftoa ( opr[0] ); i = strlen (first_operator );
opr[1] = atof ( s + dup + 1 );//printf("s -- %s %c\n",s, *(s+dup));
switch(*(s+dup))
{
case '+' : res = (double)opr[0] + opr[1]; break;
case '-' : res = (double)opr[0] - opr[1]; break;
case '*' : res = (double)(double)opr[0] * opr[1]; break;
case '/' : if(opr[1] == 0.0) { printf("Error"); break ;}
else res =(double) opr[0] / opr[1]; break;
case '%' : if(!modf(opr[0] , &frac[0]) && !modf(opr[1],&frac[1]) && (opr[1] != 0.0))
res = (int)opr[0] % (int)opr[1]; break;
} //printf("op1 -- %g\t %c op2 -- %g\n",opr[0],*(s+dup), opr[1]);
p = ftoa( res );
strcpy(s,p);
//printf("cout\n");
return s;
}
double caltrig ( char s[] )
{ //printf("trin\n");
int si = 0; si = strptr(s,"sin");
int ta = 0; ta = strptr(s, "tan");
int co = 0; co = strptr(s, "cos");
// printf("s--%s %c\n",s,*(s+si));
double res = 0.0;
if(si) res = (double) sin(atof(s + si));
if(ta) res = (double) tan(atof(s + ta));
if(co) res = (double) cos(atof(s + co));
//printf("res--%g\n",res);
//printf("triout\n");
return res;
}
int strptr ( char * s, char * p)
{
int i = 0;
if(strstr(s,p))
{
while(s[i] != '\0')
{
if(*(s + i) == *p) return i + strlen ( p );
i ++ ;
}
}i = 0;
return i;
}
char * ftoa ( double c )//converts a floating point number to a string
{
int i = 0 , sign; char * in ; char *fra;
if(( c ) < 0) { c = -c; sign = 0; } else sign = 1;
double inte; char *k;
double fr = modf(c, &inte);
in = (char *) malloc ( 21 );
fra = (char *) malloc ( 6 );
if(fr * 10 != fr){dtoa ( fr , fra);
itoa ( inte , in ,sign);
char c1[] = ".";
in = strcat(in,c1);
k = strcat(in,fra);
}
else {itoa ( inte , in, sign ); k = in;
}
return k;
}
void dtoa( double n, char s[])//converts the decimal part of float to string
{
int i = 0; long int pre;double p;
char temp2[10]; strcpy (temp2 , s);
while(i<5)
{
n = n * 10;
i++;
}
modf(n,&p);
int k = (int)n % 10;
if (k >= 5)
{
p = p + 10 - k;//printf("%g\n",p);
n = p;
}
else
{
p = p - k;
n = p;
}
pre = (long int)n;
//printf("%ld\n",pre);
while(pre % 10 == 0)
{
pre /= 10;i--;
}
itoa(pre,temp2,1);
int j = 0;char temp[i];
if(pre%10 == pre)
{ while(j<i-1) temp[j++] = '0';temp[j] = '\0';
strcat(temp,temp2);
strcpy(s,temp);}
else strcpy (s , temp2);
}
void itoa (int n, char s[], int sign) // converts integer to string
{ int i = 0;
do{
s[i++] = n % 10 + '0';
} while((n /= 10) > 0);if(!sign) s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void reverse (char s[] ) // reverses the string
{
int c, i, j;
for(i = 0 , j = strlen(s) - 1 ;i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
char * strch ( char *s, int ch1, int ch2) // to take the string and finds the innermost parentheses
{ //printf("sin\n"); printf("sin -- %s %c\n", s , *(s + 2));
char *p;int i = 0, j = 0, ptr1 = 0, ptr2 = 0;
//p = NULL;
p = (char *) malloc ( 21 );
int si = 0; si = strptr(s,"sin");
int ta = 0; ta = strptr(s, "tan");
int co = 0; co = strptr(s, "cos");
double ko = 0.0; char *num;int k = 0;
num = (char *) malloc(15);
int tri = 0;
if(si) tri = si;
if(co) tri = co;
if(ta) tri = ta;
//printf("tri -- %d\n", tri);
ptr1 = 0; ptr2 = 0;
while ( *s != '\0')
{ s++; i++;
if(*s == ch1 ) ptr1 = i;
if(*s == ch2 ) { ptr2 = i; break; }
}s = s - i; i = 0;// printf("ptr -- %d %d\n",ptr1,ptr2);
if(ptr1 >= 0 && ptr2 > 0 && ( tri <= ptr1 || tri > ptr2 ))
{
for(i=0; ptr1 != ptr2 - 1 ;i++) {*(p+i) = *(s+(ptr1++)+1);} *(p+i) = '\0';
return (char * ) p;
}
if ( ptr1 == 0 && ptr2 == 0 && tri == 0) {strcpy (p,s); return (char*) p; }
if(tri)
{ ptr1 = tri - 3;
ko = atof(s + tri);
num = ftoa(ko);
k = strlen(num);ptr2 = tri + k + 1;
} //printf("%d %d\n",ptr1,ptr2);
if(tri)
{
if(ptr1 >= 0 && ptr2 > 0)
{
for(i=0; ptr1 != ptr2 - 1 ;i++) {*(p+i) = *(s+(ptr1++));} *(p+i) = '\0';
//printf("tr -- %s\n",p);
return (char * ) p;
}
}
//printf("%s\n",s);
//printf("sout\n");
return (char *) p;
}
char * insert ( char *s , char * p )//insert the evaluated expression into parent string again
{ //printf("iin\n");
int ptr1 = 0 , ptr2 = 0 ,i = 0 , count = 0;
int cochar = charcount(s,'(');
int si = 0; si = strptr(s,"sin");
int ta = 0; ta = strptr(s, "tan");
int co = 0; co = strptr(s, "cos");
double ko = 0.0; char *num;int k = 0;
num = (char *) malloc(15);
int tri = 0;
if(si) tri = si;
if(co) tri = co;
if(ta) tri = ta;
//printf("tri -- %d\n", tri);
//printf("tr--ptr1-%d -- ptr2-%d\n",ptr1,ptr2);
ptr1 = 0; ptr2 = 0;
while(*s != '\0')
{
s++; i++;
if(*s == '(' ) ptr1 = i;
if(*s == ')' ) { ptr2 = i; break; }
} s = s - i;
// printf("pa--ptr1-%d -- ptr2-%d\n",ptr1,ptr2);
char temp2[10]; char temp1[100];
strcpy(temp2, p); strcpy(temp1, s);
if( ptr1 == 0 && ptr2 == 0 && (!tri || !cochar)) strcpy(s,p);
else if( ptr1 > 0 && ptr2 > 0&& (tri <= ptr1 || tri > ptr2))
{
int j = strlen (temp2);
for(i = ptr1 ;i <= ptr2 ; i++)
{
temp1[i] = ' ';
}
for( i = ptr1 ; i < j + ptr1 ; i++)
{
temp1[i] = temp2[count++];
}
i = 0; j = strlen (s); count = 0;
char temp3[j+1];
while(temp1[i] != '\0')
{
if( i <= ptr1 ) temp3[count++] = temp1[i];
if ( i > ptr1 && i < ptr2 && temp1[i] != ' ') temp3[count++] = temp1[i];
if( i >= ptr2 ) temp3[count++] = temp1[i];
i++;
}temp3[count] = '\0';
strcpy(s, temp3);return (char *) s;
}
if(tri)
{ ptr1 = tri - 3;
ko = atof(s + tri);
num = ftoa(ko);
k = strlen(num);ptr2 = tri + k ;
}
if(tri)
{
s = insertrig(s,p,tri);
return (char *)s;
}
//printf("iout\n");
return (char *) s;
}
char * insertrig ( char * s, char * p, int trig) // replaces the trignometric names with their values
{
int len = 0,i = 0;
len = strlen(s);
char temp1[100];
//temp1 = (char * ) malloc( len + 5 );
while( *s != '\0' )
{
temp1[i++] = *s++;
if(i == trig - 3) break;
} s = s + 3;
double res = 0.0;
res = atof ( s );
char * k = ftoa ( res );
int klen = strlen(k);
while ( *p != '\0')
{
temp1[i++] = *p++;
}s = s + klen;
while ( *s != '\0')
{
temp1[i++] = *s++;
} temp1[i] = '\0';
//printf("temp1 -- %s\n",temp1);
len = strlen(temp1);
s = (char *) malloc(len + 1);
strcpy(s, temp1);
return s;
}
int charcount ( char *s, char ch)//counts the number of a particular character
{
int count = 0;
while(*s != '\0')
{
if(*s == ch ) count++;
s++;
}
return count;
}
int oper ( char s[]) // counts the no. of operators
{ int len = strlen(s); int i = 0, count = 0, dup = 0;
while(s[i] != '\0')
{
switch(s[i])
{
case '+' : case '*': case '/': case '%': count++; break;
case '-' : count++;if(s[i+1] !=' ') count--; break;
}i++;
}i = 0;//printf("l -- %d\n",count);
while(s[i] != '\0')
{
if(islower(s[i])) {dup++; count++;} ;
i++;
} //printf("dup -- %d\n",dup);
if(dup){dup = dup/3; dup *=2; count -= dup;}
//printf("l -- %d\n",count);
return count;
}
int main()
{
char s [ 100 ];
gets(s);
int l = oper(s);
int i = 0;//printf("l -- %d\n",l);
while( l >= 0 )
{
char * inn = strch ( s, '(',')');
cal ( inn ) ;
if(l == 1){strcpy(s,inn); break; }
char * k = insert ( s , inn );
strcpy (s,k);
l--;//printf("\n%s\n", s);
}
printf("%s", s);
return 0;
}