逆波兰表达式(后缀表达式):将正常表达式(中缀表达式)转化为代码理解的类型
以下是计算逆波兰表达式的结果代码:
`
#define INIT_SIZE 10240
#define ADD_SIZE 10
typedef struct {
char* base;
char* top;
int Stacksize;
} Stack;
void Init_stack(Stack* s) //此次为初始化栈
void Push_stack(Stack* s, char e) //此处为入栈
char Pop_stack(Stack* s) //此处为出栈
int Stacklen(Stack s) //计算栈的当前大小
void Destroy_stack(Stack* s) //销毁栈
void Calculate(Stack* s)
{
char c;
char str[10];
double d, e;
int i = 0;
printf("请输入数据,用空格隔开,;以#代表结束\n");
scanf(" %c", &c); // 注意前面的空格,用于跳过空白字符
while (c != '#') {
while (isdigit(c) || c == '.')
{
str[i++] = c;
if (i >= 10)
{
printf("输入单个数据过大!\n");
return;
}
scanf(" %c", &c);
}
str[i] = '\0';
d = atof(str);
Push_stack(s, d);
i = 0;
switch (c)
{
case '+':
e = Pop_stack(s);
d = Pop_stack(s);
Push_stack(s, d + e);
break;
case '-':
e = Pop_stack(s);
d = Pop_stack(s);
Push_stack(s, d - e);
break;
case '*':
e = Pop_stack(s);
d = Pop_stack(s);
Push_stack(s, d * e);
break;
case '/':
e = Pop_stack(s);
d = Pop_stack(s);
if (e != 0)
{
Push_stack(s, d / e);
} else
{
printf("出错,除数为0\n");
return;
}
break;
default:
break;
}
scanf(" %c", &c);
}
d = Pop_stack(s);
printf("结果为:%f\n", d);
}
接下来是中缀表达式转化为后缀表达式的代码
void Calculate(Stack* s)
{
char c;
char str[10];
double d, e;
int i = 0;
printf("请输入数据,用空格隔开,以#代表结束\n");
scanf(" %c", &c);
while (c != '#')
{
// 读取数字部分
while (isdigit(c) || c == '.')
{
str[i++] = c;
if (i >= 10)
{
printf("输入单个数据过大!\n");
return;
}
scanf("%c", &c);
}
str = '\0';
if (i > 0)
{ // 有效数字才转换
d = atof(str);
Push_stack(s, d);
}
i = 0;
// 处理运算符
switch (c)
{
case '+':
e = Pop_stack(s);
d = Pop_stack(s);
if (d == -1 || e == -1) return;
Push_stack(s, d + e);
break;
case '-':
e = Pop_stack(s);
d = Pop_stack(s);
if (d == -1 || e == -1) return;
Push_stack(s, d - e);
break;
case '*':
e = Pop_stack(s);
d = Pop_stack(s);
if (d == -1 || e == -1) return;
Push_stack(s, d * e);
break;
case '/':
e = Pop_stack(s);
d = Pop_stack(s);
if (d == -1 || e == -1) return;
if (e != 0)
{
Push_stack(s, d / e);
}
else
{
printf("出错,除数为0\n");
return;
}
break;
default:
break;
}
scanf(" %c", &c);
}
d = Pop_stack(s);
if (d != -1)
{
printf("结果为:%f\n", d);
}
}
//用于比较运算符优先级
int precedence(char op)
{
if (op == '+' op == '-') return 1;
if (op == '*' op == '/') return 2;
return 0; // 其他字符(如 '(')
}
char* Change_order(Stack* s)
{
char c;
char output[1024]; // 用于存储后缀表达式
int index = 0; // output 数组的索引
printf("请输入表达式,以 # 结束:\n");
char input[1025];
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0; // 去除换行符
for (int i = 0; input[i] != '#'; i++)
{
c = input[i];
if (isdigit(c) || c == '.')
{
while (isdigit(input[i]) || input[i] == '.')
{
output[index++] = input[i++];
}
output[index++] = ' '; // 数字后加空格作为分隔符
i--; // 因为循环中 i 会自增,所以这里要减一
}
else if (c == '(')
{
Push_stack(s, c);
}
else if (c == ')')
{
while (s->top != s->base && *(s->top - 1) != '(')
{
output[index++] = Pop_stack(s);
output[index++] = ' ';
}
Pop_stack(s); // 弹出 '('
}
else if (c == '*' || c == '/' || c == '+' || c == '-')
{
while (s->top != s->base && precedence(c) <= precedence(*(s->top - 1)))
{
output[index++] = Pop_stack(s);
output[index++] = ' ';
}
Push_stack(s, c);
}
else
{
printf("输入未定义字符,请重新输入!\n");
return;
}
}
// 将栈中剩余操作符弹出到 output 数组
while (s->top != s->base)
{
output[index++] = Pop_stack(s);
output[index++] = ' ';
}
output[index] = '\0'; // 添加字符串结束符
printf("后缀表达式为:%s\n", output);
return output;
}
int main()
{
Stack s;
char* myarr;
Init_stack(&s);
myarr = Change_order(&s);
Destroy_stack(&s);
return 0;
}
`