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

📄 main.cpp

📁 数据结构中有关栈的知识
💻 CPP
字号:
#include "main.h"
#include <fstream>
#include <iostream>
using namespace std;
int i=0;
int main()
{
	
	ofstream outfile("f1.dat",ios::out);
	if(! outfile) cout<<"打开错误"<<endl;
	char p[100];
	int m=0;
	cout<<"输入完以后 以'#&'结束"<<endl;
	for(int c=0;c<100;c++)
	{
		cin>>p[c];
		if(p[c]=='&'){outfile<<p[c]; break;}
		else 
		outfile<<p[c];
	}
	outfile.close();
	cout<<"文件创建成功"<<endl;
	cout<<endl;




	ifstream infile("f1.dat",ios::in);
    if(! infile) cout<<"打开错误"<<endl;
	else cout<<"打开成功"<<endl;
	cout<<endl;


	cout<<"后缀表达式为"<<endl;
	seqstack l,l1;
	l.top->data='#';
	l1.top->data='#';
	char ch,ch1,op,ch2;
	char a[100];
	infile>>ch;
	while(ch!='&')
	{if(isdigit(ch))  {cout<<ch;a[i]=ch;i++;infile>>ch;}
		else
		{
			l.gettop(ch1);
			if(isp(ch1)<icp(ch))
			{
				l.push(ch);infile>>ch;
			}
			else if(isp(ch1)>icp(ch))
			{
				l.pop(op);cout<<op;a[i]=op;i++;
			}
			else
			{
			    l.pop(op);
				if(op=='(') infile>>ch;
				else {cout<<endl;a[i]=op;break;}
			}
		}
	}
	infile.close();

    

    int n=0;
	ch=a[n]-48;
	l1.push(ch);n++;
	ch=a[n]-48;
	l1.push(ch);n++;
	for(int n=2;n<i;n++)
	{
		//l1.push(a[n]); 
		ch=a[n];
		//if(l1.top->data=='#')  {cout<<"多项式的值为"<<l1.top->link->data<<endl;}
	    if(math(ch)==1)
	    {
		    l1.pop(ch1);
		    l1.pop(ch2);
		    op=int(ch1)+int(ch2);
		    l1.push(op);
	    }
        else if(math(ch)==2)
	    {
		    l1.pop(ch1);
		    l1.pop(ch2);
		    op=int(ch2)-int(ch1);
		    l1.push(op);
	    }
        else if(math(ch)==3)
	    {
		    l1.pop(ch1);
		    l1.pop(ch2);
		    op=int(ch1)*int(ch2);
		    l1.push(op);
	    }
	    else if(math(ch)==4)
	    {
		l1.pop(ch1);
		l1.pop(ch2);
		op=int(ch2)/int(ch1);
		l1.push(op);
	    }
		else 
		{
			ch=ch-48;
			l1.push(ch);
		}
	    
   }
	cout<<endl;
	cout<<int(l1.top->data)<<endl;
	cout<<endl;
	return 0;
}

























	/*for(int i=0;;i++)
	{
		cout<<"--------------------------------------------------------------"<<endl;
		cout<<"请输入要选择的操作 1:建栈  2:进栈  3:出栈  4:输出栈  5:退出"<<endl;
		cout<<"--------------------------------------------------------------"<<endl;
		int m;
		cin>>m;
		switch(m)
		{
		    case 1:cin>>l;break;
			case 2:l.push();break;
			case 3:l.pop();break;
			case 4:cout<<l;break;
			case 5:return 0;
		}
	}
	return 0;
}
*/

⌨️ 快捷键说明

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