c - 将 INFIX 表达式转换为等效的 POSTFIX 表达式
问题描述
我在 Turbo C++ 代码的第 28 行收到“调用‘push’时参数 1 中的类型不匹配”错误,请帮助我,因为我必须提交我的项目,我知道 Turbo C++ 是一个非常古老的编译器但这就是我们大学老师的建议,所以我对此无能为力
''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' '''''
#include <stdio.h>
#include <conio.h>
int stack[100];
int top = -1;
void in2post(char[]);
void push(int);
int pop();
int prec(char);
int cal(char[]);
void main()
{
char in[100], post[100];
clrscr();
printf("Enter an infix expression: ");
gets(in);
in2post(in);
getch();
}
void in2post(char in_exp[])
{
int x = 0, y = 0, z, result = 0;
char a, c, post_exp[100];
char t;
push("\0");
t = in_exp[x];
while (t != '\0')
{
if (isalnum(t))
{
post_exp[y] = t;
y++;
}
else if (t == '(')
{
push('(');
}
else if (t == ')')
{
while (stack[top] != '(')
{
c = pop();
post_exp[y] = c;
y++;
}
c = pop();
}
else
{
while (prec(stack[top]) >= prec(t))
{
c = pop();
post_exp[y] = c;
y++;
}
push(t);
}
x++;
t = in_exp[x];
}
while (top != -1)
{
c = pop();
post_exp[y] = c;
y++;
}
printf("\nThe equivalent postfix expression is: ");
for (z = 0; z < y; z++)
printf("%c", post_exp[z]);
printf("\n\nDo you want to evaluate the postfix expression? (Y/N): ");
scanf("%c", &a);
if (a == 'y' || a == 'y')
{
result = cal(post_exp);
printf("\nResult = % d\n", result);
getch();
}
else if (a == 'n' || a == 'N')
{
exit(0);
}
}
int cal(char post[])
{
int m, n, x, y, j = 0, len;
len = strlen(post);
while (j < len)
{
if (isdigit(post[j]))
{
x = post[j] - '0';
push(x);
}
else
{
m = pop();
n = pop();
switch (post[j])
{
case '+':
x = n + m;
break;
case '-':
x = n - m;
break;
case '*':
x = n * m;
break;
case '/':
x = n / m;
break;
}
push(x);
''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' '''''
解决方案
推荐阅读
- asp.net - .less 文件不渲染
- c - 使用数组的平均分数 - 由用户在特定时间输入
- c# - 使用 LINQ 进行聚合
- sql - #SQL优化订单模型
- javascript - 在 JavaScript 中使用字符串正则表达式的第一个字母
- ios - iOS 13 通过 AWS SDK 在后台 iOS 中上传多个文件
- html - 如何读取带有印地语字符的 CSV 文件并在 html 上显示?
- python-3.x - 在python中读取具有32位精度整数的txt文件
- python - [Dockerfile 中的 Python],如何找出“Requirements.txt”文件中包的正确顺序是什么?
- angular - 动态路线上的 Angular 8 SEO 图像和内容