Thread: calculator

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    calculator

    Ok, I made a program that can get the operator and the variables, but really, I like it to be more of a real calculator. You know, where you can even more variables then just the preset of them ( the 2 vabls, and the oper. ). Anyone have a idea how this would be done? I was thinking of using strings, and using the "find" funchion, but really, I dont know how to go about it that way either. Any help would be most helpful.
    Code:
    #include <c:\tt.h>
    
    using namespace std;
    
    int main ()
      { clrscr();
        float a, b, result;
        char oper, ans;
        cout<<" 7 8 9 +\n 4 5 6 -\n 1 2 3 *\n 0 . /\n";
        do
            {
            cout<<"Enter first number, operator, second number: ";
            cin>>a>>oper>>b;
            switch (oper)
                {
                case '+':
                result=a+b;
                break;
                case '-':
                result=a-b;
                break;
                case '*':
                result=a*b;
                break;
                case '/':
                result=a/b;
                break;
                default: cout<<"Error";
            }
            cout<<"Answer = "<<result;
            cout<<"\nDo another (y/n)? ";
            cin>>ans;
        }
        while (ans=='y');
        getchar();
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    There's a whole bunch of ways to do it. You could use find, but you should probably implement PEMDAS (order of operations) and generally be able to tokenize the string. There is a FAQ entry on this actually. And it just so happens that the FAQ entry has code for a simple calculator expression parsing dealio. If you don't want it to be spoiled, you don't have to click, and then just google stuff or something, and just think about things. Break down the steps as small as possible and implement them in terms of code.

    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by Tonto
    There's a whole bunch of ways to do it. You could use find, but you should probably implement PEMDAS (order of operations) and generally be able to tokenize the string. There is a FAQ entry on this actually. And it just so happens that the FAQ entry has code for a simple calculator expression parsing dealio. If you don't want it to be spoiled, you don't have to click, and then just google stuff or something, and just think about things. Break down the steps as small as possible and implement them in terms of code.

    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    lol ok ^.^ ty, i'll look it up and then look at that link.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include <c:\tt.h>
    You really shouldn't be writing your code in the root directory of your C drive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Also, take a look at Reverse Polish Notation, try wikipedia. It might help to do what you want to do.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    > #include <c:\tt.h>
    You really shouldn't be writing your code in the root directory of your C drive.
    Can I ask why O.o? Its only a .h file not really the work.

    Ok I Understand how to get a string to be tokenize, but I dont understand how to really read the tokenized string. After I tokenize them, how would I go about reading left and right of the operator token? Sorry to be a bother XD I tried out that link, but still didnt get it fully.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can I ask why O.o? Its only a .h file not really the work.
    Well presumably there is tt.c and tt.o which describe the real work, and they're in C:\ as well.

    Since C:\ contains some very important system files, you really don't want a mistake to completely hose your system.
    For the same reason, you don't want to pollute the compiler installation with your own work either.

    Besides, specifying absolute paths to include files makes it harder to move the project to another directory, or re-use part of the project in other projects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Do a calculator that uses reverse Polish notation It is my favourite, and it works with a stack, and it is easy to write a simple version of it. Then some additional functions like duplicating the last element in the stack, swaping the to last etc. should be implemented to make an even better calculator!
    Last edited by TriKri; 11-06-2006 at 03:37 PM.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by adr
    Can I ask why O.o? Its only a .h file not really the work.
    If you want to mess with the drive root, make some partitions.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by maxorator
    If you want to mess with the drive root, make some partitions.
    Lol, ok ok, but how do I got about reading the tokens I have made from the strings? I dont get how they are storaged really. XD Can someone make a small example? That would help more for me to understand. I get how they are Tokenzied just not storaged so I can read from then to compare then to my switch.

  11. #11
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I'm doing a similar project, which involves postfix and infix notation and a stack, it would help to look that up if you want to simulate a calculator with precedence and all. as for tokenising then you should use an input string stream. Basically they are like output streams but for strings. You can take a whole line of input and store it into an input string stream (or is it output, I always get confused which one to use), and do a loop to output each token to whatever you use to evaluate. If you are doing it like that I would look up "infix to postfix notation", as it is easier to evaluate without doing a ton of conditional loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM