把中序算法转化为后序算法的初步思路
代码如下:
#include<stdio.h>
#include<stdlib.h>
struct list
{
char fu[10];
int top;
};
struct list* Creat()
{
struct list* p;
p=(struct list *)malloc(sizeof(struct list));
p->top=-1;
return p;
}
void pop(struct list *head,char p){
head->fu[++head->top]=p;
}
void push(struct list *head,char*p2){
*p2=head->fu[head->top--];
}
int ad(struct list *head,char p,char*p2)//判断字符的操作
{
if(head->top!=-1)
{
if(p=='+'||p=='-')
{
push(head,p2);
return 1;
}
if(p=='*'||p=='/'||p=='%')
{
if(head->fu[head->top]=='*'||head->fu[head->top]=='/'||head->fu[head->top]=='%'){
push(head,p2);
return 1;
}
pop(head,p);
return 0;
}
/* if(p=='(')
{
pop(head,p);
}
if(p==')')
{
while(head->fu[head->top]!='('){
push(head,p2);
p2++;
ad(head,p,p2);
}
push(head,p2);
p2++;
ad(head,p,p2);
}*/
}
if(head->top==-1)
pop(head,p);
return 0;
}
void print(struct list* head,char *p,char*p2)//判断是否是字符的操作
{
int i1=0;
while(p[i1])
{
if(p[i1]!='+'&&p[i1]!='-'&&p[i1]!='*'&&p[i1]!='/'&&p[i1]!='('&&p[i1]!=')')
{
*p2=p[i1];
p2++;
}
else{
while(ad(head,p[i1],p2))//在print中 进行字符数组地址前进,每一次操作,必定返回print函数 若要持续进行操作。必须每次返回print
p2++;
}
i1++;
}
push(head,p2);
}
int main()
{
struct list*head;
char zhong[20],hou[20];
head=Creat();
gets(zhong);
print(head,&zhong[0],&hou[0]);
puts(hou);
return 0;
}
流程如下。输入一串字符,进行初步判断,若不为符号,则直接输出,若遇到符号,入栈(此时符号栈为空,不需要判断符号的先后),当符号栈为非空时,进行符号入栈需要进行判断,暂时只能进行非括号的运算转化,有括号出现乱码,代改进