栈-表达式

前言

栈:后进先出

在这里插入图片描述
没描述,先学的看看代码,代码都可运行,要注意输入格式哦,,

表达式-中缀

#include<iostream>
using namespace std;

#define ERROR 0;
#define OK -1;
typedef int status;

typedef struct Stack{
	int data;
	struct Stack *next;
}Stack,*LinkList;

void instack(LinkList &S){
	S=new Stack;
	S=NULL;
}

status push(LinkList &S,char e){				//进栈 
	Stack *p;
	p=new Stack;
	p->data=e;
	p->next=S;
	S=p;
	return OK;
}


status pop(LinkList &S,char &e){			//出栈 
	if(S==NULL) return ERROR;			//栈空 
	Stack *p;
	p=S;
	e=S->data;
	S=S->next;
	delete p;
	return OK;
}

int gettop(LinkList S){	
	return S->data;
}

status In(char ch){
	switch(ch){
		case '0':
		case '1':
		case '2':
		case '3': 
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9': return OK;
		default :return ERROR;
	}
}

char operate(char first,char optr,char second){
	int a,b;
	a=first-'0';
	b=second-'0';
	switch(optr){
		case '+':
			return a+b+'0';
		case '-':
			return a-b+'0';
		case '*':
			return a*b+'0';
		case '/':
			return a/b+'0';
	}
}

char precede(char ch1,char ch2){
	if(ch1==ch2&&ch1!='#')
		return '>';
	switch(ch1){
		case '+':
		case '-':
			switch(ch2){
				case '*':
				case '/':return '<';
				default :return '>';
			}
		case '*':
		case '/':
			switch(ch2){
				case '(':return '<';
				default :return '>';
			}
		case '(':
			switch(ch2){
				case ')':return '=';
				case '#':return '!';
				default :return '<';
			}
		case ')':
			switch(ch2){
				case '(':return '!';
				default :return '>';
			}
		case '#':
			switch(ch2){
				case '#':return '=';
				case ')':return '!';
				default :return '<';
			}
		}
}

int Expression(){
	char a,b,oper;
	char ch;
	Stack *oped,*optr;
	instack(oped);			//栈-数 
	instack(optr);			//栈-运算符 
	push(optr,'#');
	cin>>ch;
	while(ch!='#'||gettop(optr)!='#'){
		if(In(ch)){
			push(oped,ch);
			cin>>ch;}
		else{
			switch(precede(gettop(optr),ch)){
				case '<':
					push(optr,ch);
					cin>>ch;
					break;
				case '>':
					pop(optr,oper);
					pop(oped,b);pop(oped,a);
					push(oped,operate(a,oper,b));
					break;
				case '=':
					pop(optr,oper);
					cin>>ch;
					break;
				case '!':
					cout<<"表达式有误"<<endl;
					return 0;
			}
		}
	}
	return gettop(oped)-'0';
}



int main()
{
	int choose=-1;
	cout<<1<<":输入表达式"<<endl;
	cout<<0<<":退出"<<endl;
	while(choose!=0){
		cout<<"输入表达式:"<<endl; 
		cout<<"值:"<<Expression()<<endl;
		cout<<"请选择:";
		cin>>choose;
		cout<<endl;
	}
	return 0;
}

表达式-后缀

#include<iostream>
using namespace std;

#define ERROR 0;
#define OK -1;
typedef int status;

typedef struct Stack{
	int data;
	struct Stack *next;
}Stack,*LinkList;

void Instack(LinkList &S){
	S=new Stack;
	S=NULL;
}

status push(LinkList &S,char e){				//进栈 
	Stack *p;
	p=new Stack;
	p->data=e;
	p->next=S;
	S=p;
	return OK;
}

status pop(LinkList &S,char &e){			//出栈 
	if(S==NULL) return ERROR;			 
	Stack *p;
	p=S;
	e=S->data;
	S=S->next;
	delete p;
	return OK;
}

int gettop(LinkList S){	
	return S->data;
}

status In(char ch){
	switch(ch){
		case '0':
		case '1':
		case '2':
		case '3': 
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9': return OK;
		default :return ERROR;
	}
}

char operate(char first,char optr,char second){
	int a,b;
	a=first-'0';
	b=second-'0';
	switch(optr){
		case '+':
			return a+b+'0';
		case '-':
			return a-b+'0';
		case '*':
			return a*b+'0';
		case '/':
			return a/b+'0';
	}
}

int Expression(){
	char ch;
	char a,b;
	LinkList S;
	Instack(S);
	cin>>ch;
	while(ch!='#'){
		if(In(ch))
			push(S,ch);
		else{
			pop(S,b);pop(S,a);
			push(S,operate(a,ch,b));
			}
		cin>>ch;
		}
	return gettop(S)-'0';
}

int main(){
	int choose=-1;
	cout<<1<<":输入表达式"<<endl;
	cout<<0<<":退出"<<endl;
	while(choose!=0){
		cout<<"输入表达式:"<<endl; 
		cout<<"值:"<<Expression()<<endl;
		cout<<"请选择:";
		cin>>choose;
		cout<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值