zoukankan      html  css  js  c++  java
  • 数学表达式配对栈

    /*#include<iostream.h>
    class stack
    {
    private:
    	int count,maxsize;
        char *elem;
    public:
    	stack(int size)
    	{
    		maxsize=size;
    		count=0;
    		elem=new char[maxsize];
    	}
    	void input(int size,char *s)
    	{
    		cout<<"请输入一个长度不大于"<<size<<"的式子"<<endl;
    		cin>>s;
    	}
       void push(char &ch1)
       {
    	   if(count==maxsize)
    		   cout<<"栈已满,无法入栈"<<endl;
    	   else
    	   {
    		   elem[count++]=ch1;
    	   }
       }
       void pop(char &ch2)
       {
    	   if(count==0)
    		   cout<<"栈中已无元素"<<endl;
    	   else
    	   {
    		   ch2=elem[count-1];
    		   count--;
    	   }
       }
       bool empty()
       {
    	   return count==0;
       }
    };
    void main()
    {
    	int size;
    	char *s;
    	char temp;
    	char ch1='{',ch3='(',ch2='[',ch4=')',ch5=']',ch6='}';
    	cout<<"请输入一个式子的最大长度"<<endl;
    	cin>>size;
    	stack stack1(size);
    	s=new char[size];
    	stack1.input(size,s);
    	for(int i=0;i<size;i++)
    	{
    		if(s[i]==ch1)
    		{
    			stack1.push(ch6);
    		}
    		else if(s[i]==ch2)
    		{
                stack1.push(ch5);
    		}
    		     else if(s[i]==ch3)
    			 {
    			   stack1.push(ch4);
    			 }
    		          else if(s[i]==ch4||s[i]==ch5||s[i]==ch6)
    				  {
    			         stack1.pop(temp);
    			         if(s[i]!=temp)
    					 {
    						 cout<<"符号匹配出错"<<endl;
    						 break;
    					 }
    					 
    				  }
    				  
    	}
    	if(!stack1.empty())
    		 cout<<"匹配出错"<<endl;
    	
    }
    

  • 相关阅读:
    HGE tutorial04
    HGE tutorial03
    HGE tutorial02 plus
    HGE tutorial02
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
    C 语言实例
  • 原文地址:https://www.cnblogs.com/zztong/p/6695314.html
Copyright © 2011-2022 走看看