【无标题】利用堆栈,实现数据的加减乘除运算

实验要求:

代码如下:

 #include<iostream>
#include<string>
#include<math.h>
using namespace std;
//用链表表示的每一个节点
template <class T>
class Node
{
public:
    T date;
    Node<T>* next;
};
//创建一个栈
template<class T>
class stack
{
public:
    stack()    //构造函数
    {
        topNode =NULL; size = 0;
    };
    ~stack();  //析构函数
    bool empty()const  //判断栈是否为空
    {
        return { topNode == NULL };
    }
    int sizeofstack()const;//输出栈中元素个数
    T top();//返回栈顶元素
    Node<T>* push(const T& ele);//将元素ele压入栈中
    Node<T>* pop(T&x);//删除栈顶元素
    void display()const;//输出栈中的元素
private:
    Node<T>*topNode;
    int size;
};
template<class T>//;//输出栈中的元素
void stack<T>::display()const
{
    Node<T>*head = topNode;
    for (; head != NULL;)
    {
        cout << head->date << endl;
            head = head->next;
    }
}
template<class T> //析构函数
stack<T>::~stack()
{
    Node<T>*newhead;
        while (topNode != NULL)
        {
            newhead = topNode->next;
            delete topNode;
            topNode = newhead;
            break;
        }
}
template<class T>//输出栈中元素个数
int stack<T>::sizeofstack ()const
{
    int count = 0;
    Node<T>*head = topNode;
    for(;head!=NULL;head=head->next)
    {
        count++;
    }
    return count;
}
template<class T>//返回栈顶元素
T stack<T>::top()
{
    return topNode->date;
}
template<class T>//将元素ele压入栈中
Node<T>* stack<T>::push(const T &ele)
{
    Node<T>*p = new Node<T>;
    p->date = ele;
    p->next = topNode;
    topNode = p;
    return topNode;
}
template<class T>//删除栈顶元素
Node<T>* stack<T>::pop(T&a)
{
    if (empty())cout << "Stack is empty" << endl;
    a = topNode->date;
    Node<T>*head = topNode->next;
    delete topNode;
    topNode = head;
    return topNode;
}
int priority(char x)//规定符号优先级
{
    switch (x)
    {
    case'+':
    case'-':return 1; break;
    case'*':
    case'/':return 2; break;
    case'(':
    case')':return 3; break;
    }
}
int compute(int a,int b,char c)//规定运算方法
{
    switch (c)
    {
    case'+':return b + a; break;
    case'-':return b - a; break;
    case'*':return b * a; break;
    case'/':return b / a; break;
    default:cout << "Error" << endl; break;
    }
}
int main()
{
    cout << "Input" << endl;
    string expression;
    cin >> expression;
    stack<int>number;
    stack<char>oper;
    int index = 0;
    int num = 0;
    int oper1, oper2;
    char character;
    int result;
    for( ;index < expression.length();)
    {
        double weight = 10;//输入的字符的权值
        int value = 0;//输入的数字的值
        if (expression[index] >= 48 && expression[index] <= 57)//对数字的入栈处理
        {
            int i = 0;//记录有几个数字字符,1个为0,2个为1
            for (;expression[index+1] >= 48&&expression[index+1]<=57;)
            {
                i++;
                index++;
            }
            for (i; i >= 0; i--)
            {
                value =(int)((double)value + ((double)expression[index - i] - 48)*pow(weight,i));
            }
            number.push(value);
            if (expression[index + 1] != '0')
            {
                index++;
            }
        }
        if (index == expression.length())
        {
            int a = oper.sizeofstack();
            for (a; a > 0; a--)
            {
                number.pop(oper1);
                number.pop(oper2);
                oper.pop(character);
                result = compute(oper1, oper2, character);
                number.push(result);
            }
            break;
        }
        //对符号进行处理
        if (expression[index] >= 40 && expression[index] <= 43 || expression[index] == 45 || expression[index] == 47)
        {
            if (expression[index] == 40)//对括号内的数据进行处理
            {
                oper.push(expression[index]);
                int op = 0;//用于标记括号内的操作符数目
                char e;
                for (;;)
                {
                    index++;
                    int value1=0;
                    if (expression[index] == 41)
                    {
                        for (op; op>0; op--)
                        {
                            number.pop(oper1);
                            number.pop(oper2);
                            oper.pop(character);
                            result = compute(oper1, oper2, character);
                            number.push(result);
                        }
                        oper.pop(e);
                        if (expression[index + 1] != '0')
                        {
                            index++;
                        }
                        break;
                    }
                    if (expression[index] >= 48 && expression[index] <= 57)//对数字的入栈处理
                    {
                        int i = 0;//记录有几个数字字符,1个为0,2个为1
                        for (; expression[index + 1] >= 48 && expression[index + 1] <= 57;)
                        {
                            i++;
                            index++;
                        }
                        for (i; i >= 0; i--)
                        {
                            value1 = (int)((double)value1 + ((double)expression[index - i] - 48)*pow(weight, i));
                        }
                        number.push(value1);
                        continue;
                    }
                    if (expression[index] >= 40 && expression[index] <= 43 || expression[index] == 45 || expression[index] == 47)
                    {
                        if (op== 0)//操作符栈为空,将符号入栈
                        {
                            oper.push(expression[index]);
                            op++;
                            continue;
                        }
                        if (priority(expression[index]) > priority(oper.top()))
                        {
                            oper.push(expression[index]);
                            index++;
                            continue;
                        }
                        if (priority(expression[index]) <= priority(oper.top()))
                        {
                            if (op != 1)
                            {
                                number.pop(oper1);
                                number.pop(oper2);
                                oper.pop(character);
                                result = compute(oper1, oper2, character);
                                number.push(result);
                                oper.push(expression[index]);
                            }
                            else oper.push(expression[index]);
                        }
                    }
                }
            }
            if (oper.sizeofstack() == 0)//操作符栈为空,将符号入栈
            {
                oper.push(expression[index]);
                index++;
                continue;
            }
            if (priority(expression[index]) > priority(oper.top()))
            {
                oper.push(expression[index]);
                index++;
                continue;
            }
            if (priority(expression[index]) <= priority(oper.top()))
            {
                number.pop(oper1);
                number.pop(oper2);
                oper.pop(character);
                result = compute(oper1, oper2, character);
                number.push(result);
                oper.push(expression[index]);
            }
            index++;
        }
    }
    cout << "Output" << endl;
    cout << number.top() << endl;//
    cout << "End";
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨蛏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值