В чем заключается ошибка в следующем коде?
#include<stdio.h> #include<stdlib.h> struct stack { char data; struct stack * next; }; struct stack * createstack() { struct stack * s; s=(struct stack*)malloc(sizeof(struct stack)); return s; } void push(struct stack * s,char data) { struct stack * p; p=createstack(); p->data=data; p->next=NULL; if(s==NULL) { s=p; return ; } p->next=s; s=p; } char pop( struct stack * s) { struct stack * p; p=s; char d; d=p->data; s=p->next; free(p); return d; } int prec(char x) { if(x=='(') return 0; else if(x=='+' || x=='-') return 1; else if(x=='*' || x=='/') return 2; else if(x=='^') return 3; else return -1; } int isaplpha(char x) { if((x>='A' || x>='a')&&(x>='Z' || x>='z' )) return 1; return 0; } int main() { char * c; char s[20],x[20],d; int j=0; struct stack * head; head=NULL; printf("Enter the expression to convert it into postfix \n"); gets(s); c=&s[0]; while(*c!='\0') { if(isaplpha(*c)) x[j++]=*c; else if(*c=='(') push(head,*c); else if(*c==')') { d=pop(head); while(d!='(') { d=pop(head); x[j++]=d; } push(head,*c); } else { while(prec(head->data)>=prec(*c)) x[j++]=pop(head); push(head,*c); } c++; } while(head!=NULL) { x[j++]=pop(head); head=head->next; } puts(x); return 0; }
Что я уже пробовал:
#include<stdio.h> #include<stdlib.h> struct stack { char data; struct stack * next; }; struct stack * createstack() { struct stack * s; s=(struct stack*)malloc(sizeof(struct stack)); return s; } void push(struct stack * s,char data) { struct stack * p; p=createstack(); p->data=data; p->next=NULL; if(s==NULL) { s=p; return ; } p->next=s; s=p; } char pop( struct stack * s) { struct stack * p; p=s; char d; d=p->data; s=p->next; free(p); return d; } int prec(char x) { if(x=='(') return 0; else if(x=='+' || x=='-') return 1; else if(x=='*' || x=='/') return 2; else if(x=='^') return 3; else return -1; } int isaplpha(char x) { if((x>='A' || x>='a')&&(x>='Z' || x>='z' )) return 1; return 0; } int main() { char * c; char s[20],x[20],d; int j=0; struct stack * head; head=NULL; printf("Enter the expression to convert it into postfix \n"); gets(s); c=&s[0]; while(*c!='\0') { if(isaplpha(*c)) x[j++]=*c; else if(*c=='(') push(head,*c); else if(*c==')') { d=pop(head); while(d!='(') { d=pop(head); x[j++]=d; } push(head,*c); } else { while(prec(head->data)>=prec(*c)) x[j++]=pop(head); push(head,*c); } c++; } while(head!=NULL) { x[j++]=pop(head); head=head->next; } puts(x); return 0; }
jeron1
Расскажите нам, что вы пытаетесь сделать (ожидаемые входы и выходы) и что ваша программа делает не то, что вы хотите. Попробуйте сузить его до раздела кода, если сможете. Не так уж много людей будут проходить строчку за строчкой через ваш код, чтобы диагностировать его. Чем больше деталей, тем лучше, и задавайте конкретные вопросы.