C++栈的实现

#include<iostream>
#include<stdlib.h>
typedef double DataType;
const int MaxStackSize = 50;
class Stack
{
    private:
        DataType stacklist[MaxStackSize];
        int top;
    public:
        Stack(void);
        void push(const DataType& item);
        void clearStack(void);
        //访问栈顶元素
        DataType peek(void)const;
        //检测栈顶状态方法
        int stackEmpty(void) const;
        int stackFull(void) const;
        DataType pop(void);

};




#include"astack.h"
//初始化栈的下标top
using namespace std;
Stack::Stack(void):top(-1)
{
}

void Stack::push(const DataType& item)
{
    if(top == MaxStackSize-1)
    {
        
            std::cerr<<"Stack overflow!"<<endl;
            exit(1);
    }
    top++;
    stacklist[top] = item;
}


 DataType Stack::pop()
 {
     DataType temp;
     //若栈为空 程序退出
     if(top == -1)
     {
         cerr<<"Attempt to pop an empty stack!"<<endl;
         exit(-1);
     }
     //将顶元素赋值给temp
     temp = stacklist[top];
     //top-1 返回temp值
     top--;
     return temp;
 }
 
 
 //返回栈顶元素的值
 DataType Stack::peek()const
 {
     if(top == -1)
     {
         cerr<<"Attempt to peek t an empty stack!"<<endl;
         exit(1);
     }
     return stacklist[top];
  }
 
  //检查栈是否为空
  int Stack::stackEmpty() const
  {
       return top==-1;
   }
 //检查栈是否满
 int Stack::stackFull()const
 {
     return top==MaxStackSize -1;
  }
 
 //从栈中清除所有元素
 void Stack::clearStack()
 {
     top = -1;
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

好的,以下是C++实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的 stack<char> opStack; // 存储运算符的 string expression; // 表达式字符串 cin >> expression; int len = expression.length(); for (int i = 0; i < len; i++) { char ch = expression[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,入数字 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符为空或者当前运算符是左括号,直接入 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符是加减乘除 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.push(ch); } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ``` 这个程序可以处理包含加减乘除和括号的表达式,但是没有考虑负数的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值