#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
char data[MAX];
int top;
}Stack1;
typedef struct
{
char data[MAX];
int top;
}Stack2;
void Init(Stack1 *stack1,Stack2 *stack2)
{
stack1->top=stack2->top=-1;
}
int check(char ch,Stack1 *stack1,Stack2 *stack2)
{
int i;
switch(ch)
{
case '+':
if(stack1->data[stack1->top]=='-'||stack1->data[stack1->top]=='*'||stack1->data[stack1->top]=='/')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case '-':
if(stack1->data[stack1->top]=='+'||stack1->data[stack1->top]=='*'||stack1->data[stack1->top]=='/')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case '*':
if(stack1->data[stack1->top]=='/')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case '/':
if(stack1->data[stack1->top]=='*')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case ')':
i=stack1->top;
while(stack1->data[i]!='(' && i>=0)i--;
if(i==-1)return 0;
while(stack1->data[stack1->top]!='(')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
}
stack1->top--;
return 1;
}
}
void main(){
int i,flag,k=0,sum1,sum2;
char ch[MAX];
Stack1 *stack1=(Stack1 *)malloc(sizeof(Stack1));
Stack2 *stack2=(Stack2 *)malloc(sizeof(Stack2));
Init(stack1,stack2);
printf("请输入中缀表达式:");
gets(ch);
while(ch[k])
{
if(ch[k]>='0'&&ch[k]<='9')
stack2->data[++stack2->top]=ch[k];
else if(ch[k]>='a'&&ch[k]<='z' || ch[k]>='A'&&ch[k]<='Z')
stack2->data[++stack2->top]=ch[k];
else if(ch[k]=='(')
stack1->data[++stack1->top]=ch[k];
else if(ch[k]=='+' ||ch[k]=='-'||ch[k]=='*' || ch[k]=='/'||ch[k]==')')
flag=check(ch[k],stack1,stack2);
if(!flag) break;
k++;
}
if(flag)
{
for(i=0;i<=stack1->top;i++)
{
if(ch[i]=='(') sum1++;
if(ch[i]==')') sum2++;
}
if(sum1!=sum2)
{
printf("表达式错误. ");
return;
}
printf("后缀表达式为:");
for(i=0;i<=stack2->top;i++)
printf("%c",stack2->data[i]);
for(i=stack1->top;i>=0;i--)
printf("%c",stack1->data[i]);
printf(" ");
}
else
printf("表达式错误. ");
getch();
}
#include<stdlib.h>
#define MAX 100
typedef struct
{
char data[MAX];
int top;
}Stack1;
typedef struct
{
char data[MAX];
int top;
}Stack2;
void Init(Stack1 *stack1,Stack2 *stack2)
{
stack1->top=stack2->top=-1;
}
int check(char ch,Stack1 *stack1,Stack2 *stack2)
{
int i;
switch(ch)
{
case '+':
if(stack1->data[stack1->top]=='-'||stack1->data[stack1->top]=='*'||stack1->data[stack1->top]=='/')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case '-':
if(stack1->data[stack1->top]=='+'||stack1->data[stack1->top]=='*'||stack1->data[stack1->top]=='/')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case '*':
if(stack1->data[stack1->top]=='/')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case '/':
if(stack1->data[stack1->top]=='*')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
stack1->data[++stack1->top]=ch;
}
else
stack1->data[++stack1->top]=ch;
return 1;
case ')':
i=stack1->top;
while(stack1->data[i]!='(' && i>=0)i--;
if(i==-1)return 0;
while(stack1->data[stack1->top]!='(')
{
stack2->data[++stack2->top]=stack1->data[stack1->top--];
}
stack1->top--;
return 1;
}
}
void main(){
int i,flag,k=0,sum1,sum2;
char ch[MAX];
Stack1 *stack1=(Stack1 *)malloc(sizeof(Stack1));
Stack2 *stack2=(Stack2 *)malloc(sizeof(Stack2));
Init(stack1,stack2);
printf("请输入中缀表达式:");
gets(ch);
while(ch[k])
{
if(ch[k]>='0'&&ch[k]<='9')
stack2->data[++stack2->top]=ch[k];
else if(ch[k]>='a'&&ch[k]<='z' || ch[k]>='A'&&ch[k]<='Z')
stack2->data[++stack2->top]=ch[k];
else if(ch[k]=='(')
stack1->data[++stack1->top]=ch[k];
else if(ch[k]=='+' ||ch[k]=='-'||ch[k]=='*' || ch[k]=='/'||ch[k]==')')
flag=check(ch[k],stack1,stack2);
if(!flag) break;
k++;
}
if(flag)
{
for(i=0;i<=stack1->top;i++)
{
if(ch[i]=='(') sum1++;
if(ch[i]==')') sum2++;
}
if(sum1!=sum2)
{
printf("表达式错误. ");
return;
}
printf("后缀表达式为:");
for(i=0;i<=stack2->top;i++)
printf("%c",stack2->data[i]);
for(i=stack1->top;i>=0;i--)
printf("%c",stack1->data[i]);
printf(" ");
}
else
printf("表达式错误. ");
getch();
}