I have to give this in around 10 hours and i still have lots to do.
Please report any bugs;

Its a scientific calculator, also supports sin, cos,tan and log
Take care to put the entire expression under brackets

For example, a valid input will be:-

(5+4*-3+sin(43+7)+cos(90/2))

I've attached the .exe as well.

Please try as many different combinations as you can.

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <math.h>

class expsolver
{
	char inputstring[100],str[100],exp[100], prec[10],subexp[20];
	int pos[100],posl, no[5], neighbours[2], step,pass,neg, no_ops;
	double t1,t2,ta;

	public:
	expsolver()
	{
		strcpy(prec,"^/*+-");
		step=0;
		pass=1;
		neg=0;
		no_ops=0;
		display();
	}
	void display();
	void manager();
	void master();
	void neighbourset(int);
	void pos_setter();
	void copier(char*,char*,int ,int);
	void convertno(char*,double &t);
	void operation(double,double,int);
	void convertalpha(double);
	void replacer(char*,char*,int,int);
	void endcopier(char*,char*,int);
	void negbugfix();
};

void expsolver::display()
{
	
	char ch;
	do
	{
			clrscr();
			cout<<"Enter the expression to be evaluated\n";
			gets(inputstring);
			strcpy(str,inputstring);
			manager();
			cout<<"\n\nTry again? :";
			cin>>ch;

	}
	while (ch!='n');
}

void expsolver::manager()
{
	int i,lcount=0,rcount=0,li=0,ri=0;
	for (i=0; i<strlen(str); i++)
	{
		if(str[i]=='(')
			lcount+=1;
		if(str[i]==')')
			rcount+=1;
	}
	for(int j=0;j<lcount;j++)
	{
	if (lcount==rcount)
	{
		for (i=0; i<strlen(str); i++)
		{
			if (str[i]=='(')
			{
				li=i;
			}
			if (str[i]==')')
			{
				ri=i;
				break;
			}
		}
	}
	copier(exp,str,li+1,ri-1);
	cout<<"\nPass "<<pass;
	master();
	replacer(str,exp,li,ri);
	cout<<"\nAfter the operation, result is "<<str<<endl;
	pass++;
	}
	cout<<"\nResult -"<<str;
}
void expsolver::master()
{
	for (int i=0; i<4; i++)
	{
		no[i]=0;
		for (int j=1; j<(strlen(exp)); j++)
			if (exp[j]==prec[i])
			{			
				no[i]+=1;
				no_ops++;
			}
	}

	if(no_ops==0)
	{
		convertno(exp,ta);
		convertalpha(ta);
		replacer(exp,subexp,0,strlen(exp));
	}			

	for(int i=0; i<4; i++)
	{
		if(i==3)
			negbugfix();		
		for (int j=0; j<no[i]; j++)
		{
			pos_setter();
			
			if(pos[0]!=32767)
				for (int k=1; k<(strlen(exp)); k++)
					{
						if (exp[k]==prec[i])
						{
							neighbourset(k);
							char *a =new char [k-neighbours[0]];
							char *b =new char [neighbours[1]-k];
							copier (a,exp,neighbours[0],(k-1));
							copier (b,exp,(k+1),neighbours[1]);
							convertno (a,t1);
							convertno (b,t2);
							operation (t1,t2,i);
							convertalpha (ta);
							replacer (exp,subexp,neighbours[0],neighbours[1]);
							cout<<"\nStep "<<++step<<"- Do "<<prec[i]<<endl;
							puts(exp);
							break;
						}
					}

		}

	}
}
void expsolver::pos_setter()
{
	unsigned int i=1,j=0;
	for (;exp[i]!='\0';i++)
	{
		if((exp[i]=='^')||(exp[i]=='/')||(exp[i]=='*')||(exp[i]=='+')||((exp[i]=='-')&&(neg)))
		{
				pos[j]=i;
				j++;
		}
		pos[j]=32767;
		posl=j;
	}
}
void expsolver::neighbourset (int k)
{
	for (int i=0; pos[i]!=32767; i++)
	{
		if (k==pos[i])
		{
			neighbours[0]=pos[i-1]+1;
			neighbours[1]=pos[i+1]-1;
			if (i==0)
				neighbours[0]=0;
			if(i==posl-1)
				neighbours[1]=((strlen(exp))-1);
		}
	}
}
void expsolver::copier(char* a,char* b,int m,int n)
{
	int i,j;
	for(i=m,j=0;i<=n ;i++,j++)
	{a[j]=b[i];
		}
	a[j]='\0';
}
void expsolver::endcopier(char* x,char* y,int start)
{
	int i,j;
	for (i=start,j=0;x[i]!='\0' ;i++,j++ )
		y[j]=x[i];
	y[j]='\0';
}

void expsolver::convertno(char* a,double &t)
{
	char input[20], *endptr;
	double inputval;

	if((strncmpi(a,"sin",3)==0)||(strncmpi(a,"cos",3)==0)||(strncmpi(a,"tan",3)==0)||(strncmpi(a,"log",3)==0))
	{
		if(strncmpi(a,"sin",3)==0)
		{
			endcopier(a,input,3);
			inputval=strtod(input,&endptr);
			t=sin(inputval*(3.1415926535/180));
		}
		if(strncmpi(a,"cos",3)==0)
		{
			endcopier(a,input,3);
			inputval=strtod(input,&endptr);
			t=cos(inputval*(3.1415926535/180));
		}
		if(strncmpi(a,"tan",3)==0)
		{
			endcopier(a,input,3);
			inputval=strtod(input,&endptr);
			t=tan(inputval*(3.1415926535/180));
		}
		if(strncmpi(a,"log",3)==0)
		{
			endcopier(a,input,3);
			inputval=strtod(input,&endptr);
			t=log(inputval);
		}
	}
	else
		t=strtod(a,&endptr);
}
void expsolver::operation(double x,double y,int i)
{
	if (i==0)
		ta=pow(x,y);
	if (i==1)
		ta=x/y;
	if (i==2)
		ta=x*y;
	if (i==3)
		ta=x+y;
	if (i==4)
		ta=x-y;
}
void expsolver::replacer(char* a,char* b,int m,int n)
{
	int j=0,k,i,l,size=(strlen(a)-n);
	char *sub=new char[size];

	for(i=n+1; a[i]!='\0' ;i++)
	{
		sub[j]=a[i];
		j++;
	}
	sub[j]='\0';

	for (i=m,j=0; b[j]!='\0' ; i++,j++,k=i)
		a[i]=b[j];

	for (i=k,j=0; sub[j]!='\0'; i++,j++)
		a[i]=sub[j];
	a[i]='\0';
}
void expsolver::convertalpha(double x)
{
   sprintf ( subexp, "%.2f", x );
}
void expsolver::negbugfix ()
{
	for(int i=1; i<strlen(exp); i++)
		if(exp[i]=='-')
		{
			replacer(exp,"+",i,i-1);
			i++;
			puts(exp);
			no[3]++;
		}
}
void main()
{
	expsolver obj;
	getch();
}

Recommended Answers

All 3 Replies

Nothing urgent about it for any of us. You should have started earlier if you think 10 more hours isn't enough.

> You should have started earlier if you think 10 more hours isn't enough.
Well it's down to 5 hours now ;)

As a C++ program, I would score this at 1/10 no matter how bug-free you thought it was.

It's basically a C program with a rudimentary class in it.

> gets(inputstring);
This earns you 0/10.
gets() is totally unsafe; there is no reason at all for being anywhere near this function.

All your allocated memory (new) gets leaked.

Dude i'm a beginner

The rudimentary class is there because its still a WIP.
i'm neither an expert, nor do i claim to be.

anyways i have one more day now.
So please.

It's basically a C program with a rudimentary class in it.

OK, so what do i do about that;

gets() is totally unsafe; there is no reason at all for being anywhere near this function.

All your allocated memory (new) gets leaked.

thanks for that
they tell us nothing in class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.