求c++编译一个简单的计算程序(四则运算)。

但为什么我编译出来的程序没有出现我想要的结果 ,但我不知道问题出在哪里。。求 正解啊 大神吗,顺便有没有一个四则运算计算器的源码,第一次用百度知道。希望有人能帮助我

//正解代码很长很复杂,也许对新人来说太难了。
//此程序可以运算+、-、*、/、乘方(^)、求余数(%),也可以出现( )规定优先级。
//按Ctrl+C退出。
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <math.h>

typedef enum BinOpr
{
OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_POW, OP_NON
} BinOpr;

static struct { int left, right; } binop_prio[] =
{
{6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, {10,9},
};

#define UNARY_PRIO 8

static BinOpr get_binop(const char **s)
{
switch (**s)
{
case '+': ++*s; return OP_ADD;
case '-': ++*s; return OP_SUB;
case '*': ++*s; return OP_MUL;
case '/': ++*s; return OP_DIV;
case '%': ++*s; return OP_MOD;
case '^': ++*s; return OP_POW;
default: return OP_NON;
}
}

static double doexpr(int op, double a, double b)
{
switch (op)
{
case OP_ADD: return a+b;
case OP_SUB: return a-b;
case OP_MUL: return a*b;
case OP_DIV: return a/b;
case OP_MOD: return a-floor(a/b)*b;
case OP_POW: return pow(a, b);
default: return 0;
}
}

typedef struct ExprContext
{
jmp_buf jbuf;
const char *errmsg, *s;
BinOpr op;
} Expr;

static double error(Expr *e, const char *msg)
{
e->errmsg = msg;
longjmp(e->jbuf, 1);
}

static double expr(Expr *e, int limit)
{
double n;
BinOpr op;
if (*e->s == '-')
{
++e->s;
n = -expr(e, UNARY_PRIO);
}
else if (*e->s == '(')
{
++e->s;
n = expr(e, 0);
if (*e->s++ != ')') error(e, "')' expected");
}
else {
const char *s = e->s;
n = strtod(s, (char**)&e->s);
if (e->s == s) error(e, "'number' expected");
}
op = get_binop(&e->s);
while (op != OP_NON && binop_prio[op].left > limit)
{
n = doexpr(op, n, expr(e, binop_prio[op].right));
op = e->op;
}
e->op = op;
return n;
}

double calc(const char *s, const char **perr)
{
Expr e;
e.s = s;
e.errmsg = NULL;
if (setjmp(e.jbuf) == 0)
{
double n = expr(&e, 0);
if (*e.s != '\n' && *e.s != '\0' && *e.s != '=')
error(&e, "traling chars detected");
return n;
}
if (perr) *perr = e.errmsg;
return 0;
}

int main(void)
{
char buff[BUFSIZ];
while (printf("> "), fgets(buff, BUFSIZ, stdin) != NULL)
{
const char *errmsg = NULL;
double n = calc(buff, &errmsg);
if (errmsg) printf("ERROR: %s\n", errmsg);
else printf("%g\n", n);
}
return 0;
}
//可以把这个程序留下来,等以后再慢慢研究。正解太复杂太复杂了。
//望采纳
------------------------------------------------------------------------------------
温馨提示:答案为网友推荐,仅供参考
相似回答