Create Calculation

This is a discussion on Create Calculation within the C Programming forums, part of the General Programming Boards category; I'm trying to create a calculator that uses a text user interface. I have the interface writtern the way I ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    Create Calculation

    I'm trying to create a calculator that uses a text user interface. I have the interface writtern the way I want for now. Now I need help on writting the calculation code.

    Would main() contain something like the code below?
    Code:
    char operation;
    int quit = 0;
    while(!quit)
    {
        while(operation != '=')
        {
            getNumber();
            getOperation();
        }
        performCalculations();
    }
    Thank you
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    More easy would be to let the user enter a string with an expression and analyse this expression. In your code for example, what if the user enters more than one number or more than one operator at a time?

    Also you should initialise operation before using it.

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    One good way to make a command line calulator is to take in expressions in postfix notation ( because paranthesis are not require so it's a little simpler ).

    Then all you have to do is make a stack.

    and to evaluate the expression just ( in psuedocode),
    Code:
    while(true){
         x = read();
         if(x == aNumber) stack.push(x)
         else if(x == anOperator) {
              y = stack.pop();
              z = stack.pop();
              result = (x=='*')?y * z:
                           (x=='/')?y / z:
                           (x=='+')?y + z:
                           (x=='-')?y - z: y % z; 
              stack.push(result);
         }
    }
    this will work for expressions of any size in postfiz notation.

    If you want to use infix notation ( from the users point-of-view) you can convert the equation before you do this calulation.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    The above code was meant to see if I had the right idea and I want a text interface for my calculator similar to the Windows calculator.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I want a text interface for my calculator similar to the Windows calculator.
    Eh? The Windows calculator (calc.exe) is GUI based
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Yes, I know thats GUI based. if text isn't the right term than how about ASCII?

    []
    [7][8][9][+]
    [4][5][6][-]
    [1][2][3][/]
    [C][9][0][*]
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 05:13 PM
  2. Cannot create shared memory
    By Phoenix_Rebirth in forum C Programming
    Replies: 3
    Last Post: 11-07-2008, 11:32 AM
  3. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 09:50 AM
  4. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 05:25 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM

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