当前位置:网站首页>Stack expression
Stack expression
2022-07-28 15:12:00 【Demon YoY】
Preface
Stack : Last in, first out

No description , First learn to look at the code , Code can run , Pay attention to the input format ,,
expression - Infix
#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){
// Into the stack
Stack *p;
p=new Stack;
p->data=e;
p->next=S;
S=p;
return OK;
}
status pop(LinkList &S,char &e){
// Out of the stack
if(S==NULL) return ERROR; // The stack is empty
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); // Stack - Count
instack(optr); // Stack - Operator
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<<" The expression is wrong "<<endl;
return 0;
}
}
}
return gettop(oped)-'0';
}
int main()
{
int choose=-1;
cout<<1<<": Enter the expression "<<endl;
cout<<0<<": sign out "<<endl;
while(choose!=0){
cout<<" Enter the expression :"<<endl;
cout<<" value :"<<Expression()<<endl;
cout<<" Please select :";
cin>>choose;
cout<<endl;
}
return 0;
}
expression - suffix
#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){
// Into the stack
Stack *p;
p=new Stack;
p->data=e;
p->next=S;
S=p;
return OK;
}
status pop(LinkList &S,char &e){
// Out of the stack
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<<": Enter the expression "<<endl;
cout<<0<<": sign out "<<endl;
while(choose!=0){
cout<<" Enter the expression :"<<endl;
cout<<" value :"<<Expression()<<endl;
cout<<" Please select :";
cin>>choose;
cout<<endl;
}
return 0;
}
边栏推荐
- Crawler: from entry to imprisonment (II) -- Web collector
- What are the main threats to cloud security
- C language related programming exercises
- SSH service
- Mlx90640 infrared thermal imager sensor module development notes (VIII)
- 使用cpolar发布树莓派网页(apache2的安装测试)
- PS modify the length and width pixels and file size of photos
- Find papers and their open source code
- 2021-09-02
- 经典Dijkstra与最长路
猜你喜欢

21、 TF coordinate transformation (I): coordinate MSG message

边缘技术和小程序容器在智能家居中的应用
![What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]](/img/a9/336390db64d871fa1655800c1e0efc.png)
What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]

Introduction to MITK

Multi merchant mall system function disassembly lecture 17 - platform side order list

Talk about low code / zero code tools

When MySQL uses left join to query tables, the query is slow because the connection conditions are not properly guided

Introduction to mqtt protocol

2021-09-02

Compose learning notes 1-compose, state, flow, remember
随机推荐
10、 C enum enumeration
Mysql易错知识点整理(待更新)
RepVGG论文详解以及使用Pytorch进行模型复现
QT environment cannot run error set
iframe 标签
Install biological sequence de redundancy software CD hit
SSL socket cross platform solution libevent OpenSSL
Machine learning related concepts
Install pytorch geometric on colab, and libcudart.so.10.2 appears when importing the package
The second 1024, come on!
MLX90640 红外热成像仪传感器模块开发笔记(八)
云计算需要考虑的安全技术列举
MySQL authorization method
Examples of Pareto optimality and Nash equilibrium
Talk about low code / zero code tools
Matlab load usage
【游戏测试工程师】初识游戏测试—你了解它吗?
C language related programming exercises
What are the CCSP cloud security design principles
MITK create module