前言
栈:后进先出
没描述,先学的看看代码,代码都可运行,要注意输入格式哦,,
表达式-中缀
#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;
}