Thread: Simple Calculator (input problem)

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Like this:
    Code:
    std::cin>>operand1>>operator>>operand2;
    ...where operands are numbers and operator is a char.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    @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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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;
    }

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

    Thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  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-28-2011 at 12:26 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Just read the input character by character and perform the operation with a switch-case when you have two operands.

  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?

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, 08: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