Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:52 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: C++ Calculator
PostPosted: Thu Nov 13, 2008 2:18 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
/**********************************************************************
* Calculator program.  Runs in integer or floating point mode.
* Commands are:
*      = <number>      Enter <number> into the display.
*      <op> <number>   Compute the operation: + - * / ^
*              Note: The space after = or <op> is required.
*      i[nteger]       Go to integer mode.
*      d[ouble]        Go to double mode.
*      f[loat]         Go to double mode.
*      q[uit]          Exit
*
* There is a "display," and after each command is executed, the display
* is printed out.  Operation commands operate on the display as left
* operand and the input number as the right operand, and return the
* result to the display.  Each command must be followed by a newline.
* The operations are on integer or double type, depending on mode.
* Changes into the current mode do nothing.  Initially, the mode is
* double and the display is zero, and the display is printed once
* before the first command is read.  Assumes that execution is
* terminated with a 'q' command, so does not check EOF.
**********************************************************************/
#include <iostream>
#include <math.h>
#include <ctype.h>

using namespace std;

main()
{
        int idisp;              // Integer mode display val
        char mode = 'd';        // Mode = 'd' or 'i'
        double ddisp = 0.0;     // Double mode display val

        // Loop until exited by a quit command.
        while(1)
        {
                // Display.
                if(mode == 'd') cout << "  " << ddisp << endl;
                else cout << "  " << idisp << endl;

                // Input a command.  We issue a prompt, skip leanding
                // blanks, read one non-blank which is our command, then
                // skip to the next digit, space, or lineend.
                char comd;              // Command character.
                char ch;                // Other input character.
                cout << '>';
                if(cin >> comd) {
                        // Skip to a digit, space, lineend, or EOF.
                        while(cin.get(ch)) {
                                if(isspace(ch)) break;
                                if(isdigit(ch)) break;
                        }

                        // If we reached a digit, pretend we never saw it.
                        if(isdigit(ch)) cin.unget();
                } else
                        // If we reached EOF, pretend command is quit.
                        comd = 'q';

                // Obey the command.
                if(comd == 'q') break;
                switch(comd)
                {
                        int in;         // Integer input value.
                        double din;     // Double input value.
                case '=': // Enter.
                        if(mode == 'd')
                                cin >> ddisp;
                        else
                                cin >> idisp;
                        break;

                case '+': // Add.
                        if(mode == 'd')
                        {
                                cin >> din;
                                ddisp += din;
                        }
                        else
                        {
                                cin >> in;
                                idisp += in;
                        }
                        break;

                case '-': // Subtract.
                        if(mode == 'd')
                        {
                                cin >> din;
                                ddisp -= din;
                        }
                        else
                        {
                                cin >> in;
                                idisp -= in;
                        }
                        break;

                case '*': // Multiply.
                        if(mode == 'd')
                        {
                                cin >> din;
                                ddisp *= din;
                        }
                        else
                        {
                                cin >> in;
                                idisp *= in;
                        }
                        break;

                case '/': // Divide.
                        if(mode == 'd')
                        {
                                cin >> din;
                                ddisp /= din;
                        }
                        else
                        {
                                cin >> in;
                                idisp /= in;
                        }
                        break;

                case '^': // Raise to power.
                        if(mode == 'd')
                        {
                                cin >> din;
                                ddisp = pow(ddisp,din);
                        }
                        else
                        {
                                cin >> in;
                                din = rint(pow((double)idisp,(double)in));
                        }
                        break;

                case 'i': // Integer mode.
                        if(mode == 'd')
                        {
                                idisp = rint(ddisp);
                                mode = 'i';
                        }
                        if(ch == '\n') continue;
                        break;

                case 'd': // Floating mode.
                case 'f':
                        if(mode == 'i')
                        {
                                ddisp = idisp;
                                mode = 'd';
                        }
                        if(ch == '\n') continue;
                        break;
                default:
                        cout << "Bad command, " << comd << endl;
                }

                /* discard up to a newline. */
                while(ch != '\n')
                        if(!cin.get(ch)) break;
        }

        exit(0);
}


This is a C++ program, but it includes a C header file, ctype.h. Ctype classifies characters; the isspace and isdigit functions come from this header.

Some new streams calls:

* The cin.get(ch) reads one chararacter into ch, but does not skip leading spaces. The cin >> ch construct does, so it won't read a space.

* The cin.unget() construct unreads the last character gotten from cin, so the next read from cin will fetch the same character again. (Generally, you can only go back one character.)


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Simple JavaScript calculator
 Scientific Calculator
 Build Calculator in OpenGL
 Making calculator in JAVA
 C++ calculator with modification.
 how to program a windows standard calculator
 Complex numbers calculator (C++)
 Calculator
 Simple Calculator
 Calculator

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team