Write code for a program in c++ preferably in visual studio 2008 or 2015 that ta
ID: 3834470 • Letter: W
Question
Write code for a program in c++ preferably in visual studio 2008 or 2015 that takes an input of any number up to 2 billion and converts the number into its equivalent string word. For instance the number 1 equals ONE when typed inby user all the way to 2,000,000,000 = TWO BILLION take account of numbers like 11 = eleven, twelve = 12, 112 = ONE HUNDRED AND TWELVE. Also take account for the decimals place and commas. It works - for all numbers between 0 and 2 billion , for all positive, signed integers. Each function should be small, single-purposed, and readable. You should have decomposed the problem using the repeating three-digit pattern. You really really should havea function string threeDigitsToWrittenStr ( int onesTensHundreds ) Your names don't need to be my suggestions... but should be readable, convey meaning, and make your design more understandable. Your main function should have explicit tests for boundaries of interest: 0-31, all decades, 99-119, all hundreds, 999-1019, 9999-10019, 99999-100019, 999999-1000019, up to the maximum. You should have a basic input loop using cin >> double, and have begun working on the input/floating point.
Explanation / Answer
// C++ code
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned long long int Denominations[] = {1000000000000000000,1000000000000000,1000000000000,1000000000,1000000,1000,100,1};
const int Denums_Count = sizeof(Denominations)/sizeof(long long int);
char Ones[][20] = {"zero","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
char Tens[][20] = {"twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
char Scale[][20] = {"hexillion ","quintillion ","quadrillion ","trillion ","billion ","million ","thousand ","hundred ",""};
char Teens[][20] = {"ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
string threeDigitsToWrittenStr(int n)
{
int n1,n2,n3;
n1=n%10; n/=10;
n2=n%10; n/=10;
n3=n%10;
string s="";
if (n3!=0)
{
s+=Ones[n3];
s+="hundred ";
}
if (n2!=0)
{
if (n2==1)
{
s+=Teens[n1];
return s;
}
s+=Tens[n2-2];
s.replace(s.length()-1,1,"-");
}
s+=Ones[n1];
return s;
}
string Words(unsigned long long int n)
{
int DenumCount[Denums_Count] = {0};
int DenumPointer=0;
while (true)
{
if (n < Denominations[DenumPointer])
{
DenumPointer++;
if (DenumPointer>=Denums_Count)
{
break;
}
continue;
}
DenumCount[DenumPointer]++;
n-=Denominations[DenumPointer];
}
string s="";
//creating a string on the basis of this values
for (int i=0;i<Denums_Count;i++)
{
if (DenumCount[i]==0) continue;
s+=threeDigitsToWrittenStr(DenumCount[i]);
s+=Scale[i];
}
return s;
}
int main()
{
unsigned long long int i;
cout << " Enter the number you wish to convert. (0) to exit ";
while(true)
{
cout << " Enter a number to find the word spell " << endl;
cin >> i;
if (!i) break;
string s = Words(i);
cout << Words(i) << endl;
}
return 0;
}
/*
output:
Enter the number you wish to convert. (0) to exit
Enter a number to find the word spell
85788868686
eighty-five trillion seven hundred eighty-eight billion eight hundred sixty-eight million six thousand eighty-six hundred
Enter a number to find the word spell
9940505
nine billion nine hundred forty-zeromillion five thousand five hundred
Enter a number to find the word spell
29394949959599595
twenty-nine quintillion three hundred ninety-four quadrillion nine hundred forty-nine trillion nine hundred fifty-nine billion five hundred ninety-nine million five thousand ninety-five hundred
Enter a number to find the word spell
0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.