-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-10.c
117 lines (108 loc) · 2.29 KB
/
5-10.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTACK 1024
#define NUMBER '0'
#define ERROR_OK 0
#define ERROR_DIV_ZERO 1
#define ERROR_UNKNOWN_OPERATOR 2
double stack[MAXSTACK];
int stackp = 0;
int calc(double* val, char op, double left, double right);
char getop(char* op, double* val);
int push(double val);
int pop(double* val);
int main(int argc, char* argv[])
{
if (argc < 2)
printf("\tUsage: %s argv", *argv);
else
{
while (++argv)
{
double val = 0;
char op = getop(argv, &val);
if (op != NUMBER)
{
double rights, lefts;
if (!pop(&rights) || !pop(&lefts))
{
printf("[ERROR] 操作数过少。\n");
return 0.0;
}
double val;
int ret = calc(&val, op, lefts, rights);
switch (ret)
{
case ERROR_DIV_ZERO:
printf("[ERROR] 除数不可为0。\n");
return 0;
case ERROR_UNKNOWN_OPERATOR:
printf("[ERROR] 未知的运算符 %c。\n", op);
return 0;
}
if (!push(val))
{
printf("[ERROR] 表达式过于复杂。\n");
return 0;
}
}
else if (!push(val))
{
printf("[ERROR] 表达式过于复杂。\n");
return 0;
}
}
double val;
pop(&val);
printf("%lf\n", val);
}
return 0;
}
int calc(double* val, char op, double left, double right)
{
switch (op)
{
case '+':
*val = left + right;
return ERROR_OK;
case '-':
*val = left - right;
return ERROR_OK;
case '*':
*val = left * right;
return ERROR_OK;
case '/':
if (right == 0)
return ERROR_DIV_ZERO;
*val = left / right;
return ERROR_OK;
default:
return ERROR_UNKNOWN_OPERATOR;
}
}
char getop(char* op, double* val)
{
if (op == NULL)
return EOF;
int len = strlen(op);
if (len < 0)
return EOF;
if (!isdigit(*op))
return *op;
*val = atof(op);
return NUMBER;
}
int push(double val)
{
if (stackp >= MAXSTACK) return 0;
stack[stackp++] = val;
return 1;
}
int pop(double* val)
{
if (stackp <= 0) return 0;
*val = stack[--stackp];
return 1;
}