1.求c++编译一个简单的程序程序计算程序(四则运算)。
求c++编译一个简单的源码计算程序(四则运算)。
//正解代码很长很复杂,程序程序易语言入侵源码也许对新人来说太难了。源码
//此程序可以运算+、程序程序js导入excel源码-、源码java int类源码*、程序程序/、源码乘方(^)、程序程序求余数(%),源码也可以出现( )规定优先级。程序程序
//按Ctrl+C退出。源码
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <math.h>
typedef enum BinOpr
{
OP_ADD,程序程序jfinal 源码导入报错 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}, { ,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;
}
//可以把这个程序留下来,等以后再慢慢研究。源码正解太复杂太复杂了。程序程序joda time 源码下载
//望采纳
------------------------------------------------------------------------------------
2024-12-29 01:13
2024-12-29 01:11
2024-12-29 00:05
2024-12-28 23:53
2024-12-28 23:51
2024-12-28 23:24