Page 2 of 2

Posted: Mon Aug 28, 2006 6:04 am
by sclo
I seemed to get AC after i added 1e-7 to my results, so that could be that i was displaying -0.00

Posted: Tue Aug 29, 2006 4:41 am
by Darko
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

Posted: Mon Jul 16, 2007 7:13 pm
by abdullah<cse du>
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_leadingspace(char []);
void remove_trailspace(char []);
int cheack_attribution(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_leadingspace(input);
		remove_trailspace(input);
		flag=cheack_attribution(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;
}

void remove_leadingspace(char input[])
{
	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 cheack_attribution(char input[])
{
	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]);
}



Please help me. :oops:

ABDULLAH.

Posted: Tue Jul 17, 2007 4:46 am
by abdullah<cse du>
Hi all,

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

ABDULLAH. :oops:

Posted: Tue Jul 17, 2007 5:09 pm
by Jan
You should check the board before posting your problem. There are some cases in the previous page, which may be helpful.