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

Assignment Help:

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.


Related Discussions:- Program for simple 4-function calculator

Is it legal for a member function to say delete this?, Is it legal for a me...

Is it legal for a member function to say delete this? A: As long as you're cautious, it's OK for an object to delete this. Here's how I define "cautious": You have to be a

Questions on functions, Write a function for finding out highest and lowest...

Write a function for finding out highest and lowest marks obtained by a student from an array contained student name and marks. For Example : GetHighestLowestMarks{Student{n}, Ma

Which constructor gets called while i create an array of , Which constructo...

Which constructor gets called while I create an array of Fred objects?

Briefly describe how a linear search algorithm works, Question 1: (a) D...

Question 1: (a) Describe the following objects and condition states: (i) ifstream and ofstream objects (ii) eof(), fail(), bad() and good() functions. (b) Write a C++ pr

Program to compare the two dates given by the user, THIS PROGRAM IS TO COM...

THIS PROGRAM IS TO COMPARE THE TWO DATES GIVEN BY THE USER #include #include #include struct date  {   int dd;   int mm;   int yy;  }; date compare(date d1,date d2)  {

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Define the multi dimensional arrays, Define the Multi Dimensional Arrays? ...

Define the Multi Dimensional Arrays? Consequently far we have considered arrays with only one dimension. It is as well possible to define an array which is having a 2-dimension

Creating shopping carts, how to create a shopping cart and sibmit the code ...

how to create a shopping cart and sibmit the code segment on screan

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd