问题
My lecturer gave me an assignment to create a program to convert an infix expression to postfix using Stack. I've made the stack classes and some functions to read the infix expression.
But this one function, called inToPos(char string[]) which is responsible to convert the inFix expression in the string inFix to the post fix expression in the string postFix using stacks, is creating a breakpoint. Can you guys help me out and tell me what I'm doing wrong?
These are my codes that badly needs your help.. :)
#include<stdio.h>
#include<stdlib.h>
#define MAX 15
#define true 1
#define false 0
typedef struct node* nodeptr;
typedef struct node{
int data;
nodeptr next;
}Node;
typedef struct{
int count;
nodeptr top;
}Stack;
typedef Stack* StackList;
StackList create();
void display(StackList list);
int isEmpty(StackList list);
void push(StackList list, int item);
void pop(StackList list);
int inToPos(char string[]);
int isOperator(char string[], int i);
int precedence(char x);
StackList create(){
StackList list;
list=(StackList)malloc(sizeof(Stack));
list->count=0;
list->top=NULL;
return list;
}
void display(StackList list){
nodeptr ptr;
ptr=list->top;
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
int isEmpty(StackList list){
return list->count==0;
//return list->top==NULL;
}
void push(StackList list, int item){
nodeptr temp;
temp=(nodeptr)malloc(sizeof(Node));
temp->data=item;
temp->next=list->top;
list->top=temp;
(list->count)++;
}
void pop(StackList list){
nodeptr temp;
temp=list->top;
list->top=temp->next;
temp->next=NULL;
free(temp);
(list->count)--;
}
int inToPos(char string[]){
int i,a=0;
char postfix[MAX];
StackList list=create();
for(i=0;string[i]!='\0';i++){
if(!isOperator(string,i)){
postfix[a]=string[i];
a++;
}
else if(isEmpty(list))
push(list,string[i]);
else{
if(precedence(string[i])>precedence(list->top->data))
push(list,string[i]);
else{
postfix[a]=list->top->data;
a++;
pop(list);
if(!isEmpty(list)){
while(precedence(list->top->data)<=precedence(string[i])){
postfix[a]=list->top->data;
a++;
pop(list);
}
}
else
push(list,string[i]);
}
}
}
puts(postfix);
}
int isOperator(char string[], int i){
switch(string[i])
{
case '+':
case '-':
case '*':
case '%':
case '/': return true;
default: return false;
}
}
int precedence(char x){
switch(x)
{
case '%':
case '*':
case '/': return 2;
case '+':
case '-': return 1;
default: return 0;
}
}
int main(void){
char string[MAX]="a+b*c-d";
inToPos(string);
}
Note the function inToPos was made using this algorithm:
- Scan the Infix string from the left to right.
- Initialise an empty stack.
- If the scanned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
- If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack(topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character. Repeat this step till the characters are scanned.
- (After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.
- Return the postfix string.
回答1:
You really should learn how to use a debugger, it's a great tool for figuring out problems like these. However, I ran it and figured out your problem:
On this line:
while(precedence(list->top->data)<=precedence(string[i])){
When you're iterating through the list, you need to check whether the stack is empty each time, not only before you go into the loop. So, do something like this:
while(!isEmpty(list) && precedence(list->top->data)<=precedence(string[i])){
instead.
回答2:
[Program to convert infix to postfix using stack][1]
C program to convert infix to postfix using stackC
#define SIZE 50
#include <ctype.h>
char s[SIZE];
int top=-1; /* Global declarations */
push(char elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}
char pop()
{ /* Function for POP operation */
return(s[top--]);
}
int pr(char elem)
{ /* Function for precedence */
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
main()
{ /* Main Program */
char infix[50],postfix[50],ch,elem;
int i=0,k=0;
printf("\n\nEnter Infix Expression : ");
scanf("%s",infix);
push('#');
while( (ch=infix[i++]) != '\0')
{
if( ch == '(') push(ch);
else
if(isalnum(ch)) postfix[k++]=ch;
else
if( ch == ')')
{
while( s[top] != '(')
postfix[k++]=pop();
elem=pop(); /* Remove ( */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(ch) )
postfix[k++]=pop();
push(ch);
}
}
while( s[top] != '#') /* Pop from stack till empty */
postfix[k++]=pop();
postfix[k]='\0'; /* Make postfix as valid string */
printf("\nPostfix Expression = %s\n",postfix);
}
来源:https://stackoverflow.com/questions/15329919/infix-to-postfix-using-stack