Expression Manipulator v0.1

This is a discussion on Expression Manipulator v0.1 within the A Brief History of Cprogramming.com forums, part of the Community Boards category; It's finally here... after a month... It's got plenty of bugs to be worked out... but this is pretty far ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Expression Manipulator v0.1

    It's finally here... after a month...

    It's got plenty of bugs to be worked out... but this is pretty far along from my last release.

    This is a set of classes designed to manipulated Expressions, functions, and what-have-you.

    Features:
    • Expressions can be typed in almost as they would be on a graphing calculator
    • The classes are const-protected; with private parts protected
    • It will accept equations, too, if the program detects an equals sign
    • It can combine like terms and like factors easily
    • it can distribute factors, FOIL them, or expand them according to exponents. (It's not limited to FOIL; it can multiply any number of terms)
    • There is buggy and limited support for trig functions (sin, cos, tan, cot, sec, csc)
    • There is the ability to define functions in run-time; though the command-line isn't ready for it yet. (You can manually experiment from int main(), though)
    • Every function has a domain, and several pieces of domain can be combined to get a whole domain (see code for details)
    • The classes are organized by file this time
    • expandable framework

    Any questions? I haven't attached an executable yet, but I may in the morning.
    Attached Files Attached Files

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Could you provide a short code example showing how it is used?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    after it is compiled, run it, and type in an expression:
    Code:
    9x^2+b
    and it will parse it into objects and print it back out again. If there's a way to simplify it, it will do that automatically. ie:
    Code:
    9(x+3)^2
    
    9x^2 + 54x + 81
    note: minus signs must have a space after them, otherwise they'll be counted as negative signs

    For examples of its use in code, look at int main()
    (located in expression.cpp)
    Last edited by ygfperson; 05-16-2003 at 02:07 PM.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Ahh, I missed the main() function.

    These are the changes I had to make to make the code compile in MSVC++.NET:

    Added lines to Expression.h
    Code:
    #include <algorithm>
    #include <cmath>
    using std::sort;
    using std::cos;
    using std::sin;
    using std::tan;
    using std::log;
    #include <limits>
    namespace
    {
        const double INFINITY = std::numeric_limits<double>::infinity();
    }
    Added lines to Expression.cpp
    Code:
    const double Float::EPSILON = 0.0000000000001; //double is not an integral type -- inline definition not possible
    I also had to add return *this at numerous places where it had been omitted.
    At one place (Term::return_first_factor()) I couldn't add "return *this" so I added a throw-statement instead.

    Now I'll try the program.
    Last edited by Sang-drax; 05-16-2003 at 03:20 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The simplifying seem to work, I've tested it with some expressions.

    If the string "(5)" is entered, the output will not be the same as if "5" is entered, but it could be me not understanding the output.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    You're right... I didn't notice that. That's a bug.

    Here is the executable:

    //edit: another note: make sure there is a '*' between a variable and a parentheses. ie:
    Code:
    f(x)
    will be interpreted as the function 'f' with parameter 'x'.
    Code:
    f*(x)
    is f * x.
    Attached Files Attached Files
    Last edited by ygfperson; 05-16-2003 at 09:43 PM.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It has the wrong output for (x-y)^2
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Whoops. Nevermind. Forgot about the whole 'space after a minus sign' thing.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Can u post the binary please.. could not compile the file

  10. #10
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    804
    this is WAYYYYY better then i thought it would be!!!!!

    great job man!!
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: commander_android@hotmail.com []*[]

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by vasanth
    Can u post the binary please.. could not compile the file
    He did at the end of his last post.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Nice. A few thoughts/bugs:

    1) Why is the result printed 4 times? I also noticed that (sometimes) the last two were the 'reverse' of the first two.
    Did you use different methods of simplifying the expression, or?

    5 + x = 5 + x
    5 + x = 5 + x
    x + 5 = x + 5
    x + 5 = x + 5


    2) Sometimes a "+0" or a "*1" is inserted:

    Input: (x) = z

    x = z
    x = z
    x + 0 = z
    x + 0 = z

    Input: f(x) = (f(x + h) - f(x)) / h

    f(x) = 1*f([x + h]) + -f(x)*h
    f(x) = 1*f([x + h]) + -f(x)*h
    f(x) = 0+f([x + h]) + -f(x)*h
    f(x) = 0+f([x + h]) + -f(x)*h


    Otherwise it seems to work ok.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by Magos
    Nice.
    Thanks.
    A few thoughts/bugs:

    1) Why is the result printed 4 times? I also noticed that (sometimes) the last two were the 'reverse' of the first two.
    Did you use different methods of simplifying the expression, or?
    Yes, actually. Sorry for burying int main() in there.
    1) Expression after being parsed (unsimplified)
    2) Combined like terms and factors
    3) Used distributive property to expand factors
    4) Evaluated simple functions like trig funcs and logs

    That's the gist of it. #4 doesn't work for much of anything more than sin(cos(0)) or sin(3.14) or cot(1)... etc...

    2) Sometimes a "+0" or a "*1" is inserted:

    Input: (x) = z

    x = z
    x = z
    x + 0 = z
    x + 0 = z

    Input: f(x) = (f(x + h) - f(x)) / h

    f(x) = 1*f([x + h]) + -f(x)*h
    f(x) = 1*f([x + h]) + -f(x)*h
    f(x) = 0+f([x + h]) + -f(x)*h
    f(x) = 0+f([x + h]) + -f(x)*h


    Otherwise it seems to work ok.
    The '*1' and the '+ 0' are side-effects of the simplification. The function 'clear_extra' should get rid of them, but I haven't put it in int main() because I haven't seen a need to.

    BTW, There's a parsing bug I just found out from your output... don't put any whitespace between the '/' and its factors. I'll fix this in the next release so that your input would work.

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yeah, you should enhance your parser.

    x(y+z)
    x*-y
    (a+b)(c+d)

    should be possible
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    x*(y+z)
    x*-y works fine.
    (a+b)*(c+d)
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-20-2007, 12:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21