链式堆栈完成中缀表达式到后缀表达式的转换
一直在gets函数报错,求问??请问代码正确了吗。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 100
/* run this program using the console pauser or add your own getch, system("pause") or str loop */
typedef ElementType;
typedef struct SNode *PtrToSNode;
struct SNode {
ElementType Data;
PtrToSNode Next;
};
typedef PtrToSNode Stack;
Stack CreateStack( )
{ /* 构建一个堆栈的头结点,返回该结点指针 */
Stack S;
S = (Stack)malloc(sizeof(struct SNode));
S->Next = NULL;
return S;
}
int IsEmpty ( Stack S )
{ /* 判断堆栈S是否为空,若是返回true;否则返回false */
return ( S->Next == NULL );
}
void Push( Stack S, ElementType X )
{ /* 将元素X压入堆栈S */
PtrToSNode TmpCell;
TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
TmpCell->Data = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
ElementType Pop( Stack S )
{ /* 删除并返回堆栈S的栈顶元素 */
PtrToSNode FirstCell;
ElementType TopElem;
if( IsEmpty(S) ) {
printf("堆栈空");
return 0;
}
else {
FirstCell = S->Next;
TopElem = FirstCell->Data;
S->Next = FirstCell->Next;
free(FirstCell);
return TopElem;
}
}
void change(Stack S, ElementType str[])
{
int i =0;
ElementType e;
CreatStack();
while(str[i]!='\0')
{
while(isdigit(str[i]))
{
printf("%c",str[i++]);
if(!isdigit(str[i]))
{
printf(" ");
}
}
if(str[i]=='+'||str[i]=='-')
{
if(S->Next=NULL)
{
Push(S,str[i]);
}
else
{
do
{
Pop(S);
if(e=='(')
{
Push(S, e);
}
else
{
printf("%c",e);
}
}
while(e!='(');
{
Push(S,str[i]);
}
}
}
else if(str[i]==')')
{
Pop( S);
while(e!='(')
{
printf("%c",e);
Pop(S);
}
}
else if(str[i]=='*'||str[i]=='/'||str[i]=='(')
{
Push(S,str[i]);
}
else if(str[i]=='\0')
{
break;
}
else
{
printf("\n输入格式有误\n");
return;
}
i++;
}
while (str[i]='\0')
{
Pop(S);
printf("%c",e);
}
}
int main(int argc, char *argv[]) {
ElementType str[m];
gets(str);
Stack S;
change(S,str);
return 0;
}