#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define MAX_SIZE 50
#define LEN sizeof(LinkStackNode)
#define StackElementType char
typedef struct node
{
StackElementType data;
struct node* next;
}LinkStackNode;
typedef LinkStackNode* LinkStack;
int InitStack(LinkStackNode **L);
int IsEmpty(LinkStackNode *L);
int IsFull(LinkStackNode *p);
int Push(LinkStackNode *L, StackElementType x);
int Pop(LinkStackNode *L, StackElementType *x);
int GetTop(LinkStackNode *L, StackElementType *x);
int ExpEvaluation(LinkStack operand, LinkStack operator1);
char Compare(StackElementType x, StackElementType ch);
int Grading(StackElementType x);
int main()
{
LinkStack L_Digit, L_Operator;
InitStack(&L_Digit);
InitStack(&L_Operator);
printf("\n%d\n",ExpEvaluation(L_Digit, L_Operator));
return 0;
}
int Grading(StackElementType x)
{
switch(x)
{
case '#':
return -1;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
case '^':
return 4;
case ')':
return 5;
}
return (FALSE);
}
char Compare(StackElementType Left, StackElementType Right)
{
if(Left == '(' && Right == ')')
return '=';
if(Left == '(' || Right == '(')
return '<';
if(Left == ')' || Right == ')')
return '>';
if(Grading(Left) > Grading(Right))
return '>';
else if(Grading(Left) < Grading(Right))
return '<';
else
{
if(Left == '#')
return '=';
else
return '>';
}
}
int Execute(int a, char op, int b)
{
switch(op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/' :
return a / b;
case '^':
return pow(a,b);
}
}
int ExpEvaluation(LinkStack L_C, LinkStack L_D)
{
char ch;
StackElementType top_ch, op, digit, a, b;
int value = 0, temp;
Push(L_C, '#');
printf("\n \nPlase input an expression(Ending with #) :");
ch = getchar();
GetTop(L_C, &top_ch);
while(ch != '#' || top_ch != '#')
{
if(ch >= '0' && ch <= '9')
{
temp = ch - '0';
ch = getchar();
while(ch >= '0' && ch <= '9')
{
temp = temp * 10 + ch - '0';
ch = getchar();
}
Push(L_D, temp);
putchar(temp+'0');
}
else
{
GetTop(L_C, &top_ch);
switch(Compare(top_ch, ch))
{
case '<':
Push(L_C, ch);
ch = getchar();
break;
case '=' :
Pop(L_C, &top_ch);
ch = getchar();
break;
case '>':
Pop(L_C, &op);
if(op != '(' && op != ')')
putchar(op);
Pop(L_D, &b);
Pop(L_D, &a);
value = Execute((int)a, op, (int)b);
Push(L_D, value);
break;
}
}
GetTop(L_C, &top_ch);
}
GetTop(L_D, &digit);
value = digit;
return (value);
}
//进栈操作
int Push(LinkStack top, StackElementType x)
{
LinkStackNode* temp;
temp = (LinkStackNode*)malloc(sizeof(LinkStackNode));
if(temp == NULL)
return (FALSE);
temp->data = x;
temp->next = top->next;
top->next = temp;
return (TRUE);
}
//出栈操作
int Pop(LinkStack top, StackElementType *x)
{
LinkStackNode* temp;
temp = top->next;
if(temp == NULL)
return (FALSE);
top->next = temp->next;
*x = temp->data;
free(temp);
temp = NULL;
return (TRUE);
}
int InitStack(LinkStackNode **L)
{
(*L) = (LinkStackNode*)malloc(LEN);
(*L)->next = NULL;
if(*L == NULL)
return (FALSE);
else
return (TRUE);
}
int IsEmpty(LinkStackNode *L)
{
return (L->next == NULL? TRUE:FALSE);
}
int IsFull(LinkStackNode *p)
{
int num = 0;
while(p->next != NULL)
{
p = p->next;
num++;
}
return (num == MAX_SIZE? TRUE:FALSE);
}
int GetTop(LinkStackNode *L, StackElementType *x)
{
if(IsEmpty(L) == TRUE)
return (FALSE);
else
{
*x = L->next->data;
return (TRUE);
}
}
用栈来完成表达式求值
最新推荐文章于 2022-10-02 19:47:33 发布