Simple Calculator (input problem)

This is a discussion on Simple Calculator (input problem) within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a simple calculator, but it has to receive input like this: operand-operator-operand i.e. 6*5 76+2 670/10 ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Simple Calculator (input problem)

    I'm trying to make a simple calculator, but it has to receive input like this:

    operand-operator-operand

    i.e.

    6*5
    76+2
    670/10

    My problem is, how I can I write it so it knows when the first operand has ended (when it could be 1 digit or 5 for example) and then see what the operation is (multiplication, division, etc) and then start on the second operand?

    Sorry for the rather silly question, and yes I need it to work this way, not enter the first operand, then enter the second, all together with the operator in one line.

    Thanks in advance

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Like this:
    Code:
    std::cin>>operand1>>operator>>operand2;
    ...where operands are numbers and operator is a char.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    @manasij that only works if there are spaces or other whitespace between operators and operands.

    if you make the assumption that the first operand will be all digits, you can just read in single characters, appending them to a string, until you find the first non-digit. then read that in, and then read the remainder of the line into another string. convert both strings to integer values, and apply the operator.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Elkvis View Post
    @manasij that only works if there are spaces or other whitespace between operators and operands.
    No..(with some conditions)
    The following works even if there is no space between the tokens.
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
            int op1,op2;char opr;
            cin>>op1>>opr>>op2;
            cout<<op1<<endl<<opr<<endl<<op2;
            return 0;
    }
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    doh *facepalm*

    Thanks

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Quote Originally Posted by Elkvis View Post
    @manasij that only works if there are spaces or other whitespace between operators and operands.
    Or, I suppose, a non-integer.
    Although, don't quote me on that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Elysia View Post
    Or, I suppose, a non-integer.
    Although, don't quote me on that.
    Quoted.. :P
    Anyway.. It works when whitespaces are absent...for integers ( and in some cases floats and doubles)... The only way it'd fails in absence of whitespace is when the operators + or - are confused as parts of the floating points.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Ok I got it working via a swtich that does this:

    std::cin>> op1 >> opr >> op2;

    switch(opr){

    case '+':
    std::cout << op1+op2 << "\n";
    break;

    case '-':
    std::cout << op1-op2 << "\n";
    break;.......................


    Now here's the million dollar question, how can I handle this:

    2*2*2-0.5
    ??

    Or any input that has multiple operands and multiple operators

    (Sorry if this isn't formatted pretty)
    Last edited by grache28; 11-27-2011 at 11:26 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    Quote Originally Posted by grache28
    Now here's the million dollar question, how can I handle this:

    2*2*2-0.5
    ??

    Or any input that has multiple operands and multiple operators
    There are a few ways, but one approach that could work as a starting point is to look up the Shunting Yard algorithm and the use of reverse Polish notation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks, that looks like a good way of doing it, but probably a little more advanced than what I need. Theres no other way to adjust for multiple operators and operands?

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by grache28 View Post
    Thanks, that looks like a good way of doing it, but probably a little more advanced than what I need. Theres no other way to adjust for multiple operators and operands?
    No..(Unless the expression is so short that you can afford numerous passes over it/)
    (Well...there is.. but I think you'll find it more complicated..) You could always build a tree... but Shunting Yard is way more easy to implement and understand ..IMHO.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Ok, thanks to you both. I'll work out a way to do it with the Shunting-yard algorithm and come back if I have any problems.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    What if I don't need the calculator to perform the correct order of operations, for example:

    5-3*5
    =10

    It just reads from left to right, but the amount of operators and operands is unknown. So the user could input:
    5-4

    or

    5-4+3+3+2/2-2*5

    Again the expression would be evaluated from left to right without using any order of operations, but the number of operators and operands is unknown.

    Thanks

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Just read the input character by character and perform the operation with a switch-case when you have two operands.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Ok, great. The problem is I only know how to get input with cin. So I wouldn't have to make a lot of variables, or? How do I know how many variables to make?

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

Similar Threads

  1. Simple Calculator
    By green_ghoul in forum C Programming
    Replies: 6
    Last Post: 11-08-2011, 07:55 PM
  2. simple calculator problem...
    By yeohwl91 in forum C Programming
    Replies: 14
    Last Post: 09-27-2011, 08:35 AM
  3. Simple calculator
    By princeyeni in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2010, 03:54 AM
  4. simple calculator
    By Micko in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2005, 06:47 AM
  5. Having some problem with my simple calculator
    By Aven in forum C Programming
    Replies: 3
    Last Post: 07-21-2002, 09:03 AM

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