手写栈和队列实现计算器

 要求

字符串读入,含义+,-,*,/,(,)
数据含小数

思路

字符串截取读入(数字位数可能不为一位)
template简化代码复杂度
enum增加代码可读性
class 抽象出栈
 ~~~~namespace定义域~~好玩~~


main函数

#include <bits/stdc++.h>

#define stack

#define rep(i,a,b) for(int i=a;i<=b;i++)

std::string s;
#ifdef queue
#include "queue.hpp"
#endif

#ifdef stack
#include "stack.hpp"
My_stack::Stack <double> q1;
My_stack::Stack <char> q2;
#endif

void work()
{
        char c2;
        q2.get_top(c2);q2.pop();
        double a1,a2;
        q1.get_top(a1);q1.pop();
        q1.get_top(a2);q1.pop();
        std::cout<<c2<<" "<<a1<<" "<<a2<<'\n';
       // std::cout<<a1+a2;
        if(c2=='+') q1.push(a1+a2);
        else if(c2=='-')
        {
                q1.push(a2-a1);
        }
        else if(c2=='/')
        {
                q1.push(a2/a1);
        }
        else if(c2=='*')
        {
                q1.push(a1*a2);
        }
}

int main()
{
        #ifdef stack
        
        std::cin>>s;
        rep(i,0,s.length()-1)
        {
                if(s[i]<='9'&&s[i]>='0') 
                {
                        int id1=0;
                        rep(j,i,s.length()-1)
                        {
                                if(s[j]=='/'||s[j]=='*'||s[j]=='+'||s[j]=='-')
                                {
                                        id1=j;break;
                                }
                        }
                        if(!id1) id1=s.length()-1;
                        std::string s2=s.substr(i,id1-i);
                        std::cout<<s2<<'\n';
                        char s_2[50]={0};
                        rep(j,0,s2.length()-1)
                        {
                                s_2[j]=s2[j];
                        }
                        i=id1-1;
                        double ans=atof(s_2);
                        q1.push(ans);
                }
                else if(s[i]=='+'||s[i]=='-')
                {	//std::cout<<"dwq";
                        while(!q2.empty()&&(q2.top()=='+'||q2.top()=='-'||q2.top()=='*'||q2.top()=='/'))
                        {
                        	
                                        work();
                        }
                        q2.push(s[i]);
                }
                else if(s[i]=='*'||s[i]=='/')
                {
                        while(!q2.empty()&&(q2.top()=='*'||q2.top()=='/'))
                        {
                                work();
                        }
                        q2.push(s[i]);
                }
                else if(s[i]=='(') q2.push(s[i]);
                else if(s[i]==')')
                {
                        while(q2.top()!='(')
                        {
                                work();
                        }
                        q2.pop();
                }
        }
        while(!q2.empty())
        {
        	work();
        }
        double ans=0;
        q1.get_top(ans);
        std::cout<<ans<<'\n';
        #endif
        #ifdef queue
        My_queue::Queue <int> q;
        rep(i,1,5) q.push(i);
        rep(i,1,2) q.pop();
        rep(i,6,8) q.push(i);
        rep(i,1,1) q.pop();
        int n;
        q.get_front(n);
        std::cout<<n<<'\n';
        #endif
        return 0;
}

//stack.cpp

#include <iostream>

enum error_code {success,overflow,underflow};

namespace My_stack
{
        template<class T>
        class Stack
        {
        public:
        Stack();
        ~Stack();
        bool empty() const;
        bool full() const;
        error_code get_top(T &a) const;
        error_code push(T a) ;
        double top() ;
        error_code pop() ;
        
        private:
        T* my_date;
        int max_size;
        int cnt;
        };
        
        template <class T> Stack<T>::Stack()
        {
                this->max_size=10000;
                this->cnt=0;
                my_date = new T [max_size];
        }
        template <class T> Stack<T>::~Stack()
        {
                delete my_date;
        }
        template <class T> bool Stack<T>::full() const
        {
                if(this->cnt==this->max_size) return 1;
                else return 0;
        }
        template <class T> bool Stack<T>::empty() const
        {
                if(this->cnt==0) return 1;
                else return 0;
        }
        template <class T> error_code Stack<T>::get_top(T &a) const
        {
                if(this->empty()) return underflow;
                else 
                {
                        a = my_date[cnt-1];
                        return success;
                }        
        }
        template <class T>  double Stack<T>::top() 
        {
                if(!empty()) return my_date[cnt-1];
        }
        template <class T> error_code Stack<T>::push(T a) 
        {
                if(full()) return overflow;
                else 
                {
                my_date[cnt]=a;
                cnt++;
                //std::cout<<cnt<<'\n';
                return success;
                }
        }
        template <class T> error_code Stack<T>::pop() 
        {
                if(this->empty()) return overflow;
                else 
                {
                        my_date[cnt-1]=0;
                        --cnt;
                        return success;
                }
        }
}

//附赠queue抽象

#include <iostream>

enum error_code {success,overflow,underflow};

const int max_size=1000;

namespace My_queue
{
        template<class T>
        class Queue
        {
        public:
        Queue();
        ~Queue();
        bool empty() const;
        bool full() const;
        error_code get_front(T &a) const;
        error_code push(T a) ;
        error_code pop() ;
        private:
        T* my_date;
        int cnt;
        int left;
        int right;
        };
        
        template <class T> Queue<T>::Queue()
        {
                this->cnt=0;
                this->left=this->right=0;
                my_date = new T [max_size];
        }
        template <class T> Queue<T>::~Queue()
        {
                delete my_date;
        }
        template <class T> bool Queue<T>::full() const
        {
                if(this->cnt==max_size) return 1;
                else return 0;
        }
        template <class T> bool Queue<T>::empty() const
        {
                if(this->cnt==0) return 1;
                else return 0;
        }
        template <class T> error_code Queue<T>::get_front(T &a) const
        {
                if(this->empty()) return underflow;
                else 
                {
                        a = my_date[(left+1)%max_size];
                        return success;
                }        
        }
        template <class T> error_code Queue<T>::push(T a) 
        {
                if(full()) return overflow;
                else 
                {
                right=(++right)%max_size;
                my_date[right]=a;
                cnt++;
                //std::cout<<cnt<<'\n';
                return success;
                }
        }
        template <class T> error_code Queue<T>::pop() 
        {
                if(this->empty()) return overflow;
                else 
                {
                        left=(left+1)%max_size;
                        //y_date[left]=0;
                        --cnt;
                        return success;
                }
        }
}
原创文章 17 获赞 14 访问量 1264

猜你喜欢

转载自blog.csdn.net/qq_40493829/article/details/104845150
今日推荐