⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function1.h

📁 栈的操作与应用 定界符配对检查 中缀转后缀并求值
💻 H
字号:
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<conio.h>
#include<process.h>
#include <ctype.h>
#include<fstream.h>

template<class ElemType>
class sNode{
public:
	ElemType data;
	sNode *next;
	int pos;
public:
	void InitStack(sNode *&hs){ hs=NULL;}            
    
	void ClearStack(sNode *&hs)              
		{ 
			sNode *cp,*np;
		cp=hs;
		while(cp!=NULL)
		{
			np=cp->next;
			delete cp;
			cp=np;
		}
		hs=NULL;
		}
	    int StackEmpty(sNode *hs){
			return hs==NULL;
		}      

        ElemType Peek(sNode *hs)                                
		{
			if(hs==NULL){cerr<<"Stack is empty!"<<endl;
			exit(1);}
			return hs->data;
		}
		void Push(sNode *&hs,ElemType& item,int &temp)              
		{
			sNode *newptr=new sNode;
			newptr->data=item;
			newptr->pos=temp;
			newptr->next=hs;
			hs=newptr;
		}
		void Pop(sNode *&hs)                             
		{
			if(hs==NULL){cerr<<"Stack is empty!"<<endl; exit(1);}
			sNode *p=hs;
			hs=hs->next;
			delete p;
			
		}
};



void TraverseList(sNode <char>*&hs)
{
	while(hs!=NULL)
	{
		cout<<"第"<<hs->pos<<"个字符"<<hs->data<<"匹配错误!"<<endl;
			if(hs==NULL){cerr<<"Stack is empty!"<<endl; exit(1);}
			sNode <char>*p=hs;
			hs=hs->next;
			delete p;
	}

}
void Check()
{int i=0;
	char ch1;
	ofstream ou("a.dat");
	cout<<"请输入一个以'@'为结尾字符字符串"<<endl;
	cin>>ch1;
	ou<<ch1;
	while(ch1!='@')
	{
		cin>>ch1;
		ou<<ch1;
	}
	ou.close();
	ifstream in;
	in.open("a.dat");
	sNode<char> *a2;
	sNode<char> b2;
	b2. InitStack(a2);
	char ch;
	in>>ch;
	while(ch!='@')
	{
		if(ch==39)
		{
			i++;
		    while(in>>ch)
			{
				if(ch==39){i++;break;}
				else i++;
			}
			if(!in)
				cout<<"字符串尾部缺少一个单引号"<<endl;
		}
		else if(ch==34)
		{
			i++;
		    while(in>>ch)
			{
				if(ch==34){i++;break;}
				else i++;
			}
			if(!in)
				cout<<"字符串尾部缺少一个双引号"<<endl;
		}
		switch(ch)
		{
		case '{':
		case '[':
		case '(':
			i++;
			b2.Push(a2,ch,i);
			break;
		case '}':
			if(b2.StackEmpty(a2))
			{
				i++;
				b2.Push(a2,ch,i);
			}
			else
			{
				if(b2.Peek(a2)=='{')
				{
				i++;
				b2.Pop(a2);
				}
				else
				{
					i++;
					b2.Push(a2,ch,i);
				}
			}

			break;
		case ']':
			if(b2.StackEmpty(a2))
			{
				i++;
				b2.Push(a2,ch,i);
			}
			else
			{
				if(b2.Peek(a2)=='[')
				{
				i++;
				b2.Pop(a2);
				}
				else
				{
				i++;
				b2.Push(a2,ch,i);
				}
			}

			break;
		case ')':
			if(b2.StackEmpty(a2))
			{
				i++;
				b2.Push(a2,ch,i);
			}
			else
			{
				if(b2.Peek(a2)=='(')
				{
				b2.Pop(a2);
				i++;
				}
				else 
				{
					i++;
				    b2.Push(a2,ch,i);
				}
			}

			break;
		}
		in>>ch;
	}
	if(b2.StackEmpty(a2))
		cout<<"定界符匹配成功"<<endl;
	else TraverseList(a2);
	in.close();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -