-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_calculator.c
187 lines (172 loc) · 4.44 KB
/
arithmetic_calculator.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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
typedef struct
{
int iitem[MAX]; //integer stack
int itop; //integer stack's top element
char citem[MAX]; //char stack
int ctop; //char stack's top element
}
STACK;
// flag shows that if the element that going to push is char or int
void push(int x, char y, STACK *s, int flag)
{
if (flag == 0) // if it is int
{
s->iitem[s->itop] = x;
s->itop++;
}
else //if it is char
{
s->citem[s->ctop] = y;
s->ctop++;
}
}
//it pops integer element from integer stack
int intPop(STACK *s)
{
return s->iitem[--s->itop];
}
//it pops char element from char stack
char charPop(STACK *s)
{
return s->citem[--s->ctop];
}
//it shows the peek element of the char stack
int peek(STACK *s)
{
return s->citem[s->ctop - 1];
}
//it makes calculation in order to sign of the operation
int compute(int num1, int num2, char sign)
{
if (sign == '+')
return num1 + num2;
else if (sign == '-')
return num1 - num2;
else if (sign == '*')
return num1 * num2;
else
return num1 / num2;
}
//it prints the elements in the stack
void stackPrinter(STACK *s)
{
printf("OPERAND STACK : ");
for (int i = 0; i<s->itop; i++)
{
printf("%i ", s->iitem[i]);
}
printf("\n");
printf("OPERATOR STACK : ");
for (int i = 0; i<s->ctop; i++)
{
printf("%c ", s->citem[i]);
}
printf("\n");
}
//it pushes two number from operand stack and makes calculation
void pushAndCalculate( STACK *s, char sign)
{
int num2 = intPop(s);
int num1 = intPop(s);
push(compute(num1, num2, sign), 0, s, 0);
stackPrinter(s);
}
//it converts the numbers in char array to integer
int strToInt (char str[])
{
int num = 0;
int i = 0;
while(i < strlen(str)) {
num += pow(10, strlen(str)-1-i)*(str[i] - '0');
i++;
}
return num;
}
int main(int argc, char **argv)
{
char str[MAX], num[MAX], sign;
int i = 0, j = 0;
STACK stack;
stack.itop = 0;
stack.ctop = 0;
printf("___________ARITHMETIC CALCULATOR__________\n");
printf("Insert the operation that you want to calculate : ");
scanf("%[^\n]s ", str);
// string tokenizer
while(i < strlen(str))
{
if ((str[i]>='0') && (str[i]<='9'))
{
while ( (str[i]>='0') && (str[i]<='9') )
{
num[j] = str[i];
i++;
j++;
}
num[j] = '\0';
push(strToInt(num), 0, &stack, 0);
stackPrinter(&stack);
j = 0;
num[0] = '\0';
}
else if (str[i] == ' ')
{
i++;
}
else if ( str[i] == ')')
{
sign = charPop(&stack);
stackPrinter(&stack);
// it push values until '(' sign comes
while (sign != '(')
{
pushAndCalculate(&stack, sign);
sign = charPop(&stack);
stackPrinter(&stack);
}
i++;
}
else if (str[i] == '(')
{
push(0 ,str[i], &stack, 1);
stackPrinter(&stack);
i++;
}
else if ( (str[i] == '*') || (str[i] == '/') )
{
if ( (peek(&stack) == '*') || (peek(&stack) == '/') )
{ // priority operator control
sign = charPop(&stack);
stackPrinter(&stack);
pushAndCalculate(&stack, sign);
}
push(0, str[i], &stack, 1);
stackPrinter(&stack);
i++;
}
else if ( (str[i] == '+') || (str[i] == '-') )
{
if ( (peek(&stack) == '*') || (peek(&stack) == '/') || (peek(&stack) == '+') || (peek(&stack) == '-'))
{ //priority operand control
sign = charPop(&stack);
stackPrinter(&stack);
pushAndCalculate(&stack, sign);
}
push(0, str[i], &stack, 1);
stackPrinter(&stack);
i++;
}
}
while(peek(&stack))
{ //after the string reading the elements remaining elements from stack pops
sign = charPop(&stack);
stackPrinter(&stack);
pushAndCalculate(&stack, sign);
}
printf("Operation result : %d \n", stack.iitem[0]);
}