This is the first big program I'm gonna do and so far, i have really no idea on how to go about it.
Basically, I aim to create a C++ program that:-

  1. Takes expressions of the form...
    [TEX](3+4(5*2)+(3/(sin 62))-4)[/TEX]
    or similar ones and evaluates them
  2. Takes equations of the form
    [TEX]F=qQ/(4*3.14*8.85*r^2)[/TEX]
    Asks the user what is constant and what varies, and creates a graph on the screen. Options would include saving graphs,reading from disk,etc.
  3. May add a few more things later

Some help and basic stuff on how to approach this would be really helpful.
And oh, this is my first post here. Thanks!:)

Thats not going to be very easy for a beginner. What part do you need
help with? Also is the function for (2) correct? Is that column's law.

Well I have written a system for extracting maths equations from input text which I currently use in several projects. So I just want to ensure you know what you are getting into it is about 2500 lines of code.

So structure:

you can break the functions down into a tree or a stack. I used a stack system (effectively reverse polish). This leaves a parsing issue.
I used two level parse. (a) the first parse found errors. e.g. bracket errors, syntax errors e.g. 5+-4 is ok but 5+*4 is not, function errors,
sin(56) is ok sinx(56) is not, and so on. Then the actual compiler (into a stack byte code is a lot simpler.

Use a lookup-map for variables. Write a generic functional for a maths component.

Post stuff as you go along, it is going to take you two weeks if you are an experienced programmer. Write lots and lots of test code. Enjoy writing it!

First bit of code. Please test and report errors.
So far the first part is somewhat done, trignometric functions are also supported.
Keep in mind:-
1- str[] is the expression you will enter
2- no spaces, no single numbers that don't have operators
example:- dont give 3*(4)

having a bit of problem with -ve no.s .
I need suggestions on the graph generator part, which will use this to calculate the expression.

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

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

	public:
	expsolver()
	{
		cout<<"enter the expression :\n";
		strcpy(str,"(3.5*4+6/2+sin90-2+(3/4))");
		strcpy(prec,"^/*+-");
		step=0;
		pass=1;
		manager();
	}
	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 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<5; i++)
	{
		no[i]=0;
		for (int j=1; j<(strlen(exp)); j++)
			if (exp[j]==prec[i])
				no[i]+=1;
	}

	for(int i=0; i<5; i++)
	{
		for (int j=0; j<no[i]; j++)
		{
			pos_setter();
			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]=='-'))
		{
				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 main()
{
	expsolver obj;
	getch();
}
This article has been dead for over six months. Start a new discussion instead.