中缀到后缀
我一直在试图解决这个问题。 我有一个任务是制作一个基本计算器。
为此,我需要 postfix 中的说明。 我在网上找到了一些代码,该代码有效但使用了 gets()。
我尝试更换 gets... 但程序不再工作。这是代码,我希望有人能找到错误(使用 2+4 作为输入,它读取并识别 2 作为数字,然后 + 作为运算符,然后 4 作为数字......然后陷入困境沿线某处循环)
要明确的是,使用此代码对于我的作业来说是公平的游戏,只要我引用它作为参考(因为它只是一小部分)。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 10
#define EMPTY -1
struct stack
{
char data[MAX];
int top;
};
int isempty(struct stack *s)
{
printf("isempty\n");
return (s->top == EMPTY) ? 1 : 0;
}
void emptystack(struct stack* s)
{
printf("emptystack\n");
s->top=EMPTY;
}
void push(struct stack* s,int item)
{
printf("push\n");
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
printf("add to stack\n");
++s->top;
s->data[s->top]=item;
}
}
char pop(struct stack* s)
{
printf("pop\n");
char ret=(char)EMPTY;
if(!isempty(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void display(struct stack s)
{
printf("display\n");
while(s.top != EMPTY)
{
printf("not empty\n");
printf("\n%d",s.data[s.top]);
s.top--;
}
}
int isoperator(char e)
{
getchar();
printf("isoperator\n");
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
return 1;
else
return 0;
}
int priority(char e)
{
printf("priority\n");
int pri = 0;
if(e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
return pri;
}
void infix2postfix(char* infix, char * postfix, int insertspace)
{
printf("in infix2postfix\n");
char *i,*p;
struct stack X;
char n1;
emptystack(&X);
i = &infix[0];
p = &postfix[0];
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
printf("is digit.\n");
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
i++;
}
if( isoperator(*i) )
{
if(isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
*p = '\0';
}
int main()
{
char in[50],post[50],temp[50];
strcpy(&post[0],"");
printf("Enter Infix Expression : ");
fflush(stdin);
fgets(in,50,stdin);
printf("%s",in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is : %s\n",&post[0]);
return 0;
}
感谢您的帮助,我真的很感激:)。
I've been trying to figure out this problem.
I have an assignment to make a basic calculator.
To do so i need the instructions in postfix.
I have found some code online, which worked but used gets().
I tried replacing the gets... but the program no longer works. Here is the code, i was hoping someone could find the error (using 2+4 as the input, it reads and recognizes 2 as a digit, then + as an operator, then 4 as a digit... then gets stuck in a loop somewhere along the line)
To be clear, using this code is fair game for my assignment, as long as I cite is as a reference (because it is only a small part).
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 10
#define EMPTY -1
struct stack
{
char data[MAX];
int top;
};
int isempty(struct stack *s)
{
printf("isempty\n");
return (s->top == EMPTY) ? 1 : 0;
}
void emptystack(struct stack* s)
{
printf("emptystack\n");
s->top=EMPTY;
}
void push(struct stack* s,int item)
{
printf("push\n");
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
printf("add to stack\n");
++s->top;
s->data[s->top]=item;
}
}
char pop(struct stack* s)
{
printf("pop\n");
char ret=(char)EMPTY;
if(!isempty(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void display(struct stack s)
{
printf("display\n");
while(s.top != EMPTY)
{
printf("not empty\n");
printf("\n%d",s.data[s.top]);
s.top--;
}
}
int isoperator(char e)
{
getchar();
printf("isoperator\n");
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
return 1;
else
return 0;
}
int priority(char e)
{
printf("priority\n");
int pri = 0;
if(e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
return pri;
}
void infix2postfix(char* infix, char * postfix, int insertspace)
{
printf("in infix2postfix\n");
char *i,*p;
struct stack X;
char n1;
emptystack(&X);
i = &infix[0];
p = &postfix[0];
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
printf("is digit.\n");
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
i++;
}
if( isoperator(*i) )
{
if(isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
*p = '\0';
}
int main()
{
char in[50],post[50],temp[50];
strcpy(&post[0],"");
printf("Enter Infix Expression : ");
fflush(stdin);
fgets(in,50,stdin);
printf("%s",in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is : %s\n",&post[0]);
return 0;
}
Thanks for the help, i really appreciate it :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
fgets()
当字符串到达 1 时,会在字符串中包含一个换行符,因此您将得到一个显示为“2+4\n”的字符串。将while (*i)
替换为while (*i && *i != '\n')
并看看结果如何。fgets()
includes a newline in the string when it gets to one, so you've got a string reading "2+4\n". Replace thewhile (*i)
withwhile (*i && *i != '\n')
and see where that gets you.