-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expr.h
75 lines (68 loc) · 1.62 KB
/
Expr.h
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
#ifndef _EXPR_H
#define _EXPR_H
#include <math.h>
#include "Varible.h"
#define EXPL (100)
int opt[EXPL];
floatT arg[EXPL];//store the value of parameter
floatT *adr[EXPL];//store the address of the variable
//get the id of an operator
int optid(char opt)
{
int id=-1;
switch (opt)//turn the operators into program internal coding so as to determine the priority
{
case '=': id=0x11; break;
case '+': id=0x21; break;
case '-': id=0x22; break;
case '*': id=0x31; break;
case '/': id=0x32; break;
case '^': id=0x41; break;
case '(': id=0x01; break;
case ')': id=0x02; break;
}
return id;
}
//get the priority of an operator
int optpri(int opt)
{
return opt>=0?(opt>>4):opt;
}
//calculation
//'=' is considered as a special operator
floatT cal(floatT *x1,floatT x2,int opt)
{
switch (opt)
{
case 0x21: return *x1+x2;
case 0x22: return *x1-x2;
case 0x31: return *x1*x2;
case 0x32: return *x1/x2;
case 0x41: return pow(*x1,x2);
case 0x11: return (*x1)=x2;
}
return 0;
}
//calculate reverse Polish expression
floatT calexpr(floatT *arg,int *opt,floatT **adr)
{
int t=0;
for (int i=0;opt[i]>=-2;++i)
if (opt[i]<0)
{
arg[t]=arg[i],opt[t]=opt[i];
if (opt[i]==-2) adr[t]=adr[i];
++t;
}
else
{
--t;
if (opt[t-1]>=-1)
arg[t-1]=cal(&arg[t-1],arg[t],opt[i]);
else
arg[t-1]=cal(adr[t-1],arg[t],opt[i]);
opt[t-1]=-1;
}
return arg[0];
}
#endif