Start New Discussion within our Software Development Community

only a beginner, please bear with me..
there are no errors, but wrong output
assumptions:
input is correctly formed
array size is 20
single digits only
4 basic operations

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>

int stk[20];

void push(int a, int b)
{
stk[a]=b;
}

int pop(int a)
{
int b=stk[a];
stk[a]=NULL;
return b;
}

main()
{
char yorn, str[20], *tmp;
int chr, sctr, a, x, y, ans;

do{
clrscr();
printf("input an equation in prefix notation: ");
scanf("%s", &str[20]);
sctr=0;
for(a=20; a>0; a--)
{
if(isdigit(str[a-1])!=0)
{
*tmp=str[a-1];
chr=atoi(tmp);
push(sctr, chr);
sctr++;
}
if(isdigit(str[a-1])==0)
{
if(str[a-1]=='+')
{
x=pop(sctr-1);
y=pop(sctr-2);
sctr-2;
ans=x+y;
push(sctr, ans);
}
if(str[a-1]=='-')
{
x=pop(sctr-1);
y=pop(sctr-2);
sctr-2;
ans=x-y;
push(sctr, ans);
}
if(str[a-1]=='/')
{
x=pop(sctr-1);
y=pop(sctr-2);
sctr-2;
ans=x/y;
push(sctr, ans);
}
if(str[a-1]=='*')
{
x=pop(sctr-1);
y=pop(sctr-2);
sctr-2;
ans=x*y;
push(sctr, ans);
}
}
}
ans=pop(sctr);
printf("the answer is %d", ans);
printf("\ntry again?[y/n]: ");
scanf("%s", &yorn);
getch();
}while((yorn=='y')||(yorn=='Y'));

getch();
return 0;
}

Can you describe the problem in more detail ?
Simple saying that the O/P is wrong is not sufficient .
put some printf's in your code and see how the control is flowing

Of the top of my head I see you have used the pointer *tmp but there is no memory allotted for it

PS Use code tags when you are posting code

This article has been dead for over six months. Start a new discussion instead.