一般算术表达式转化为逆波兰式#include#include#define SM 100typedef struct {char data[SM];int top;}Sqstack;void main(){Sqstack *sq;sq->top=0;int i=0,t=0;char ch,str[100],exp[100];printf("输入表达式(以#结尾):");do {scanf("%c"
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/18 23:53:47
一般算术表达式转化为逆波兰式#include#include#define SM 100typedef struct {char data[SM];int top;}Sqstack;void main(){Sqstack *sq;sq->top=0;int i=0,t=0;char ch,str[100],exp[100];printf("输入表达式(以#结尾):");do {scanf("%c"
一般算术表达式转化为逆波兰式
#include
#include
#define SM 100
typedef struct
{
char data[SM];
int top;
}Sqstack;
void main()
{
Sqstack *sq;
sq->top=0;
int i=0,t=0;
char ch,str[100],exp[100];
printf("输入表达式(以#结尾):");
do
{scanf("%c",&ch);
str[i++]=ch;}
while(ch!='#');
//输入要转换的表达式
for(i=0;str[i]!='#';i++)
{
if(ch>='0'&&chdata[sq->top]=str[i];
sq->top++;}
if(str[i]==')')
{while(sq->data [sq->top-1]!='(')
{exp[t++]=sq->data [sq->top-1];
sq->top--;
}
sq->top--;}
else
if(str[i]=='+'||str[i]=='-')
{while (sq->top==0&&sq->data[sq->top-1]!='(')
{exp[t]=sq->data[sq->top-1];
sq->top--;
t++;}
sq->data[sq->top]=str[i];
sq->top++;}
else
if(str[i]=='*'||str[i]=='/')
{while(sq->data[sq->top-1]=='*'||sq->data[sq->top-1]=='/')
{exp[t]=sq->data[sq->top-1];
sq->top--;
t++;}
sq->data[sq->top]=str[i];
sq->top++;}
}
while(sq->top!=0)
{exp[t++]=sq->data[sq->top---1];}
exp[t]='#';
for(i=0;i
一般算术表达式转化为逆波兰式#include#include#define SM 100typedef struct {char data[SM];int top;}Sqstack;void main(){Sqstack *sq;sq->top=0;int i=0,t=0;char ch,str[100],exp[100];printf("输入表达式(以#结尾):");do {scanf("%c"
人家不是告诉你了嘛,变量sq没有被初始化,那你就把它初始化一下吧