-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalco.c
61 lines (56 loc) · 1.58 KB
/
calco.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//need to modify
float out_num(float nums[],char opr[],int k){
char opt[4] = {'/', '*', '-', '+'};
int opIndex[4] = {0};
int opCount[4] = {0};
for (int j = 0; j < k; j++) {
for (int i = 0; i < 4; i++) {
if (opr[j] == opt[i]) {
opCount[i]++;
opIndex[i] = j;
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < opCount[i]; j++) {
int index = opIndex[i] - j;
if (opt[i] == '/') {
nums[index] = nums[index] / nums[index + 1];
nums[index + 1] = 0;
} else if (opt[i] == '*') {
nums[index] = nums[index] * nums[index + 1];
nums[index + 1] = 0;
} else if (opt[i] == '-') {
nums[index] = nums[index] - nums[index + 1];
nums[index + 1] = 0;
} else if (opt[i] == '+') {
nums[index] = nums[index] + nums[index + 1];
nums[index + 1] = 0;
}
}
}
return nums[0];
}
void equle(char *str,float *r){
char opr[100];//may change the number
float nums[100];//may change the number
//maybe shuold use dynmic array or linked list for nums
char strn[100] = "\0";//may change the number
int k = 0;
for(int i = 0;i<strlen(str);i++){
if(str[i]<=47){
opr[k] = str[i];
k++;
strn[0] = '\0';
}
else{
sprintf(strn+strlen(strn),"%c",str[i]);
nums[k] = atof(strn);
//printf("%f , %s\n",nums[k],strn);
}
}
*r = out_num(nums,opr,k);
}