Program for simple 4-function calculator, C/C++ Programming

Most first graders know that nine hundred and ninety nine plus one is one thousand, but C++ doesn't! Sure, a computer can easily compute 999 + 1 and get 1000. But reading and writing the numbers in words is not so easy.

Task

Consider the following code, which implements a very simple 4-function calculator:
float n1, n2;
char op;
while (cin >> n1 >> op >> n2) {
switch (op) {
case '+': cout << n1 + n2 << endl; break;
case '-': cout << n1 - n2 << endl; break;
case '*': cout << n1 * n2 << endl; break;
case '/': cout << n1 / n2 << endl; break;
}
}
The code will read input consisting of expressions like these
2.1 + 3.7
1.732906E-9 / 4.5870021E-25
and for each expression it will display the result:
5.8
3.77786e+15
By simply changing the type of the numbers n1 and n2 (here it's float), the calculator can compute the answers for various kinds of number. For example, changing the code to read
int n1, n2;
will mean that numbers are computed and displayed according to the rules of integer arithmetic (so that you could compute that 7 / 2 is 3), and changing it to read
complex n1, n2; // also need #include
will let you verify that i is the square root of -1. [Compute (0,1) * (0,1) and get the answer (-1,0)]

Your task for this assignment is to define a class, named Wordnum, that will read and write numbers expressed in word form. To use the class in the calculator, simply include the class header file and change the type of the numbers n1 and n2 to Wordnum. Then the program can read input consisting of expressions such as
five + six
ninety_nine - ninety_nine
one_thousand_two_hundred_thirty_four * five_hundred_forty_three
one million / negative_one hundred
and for each line it will write the result in words:
eleven
zero
six_hundred_seventy_thousand_sixty_two
negative_ten_thousand

Numbers are written as English words separated by underscore characters, with the default wording following the US conventions (because they are simpler to program). Thus 1001 is written "one_ thousand_ one" and 123000009 is "one_ hundred_ twenty_ three_ million_ nine".

If British format is in effect (from level 6), tens and units are separated by a hyphen and preceded by the word "and". For example 123 would be written one_ hundred_ and_ twenty-three.

Posted Date: 3/4/2013 6:25:17 AM | Location : United States







Related Discussions:- Program for simple 4-function calculator, Assignment Help, Ask Question on Program for simple 4-function calculator, Get Answer, Expert's Help, Program for simple 4-function calculator Discussions

Write discussion on Program for simple 4-function calculator
Your posts are moderated
Related Questions
Available, and the right kind of programs. My son wants to stop using ice. He has organised detox a number of times, and is always successful. It is the next stage, rehab, where

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

What are the benefits of using friend classes? - Friend classes are useful when a class wants to hide features from users which are required only by another, tightly coupled cl

Question : (a) What is a structure? Provide a simple example of your own to illustrate this feature. (b) How does a structure differ from an array? (c) Describe a structu

C program to demonstrate Pointer to string: void main() {                 int a;                 char str[]="hello how are you?",*ptr_str;                 ptr_str=&

A: A reference has to always refer to some object and, so, must always be initialized; pointers do not have such limits. A pointer may be reassigned to point to distinct objects wh

Write the statements which are used to connect PHP with MySQL? Statements which can be used to connect PHP with MySQL is: $conn = mysql_connect('localhost'); echo $co

Objective:  Construct a C program that controls the LCD display, and is capable of displaying strings.  Add functions to the C program that allows more control over the display.

hwat is the area fsdjlakl;aeklfjtealrtl;gka

Defines the entry point for the console application. // #include "stdafx.h" #include #include #include"conio.h" using namespace std; double Determinant(double a[][3],int forde