# Algebra project

• 04-02-2005
treenef
Algebra project
Hi,

Just finished the add/subtract function of my algebra solver.
At the minute it just handles 'x' and numbers on one line.
I have yet to develop the expand out brackets and cross multiply
function.

Tell me what you think?

Sample input: x+7x-4=5x

Ps. The code is pretty ugly and I haven't used <vectors> or functions
• 04-02-2005
mrafcho001
For some reason when i comiple it, it just crashes when i enter the equeation...

i entered the sample one.
• 04-02-2005
treenef
ok so the screen comes up which tells you to enter the equation.

But then it just crashes as soon as you type one in?

Not sure i'm using Devshed.

Does anyone else have this problem you should get

x=4/3

:confused:

Does everyone else get this?
• 04-02-2005
Dave Evans
Quote:

Originally Posted by treenef
ok so the screen comes up which tells you to enter the equation.

But then it just crashes as soon as you type one in?

Not sure i'm using Devshed.

Does anyone else have this problem you should get

x=4/3

:confused:

Does everyone else get this?

Maybe his input had some spaces in it? Have you tested it with spaces and with other inputs that contain characters other than what the program requires for proper output?

Regards,

Dave
• 04-02-2005
7smurfs
Code:

```        if (string[a]=='0')         {           num=0;         }          if (string[a]=='1')         {           num=1;         }         if (string[a]=='2')         {           num=2;         }         if (string[a]=='3')         {           num=3;         }          if (string[a]=='4')         {           num=4;         }         if (string[a]=='5')         {           num=5;         }         if (string[a]=='6')         {           num=6;         }         if (string[a]=='7')         {           num=7;         }         if (string[a]=='8')         {           num=8;         }          if (string[a]=='9')         {           num=9;         }```
Wouldn't it be easier to do this:
Code:

`num=string[a];`
???
• 04-02-2005
misplaced
Quote:

Originally Posted by 7smurfs
Wouldn't it be easier to do this:
Code:

`num=string[a];`
???

yea, it would be easier if he wanted 'num' to equal the ascii value of string[a]. unless, of course, 'num' is really of type char.
• 04-02-2005
misplaced
Quote:

Originally Posted by treenef
Tell me what you think?

honestly, if you're going to expand on this project, you should probably scrap what you got and think carefully about the design. it's going to get pretty big. you might also want to think about using binary trees to store your equation.
• 04-02-2005
Kybo_Ren
Quote:

Wouldn't it be easier to do this:
Code:

`num=string[a];`
???
No.
First he would have to checking that string[a] is an actual number (between '0' and '9'). Then he could assign it to an integer, subtracting the value of '0'. I.e. /*AFTER CHECKING FOR A VALID NUMBER */ integer = string[a] - '0';
• 04-03-2005
misplaced
Quote:

Originally Posted by Kybo_Ren
No.
First he would have to checking that string[a] is an actual number (between '0' and '9').

he already is doing that --- 10 times.

Code:

`isdigit(string[a]) ? num = (string[a] - '0') : /*call to a function to handle errors*/;`
even more efficient (depending on how many times string[a] is used without it's value changing - i haven't really look at the code):

Code:

```char current = string[a]; //or even register char current = string[a]; isdigit(current) ? num = (current - '0') : /*call to a function to handle errors*/;```
• 04-03-2005
treenef
Quote:

honestly, if you're going to expand on this project, you should probably scrap what you got and think carefully about the design. it's going to get pretty big. you might also want to think about using binary trees to store your equation.

This is a fair comment. I realise using vectors and binary trees is probably the most efficient solution, as indeed 'Hunter2' has demonstrated. Moreover, my reluctance to use functions common to c++ such as 'isdigit' has lengthened the size of my code. Using functions would probably be a good thing as well.

At the minute I'm just playing with what I know already. I know nothing about binary trees or vectors so perhaps I should really read up on them.

Much thanks

Treenef
• 04-03-2005
treenef:

I've finished the first draft of a program that evaluates first order algebra expressions using lists (and structs) rather than trees. The current version doesn't use iterators. It doesn't validate the input in any way. It doesn't evaluate equations with nested parenthesis or when the unknown is in denominator and I haven't tested it on something like

(x + 1)/3 = 2x;

yet, but it does things like

6(x + 2) + x/2 = 5(x + 1);

and simpler things like

x + 1 = x + 2; //indicates no valid values of x

or

x = x; //indicates x valid for all values of x

etc. It's probably over documented, per my routine as I develop programs, but that shouldn't matter. It outputs the results at the various steps as you had outlined in your first post a while back.

If you are interested I can try to post it as an attachment or send it by email (I haven't used private mail from this site to know how it works).

Even if you're not interested, thanks for the original post, and good luck with your efforts. I'm intrigued enough by the project that I'm working on a more detailed version in an attempt to cover the issues the current one doesn't, but it may take a while.