Home > Back-end >  Single-digit expression evaluation for bosses to see code which is wrong
Single-digit expression evaluation for bosses to see code which is wrong

Time:09-21

# include
# include
# include
Typedef char ElemType;
# define MAXSIZE 30
Typedef struct
{ElemType elem [MAXSIZE];
int top;
}SeqStack;

SeqStack A, B;/* defined before a stack for operator, an operand stack after */
Char a [255]=""; Deposit/* * expression string/
Void InitStack_Sq SeqStack * (s)/* initialization stack operation */
{s - & gt; Top=1;
}
Int Empty_Sq SeqStack * (s)/* judge whether the stack is empty */
{return (s - & gt; Top==1);
}
Int Push_SeqStack SeqStack * s, ElemType (x)
{if (s - & gt; Top==MAXSIZE return - 1-1);/* not full into the stack */
The else {s - & gt; Top++;
S - & gt; Elem [s - & gt; top]=x;
return 1;
}
}
Int Pop_SeqStack (SeqStack * s, ElemType * y)
{if (Empty_Sq (s)) return - 1;/* empty can't out of the stack */
The else
{* y=s - & gt; Elem [s - & gt; top];
S - & gt; Top -; return 1; }/* stack into * y return */
}
ElemType GetTop (SeqStack * s, ElemType * y)/* * take top elements/
{if (Empty_Sq (s)) return - 1;/* empty can't out of the stack */
The else {* y=s - & gt; Elem [s - & gt; top];
return 1;
}
}
Char Precede (char char c1, c2)
{/* judgment operator precedence */
Int I=0, j=0;
The static char array [49]={
'& gt; ', '& gt; ', '& lt; ', '& lt; ', '& lt; ', '& gt; ', '& lt; '
'& gt; ', '& gt; ', '& lt; ', '& lt; ', '& lt; ', '& gt; ', '& lt; '
'& gt; ', '& gt; ', '& gt; ', '& gt; ', '& lt; ', '& gt; ', '& lt; '
'& gt; ', '& gt; ', '& gt; ', '& gt; ', '& lt; ', '& gt; ', '& lt; '
'& lt; ', '& lt; ', '& lt; ', '& lt; ', '& lt; 'and'=', '& lt; '
'& gt; ', '& gt; ', '& gt; ', '& gt; ', '! ', '& gt; ', '! '
'& gt; ', '& gt; ', '& gt; ', '& gt; ', '& lt; ', '& gt; ', '& gt; '};
The switch (c1)
{
/* I for the following array of abscissa */
A case of '+' : I=0; break;
Case '-' : I=1; break;
Case '*' : I=2; break;
Case '/' : I=3; break;
Case '(' : I=4; break;
Case ') : I=5; break;
Case '^' : I=6; break;
}
The switch (c2)
{/* j in the array of ordinate */
Case '+' : j=0; break;
Case '-' : j=1; break;
Case '*' : j=2; break;
Case '/' : j=3; break;
Case '(' : j=4; break;
Case ') : j=5; break;
Case '^' : j=6; break;
}
Return (array (7 * I + j]);/* return operator */
}
Int Operate (double a, char op, double b)
{/* * operation function/
The switch (op) {
Case: '+' return (a + b);
Case '-' : return (b - a);
Case: '*' return (a * b);
Case '/' : return (b/a);
Case: '^' return (int) pow ((double) b, a);
}
return 0;
}
Int (char ch)/* In determining if a character operation additional program */
{
If (ch=='+' | | ch=='-' | | ch=='*' | | ch=='/' | | ch=='(' | | ch==') '| | ch==' ^ ')
return 1;
The else
return 0;
}
Int the process (char a [255])
{
Int I=0, temp=0, m=0, y, x, flag=0, glag=0, hlag=0;
Char ch;
While (a [I]!=NULL)
{
If (In (a) [I])
{
If (Empty_Sq (& amp; A))
{
Push_SeqStack (& amp; A, A, [I]).
Glag=1;
}
If (glag==0)
{
GetTop (& amp; A, & amp; Ch);
The switch (Precede (ch, a [I]))
{
Case '& lt; ':
Push_SeqStack (& amp; A, A, [I]).
break;
Case: '='
Pop_SeqStack (& amp; A, & amp; Ch);
break;
Case '& gt; ':
flag=1;
Pop_SeqStack (& amp; B, & amp; Ch); X=ch;
Pop_SeqStack (& amp; B, & amp; Ch); Y=ch;
Pop_SeqStack (& amp; A, & amp; Ch);
M=Operate (x, ch, y);
Push_SeqStack (& amp; B, m);
GetTop (& amp; B, & amp; Ch);
Printf (" % d \ n ", ch);
break;


}
} glag=0;
}
The else
{
Temp=a, [I] - '0'.
Push_SeqStack (& amp; B, temp);
}
If (flag==0)
i++;
flag=0;

}
While (A.t op!=1)
{
Pop_SeqStack (& amp; B, & amp; Ch);
X=ch;
Pop_SeqStack (& amp; B, & amp; Ch);
Y=ch;
Pop_SeqStack (& amp; A, & amp; Ch);
M=Operate (x, ch, y);
Push_SeqStack (& amp; B, m);
GetTop (& amp; B, & amp; Ch);
Printf (" % d \ n ", ch);


}
Pop_SeqStack (& amp; B, & amp; Ch);
Return ch;
}
Int main ()
{
Int num=0;
Gets (a);
InitStack_Sq (& amp; A);
InitStack_Sq (& amp; B);
Num=process (a);
Printf (" % d ", num);

}

CodePudding user response:

What is wrong?

CodePudding user response:

Not an error, but is not to come out,,, I also don't know what wrong is 1 + (3 + 2) * (7 ^ 2 + 6 * 9)/(2) to calculate the formula to the penultimate step is wrong results
  • Related