wazouu 2020-02-09 11:36 采纳率: 0%
浏览 332

一个把中缀表达式转化成后缀表达式的程序,简单的1+1#正确,但稍微复杂一点就报错访问权限冲突,请问哪里错了啊

#include
#include
#include
typedef struct stacke* stack;
struct stacke {
char* chy;
int tope;
};
stack creat(int);
void pop(int, stack);//入栈
void top(stack);//出zhan
void prin(stack);
int main()
{
char ch;
stack st1, st2;
st1 = creat(100); st2 = creat(100);
while (1)
{
scanf_s("%c", &ch, sizeof(ch));
if (isdigit(ch))
{
pop(ch, st1);
}
else if (ch != '#')
{
if (st2->tope == -1)
{
pop(ch, st2);
}
else
{
if (ch == '+' || ch == '-')
{
while (st2->tope != -1 && st2->chy[st2->tope] != '(')
{
pop(st2->chy[st2->tope], st1); top(st2);
}
pop(ch, st2);
}
else if (ch == '*' || ch == '/')
{
while (st2->chy[st2->tope] != '+' && st2->chy[st2->tope] != '-')
{
if (st2->chy[st2->tope] == '(')
{
pop(ch, st2); break;
}
pop(st2->chy[st2->tope], st1); top(st2);
}
pop(ch, st2);
}
else if (ch == '(') pop(ch, st2);
else if (ch == ')')
{
while (st2->chy[st2->tope] != '(')
{
pop(st2->chy[st2->tope], st1); top(st2);
}
top(st2);
}
}
}
else
{
while (st2->tope != -1)
{
top(st2); pop(st2->chy[st2->tope + 1], st1);
}
break;
}
}
prin(st1);
return 0;
}
void pop(int n, stack s)
{
s->chy[++s->tope] = n;
}
void top(stack s)
{
s->tope--;
}
void prin(stack s)
{
int i = 0;
while (i != s->tope + 1)
{
printf("%c ", s->chy[i++]);
}
}
stack creat(int a)
{
stack s;
s = (stack)malloc(sizeof(stack));
s->chy = (char*)malloc(sizeof(char*) * a);
s->tope = -1;
return s;
}

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档