表达式求值输出结果不正确,我打了断点调试,感觉是判断了数字之后没有往下再运行,不知道为啥,请大家帮忙看看问题在哪里
还有这个问题怎么判断命令行参数是否正确
https://pastebin.ubuntu.com/p/cd8xJXx859/
代码修改如下:
#pragma warning(disable:4996)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 1000//存储空间初始分配量
#define STACKINCREMENT 100//分配增量
typedef int Status;
typedef char ElemType;
typedef struct {
ElemType* base;
ElemType* top;
int stacksize;
}OPTRStack;
typedef struct {
double* base;
double* top;
int stacksize;
}OPNDStack;
Status InitStackOPTR(OPTRStack& s) {
s.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s.base) {
exit(1);
}
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return 0;
}//
Status InitStackOPND(OPNDStack& s) {
s.base = (double*)malloc(STACK_INIT_SIZE * sizeof(double));
if (!s.base) {
exit(1);
}
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return 0;
}//
ElemType GetTopOPTR(OPTRStack s) {//获取栈顶元素
ElemType e;
if (s.top == s.base) {
return 0;
}
e = *(s.top - 1);
return e;
}
double GetTopOPND(OPNDStack s) {//获取栈顶元素
double e; //MOD
if (s.top == s.base) {
return 0;
}
e = *(s.top - 1);
return e;
}
void PushOPTR(OPTRStack& s, ElemType e) {//插入元素e为新的栈顶元素
if (s.top - s.base >= s.stacksize) {//如果栈满,扩充空间
s.base = (ElemType*)realloc(s.base, (s.stacksize + STACKINCREMENT) * sizeof(ElemType));
if (!s.base) {
exit(1);
}
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top++ = e;//赋值后栈顶指针+1
}
void PushOPND(OPNDStack& s, double e) {//插入元素e为新的栈顶元素 //MOD ElemType e 改成double e
if (s.top - s.base >= s.stacksize) {//如果栈满,扩充空间
s.base = (double*)realloc(s.base, (s.stacksize + STACKINCREMENT) * sizeof(double));
if (!s.base) {
exit(1);
}
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top++ = e;//赋值后栈顶指针+1
}
ElemType PopOPTR(OPTRStack& s) {//删除栈顶元素
ElemType e;
if (s.top == s.base) {
return 1;
}
e = *--s.top;//栈顶指针-1,给e赋值
return 0;
}
Status PopOPND(OPNDStack& s) {//删除栈顶元素
double e;
if (s.top == s.base) {
return 1;
}
e = *--s.top;//栈顶指针-1,给e赋值
return 0;
}
Status In_sign(ElemType c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '[' || c == ']' || c == '^')
return 1;
else
return 0;
}
Status In_Num(ElemType e) {
if (e >= '0' && e <= '9')
return 1;
else
return 0;
}
Status Precede(ElemType op) {//存储函数的优先级
switch (op) {
case'+':
case'-':
return 1;
case'*':
case'/':
return 2;
case'^':
return 3;
case'(':
case'[':
return 4;
case')':
case']':
return 0;
}
}
Status arity(ElemType x) {//存储运算符的元数
switch (x) {
case'+':
case'-':
case'*':
case'/':
case'^':
return 2;
case'(':
case')':
case'[':
case']':
return 0;
}
}
double Operate(double a, ElemType x, double b) {//进行运算的函数
if (arity(x) == 2) {
switch (x) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
case'^':
return pow(a, b);
}
}
}//Operate
Status check(ElemType left, ElemType right) {
if (left == '(' && right == ')')
return 1;
else if (left == '[' && right == ']')
return 1;
else
return 0;
}//判断括号
Status emptyOPND(OPNDStack s) {
if (s.top == s.base || s.top < s.base)
return 0;
else
return 1;
}//判断栈是否为空
Status emptyOPTR(OPTRStack s) {
if (s.top == s.base || s.top < s.base)
return 0;
else
return 1;
}//判断栈是否为空
int main() {
OPTRStack OPTR, BRACKETS;//OPTR:运算符栈 OPND:运算数栈 BRACKETS:括号栈
OPNDStack OPND;
ElemType s[100], theta;
InitStackOPTR(OPTR);
InitStackOPND(OPND);
InitStackOPTR(BRACKETS);
Status len, n;
scanf("%s", s);
len = strlen(s);
s[len] = '#';
len++;
Status i;
double a, b, c = 0;
for (i = 0; i < len; i++) {
if (s[i] == '(' || s[i] == '[') {
PushOPTR(BRACKETS, s[i]);
}
else if (s[i] == ')' || s[i] == ']') {
if (check(GetTopOPTR(BRACKETS), s[i]) == 1) {
PopOPTR(BRACKETS);
}
else
{
printf("ERROR_02");
return 0;
}
}
}//判断括号是否匹配
for (i = 0; i < len - 1; i++) {
if (In_sign(s[i]) == 0 && In_Num(s[i]) == 0 && s[i] != '.') {
printf("ERROR_02");
return 0;
}//判断是否是数字和运算符号
}
i = 0;
if (s[0] == '+' || s[0] == '-'|| s[0] == '*'||s[0] == '/'|| s[0] == '^'||s[0] == '.') {
printf("ERROR_02");
return 0;
}
while (s[i] != '#') {
double x = 0;
if (In_Num(s[i]) == 1) {
while (s[i] >= '0' && s[i] <= '9') {
x *= 10;
x += s[i++] - '0';
}
if (s[i] == '.') {
double d = 0.1;
i++;
while (s[i] >= '0' && s[i] <= '9') {
x += ((s[i] - '0') * d);
d *= 0.1;
i++;
}
}
PushOPND(OPND, x);
continue;
}
else {
ElemType theta = s[i];
//printf("pre = %c current=%c\n",GetTopOPTR(OPTR),s[i]);
while (emptyOPTR(OPTR) == 1 && Precede(GetTopOPTR(OPTR)) >= Precede(s[i])) {
if (arity(GetTopOPTR(OPTR)) == 2) {
a = GetTopOPND(OPND);
PopOPND(OPND);
b = GetTopOPND(OPND);
PopOPND(OPND);
c = Operate(b, GetTopOPTR(OPTR), a);
//printf("%g %c %g\n",b,GetTopOPTR(OPTR),a);
PushOPND(OPND, c);
//printf("%g in stack\n",c);
PopOPTR(OPTR);
//printf("current data: ");
//showData(OPND);
}
else {
break;
}
}
if (Precede(theta) == 0 && Precede(GetTopOPTR(OPTR))==4 ) {
PopOPTR(OPTR);
}
if (Precede(theta) != 0) {
PushOPTR(OPTR, theta);
}
i++;
}
}
while (emptyOPTR(OPTR) == 1) {
a = GetTopOPND(OPND);
PopOPND(OPND);
b = GetTopOPND(OPND);
PopOPND(OPND);
c = Operate(b, GetTopOPTR(OPTR), a);
PushOPND(OPND, c);
PopOPTR(OPTR);
}
printf("%g", GetTopOPND(OPND));
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!