M
Maxx
I'm writing this program which converts a given infix expression to
postfix expression, store the postfix expression in array prints
it ,and then prints the sum. I have used link list to implement the
pushdown stack. The program gets compiled correctly but its not giving
correct output at all. Here is the program i wrote::
#include <stdio.h>
#include <ctype.h>
#define MAX 1000
int rev[MAX];
int *p=rev;
struct node
{
int key;
struct node *next;
};
struct node *head,*z,*t;
void stackinitze()
{
head=(struct node *)malloc(sizeof *head);
z=(struct node *)malloc(sizeof *z);
head->next=z;
head->key=0;
z->next=z;
}
void push(int v)
{
t=(struct node *)malloc(sizeof *t);
t->key=v;
t->next=head->next;
head->next=t;
}
int pop()
{
int x;
t=head->next;
head->next=t->next;
x=t->key;
free(t);
return x;
}
int main(int argc, char **argv)
{
int c=*argv[1],i=0,j=1;
for(stackinitze();--argc>0;c=*argv[j++])
{
if(c>='0' && c<='9')
*p++=(c-'0');
if(c=='+' || c=='*')
push((char)c);
if(c==')')
*p++=pop(c);
}*p='\0';
while((c=rev[i++])!='\0')
{
if(isdigit(c))
printf("%d",c);
else
printf("%c",c);
}
while((c=rev[i++])!='\0')
{
if(c>='0'&& c<='9')
push(c-'0');
if(c=='+')
push(pop()+pop());
if(c=='*')
push(pop()*pop());
}
printf("Result is %d",pop());
return 0;
}
The program gets compiled correctly but the output it just gibberish.
Here is the output::
rev_polish 5 * ( ( 9 + 7 ) + 7 )
5353424040574355414355 +Result is 43.
Please help, i can't find any headway around this.
postfix expression, store the postfix expression in array prints
it ,and then prints the sum. I have used link list to implement the
pushdown stack. The program gets compiled correctly but its not giving
correct output at all. Here is the program i wrote::
#include <stdio.h>
#include <ctype.h>
#define MAX 1000
int rev[MAX];
int *p=rev;
struct node
{
int key;
struct node *next;
};
struct node *head,*z,*t;
void stackinitze()
{
head=(struct node *)malloc(sizeof *head);
z=(struct node *)malloc(sizeof *z);
head->next=z;
head->key=0;
z->next=z;
}
void push(int v)
{
t=(struct node *)malloc(sizeof *t);
t->key=v;
t->next=head->next;
head->next=t;
}
int pop()
{
int x;
t=head->next;
head->next=t->next;
x=t->key;
free(t);
return x;
}
int main(int argc, char **argv)
{
int c=*argv[1],i=0,j=1;
for(stackinitze();--argc>0;c=*argv[j++])
{
if(c>='0' && c<='9')
*p++=(c-'0');
if(c=='+' || c=='*')
push((char)c);
if(c==')')
*p++=pop(c);
}*p='\0';
while((c=rev[i++])!='\0')
{
if(isdigit(c))
printf("%d",c);
else
printf("%c",c);
}
while((c=rev[i++])!='\0')
{
if(c>='0'&& c<='9')
push(c-'0');
if(c=='+')
push(pop()+pop());
if(c=='*')
push(pop()*pop());
}
printf("Result is %d",pop());
return 0;
}
The program gets compiled correctly but the output it just gibberish.
Here is the output::
rev_polish 5 * ( ( 9 + 7 ) + 7 )
5353424040574355414355 +Result is 43.
Please help, i can't find any headway around this.