Hello can someone help me with this in C++ code. Skeleton Code: #include <string
ID: 3742011 • Letter: H
Question
Hello can someone help me with this in C++ code.
Skeleton Code:
#include <string>
#include <istream>
#include <ostream>
#include "Wordnum.h"
using namespace std;
/**
* Creates a Wordnum for a given value
*
* @param n the value of the number
*/
Wordnum::Wordnum(int n) {
value_ = n;
}
/**
* Creates a Wordnum equivalent to other
*
* @param other the other value
*/
Wordnum::Wordnum(const Wordnum& other) {
value_ = other.value_;
}
/**
* Inserts a Wordnum into an output stream as a word string
*
* @param os the stream to insert into
* @param n the value to insert
* @return the modified stream
*/
ostream& operator<<(ostream& os, const Wordnum& n) {
return os << Wordnum::write_number(n.value_);
}
/**
* Extracts a Wordnum in word string form from an input stream
*
* @param is the stream to extract from
* @param n the value to assign
* @return the modified stream
*/
istream& operator>>(istream& is, Wordnum& n) {
string text;
if (is >> text) {
n.value_ = Wordnum::read_number(text);
}
return is;
}
/**
* Returns sum, difference, product, or quotion of n1 and n2
*/
Wordnum operator+(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ + n2.value_);
}
Wordnum operator-(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ - n2.value_);
}
Wordnum operator*(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ * n2.value_);
}
Wordnum operator/(const Wordnum& n1, const Wordnum& n2) {
return Wordnum(n1.value_ / n2.value_);
}
/**
* Writes a number word string
*
* @param n the number to write
* @return the word string
*/
string Wordnum::write_number(int n) {
return ""; // replace with your conversion code
}
/**
* Reads a number word string
*
* @param n the string to read
* @return the value read
*/
int Wordnum::read_number(string n) {
return 0; // replace with your conversion code
}
Explanation / Answer
//File Name: Wordnum.h
#ifndef WORDNUM_H
#define WORDNUM_H
#include <iostream>
#include<string>
using namespace std;
// strings at index 0 is not used, it is to make array indexing simple
string onePos[] = { "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen "
};
// strings at index 0 and 1 are not used, they is to make array indexing simple
string tenPos[] = { "", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " };
// Class Wordnum definition
class Wordnum
{
// Data member to store number
int value;
public:
// Prototype of member function
Wordnum();
Wordnum(int n);
Wordnum(const Wordnum& other);
friend ostream& operator<<(ostream& os, const Wordnum& n);
friend istream& operator>>(istream& is, Wordnum& n);
friend Wordnum operator+(const Wordnum& n1, const Wordnum& n2);
friend Wordnum operator-(const Wordnum& n1, const Wordnum& n2);
friend Wordnum operator*(const Wordnum& n1, const Wordnum& n2);
friend Wordnum operator/(const Wordnum& n1, const Wordnum& n2);
static string write_number(long n);
static int read_number(string n);
void setValue(long n);
};// End of class
#endif
--------------------------------------------------------------------------------------------
//File Name: NumberToWord.h
#include <istream>
#include <ostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "Wordnum.h"
using namespace std;
/**
* To set value to n value
*
* @param n the value of the number
*/
void Wordnum::setValue(long n)
{
value = n;
}// End of function
/**
* Creates a Wordnum to initialize value to zero
*/
Wordnum::Wordnum()
{
value = 0;
}// End of function
/**
* Creates a Wordnum for a given value
*
* @param n the value of the number
*/
Wordnum::Wordnum(int n)
{
value = n;
}// End of function
/**
* Creates a Wordnum equivalent to other
*
* @param other the other value
*/
Wordnum::Wordnum(const Wordnum& other)
{
value = other.value;
}// End of function
/**
* Inserts a Wordnum into an output stream as a word string
*
* @param os the stream to insert into
* @param n the value to insert
* @return the modified stream
*/
ostream& operator<<(ostream& os, const Wordnum& n)
{
return os << Wordnum::write_number(n.value);
}// End of function
/**
* Extracts a Wordnum in word string form from an input stream
*
* @param is the stream to extract from
* @param n the value to assign
* @return the modified stream
*/
istream& operator>>(istream& is, Wordnum& n)
{
string text;
is >> text;
// Calls the function to convert the text to number
n.value = Wordnum::read_number(text);
return is;
}// End of function
/**
* Returns sum of n1 and n2
*/
Wordnum operator+(const Wordnum& n1, const Wordnum& n2)
{
// Returns the added objects result object
return Wordnum(n1.value + n2.value);
}// End of function
/**
* Returns difference of n1 and n2
*/
Wordnum operator-(const Wordnum& n1, const Wordnum& n2)
{
// Returns the subtracted objects result object
return Wordnum(n1.value - n2.value);
}// End of function
/**
* Returns product of n1 and n2
*/
Wordnum operator*(const Wordnum& n1, const Wordnum& n2)
{
// Returns the multiplied objects result object
return Wordnum(n1.value * n2.value);
}// End of function
/**
* Returns quotion of n1 and n2
*/
Wordnum operator/(const Wordnum& n1, const Wordnum& n2)
{
// Returns the divided objects result object
return Wordnum(n1.value / n2.value);
}// End of function
/**
* Reads a number word string
*
* @param num the number string s string number
* @return the string concatenated number
*/
string numToWords(int num, string s)
{
string str = "";
// if n is more than 19, divide it
if (num > 19)
str += tenPos[num / 10] + onePos[num % 10];
else
str += onePos[num];
// if n is non-zero
if (num)
str += s;
return str;
}// End of function
/**
* Reads a number word string
*
* @param no the string to read
* @return the value read
*/
int Wordnum::read_number(string no)
{
// object from the class stringstream
stringstream stringNo(no);
// The object has the value 12345 and stream it to the integer num
int num;
stringNo >> num;
// Returns the number
return num;
}// End of function
/**
* Writes a number word string
*
* @param no the number to write
* @return the word string
*/
string Wordnum::write_number(long no)
{
// String object to store word representation of given number no
string stringNo;
// Calculates digits at ten millions and hundred millions places (if any)
stringNo += numToWords((no / 10000000), "crore ");
// Calculates digits at hundred thousands and one millions places (if any)
stringNo += numToWords(((no / 100000) % 100), "lakh ");
// Calculates digits at thousands and tens thousands places (if any)
stringNo += numToWords(((no / 1000) % 100), "thousand ");
// Calculates digit at hundreds places (if any)
stringNo += numToWords(((no / 100) % 10), "hundred ");
// Checks if the number is greater than 100 and remainder is not zero
if (no > 100 && no % 100)
stringNo += "and ";
// handles digits at ones and tens places (if any)
stringNo += numToWords((no % 100), "");
// Returns the string form of the no
return stringNo;
}// End of function
/**
* Display menu
*
* @return user choice
*/
char menu()
{
// To store choice
char ch;
// Displays menu
cout<<" + - Add"<<endl;
cout<<"- - Sub"<<endl;
cout<<"* - Mul"<<endl;
cout<<"/ - Div"<<endl;
cout<<"E - Exit"<<endl;
// Accepts choice
cout<<"Enter your choice: ";
cin>>ch;
// Returns choice
return ch;
}// End of function
// main function definition
int main()
{
// Declares objects
Wordnum first, sec, output;
// Loops till user choice is not 'E' or 'e'
do
{
// Accepts two numbers for the objects
cout<<" Enter first number string: ";
cin>>first;
cout<<" Enter second number string: ";
cin>>sec;
// Calls the menu() function to accept user choice and
// Calls appropriate function based on the user choice
switch(menu())
{
case '+':
output = first + sec;
break;
case '-':
output = first - sec;
break;
case '*':
output = first * sec;
break;
case '/':
output = first / sec;
break;
case 'E':
case 'e':
exit(0);
default:
cout<<" Invalid choice!";
}// End of switch case
// Displays result object
cout<<output<<endl;
}while(1);// End of do - while loop
return 0;
}// End of main function
Sample Output:
Enter first number string: 12
Enter second number string: 2
+ - Add
- - Sub
* - Mul
/ - Div
E - Exit
Enter your choice: +
fourteen
Enter first number string: 22
Enter second number string: 3
+ - Add
- - Sub
* - Mul
/ - Div
E - Exit
Enter your choice: -
nineteen
Enter first number string: 10
Enter second number string: 3
+ - Add
- - Sub
* - Mul
/ - Div
E - Exit
Enter your choice: *
thirty
Enter first number string: 24
Enter second number string: 5
+ - Add
- - Sub
* - Mul
/ - Div
E - Exit
Enter your choice: /
four
Enter first number string: 33
Enter second number string: 2
+ - Add
- - Sub
* - Mul
/ - Div
E - Exit
Enter your choice: e
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.