四则运算表达式的处理,并计算出结果。
代码如下
权当练习吧。
反正有点水。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct SNode* Stack;
typedef struct element
{
double number;
char sign;
} ElementType;
ElementType ERROR;
typedef struct SNode
{
ElementType data;
Stack next;
};
Stack CreatStack()//创建一个空链表,并且返回头结点
{
Stack S;
S=(Stack)malloc(sizeof(struct SNode));
S->next=NULL;
return S;
}
int isEmpty(Stack S)//探测栈是否是空,若为空则返回1,反之返回0
{
return (S->next==NULL);
}
void Push(ElementType item,Stack S)//填装栈
{
Stack tempCell;
tempCell=(Stack)malloc(sizeof(struct SNode));
tempCell->data=item;
tempCell->next=S->next;
S->next=tempCell;
return S;
}
ElementType Pop(Stack S)//释放栈顶元素
{
Stack ptr;
ElementType topElm;
if(isEmpty(S))
{
printf("ERROR!\nThis Stack is empty\n!");
ERROR.sign=-255;
return ERROR;
}
else
{
ptr=S->next;
S->next=ptr->next;
topElm=ptr->data;
free(ptr);
return topElm;
}
}
void Destroy(Stack S)//销毁一个栈
{
while(!isEmpty(S))
{
Pop(S);
}
free(S);
}
St