Numbers.h
#pragma once
#include <iostream>
#include <string.h>
using namespace std;
class Numbers
{
public:
//nonnegative integer
unsigned int numbers;
const int MAX_NUM = 3000;
string lessThan20[20] = { "zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen" };
string tens[10] = { "", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety" };
string hundred = "hundred";
string thousand = "thousand";
//ctor with nonnegative integer
Numbers(unsigned int num);
//prints the English description of the Numbers object
void print(unsigned int numbers);
};
Numbers.cpp
#include <iostream>
#include <string>
#include "Numbers.h"
using namespace std;
Numbers::Numbers(unsigned int num)
{
numbers = num;
}
void Numbers::print(unsigned int numbers)
{
if (numbers > MAX_NUM)
{
cout << "Wrong input data!!!";
}
else if (numbers >= 1000)
{
print(numbers / 1000);
cout << thousand;
if (numbers % 1000)
{
if (numbers % 1000 < 100)
{
cout << " and";
}
cout << " ";
print(numbers % 1000);
}
}
else if (numbers >= 100)
{
print(numbers / 100);
cout << hundred;
if (numbers % 100)
{
cout << " and ";
print(numbers % 100);
}
}
else if (numbers >= 20)
{
cout << tens[numbers / 10];
if (numbers % 10)
{
cout << " ";
print(numbers % 10);
}
}
else
{
cout << lessThan20[numbers] << " ";
}
return;
}
NumbersToString.cpp
#include <iostream>
#include <string>
#include "Numbers.h"
using namespace std;
void main()
{
unsigned int inputNum;
while (true)
{
cout << "Enter a number : ";
cin >> inputNum;
Numbers MyNumber = Numbers(inputNum);
MyNumber.print(inputNum);
cout << endl;
}
}
Comments
Leave a comment