求达人用C语言编写一个带括号的四则运算

2024-11-16 09:00:20
推荐回答(1个)
回答(1):

#include
#include
#include

char* next;

double opd[1000];
int opt;
double *popd = opd;
#define PUSH(n) { *popd++ = n; }
#define POP() (*--popd)
#define TOP() (*(popd-1))
#define EMPTY() (popd == opd)
#define SIZE() (popd - opd)

void fail(char* s)
{
printf("match '%s'余皮 failed near '%c'.\n", s, *--next);
exit(0);
}

#define lbr() { if(*next++ == '(') ; else fail("("); }
#define rbr() { if(*next++ == ')') ; else fail(")"); }
void term();
void lv1();
void lv2();
void cal(char);

void expr()
{
term(); lv1();
}

void term()
{
double num = 0;
if(isdigit(*next)) {
do {
num = num * 10 + (*next - '桐御0');
} while(isdigit(*++next));
printf("局毁岩%g ", num);
PUSH(num);
} else if(*next == '(') {
lbr(); expr(); rbr();
} else {
fail("term");
}
}

void lv1()
{
while(1) {
char c = *next;
if(c == '+' || c == '-') {
++next;
lv2(); printf("%c ", c); ++opt; cal(c);
} else if(*next == '*' || *next == '/') {
lv2();
} else {
break;
}
}
}

void lv2()
{
while(1) {
char c = *next;
if(c == '*' || c == '/') {
++next;
term(); printf("%c ", c); ++opt; cal(c);
} else if(isdigit(*next)) {
term();
} else {
break;
}
}
}

double eval(char* s)
{
next = s;
expr();
if(*next || opt != 0) {
puts("invalid expression.");
exit(0);
}
return TOP();
}

void cal(char c)
{
double lhs, rhs, r;
if(SIZE() > 1) {
rhs = POP();
lhs = POP();
switch(c)
{
case '+': r = lhs + rhs; break;
case '-': r = lhs - rhs; break;
case '*': r = lhs * rhs; break;
case '/': r = lhs / rhs; break;
}
PUSH(r);
--opt;
}
}

int main()
{
printf("\n%g\n", eval("123+213-67*34+345/23*45*(34+34-345+245+567)"));
return 0;
}