current position:Home>If the stack is required to write four letters wrong, the operation chain should be reported
If the stack is required to write four letters wrong, the operation chain should be reported
2022-02-02 19:57:54 【CSDN Q & A】
#define MaxSize 30
#include
#include <stdlib.h>
using namespace std;
typedef float datatype;
typedef char chartype;
// Define stack format
typedef struct node
{
int flag; // Marker bit ,1 Is a floating point number ,2 It's character type
datatype num;
chartype sign;
node* next;
}node;
typedef struct Sqstack
{
node *Top;
}SqStack;
// Construct an empty stack
void InitStack(SqStack *& S);
// Empty stack
void SetNull(SqStack S);
// Judge stack empty
int Empty(SqStackS);
// Out of the stack
void Pop(SqStack *S,datatype &e);
void Pop(SqStack *S,chartype &e);
// Push
void Push(SqStack *S,datatype n);
void Push(SqStack *S,chartype s);
//
void GetTop(SqStack *S,datatype &e);
void GetTop(SqStack S,chartype &e);
// Determine whether it is character type , Is to return 1, No return 0
int CharOrNot(char ch);
// Enter an expression to save
void GetExpression(SqStack &S);
// I / O swap
void ReturnStack(SqStack &S);
// arithmetic
datatype EvaluateExpression(SqStack C);
// Four operations with input
datatype EvaluateExpression();
// Determine priority
chartype Precede(chartype a,chartype b);
// Encapsulate related operations , Returns the result of the operation
datatype Operate(datatype a,chartype b,datatype c);
// Construct an empty stack
void InitStack(SqStack & S)
{
S = (SqStack)malloc(sizeof(SqStack));
S->Top = NULL;
}
// Empty stack
void SetNull(SqStack S)
{
node p = NULL;
while(S->Top != NULL)
{
p = S->Top;
S->Top = p->next;
free(p);p = NULL;
}
//cout<<" Empty successfully "<<endl;
}
// Judge stack empty , Empty return 1
int Empty(SqStack*S)
{
if(S->Top<=0) return 1;
else return 0;
}
// Push
void Push(SqStack *S,datatype n)
{
node *p = (node*)malloc(sizeof(node));
if(p != NULL)
{
p->flag = 1;
p->num = n;
p->next = S->Top;
S->Top = p;
//cout<<"Num Stack successfully :"<num<<endl;
}
else
cout<<"Error"<<endl;
}
// Push
void Push(SqStack *S,chartype s)
{
node *p = (node*)malloc(sizeof(node));
if(p != NULL)
{
p->flag = 2;
p->sign = s;
p->next = S->Top;
S->Top = p;
//cout<<"Sign Stack successfully :"<sign<<endl;
}
else
cout<<"Error"<<endl;
}
// Out of the stack
void Pop(SqStack *S,datatype &e)
{
if(S->Top != NULL)
{
node p = S->Top;
e = p->num;
S->Top = p->next;
free(p);
//cout<<" Stack out successfully : "<<e<<endl;
}
else {}
}
void Pop(SqStack *S,chartype &e)
{
if(S->Top != NULL)
{
node p = S->Top;
e = p->sign;
S->Top = p->next;
free(p);
//cout<<" Stack out successfully : "<<e<<endl;
}
else {}
}
// Get the stack top element
void GetTop(SqStack *S,chartype &e)
{
if(S->Top != NULL)
e = S->Top->sign;
else {}
}
void GetTop(SqStack S,datatype &e)
{
if(S->Top != NULL)
e = S->Top->num;
else {}
}
// Judgment operator Go back to 1
int CharOrNot(char ch)
{
if(ch!='+'&&ch!='-'&&ch!=''&&ch!='/'&&ch!='('&&ch!=')'&&ch!='#')
return 0;
else return 1;
}
// Get expression
void GetExpression(SqStack &S)
{
cout<<" Please enter the calculation question :( Please add... After the formula # End of symbol )"<<endl;
char ch=0;
ch = getchar();
while (ch !='\n')
{
if(CharOrNot(ch))// Character
{
Push(S,ch);
ch = getchar();
}
else
{
datatype k=1;
datatype m=datatype(ch)-48;
ch = getchar();
while(ch!='.' && !CharOrNot(ch))
{
m=m10+int(ch)-48;
ch = getchar();
}
if(ch=='.') ch=getchar();
while(!CharOrNot(ch))
{
k=0.1k;
m=m+k(int(ch)-48);
ch = getchar();
}
Push(S,m);
}
}
//cout<<" Input complete "<<endl;
}
// Change order
void ReturnStack(SqStack *&S)
{
SqStack *Q;node *p;
InitStack(Q);
datatype a;chartype b;
while(S->Top!=NULL){ p=S->Top; S->Top = p->next; p->next = Q->Top; Q->Top=p;}S->Top = Q->Top;Q->Top = NULL;free(Q);//cout<<" The transformation is complete "<<endl;
}
// Input stack Handle
datatype EvaluateExpression(SqStack *C)
{
SqStack *S,*Q;node *p;
InitStack(S);InitStack(Q);
Push(Q,'#'); // As a sign of ending judgment
chartype b='!';datatype a;datatype c;
// When the last and first elements are both # End of time
while(p->sign!='#' || b!='#')
{
if(p->flag == 1)//flag = 1 Is the number
{
Push(S,p->num);p=p->next;// Enter the digital stack
}
else if(p->flag == 2) // Character
{
GetTop(Q,b);
// adopt switch Determine what needs to be done
switch(Precede(b,p->sign)){
case'<':// The priority of the symbol at the top of the stack is less than that of the current symbol
Push(Q,p->sign);// Stack the current
p=p->next;//p Point to next node
break;
case'=':// Off bracket
Pop(Q,b);// Out of the stack
p=p->next;//p Point to next node
break;
case'>':// The stack top symbol has priority over the current symbol
Pop(Q,b);// Out of stack operator
Pop(S,a);// Get the second number
Pop(S,c);// Get the first number
Push(S,Operate(a,b,c));// Put the result of the operation on the stack
break;
}
}
GetTop(Q,b);// to update b The symbol for the top element of the stack , Convenient judgment
}
GetTop(S,a);// End of cycle , Returns the result of the digital stack
SetNull(S);SetNull(Q);// empty
free(S);free(Q);free(p);// Release stack format
return a;// Return results
}
// Determine priority
chartype Precede(chartype a,chartype b)
{
int m,n;
switch(a)
{
case '#': m=0;break;
case '-':
case '+': m=2;break;
case '':
case '/': m=3;break;
case '(': m=1;break;
//default
//case')': m=3;
}
switch(b)
{
case '#': n=0;break;
case '-':
case '+': n=2;break;
case '':
case '/': n=3;break;
case '(': n=4;break;
case ')': n=1;break;
//default
}
if(m<n)
a='<';
else if(m==n && m == 1)// Off bracket
a='=';
else if(m==n &&m != 0 && m!= 1 || m>n)
a='>';
return a;
}
datatype Operate(datatype a,chartype b,datatype c)
{
switch(b)
Refer to the answer 1:
If you enter a letter, an error will be reported ?
Then you judge whether the input is a letter , If it's a letter, throw an exception .
Refer to the answer 2:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021957521194.html
The sidebar is recommended
- How to set a draw when making tic tac toe chess in Python
- Solving Python language C language
- How does Maple set a variable to be any integer from a certain interval
- How to realize that the opening of a website computer is one interface and the opening of a mobile phone is another interface, similar to self adaptation?
- After entering two indefinite length arrays and sorting them, merge and sort the two arrays again
- The computer has a web, but it can't access other web pages except the navigation page display
- C + + how to do this program
- Want to know how to find the parameters of this function? How should the matlab code knock
- Multi module project cannot inject mapper java
- MapReduce average score how to find the average of each line
guess what you like
-
Wechat applet rendering layer error
-
What does this mean? Why can't I open my QQ
-
CMD run Py file encountered a problem
-
Help me see what's wrong
-
Why not 13bingo
-
Decipher the password and there is no result after running?
-
Ask you why this code can't run (◞‸◟)
-
Hello, can you extract the of our applet
-
Why not show folders when attaching a database
-
How can the last line end with 0 without spaces
Random recommended
- Generate images from HTML pages and download them into PDF files
- Two Python experiment questions
- Hidden display problems in HTML
- Is there any good analysis process for push box problem
- What's going on here?.,!
- Cyclic input and output. If the input is a number, it will print. If the input is not a number, it will print No
- C language freshman PTA input an English sentence, change the first letter of each word into capital letters.
- Question e: snake walking, please help me
- Why is the attenuation of wave height in numerical simulation serious?
- Difference between digital image processing and image signal processing (ISP)
- Oracle a column of multi value update problem.
- Write a program in Python.
- How can Python write here to become a variable
- Which rstudio is installed in win7 64 bit? Dear friends, the previous connection can install w10,8,7, but now it can only install W10. Cry.
- Which is better, CCD camera tracking or gray sensor tracking
- Virtual reality combined with AR space
- How to click two label controls in QT to subtract the customized numbers in the control
- Ask you why there is a period of garbled code in front of the output?
- QR code generation of shopping scanning payment and third-party payment
- The Internet is a large network composed of many small networks. Can a local area network also be such a small network? What's more, how large is the coverage of a small network in a large network?
- Write a function to randomly generate an array of length n with elements from 0 to 99
- I don't understand very well. Can you explain it?
- Help me look at the code (I'm just a freshman, so I only know C). Why the bill The data in txt will always become 0 (after each command 1 is executed and more than one order is entered, only the last order in txt is saved, and all the others become 0) (o
- After the Ubuntu language is switched to English, two desktops appear
- Ora-01555 snapshot is too old. Fallback segment number (name) is too small. Ora-22924 snapshot is too old
- Win10 system boot unlimited automatic repair, recycling the use of Baidu methods are invalid
- A node of database RAC cannot be accessed normally
- Android SDK does not have uiautomatorviewer Bat file
- How should this program be completed
- One to many query of mybatis. The query conditions are in multiple parties. PageHelper paging is integrated. How can this XML be better implemented
- Error in NPM run dev: unknown option '-- inline'
- Java database: error caused by: Java lang.ClassNotFoundException: Didn't find class 'java. sql. Sqltype 'problem, how to solve it?
- What attachments do SQL server send mail with?
- In the built-in browser in windows, if you can't open the direct search web page, you can open the web page by using the web address. How to solve it?
- Tomcat failed to start. Would you like to see help
- How does the StrCmp function compare string sizes? What is the method
- Find the first 30 numbers of sequence 1 1 2 3 5 8 13 21
- Java finds array elements of the specified type
- Vue3 uses Axios to request JSON files in the public folder, and there is always a 500 error!
- Is a graphic inheritance problem