-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
202 lines (185 loc) · 3.98 KB
/
main.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef enum
{
PLUS,
MINUS,
MULTIPLICATION,
DEVIDE,
EXPONENTE,
EQUAL,
OPEN_PARANTES,
CLOSE_PARANTES,
}operatorType_e;
typedef struct _opertator
{
char operation;
char *Address;
}operator;
int calc(char *);
void findOperation(char *str, operator *op);
void findValue(char *str, int *value);
int _pow(int base, int exponent);
void main()
{
char string[100];
int result;
printf("write your math porblem:");
scanf("%s", string);
result = calc(string);
printf("\r\n%d", result);
return;
}
//(12+14(17-3)2-2)/2=
//(12+14*(17-3)*2-2)/2=
int calc(char * str)
{
char *dumm_buff;
int buff=0;
operator op;
int firstValue;
int afterValue;
findOperation(str, &op);
findValue(str, &firstValue);
while( op.operation == EXPONENTE)
{
findValue(op.Address+1, &afterValue);
firstValue = _pow(firstValue, afterValue);
str = op.Address+1;
memset(&op, 0, sizeof(operator));
findOperation(str, &op);
}
while( (op.operation == MULTIPLICATION) || (op.operation == DEVIDE))
{
switch (op.operation)
{
case MULTIPLICATION:
findValue(op.Address+1, &afterValue);
firstValue = firstValue * afterValue;
str = op.Address+1;
memset(&op, 0, sizeof(operator));
findOperation(str, &op);
break;
case DEVIDE:
findValue(op.Address+1, &afterValue);
firstValue = firstValue / afterValue;
str = op.Address+1;
memset(&op, 0, sizeof(operator));
findOperation(str, &op);
default:
break;
}
}
switch (op.operation)
{
case PLUS:
buff = calc(op.Address + 1);
return firstValue + buff;
break;
case MINUS:
buff = calc(op.Address + 1);
return firstValue - buff;
break;
case CLOSE_PARANTES:
return firstValue;
break;
case EQUAL:
return firstValue;
break;
default:
break;
}
}
void findOperation(char *str, operator *op)
{
int start;
int end;
int parantes=0;
if( *str == '(')
{
parantes++;
str++;
while ( parantes != 0)
{
if (*str == '(')
{
parantes++;
}
if (*str == ')')
{
parantes--;
}
str++;
}
if ( isdigit(*str))
{
str--;
op->Address = str;
op->operation = MULTIPLICATION;
return;
}
}
else
{
while ( isdigit(*str))
{
str++;
}
}
op->Address = str;
switch (*str)
{
case '+':
op->operation = PLUS;
break;
case '-':
op->operation = MINUS;
break;
case '*':
op->operation = MULTIPLICATION;
break;
case '/':
op->operation = DEVIDE;
break;
case '(':
op->operation = MULTIPLICATION;
--op->Address;
break;
case ')':
op->operation = CLOSE_PARANTES;
break;
case '=':
op->operation = EQUAL;
break;
case '^':
op->operation = EXPONENTE ;
break;
case '\0':
op->operation = EQUAL;
break;
default:
break;
}
}
void findValue(char *str, int *value)
{
if( *str == '(')
{
*value = calc( str+1 );
}
else
{
*value = atoi(str);
}
}
int _pow(int base, int exponent)
{
if (base == 0)
return 0;
if( exponent == 0)
return 1;
return _pow(base,exponent-1) * base;
}