ReadNumbersByLettersAndAddNumericCommaByUsing Recursion
Apr 4, 2023 12:30:59 GMT
Professor Hank likes this
Post by bashir on Apr 4, 2023 12:30:59 GMT
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string ArrName[] = { " Thousand"," Million"," Bilion" };
string ArrNameDigi[] = { "","One" ,"Two" ,"Three","Four","Five","Six","Seven" ,"Eight","Nine","Ten",
"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninteen" };
string ArrNameDigTerms[] = { "","","Twenty" , "Thirty","Forty","Fifty","Sixty","Seventy","Eighty" ,"Ninety" ,"Hundred" };
string ReGetStringOfDigit(string& DigitString)
{
long long N;
N = stoll(DigitString);
DigitString = to_string(N);
return DigitString;
}
void ExchangeNumberToText(string DigitString)
{
ReGetStringOfDigit(DigitString);
if (DigitString.length() == 0)
{
cout << "";
}
else if (DigitString.length() == 1)
{
cout << ArrNameDigi[stoi(DigitString)];
}
else if (DigitString.length() == 2)
{
if (stoi(DigitString) >= 10 && stoi(DigitString) <= 19)
{
cout << ArrNameDigi[stoi(DigitString)];
}
else
{
cout << ArrNameDigTerms[(stoi(DigitString) / 10)];
if ((stoi(DigitString) - ((stoi(DigitString) / 10) * 10)) > 0)
{
cout << "-" << ArrNameDigi[stoi(DigitString) - ((stoi(DigitString) / 10) * 10)];
}
}
}
else if (DigitString.length() == 3)
{
int SN1, SN2;
SN1 = stoi(DigitString) / 100;
SN2 = stoi(DigitString) - SN1 * 100;
ExchangeNumberToText(to_string(SN1));
cout << " Hundred ";
ExchangeNumberToText(to_string(SN2));
}
else if (DigitString.length() > 3)
{
vector<string> VeDig;
short Len = DigitString.length();
while (Len != 0)
{
if (Len >= 3)
{
VeDig.push_back(DigitString.substr(DigitString.length() - 3, DigitString.length()));
DigitString.erase(DigitString.length() - 3, DigitString.length());
Len -= 3;
}
else
{
VeDig.push_back(DigitString);
Len = 0;
}
}
for (int i=VeDig.size()-1;i>=0;i--)
{
cout<<VeDig.at(i);
if (i != 0)
{
cout << ",";
}
}
cout << endl;
for (int i = VeDig.size() - 1; i >= 0; i--)
{
ExchangeNumberToText(VeDig.at(i));
if (i >= 1)
{
cout << ArrName;
if(stoi(VeDig.at(i-1))>0)
cout << " and ";
}
}
}
}
int main()
{
long long num;
char Again = 'y';
while (tolower(Again) == 'y')
{
cout << "Enter the number: ";
cin >> num;
string DigitString = to_string(num);
ExchangeNumberToText(DigitString);
cout << "\n\nAre you want read Another Number just press [y]: ";
cin >> Again;
}
cin.ignore();
cin.get();
return 0;
}
#include<string>
#include<vector>
using namespace std;
string ArrName[] = { " Thousand"," Million"," Bilion" };
string ArrNameDigi[] = { "","One" ,"Two" ,"Three","Four","Five","Six","Seven" ,"Eight","Nine","Ten",
"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninteen" };
string ArrNameDigTerms[] = { "","","Twenty" , "Thirty","Forty","Fifty","Sixty","Seventy","Eighty" ,"Ninety" ,"Hundred" };
string ReGetStringOfDigit(string& DigitString)
{
long long N;
N = stoll(DigitString);
DigitString = to_string(N);
return DigitString;
}
void ExchangeNumberToText(string DigitString)
{
ReGetStringOfDigit(DigitString);
if (DigitString.length() == 0)
{
cout << "";
}
else if (DigitString.length() == 1)
{
cout << ArrNameDigi[stoi(DigitString)];
}
else if (DigitString.length() == 2)
{
if (stoi(DigitString) >= 10 && stoi(DigitString) <= 19)
{
cout << ArrNameDigi[stoi(DigitString)];
}
else
{
cout << ArrNameDigTerms[(stoi(DigitString) / 10)];
if ((stoi(DigitString) - ((stoi(DigitString) / 10) * 10)) > 0)
{
cout << "-" << ArrNameDigi[stoi(DigitString) - ((stoi(DigitString) / 10) * 10)];
}
}
}
else if (DigitString.length() == 3)
{
int SN1, SN2;
SN1 = stoi(DigitString) / 100;
SN2 = stoi(DigitString) - SN1 * 100;
ExchangeNumberToText(to_string(SN1));
cout << " Hundred ";
ExchangeNumberToText(to_string(SN2));
}
else if (DigitString.length() > 3)
{
vector<string> VeDig;
short Len = DigitString.length();
while (Len != 0)
{
if (Len >= 3)
{
VeDig.push_back(DigitString.substr(DigitString.length() - 3, DigitString.length()));
DigitString.erase(DigitString.length() - 3, DigitString.length());
Len -= 3;
}
else
{
VeDig.push_back(DigitString);
Len = 0;
}
}
for (int i=VeDig.size()-1;i>=0;i--)
{
cout<<VeDig.at(i);
if (i != 0)
{
cout << ",";
}
}
cout << endl;
for (int i = VeDig.size() - 1; i >= 0; i--)
{
ExchangeNumberToText(VeDig.at(i));
if (i >= 1)
{
cout << ArrName;
if(stoi(VeDig.at(i-1))>0)
cout << " and ";
}
}
}
}
int main()
{
long long num;
char Again = 'y';
while (tolower(Again) == 'y')
{
cout << "Enter the number: ";
cin >> num;
string DigitString = to_string(num);
ExchangeNumberToText(DigitString);
cout << "\n\nAre you want read Another Number just press [y]: ";
cin >> Again;
}
cin.ignore();
cin.get();
return 0;
}