c++栈实现简单计算器

目录

一.原理

1.数据结构设计

2.栈的相关函数

3.计算函数设计

4主函数


一.原理

1.将式子转换 按计算优先级排序为后缀表达式

2.计算后缀表达式

1.数据结构设计

//顺序栈
template <typename ElemType_stack>
    struct stack {
        ElemType_stack data[Maxstack];
        int top;
    };

2.栈的相关函数

//初始化栈:构造一个空栈S,分配内存空间;

    template <typename ElemType_stack>
    void InitStack(stack<ElemType_stack>*& S) {
        S = new stack<ElemType_stack>;
        S->top = -1;
    }

    // 判空: 断一个栈S是否为空,若S为空,则返回true, 否则返回false;
    template <typename ElemType_stack>
    bool StackEmpty(stack<ElemType_stack>* S) {
        if (S->top == -1) {
            return true;
        }
        else {
            return false;
        }
    }

    // 进栈:若栈S未满,则将x加入使其成为新栈顶;
    template <typename ElemType_stack>
    bool Push(stack<ElemType_stack>*& S, ElemType_stack x) {
        if (S->top == Maxstack - 1) {
            return false;//栈满
        }
        S->top = S->top + 1;
        S->data[S->top] = x;
        return true;
    }
    // 出栈:若栈S非空,则弹出(删除)栈顶元素,并用x返回;
    template <typename ElemType_stack>
    bool Pop(stack<ElemType_stack>*& S, ElemType_stack& x) {
        if (S->top == -1) {
            return false;//栈空
        }
        //只是逻辑上删除实际没有删除
        x = S->data[S->top];
        S->top = S->top - 1;
        return true;
    }

    // 销毁栈:销毁并释放栈S所占用的内存空间;
    template <typename ElemType_stack>
    void DestroyStack(stack<ElemType_stack>*& S) {
        delete S;
    }

    // 读取栈顶元素:若栈S非空,则用x - 返回栈顶元素;(栈的使用场景大多只访问栈顶元素);
    template <typename ElemType_stack>
    bool GetTop(stack<ElemType_stack>* S, ElemType_stack& x) {
        if (S->top == -1) {
            return false;
        }
        x = S->data[S->top];
        return true;
    }

3.计算函数设计

//转换为后缀表达式
void trans(char* exp, char postexp[]) {
	char x;
	stack<char>* optr;
	InitStack(optr);
	int i = 0;
	while (*exp != '\0') {
		switch (*exp) {
		case'(':
			Push(optr, *exp);
			exp++;
			break;

		case')':
			Pop(optr, x);
			while (x != '(') {
				postexp[i++] = x;
				Pop(optr, x);
			}
			exp++;
			break;
		case'+':
		case'-':
			while (!StackEmpty(optr)) {
				GetTop(optr, x);
				if (x != '(') {
					postexp[i++] = x;
					Pop(optr, x);
				}
				else {
					break;
				}
			}
			Push(optr, *exp);
			exp++;
			break;
		case'*':
		case'/':
			while (!StackEmpty(optr)) {
				GetTop(optr, x);
				if (x=='*'||x=='/') {
					postexp[i++] = x;
					Pop(optr, x);
				}
				else {
					break;
				}
			}
			Push(optr, *exp);
			exp++;
			break;
		default:
			while (*exp >= '0' && *exp <= '9') {
				postexp[i++] = *exp;
				exp++;
			}
			postexp[i++] = '#';
		}
	}
	while (!StackEmpty(optr)) {
		Pop(optr, x);
		postexp[i++] = x;
	}
	postexp[i] = '\0';
	DestroyStack(optr);
}
//计算后缀表达式
double commath(char* postexp) {
	double a, b, c, d,e;
	stack<double>* optr;
	InitStack(optr);
	while (*postexp != '\0') {
		switch (*postexp) {
		case'+':
			Pop(optr, a);
			Pop(optr, b);
			c = a + b;
			Push(optr, c);
			break;
		case'-':
			Pop(optr, a);
			Pop(optr, b);
			c = b - a;
			Push(optr, c);
			break;
		case'*':
			Pop(optr, a);
			Pop(optr, b);
			c = a *b;
			Push(optr, c);
			break;
		case'/':
			Pop(optr, a);
			Pop(optr, b);
			if (a != 0) {
              c = b / a;
			Push(optr, c);
			break;
			}
			else {
				cout << "除0错误"<<endl;
				exit(0);
			}
			break;
		default:
			d = 0;
			while (*postexp >= '0' && *postexp <= '9') {
				d = 10 * d + *postexp - '0';
				postexp++;
			}
			Push(optr, d);
			break;
		}
		postexp++;
	}
	GetTop(optr, e);
	DestroyStack(optr);
	return e;
}

4主函数

#include<iostream>
using namespace std;
int main() {
     char exp[] = "(56-20)/(4+2)*2+(3-0)";
    char postexp[Maxstack];
    trans(exp, postexp);
    double a=commath(postexp);
    cout << exp << endl;
    cout << postexp << endl;
    cout << a;
}

 

  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值