I have a homework assignment that requires the user to input two equations from the keyboard. The user can enter any letter of the alphabet but each equation can only have a total of 26 letters. I created a class for the equations and I was able to validate the input data. However, I am lost when it comes to calculating the equations. The program should do the following:

2A + 3B = 4
3A - 2c = 6

calculates to: 5A + 3B - 2C = 10

My program so far:

#include <iostream>
#include <string>
using namespace std;

class Equation
{
private:
enum {MAX_VARIABLES = 26};
string equation;
int num_variables;
public:
void read_equation();
void validate_user_input();
};

void Equation::read_equation()
{
//simply reads the input string
}

void Equation::validate_user_input()
{
//used a loop to count the number of letter in the string to make
//sure it was less than 26 per equation. If it is greater the
//user is prompted to re-enter the equation.
}


int main()
{
Equation eq1, eq2;

eq1.read_equation();
eqi.validate_user_input();
eq2.read_equation();
eq2.validate_user_input();

system("pause");
return 0;
}

// my two functions work fine and produce the correct results. My problem
// is determining how to calculate the two equations (adding one equations to the other)
//
// my idea was to separate the initial strings into 2D char arrays so that for example
// 2A would be one element of the array. I have no idea where else to go from here.
// Any ideas?

cgcgames commented: good job on your first post. it was easy to understand what you where meanding. keep up the good work. +0

Recommended Answers

All 9 Replies

Is the format always going to be like this :

string eq1 = "2A + 3B = 4";
string eq2 = "3A - 2c = 6";

is there any multiplication or division involved? How about parenthesis ?

well deppends on what you are trying to do. if tis always going to be the same equation then you can cheat. but if you want to do some mathmatical equation to work it at then you need to code what its going to need to check.

first of all you need to check the letters after the numbers in the first string and the second string to see if they match.

once you do that its pretty much like how you do it in your head. you just tell it to add this to that times that to this etc :P.

also what i would do is sperate the letter from the number when it does equal the same letter then put the numbers into there own veriable and run them through the calculation then at the end add the letter you seperated. so you need to store the letter somewhere else.

hope that helps you.

good luck,
cgcgames

The equation is only allowed to use addition and subtraction. The problem I am running into is how to separate the number from the letter. Also I was trying to allow the user to input the equation on one line. I'm assuming to do this I should use a string variable. However, you cannot add numbers stored in string variables and get a value. When you add strings together you simply add one string onto the end of the other. I am thinking that it may be best to make a class using an int for the number and a char for the letter.

you need to split it all up. look at the string in code. then serch for characters. tbh i havent needed to do this before. but i know you can and recently i have been working on forms in C++ which is managed code.

what i suggest you do is search in good spliting strings or somthing. or finding somthing in a string.

give me 10mins or so ill go see if i can find somthing when i do ill come back to you

I belive there is a search for character in strings, you can use this to search for + or - right?

You could use stringstreams to convert "234" to 234 i think. You will need to look up string operations for searching for + or - or = etc, then string splitting operations for getting whats before or after. Then you should have seperate strings for each "2A" bit, then repeat but look for letters etc.

Thats how I would go about it..

Hello all, I agree with hanvyj.
I'm no profi at this, but to find the position a given character I would use:
AnsiPos(const AnsiString& subStr) const
or
Pos(const AnsiString& subStr) const.
For conversion, there's
ToInt() const
and
ToIntDef(int defaultValue) const.

I'm not sure how you should go about splitting/checking for entries like "2A" given that any letter is a valid input (lower & upper case as in your example - or is that a typo?).

Should give you a start.
Good luck!

Hello jsburkdc,

did you get anywhere with your code? I found the question very interesting and had a crack at it - the prog runs and seems to do it all with a few variations which can be changed if needed.

Sorry for my misleading previous post - I missed the point that you were working with basic_string.

Let me know
David

Hi after a long time your problem is to solve right now..........
But I don't know how it will help because the behind this is to critical to understand.
Still you have to merger my program with you program.
As mention in your thread that you create a function

void validate_user_input();

which validate the user input, so in my program I assume user input is always correct
Limitation of program
1.Equation must be in string form without blank space.(example: "2A+6B-12D=22")
2.Equation must n0t repeat variable example: "2A+15A+13B=35" is a invalid eqn.

So keep in mind all those point before running this program

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
struct  node
{
int num;
char sign;
char sy;
struct node *prev;
struct node *next;
};
typedef struct node NODE;
NODE *head=NULL,*tail=NULL;
NODE *head1=NULL,*tail1=NULL;
NODE *head2=NULL,*tail2=NULL;

void ins(char str[])
{
NODE *ptr;
int len,i,d1;
ptr=(NODE*)malloc(sizeof(NODE));
len=strlen(str);

for(i=0;i<len;i++)
{
	if(i==0)
	{
	switch(str[i])
	{
	case '-':
		ptr->sign='-'; ptr->num=1;ptr->sy=NULL;break;
	default:ptr->sign='+';
		ptr->num=1;ptr->sy=NULL;
	}
	}
	if(isdigit(str[i])&&i<len)
	{
		ptr->num=0;
		while(isdigit(str[i]))
		{
		d1=str[i]-'0';
		ptr->num=ptr->num*10+d1;
		i++;
		}
		i--;
	}
	else if(str[i]!='+'&&str[i]!='-')
	{
	ptr->sy=str[i];
	if(str[i]=='='){ptr->num=NULL;ptr->sign=NULL;}
	}
}
if(tail==(NODE*)NULL)
{
ptr->prev=ptr->next=(NODE*)NULL;
head=tail=ptr;
}
else
{
ptr->next=(NODE*)NULL;
ptr->prev=tail;
tail->next=ptr;
tail=ptr;
}
}

disp(NODE *ptr)
{
while(ptr!=NULL)
{
cout<<ptr->sign;
if(ptr->num!=0)cout<<ptr->num;
cout<<ptr->sy;
ptr=ptr->next;
}
return 0;
}

void add()
{

NODE *p=NULL,*p1=NULL,*p2=NULL;
int count_p=0,count_p1=0;
p=head;p1=head1;p2=head2;

while(p!=NULL){p=p->next; count_p++;}
p=head;
while(p1!=NULL){p1=p1->next; count_p1++;}
p1=head1;
if(count_p>=count_p1){p2=p;p=p1;}
else p2=p1;

while(p!=NULL)
{     while(p2->sy!=p->sy && p2!=NULL)
	{p2=p2->next;}
      if(p2->sy==p->sy)
	{
	if(p2->sign==p->sign) p2->num=p->num+p2->num;
	else
		{
		if(p2->num>p->num) p2->num=p2->num-p->num;
		else{p2->num=p->num-p2->num; p2->sign=p->sign;}
		}
	 }
	 p=p->next;
while(p2->prev!=NULL) p2=p2->prev;
}
head2=p2;
}

void main()
{
char eqn1[50],eqn2[50];
char *temp;
char *temp_head;
int i=0,j=0;
strcpy(eqn1,"12A-30B=10");
strcpy(eqn2,"13A+22B+13D=22");
clrscr();
for(i=0;i<strlen(eqn1);i++)
{
j=0;
temp[j]=NULL;
	do
	{
	temp[j++]=eqn1[i];
	if(temp[j-1]=='='){i++;	break;}
	i++;
	}while(eqn1[i]!='+'&&eqn1[i]!='-'&&eqn1[i]!='='&&i<strlen(eqn1));
temp[j]=NULL;
ins(temp);
i--;
}
cout<<"\n\nEqn1:\t\t";
disp(head);

head1=head;
tail1=tail;
head=NULL;
tail=NULL;


for(i=0;i<strlen(eqn2);i++)
{
j=0;
temp[j]=NULL;
	do
	{
	temp[j++]=eqn2[i];
	if(temp[j-1]=='='){i++;	break;}
	i++;
	}while(eqn2[i]!='+'&&eqn2[i]!='-'&&eqn2[i]!='='&&i<strlen(eqn2));
temp[j]=NULL;
ins(temp);
i--;
}
cout<<"\n\nEqn2:\t\t";
disp(head);
add();
cout<<"\n\nFinal Eqn:\t";
disp(head2);

getch();
}

In my I use liked list to store each term as a NODE read care fully first then point your problem if you get some where in program eqn1 and eqn2 is two string variable to store equation in string form..................
Best of Luck.

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.