cxyhss 2017-10-12 02:36 采纳率: 0%
浏览 1341

C++利用堆栈实现算术表达式的求值,能够处理单目运算符,VS2017,程序异常就终止了,求教

stack.h

 #ifndef STACK
#define STACK
#include "stdafx.h"
#include <cstdlib>

typedef struct stack {
    double data;
    struct stack *next;
}stack;

stack *init_stack()
{
    stack *top;
    top = (stack*)malloc(sizeof(stack));
    top->next = NULL;
    return top;
}

int isEmpty(stack *top)
{
    if (top->next = NULL)
        return 1;
    else
        return 0;
}
float push(stack *top, float x)
{
    stack *p;
    p = (stack*)malloc(sizeof(stack));
    if (p == NULL)
        return -1;
    p->data = x;
    p->next = top->next;
    top->next = p;
    return 0;
}

float pop(stack *top, float *x)
{
    stack *p;
    if (top->next == NULL)
        return -1;
    p = top->next;
    *x = p->data;
    top->next = p->next;
    free(p);
    return 0;
}

float getTop(stack *top)
{
    float *x = NULL;
    if (top->next == NULL)
        return -1;
    *x = top->next->data;
    return *x;
}

#endif // !STACK

****## 算术表达式求值.cpp****

#include "stdafx.h"
#include
#include
#include "stack2.h"
#include
using namespace std;
class Calculator {
private:
stack *opnd;
stack *optr;

public:
Calculator();
virtual ~Calculator();
int getnext(int *n);
int getIndex(char a);
char operPrior(char a, char b);
float operation(float a, float operate, float b);
float evaluateEpression();
};

Calculator::Calculator() {
opnd = init_stack();
optr = init_stack();
}

Calculator::~Calculator() {
;
}

int Calculator::getnext(int n) {
char c;
*n = 0;
while ((c = getchar()) == ' ');
if ((!isdigit(c)) && (c != '+' || c != '-' || c != '
' || c != '/' || c != '(' || c != ')' || c != '='))
{
throw ("表达式出错!");
}
else
{
push(optr, c);
n = c;
return 1;
}
do {
*n = *n * 10 + (c - '0');
c = getchar();
} while(isdigit(c));
cin.putback(c);
return 0;
}
int Calculator::getIndex(char a)
{
int i;
switch (a)
{
case '+':
i = 0; break;
case '-':
i = 1; break;
case '
':
i = 2; break;
case '/':
i = 3; break;
case '(':
i = 4; break;
case ')':
i = 5; break;
case '=':
i = 6; break;
default:
break;
}
return i;
}
char Calculator::operPrior(char a, char b)
{
int i = getIndex(a);
int j = getIndex(b);
char pre[7][7] = {
{ '>','>','<','<','<','>','>' },
{ '>','>','<','<','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '<','<','<','<','<','=','0' },
{ '>','>','>','>','0','>','>' },
{ '<','<','<','<','<','0','=' }
};
return pre[i][j];
}
float Calculator::operation(float a, float operate, float b)
{
float result = 0.0;
switch ((int)operate)
{
case '+':
result = a + b; break;
case '-':
result = a - b; break;
case'*':
result = a * b; break;
case '/':
result = a / b; break;
default:
break;
}
return result;
}
float Calculator::evaluateEpression()
{
int flag = 0;
char priorChar;
int ch;
float *x = NULL;
char prior;
float left, right;
float operate;
float *top;
push(optr, '=');
priorChar = '=';
flag = getnext(&ch);
while (getTop(optr) != '=' || ch != '=')
{
if (!flag)
{
push(opnd, ch);
priorChar = '0';
flag = getnext(&ch);
}
else
{
priorChar = ch;
if ((priorChar == '=' || priorChar == '(') && (ch == '+' || ch == '-'))
{
push(opnd, 0);
priorChar = '0';
}
prior = operPrior(getTop(optr), ch);
switch (prior)
{
case '<':
push(optr, ch);
flag = getnext(&ch);
break;
case '>':
if (isEmpty(opnd))
throw ("表达式出错!");
pop(opnd, &right);
if (isEmpty(opnd))
throw ("表达式出错!");
pop(opnd, &left);
pop(optr, &operate);
push(opnd, operation(left, operate, right));
flag = getnext(&ch);
break;
case '=':
pop(optr, &operate);
flag = getnext(&ch);
break;
case '0':
throw ("表达式出错!");
break;
default:
break;
}
}
}
pop(opnd, x);
return *x;
}
int main()
{
Calculator calcuExpression;
float result = 0.0;
result = calcuExpression.evaluateEpression();
cout << result << endl;
return 0;
}

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2018-11-06 16:36
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)