题目:将任意一个合法的中缀表达式转换成逆波兰式
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void PushOperation(char *opera, char *string, int *op, int *s);
void PexpretoSexpre(char *string);
void PexpretoSexpre(char *string)
{
char num[100] = "0"; /* 存储后缀表达式 */
char opera[100] = "0"; /* 存储运算符 */
/*
num----j
opera----op
string----i
*/
int i, j, op;
op = i = j = 0;
while (string[i] != '\0')
{
if (isdigit(string[i])) /* 如果是数字 */
{
num[j] = string[i]; /* 数字直接入后缀表达式栈 */
j++;
i++;
}
else
{
switch (string[i]) /* 如果是操作数 */
{
case '+':
{
if (op == 0) /* 如果是空栈 */
{
PushOperation(opera, string, &op, &i); /* 入运算符栈 */
break;
}
if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
{
switch (opera[op-1])
{
case '+':
{
PushOperation(opera, string, &op, &i);
break;
}
case '-':
{
PushOperation(opera, string, &op, &i);
break;
}
case '*':
{ /* 加法优先级低于乘法 */
num[j] = opera[op-1]; /* 将操作数出栈 */
opera[op-1] = string[i]; /* 将新的操作数压入栈中 */
j++;
i++;
break;
}
case '/':
{
num[j] = opera[op-1];
opera[op-1] = string[i];
j++;
i++;
break;
}
case '(':
{
PushOperation(opera, string, &op, &i);
break;
}
}
}
break;
}
case '-':
{
if (op == 0)
{
PushOperation(opera, string, &op, &i);
break;
}
if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
{
switch (opera[op-1])
{
case '+':
{
PushOperation(opera, string, &op, &i);
break;
}
case '-':
{
PushOperation(opera, string, &op, &i);
break;
}
case '*':
{
num[j] = opera[op-1];
opera[op-1] = string[i];
j++;
i++;
break;
}
case '/':
{
num[j] = opera[op-1];
opera[op-1] = string[i];
j++;
i++;
break;
}
case '(':
{
PushOperation(opera, string, &op, &i);
break;
}
}
}
break;
}
case '*':
{
if (op == 0)
{
PushOperation(opera, string, &op, &i);
break;
}
if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
{
switch (opera[op-1])
{
case '+':
{
PushOperation(opera, string, &op, &i);
break;
}
case '-':
{
PushOperation(opera, string, &op, &i);
break;
}
case '*':
{
PushOperation(opera, string, &op, &i);
break;
}
case '/':
{
PushOperation(opera, string, &op, &i);
break;
}
case '(':
{
PushOperation(opera, string, &op, &i);
break;
}
}
}
break;
}
case '/':
{
if (op == 0)
{
PushOperation(opera, string, &op, &i);
break;
}
if (opera[op-1] == '+' || opera[op-1] == '-' || opera[op-1] == '*' || opera[op-1] == '/' || opera[op-1] == ')' || opera[op-1] == '(')
{
switch (opera[op-1])
{
case '+':
{
PushOperation(opera, string, &op, &i);
break;
}
case '-':
{
PushOperation(opera, string, &op, &i);
break;
}
case '*':
{
PushOperation(opera, string, &op, &i);
break;
}
case '/':
{
PushOperation(opera, string, &op, &i);
break;
}
case '(':
{
PushOperation(opera, string, &op, &i);
break;
}
}
}
break;
}
case '(':
{
PushOperation(opera, string, &op, &i);
break;
}
case ')': /* 如果遇到右括号 */
{
while (opera[op-1] != '(')
{
num[j] = opera[op-1]; /* 将运算符栈中的元素依次入栈到后缀表达式栈中,直到遇到左括号为止 */
j++;
op--;
}
op--;
i++;
break;
}
default:
{
printf("传入表达式不符合要求\n");
exit(0);
}
}
}
}
while (op != 0)
{
num[j] = opera[op-1]; /* 将运算符栈中的元素依次入栈到后缀表达式栈中 */
j++;
op--;
}
num[j] = '\0';
i = 0;
while (num[i] != '\0') /* 将后缀表达式存储到传入的形参string中 */
{
string[i] = num[i];
i++;
}
string[i] = '\0';
for(i=0;i<sizeof(string);i++)
printf("%c",string[i]);
}
/* Function: 入运算符栈*/
void PushOperation(char *opera, char *string, int *op, int *s)
{
opera[*op] = string[*s];
(*op)++;
(*s)++;
}
void main()
{
char string[100];
gets(string);
PexpretoSexpre(string);
}