Infix to Postfix

Hi I recently just finished my program about converting Infix to postfix, it runs okay when I input addtion+, division/ and multiplication* sign but when I input only minus- sign I always get an exe.error

heres the code . thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
h:
typedef struct{
int count;
struct node *top;
}STACK;

typedef struct node{
char data;
struct node *link;
}STACK_NODE;

void initialeq(char a[]);
void initialize(STACK *head);
void testeq(STACK *head,char a[]);
void push(STACK *head,char item);
char pop(STACK *head);
int isEmpty(STACK *head);
void display(STACK *head);

m:

#include<stdio.h>
#include<stdlib.h>
#include"InfixToPostfix.h"

main(){

STACK *head;
char arr[20],a[10];
int x,y=0,v=0;
char s;
head=(STACK*)malloc(sizeof(STACK));
initialize(head);
printf("Enter a Mathematical Equation: ");
gets(arr);
y=strlen(arr);
for(x=0;x<y;x++){
if(arr[x]!='-'&&arr[x]!='+'&&arr[x]!='*'&&arr[x]!='/'){
a[v]=arr[x];v++;
}
else if(arr[x]!='-'&&head->count==0){
push(head,arr[x]);
}
else if(arr[x]!='+'&&head->count!=0){
if(head->top->data=='*'|| head->top->data=='/'||head->top->data=='-'){
a[v]=pop(head);
v++;
}
push(head,arr[x]);
}
else{
push(head,arr[x]);
}
if(arr[x]=='-'&&head->count!=0){
if(head->top->data=='*'||head->top->data=='/'||head->top->data=='-'){
a[v]=pop(head);
v++;
}
}
}
if(!isEmpty&&head->top->data=='*'||head->top->data=='/'||head->top->data=='-'){
a[v]=pop(head);
v++;
push(head,arr[x]);
}

else{
push(head,arr[x]);
}
if(arr[x]=='*'){
push(head,arr[x]);
}
else if(arr[x]=='/'){
push(head,arr[x]);
}
for(x=0;x<v;x++){
printf("%c ",a[x]);
}
display(head);
}

c:

#include<stdio.h>
#include<stdlib.h>
#include"InfixToPostfix.h"

void initialize(STACK *head){
head->top=NULL;
head->count=0;
}

void push(STACK *head, char item){
STACK_NODE *temp;
temp=(STACK_NODE*)malloc(sizeof(STACK_NODE));
if(temp!=NULL){
temp->data=item;
temp->link=NULL;

temp->link=head->top;
head->top=temp;
(head->count)++;
}
}

char pop(STACK *head){
STACK_NODE *temp;
char a;
if(!isEmpty(head)){
temp=head->top;
a=temp->data;
head->top=temp->link;
temp->link=NULL;
free(temp);
(head->count)--;
}
return a;
}

int isEmpty(STACK *head){
return head->top==NULL;
}

void display(STACK *head){
STACK_NODE *ptr;
ptr=head->top;
while(ptr!=NULL){
printf("%c ",ptr->data);
ptr=ptr->link;
}
printf("\n");
}


Last edited on
Topic archived. No new replies allowed.