Calculator

This is a discussion on Calculator within the C++ Programming forums, part of the General Programming Boards category; Take it and compile it, see how it works. Updated again, sad how you didn't see the progress. Also, I ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Post Calculator

    Take it and compile it, see how it works.
    Updated again, sad how you didn't see the progress.
    Also, I have no idea how to "name" versions, like 1.2.3.4, so I went for what felt right
    Code:
    /*
      Name: Calculator 1.2.2
      Author: Silkey Katzewurzt
      Date: 26-07-07 21:49
      Description: A calulator!
    */
    #include <iostream>
    using namespace std;
    int add(int a,int b);
    int sub(int a,int b);
    int mul(int a,int b);
    int divi(int a,int b);
    int main() {
        char command;
        int a;
        int number = 0;
        int saved = 0;
        string avCom = "+,-,*,/";
        string instructions = "\nThis works just like a normal calculator,"
                              " available commands are: \nTo reset your calculation, "
                              "type in 'c' and then any number,\nand it will reset to 0."
                              "\nTo save, type in 'm' and then any number."
                              "\nTo load a saved calculation, type in 'l' and then any number."
                              "\nTo see these instructions, type in i and then any number.\n";
                        cout<<instructions;
                        cout<<"\n\n"<<number<<"\n";
        while(true){
                    cout<<"\n";
                    cin>>command>>a;
                  
                    switch(command) {
                        case '+':
                             cout<<number<<" plus "<<a<<" equals to "<<add(number,a);
                             number = add(number,a);
                             break;
                        case '-':
                             cout<<number<<" minus "<<a<<" equals to "<<sub(number,a);
                             number = sub(number,a);;
                             break;
                        case '*':
                             cout<<number<<" multiplied by "<<a<<" equals to "<<mul(number,a);
                             number = mul(number,a);
                             break;
                        case '/':
                             cout<<number<<" divided by "<<a<<" equals to "<<divi(number,a);
                             number = number/a;
                             break;
                        case 'm':
                             cout<<number<<" has been saved.\n";
                             saved = number;
                             break;
                        case 'l':
                             cout<<saved<<" has been loaded.\n";
                             number = saved;
                             break;;
                        case 'c':
                             cout<<"Your calculation has been reset.";
                             number = 0;
                             break;
                        case 'i':
                             cout<<instructions;
                             break;
                        default:
                             cout<<"No such command.\n";
                             break;
                    }
        }
    }
    int add(int a,int b) {
        return a+b;
    }
    int sub(int a,int b) {
        return a-b;
    }
    int mul(int a,int b) {
        return a*b;
    }
    int divi(int a,int b) {
        return a/b;
    }
    /*
    26-07-07 22:02
    26-07-07 22:39
    26-07-07 23:15
    */
    Last edited by SilkeyWurzt; 07-26-2007 at 04:08 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Sorry, what's the question?

    Apart from how to fold long lines, these are equivalent
    Code:
      cout << "This is a very long line indeed";
      cout << "This is a "
              "very long line "
              "indeed";
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Its ok

    Why have you included iostream twice? You only need to include header files once if its not a project ( ie: single source code file )

    Add a little whitespace, your program is a little squashed up
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                        case 'r':
                             cout<<"Your calculation has been reset.";
                             break;
    And this code does what?

    By the way, on a calculator, you usually use "c" to "clear" things, so why not use that here, and "m" is "store in memory" - since you don't have a separate key, "l" is as good as any for restoring from memory.

    You may also want to add upper case to your selections, such that if you hit "caps-lock", it still does the same thing - but that's optional.

    By the way, why do you set "number=0" when you save it?

    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Hmm, I didn't do
    Code:
    #include <iostream>
    twice on purpose, so to speak, that was a mistake...
    Actually I have no idea how that could ever happen!

    Also, I thought of using both lowercase and uppercase, but how do I do that?
    Code:
    switch(a) {
    case 'a'|'A':
    }
    I tried that before, but it didn't seem to work.

    I changed the save from 's' to 'm', same with clear.
    The "reset" should have a number=0; in it, but hm, I think it was when I was adding new options, I put the number=0 in the save, instead of in the reset.

    It should be good now, the first post has been edited to your liking!

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Quote Originally Posted by Salem View Post
    Sorry, what's the question?

    Apart from how to fold long lines, these are equivalent
    Code:
      cout << "This is a very long line indeed";
      cout << "This is a "
              "very long line "
              "indeed";
    Oh sorry, is this a help forum?
    I thought I could post things that I have made.
    Sorry! >.<

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To check for multiple cases, you need different case statements, such as

    Code:
       case 'a':
       case 'A':
         ...
    You can pile a heap of case-statements on the same place, such as if you wanted to convert a hex-number, you could do something like this:

    Code:
      switch(c) {
       case '0':
       case '1':
       case '2':
       case '3':
       case '4':
       case '5':
       case '6':
       case '7':
       case '8':
       case '9':
          num = c - '0';
          break;
    
       case 'a':
       case 'b':
       case 'c':
       case 'd':
       case 'e':
       case 'f':
          num = c - 'a' + 10;
          break;
    
       case 'A':
       case 'B':
       case 'C':
       case 'D':
       case 'E':
       case 'F':
          num = c - 'A' + 10;
          break;
         
       case '\0':
          return result;
        
       default:
           printf("invalid number...\n");
           break;
      }  // switch
      result <<= 4;
      result += num;
       ...  some sort of loop ...
    Note: I'm not suggesting that this is a GOOD way to convert hex-string to binary - just giving it as an example of dealing with multiple case-statements that do the same thing. [Actually, a good compiler should convert this into very similar code to comparing the bounds of digits and 'a' .. 'f', so it shouldn't be too bad - but I don't generally trust compilers to do a really great job unless I've checked it out for this purpose]

    --
    Mats

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Quote Originally Posted by matsp View Post
    To check for multiple cases, you need different case statements, such as

    Code:
       case 'a':
       case 'A':
         ...
    You can pile a heap of case-statements on the same place, such as if you wanted to convert a hex-number, you could do something like this:

    Code:
      switch(c) {
       case '0':
       case '1':
       case '2':
       case '3':
       case '4':
       case '5':
       case '6':
       case '7':
       case '8':
       case '9':
          num = c - '0';
          break;
    
       case 'a':
       case 'b':
       case 'c':
       case 'd':
       case 'e':
       case 'f':
          num = c - 'a' + 10;
          break;
    
       case 'A':
       case 'B':
       case 'C':
       case 'D':
       case 'E':
       case 'F':
          num = c - 'A' + 10;
          break;
         
       case '\0':
          return result;
        
       default:
           printf("invalid number...\n");
           break;
      }  // switch
      result <<= 4;
      result += num;
       ...  some sort of loop ...
    Note: I'm not suggesting that this is a GOOD way to convert hex-string to binary - just giving it as an example of dealing with multiple case-statements that do the same thing. [Actually, a good compiler should convert this into very similar code to comparing the bounds of digits and 'a' .. 'f', so it shouldn't be too bad - but I don't generally trust compilers to do a really great job unless I've checked it out for this purpose]

    --
    Mats
    Thanks my friend! *hug*

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And by the way, version numbers can be just about anything. But often they are x.y.z(-w), where:

    x is the MAJOR release. When this changes, something BIG has happened, e.g. Windows NT was version 4. Windows 2000 was 5. Normally, you expect software to "break compatibility" to some extent when changing this number. Say for example you added a way to "save" old calculations in your calculator. When you move from version 1.x to 2.0, you may decide to change the format so that the new version can do more things with the saved file - it should still (if possible) support the OLD version for loading, but files saved with the new version may not operate correctly with an old version of the calculator. [1]

    y is the MINOR release. Changes here are feature improvements or major bug-fixes. Normally shouldn't cause any compatibility issues, saved files should be back- and forwards compatible, and such.

    z is used to indicate small changes, such as small feature enhancements, bug-fixes and so on. (I'm not sure what this is called - I think some people call it release-number).

    w is sometimes used as a "fix-release", e.g. a security problem or some bug-fix - this should not change any user-visible behaviour (except of course if the "user" is trying to use the security hole, or the software no longer crashes when you visit a particular web-site or whatever the bug that was fixed is).

    Some people (and I have done this before now) include a "build number" or "build date-time" each time the application is built (at least for "release", e.g. you plan to send it out).


    [1] It is generally a good idea to have some sort of "version" in a save-file, so that the software can at least detect that the file is "wrong version", rather than just doing random stupid things (crashing, incorrect results, garbled stuff being printed, and so on).

    --
    Mats

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    You seem like a very wise man, do you have any suggestion what a confused SilkeyWurzt like me could do now, either a new program, or improve this calculator? Not that there is much to do with it now.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Rather than use long switch statements, there is a function that will make your job easier. http://www.cppreference.com/stdstring/tolower.html
    (Or http://www.cppreference.com/stdstring/toupper.html.)

    You can just go
    Code:
    switch(tolower(c)) {
    case 'a':
        std::cout << "You entered 'a'.\n";
        break;
    }
    and it will handle 'a' and 'A' in the same way.

    I actually think that matsp's explanation is better than this one, but anyway, here's the Wikipedia entry: http://en.wikipedia.org/wiki/Software_versioning
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SilkeyWurzt View Post
    You seem like a very wise man, do you have any suggestion what a confused SilkeyWurzt like me could do now, either a new program, or improve this calculator? Not that there is much to do with it now.
    Two obvious things:
    - Supporting parenthesis will excercise some of your skill in programming. For example, if you want to calculate (2+2) * (3+4) it should become 28, rather than 2 + (2*3) +4 = 12. [That's what your application does now].
    - Somewhat related is to support standard mathematical priorities in an expression - such that 4 + 3 *7 calculates to 4 + (3 *7) = 25, rahter than (4 + 3)*7 = 49. Not all calculators support this, but if you have an advanced one, it will.

    Parenthesis is probably the easier task to perform first, and then use the code you've added to support parenthesis to do "implicit" parenthesis so that you can do priorities correctly.

    --
    Mats

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, 03: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, 09: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, 08:31 AM

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