Polish form headache...

This is a discussion on Polish form headache... within the C++ Programming forums, part of the General Programming Boards category; This will be the last question concerning my program.. So the user inputs a string like: this + that + ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    43

    Polish form headache...

    This will be the last question concerning my program.. So the user inputs a string like:

    this + that + anotherOne - justoneMore

    where this, that etc are classes containing a name and a vector of integers. How could I create another class which will hold the result? (The result will be a class with a vector whose elements are being added or substracting).

    I got the names in a string vector and the operands in a char vector as well. I made a loop, but it works like this: it add this with that, then that with another one, and then it adds the two results...

    I would appreciate a code.. at least for a simpler thing, such as a + b - c, where a , b, c are integers..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why would you want a class for the result? Isn't the result just an int?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    No, it's a class with a vector in which I store the sumation or the difference of the components.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If this, that, etc are objects, then the result is the same type of object as this, that, etc. And presumably you would define operator+ and operator- on those objects to add or subtract element-by-element.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    I did that.... I even overloaded += so that I can have a temp class which does
    Code:
     temp += this + that
    . then it keeps adding.. but id does not add correctly..

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Veneficvs View Post
    I did that.... I even overloaded += so that I can have a temp class which does
    Code:
     temp += this + that
    . then it keeps adding.. but id does not add correctly..
    Then you didn't do it right. Actually, temp += this + that is almost certainly wrong, unless you initialized temp to start with. But without actual code goodness, we're just flailing here.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What on earth are you talking about? o_O

    Make a class overloading operator= and providing a constructor for conversion from this + that + anotherOne - justOneMore?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by anon View Post
    What on earth are you talking about? o_O

    Make a class overloading operator= and providing a constructor for conversion from this + that + anotherOne - justOneMore?
    I will translate the code and post it here... I am too tired and freaking nervous now.... good night.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Here is the code
    Code:
         CArray temp, first, second;
        for(contor = 0; contor < (int)simbols.size(); contor++)
        {
            if(simbols[contor] == '+')
            {
                for(i = 0; i < (int)arrays.size(); i++)
                {
                    if(arrays[i].name == operations[contor])
                    {
                        first = arrays[i];
                    }
                    if(arrays[i].name == operations[contor + 1])
                    {
                        second = arrays[i];
                    }
                }
                temp += (first + second);
            }
            if(simbols[contor] == '-')
            {
                temp += (first - second);
            }
        }
    Consider the following input line:
    first + second - third
    Ok. Arrays is a vector of type CArray, which contains the name of the array and a vector of type int, for the components. Operations is a string vector containing first, second, third and any other array that might appear in the line. Simbols is a string vector that contains + and - in the order they appear in the input line. Temp is going to retain the result of those sumations or differences.

    I am adding the arrays two by two ... but it acts like this: 1 + 2 + 3 should be 6... but it does 1+ 2, then 2 + 3, and then 1 + 2 + 2 + 3 , which is 8... So how could I properly adjust the loop to calculate what I want, please?..

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As I stated above, temp += (first + second) is grossly incorrect. temp = first. temp += second. temp -= third.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    As I stated above, temp += (first + second) is grossly incorrect. temp = first. temp += second. temp -= third.
    Thanks a lot! Does it work for any number of arrays?

    Edit: Doing what you said gives me a debug error.. could you modify it on my code, please?
    Last edited by Veneficvs; 05-09-2010 at 06:49 AM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If operator is plus, you should have "temp += second", and if operator is minus, you should have "temp -= second". Also, and this is big, you don't ever bother to initialize first and/or second in your '-' case. You'll need to walk through arrays just the same in '-' as you do in '+'.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    If operator is plus, you should have "temp += second", and if operator is minus, you should have "temp -= second". Also, and this is big, you don't ever bother to initialize first and/or second in your '-' case. You'll need to walk through arrays just the same in '-' as you do in '+'.
    I didn't do it because I tested with pluses. And it did not work for them..

    Edit... I found the problem... it seems that contor runs out of dimension.
    Last edited by Veneficvs; 05-09-2010 at 07:02 AM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Veneficvs View Post
    I didn't do it because I tested with pluses. And it did not work for them..
    What "doesn't work" about it?

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    What "doesn't work" about it?
    Still doesn't work. If I say
    ALpha: 2 2 2 2 2
    Beta: 3 3 3 3 3
    Teta: 4 4 4 4 4

    And then Alpha + Beta + TEta it gives me:
    1 1 1 1 1.

    Edit: corrected with temp += treia... but for more than 3 operands it still gives bad results... this cannot be solved in a loop I am afraid.. I think Polish form could do it.
    Last edited by Veneficvs; 05-09-2010 at 07:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reopening a form ?
    By Aga^^ in forum C# Programming
    Replies: 1
    Last Post: 02-11-2009, 09:28 AM
  2. Calling datas from another form?
    By Aga^^ in forum C# Programming
    Replies: 2
    Last Post: 02-06-2009, 02:17 AM
  3. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  4. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 09:55 AM
  5. Making an MFC form to suit
    By TJJ in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2004, 12:20 PM

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