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

Procedure to add two numbers in a recursive manner, (a) Write a procedure c...

(a) Write a procedure called (add x y) that adds two numbers in a recursive manner. Specifically, note that x + y = (x + 1) + (y - 1) and x + 0 = x. (b) Using the substitution m

Program to track the hours an employee worked , Description: Create a...

Description: Create a program that allows the user to track the hours an employee worked in a week. How much the employee was paid and any extra hours worked (overtime pay).

Define passing structure to a function, Define Passing Structure to a Funct...

Define Passing Structure to a Function? A structure is able to be passed as a function argument identical to any other variable. If we are merely interested in one member of a

Explain the defination and declaration of union, Explain the Defination and...

Explain the Defination and Declaration of Union? Generally in terms the composition of a union may be defined as be as union tag { member 1; member 2; member m; }; Wh

Illustrate bit fields with structures, C language lets us do this in a stru...

C language lets us do this in a structure definition by putting: bit length after the variable that is. struct packed_struct { unsigned int f1:1; unsigned int f2:1; unsigned

C program to check even & odd no, C Program to Check  EVEN & ODD NO   ...

C Program to Check  EVEN & ODD NO   main() {           int n, a;           clrscr();           printf("ENTER ANY NUMBER: ");           scanf("%d", &n);

Program to create a query from database, Implement (a part of) electronic e...

Implement (a part of) electronic election Scenario where following political parties are participating in election: PPP, PML and PTI.  Create a class PollingStation where it stor

Minimum shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

C++, write a C++ program that performs the following tasks 1. prompts the u...

write a C++ program that performs the following tasks 1. prompts the user to enter two numbers 2. displays the divisors of each number and store the divisors of number 1 in A and t

Calculation of mortgage interest rates, 1. When developing this project in ...

1. When developing this project in a Win32 Console Applications that includes the precompiled headers, enter in the Name: box, PRJ2[Your Full Last Name][Your First Initial] with no

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