Thread: Algebra project

  1. #1
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

    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

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    For some reason when i comiple it, it just crashes when i enter the equeation...

    i entered the sample one.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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



    Does everyone else get this?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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



    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

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Instead of this:
    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];
    ???
    To code is divine

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    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';

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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*/;
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Algebra project
    By treenef in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2005, 07:24 PM