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(SqStack
S);

// 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=m
10+int(ch)-48;
ch = getchar();
}
if(ch=='.') ch=getchar();
while(!CharOrNot(ch))
{
k=0.1
k;
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

Random recommended