#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
const int Size = 50;
char Stack[Size];
char SuffixExpression[Size]; /*后缀表达式数组*/
int top = -1; /*始终指向栈顶*/
int index = 0; /*后缀表达式索引*/
/*----------------栈基本操作-------------*/
void Push(char x) /*压栈*/
{
if (top == Size-1)
{
printf("top=%d stack overflow\n", top);
exit(1);
}
Stack[++top] = x;
}
char Pop() /*出栈*/
{
if (top == -1)
{
printf("top=%d stack overflow\n", top);
exit(1);
}
return Stack[top--];
}
char GetTop() /*获取栈顶元素*/
{
if (-1 != top)
{
return Stack[top];
}
return NULL;
}
bool Empty() /*判断栈是否为空*/
{
return (top == -1 ? 1: 0);
}
/*------------计算结果-----------------*/
int Calc(int num1, int num2, char op)
{
switch(op)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (0 == num2)
{
printf("The divisor is 0\n");
exit(1);
}
return num1 / num2;
default:
return 0;
}
}
/*-----------运算符优先级比较--------------*/
int PriorityCompare(char op1, char op2)
{
switch(op1)
{
case '+':
case '-':
return (op2 == '*' || op2 == '/' ? -1:0);
case '*':
case '/':
return (op2 == '+' || op2 == '-' ? 1:0);
case '(':
return 1;
default:
return 1;
}
return 1;
}
/*-----------判断字符是否是运算符---------*/
int OperatorSymbol(char op)
{
switch(op)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
return 1;
default :
return 0;
}
return 0;
}
/*--------将中缀表达式转换成后缀表达式------------*/
void ConvertSuffixExpression(const char* p)
{
while(*p != '\0')
{
if(isdigit(*p)) /*处理数字*/
{
SuffixExpression[index++] = *p;
p++;
}
else if(1 == OperatorSymbol(*p)) /*处理运算符*/
{
if (Empty()) /*栈为空,运算符直接入栈*/
{
Push(*p);
p++;
}
else if ('(' == *p) /*左括号直接压栈*/
{
Push(*p);
p++;
}
else if(')' == *p) /*右括号取出栈中()之间的运算符*/
{
while('(' != GetTop())
{
SuffixExpression[index++] = GetTop();
Pop();
}
Pop(); /* '(' 出栈*/
p++;
}
else if(1==PriorityCompare(*p,GetTop()) || '('==GetTop()) /*当前运算符比栈顶运算符优先级高或栈顶为左括号则直接入栈*/
{
Push(*p);
p++;
}
else
{
while (1!=PriorityCompare(*p,GetTop()) && !Empty()) /*栈顶运算符优先小于等于当前运算符优先级则出栈*/
{
if ('(' == GetTop()) /*栈顶为左括号*/
{
continue;
}
SuffixExpression[index++] = GetTop();
Pop();
}
Push(*p);
p++;
}
}
else
{
printf("%c is invaild symbol\n", *p);
exit(1);
}
}
while(!Empty())
{
SuffixExpression[index++] = GetTop();
Pop();
}
SuffixExpression[index++] = '\0';
}
/*------------根据后缀表达式计算结果----------*/
int Calculate()
{
int i;
char TempSum;
int num1, num2;
int len = strlen(SuffixExpression);
for(i=0; i<len; i++)
{
if (isdigit(SuffixExpression[i])) /*数字直接压栈*/
{
Push(SuffixExpression[i]);
}
else if (OperatorSymbol(SuffixExpression[i])) /*当前符号是运算符则从栈中取出两个数运算,将结果继续压栈*/
{
num1 = GetTop()-'0';
Pop();
num2 = GetTop()-'0';
Pop();
TempSum = Calc(num2, num1, SuffixExpression[i]) + '0';
Push(TempSum);
}
}
return GetTop()-'0'; /*栈顶值即为表达式值*/
}
int expr(const char* p)
{
if (NULL == p)
{
printf("null pointer\n");
exit(1);
}
memset(Stack, 0, Size);
memset(SuffixExpression, 0, Size);
ConvertSuffixExpression(p);
return Calculate();
}
void main ()
{
/*char *exp = "9+(3-1)*3+8/2";*/
/*char *exp = "1";*/
/*char *exp = "1+2";*/
/*char *exp = "3-2";*/
/*char *exp = "1+2+3";*/
/*char *exp = "3-2-1";*/
/*char *exp = "(1+2)-3";*/
char *exp = "9*1+(2-1)+2*3";
int sum = expr(exp);
printf("%s\n", SuffixExpression);
printf("%d\n", sum);
}
express
最新推荐文章于 2023-11-12 23:39:13 发布