Total members 11895 |It is currently Sat Dec 21, 2024 1:41 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Implementation of C++ calculator
cpp 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 character 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.)



_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

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



Topic Tags

C++ Projects, C++ Math






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com