## 10932 - Calculator

Moderator: Board moderators

sclo
Guru
Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Contact:
I seemed to get AC after i added 1e-7 to my results, so that could be that i was displaying -0.00

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
I can't figure out how to handle inputs like this one:

Code: Select all

``````a=.1
b=.3
c=2
d=.33333333333333
e=100101010100101010
b
b/a
b/a/c
b/a/c*d
b/a/c*d*e
``````
double in Java is not working - I get something like

Code: Select all

``````0.30
3.00
1.50
0.50
50050505050050007.04
``````
Maybe it's Java or maybe it's just doubles, here is unformatted:

Code: Select all

``````0.30000000000000004
3.0000000000000004
1.5000000000000002
0.4999999999999951
5.0050505050050008E16
``````

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Contact:
Hi all,

My code gives wrong ans for this problem. Can anyone help me to locate my errors? If unary operation is valid in this program?

Code: Select all

``````
//Problem: Calculator-10932
//Status :

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

long double array[40], result[300];
char stack[300], digit[30];
int status[300];
long double x;

void remove_trailspace(char []);
int infix_postfix(char [], int);
long double equation(int);

int main(void)
{
int flag,i;
char input[300];
long double res;

while(gets(input))
{
remove_trailspace(input);
if(!flag)
{
for(i=0;i<300;i++)  status[i]=0;
flag=infix_postfix(input,0);
res=equation(flag);
printf("%.2Lf\n",res);
}
}
return 0;
}

{
int i,j,k;
for(i=0;input[i];i++)
if(input[i]!=' ')
break;
for(j=0,k=i;input[k];j++,k++)
input[j]=input[k];
input[j]='\0';
}

void remove_trailspace(char input[])
{
int i,j;
j=strlen(input);
for(i=j-1;i>=0;i--)
{
if(input[i]==' ')
input[i]='\0';
else
break;
}
}

{
int flag=1,m,i;
for(i=0;input[i];i++)
if(input[i]=='=')
{
flag=0;
break;
}
if(!flag)
{
m=infix_postfix(input,i+1);
x=equation(m);
array[(int)(input[0]-97)]=x;
return 1;
}
else
return 0;
}

int infix_postfix(char input[], int i)
{
int j=0,k,l,p;
l=strlen(input);  input[l]=')';  input[l+1]='\0';
stack[0]='(';  k=1;
for(i=i;input[i];i++)
{
if(isalpha(input[i]))
{
p=(int)(input[i]-97);
result[j++]=array[p];
}
else if(isdigit(input[i]) || input[i]=='.')
{
p=0;
while(isdigit(input[i]) || input[i]=='.')
digit[p++]=input[i++];
digit[p]='\0';
result[j++]=atof(digit);
i--;
}
else if(input[i]=='(')
stack[k++]=input[i];
else if(input[i]==')')
{
while(k>0 && stack[k-1]!='(')
{
result[j]=(int)stack[--k];
status[j]=1;
j++;
}
k--;
}
else if(input[i]=='*' || input[i]=='/')
{
while( k>0 && stack[k-1]!='(' && (stack[k-1]=='*' || stack[k-1]=='/'))
{
result[j]=(int)stack[--k];
status[j]=1;
j++;
}
stack[k++]=input[i];
}
else if(input[i]=='+' || input[i]=='-')
{
while(k>0 && (stack[k-1]!='(' || stack[k-1]=='*' || stack[k-1]=='/'))
{
result[j]=(int)stack[--k];
status[j]=1;
j++;
}
while(k>0 && (stack[k-1]!='(' || stack[k-1]=='+' || stack[k-1]=='-'))
{
result[j]=(int)stack[--k];
status[j]=1;
j++;
}
stack[k++]=input[i];
}
}
return j;
}

long double equation(int p)
{
int i,top;
char ch;
long double stack1[300];
top=0;
for(i=0;i<p;i++)
{
if(status[i]==0)
stack1[top++]=result[i];
else if(status[i]==1)
{
ch=(char)result[i];
if(ch=='+')
{
stack1[top-2]=stack1[top-2]+stack1[top-1];
top--;
}
if(ch=='-')
{
stack1[top-2]=stack1[top-2]-stack1[top-1];
top--;
}
if(ch=='*')
{
stack1[top-2]=stack1[top-2]*stack1[top-1];
top--;
}
if(ch=='/')
{
stack1[top-2]=stack1[top-2]/stack1[top-1];
top--;
}
}
}
return(stack1[top-1]);
}

``````

ABDULLAH.
We were in past, we are in past and we will go in past.

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Contact:
Hi all,

I have changed some of the portion of my code. But also get wrong ans. Please help me.

ABDULLAH.
We were in past, we are in past and we will go in past.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm