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

Please help to solve a c program, Padovan String Problem Description A P...

Padovan String Problem Description A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes s

Operator overloading, Op e r a t o r O v e r l o a d i n g :...

Op e r a t o r O v e r l o a d i n g : W h e n   a n   o p e r a t o r   i s   u s e d   f o r   d i ff e r e n t   op e r a

Big M method, I Want a answer for solving the big M method in the topic of ...

I Want a answer for solving the big M method in the topic of simplex method...

Recursive function, Write a recursive function recursiveMin that takes an i...

Write a recursive function recursiveMin that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element in the array. The

Padovan string, padovan string program in java // aakash , suraj , p...

padovan string program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring; public class PadovanString {     publ

Cin, how we use the cin command and why we use this command????

how we use the cin command and why we use this command????

Program of 2d byte array dynamicall, In this lab, please complete a given p...

In this lab, please complete a given program to perform the following tasks: 1. Allocate a 10 by 5 2D byte array dynamically. The way of allocation must be consistent with page

What is the maximum number of constructors, Question: (a) (i) Explain...

Question: (a) (i) Explain how class members can be accessed when using objects of the class. Use a suitable example to illustrate your answer. (ii) Explain how the privat

C program to construct a structure , c program to construct a structure: ...

c program to construct a structure: struct sensus                 {                                 char name[30];                                 long int population;

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