Código
#include <iostream> #include <locale.h> #include <string> using namespace std; //---------------------------------------------------------------------------------------------------------- string val(string n) { string c, p; int i; // for (i = 0; i <= 10; i++) { c = n.at(i); if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9") { p = "0"; } break; } return p; } //---------------------------------------------------------------------------------------------------------- void Units(string n) { if (n == "0") cout << "zero"; else if (n == "1") cout << "one"; else if (n == "2") cout << "two"; else if (n == "3") cout << "three"; else if (n == "4") cout << "four"; else if (n == "5") cout << "five"; else if (n == "6") cout << "six"; else if (n == "7") cout << "seven"; else if (n == "8") cout << "eight"; else if (n == "9") cout << "nine"; } //---------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Tens(string n) { char t = n.at(0); char u = n.at(1); n = n.substr(1, 1); // if (t == '0' && u != '0') { Units(n); } if (t == '1') { if (u == '0') cout << "ten"; else if (u == '1') cout << "eleven"; else if (u == '2') cout << "twelve"; else if (u == '3') cout << "thirteen"; else if (u == '4') cout << "fourteen"; else if (u == '5') cout << "fifteen"; else { Units(n); cout << "teen"; } } if (t == '2') { if (u == '0') cout << "twenty"; else { cout << "twenty- "; Units(n); } } if (t == '3') { if (u == '0') cout << "thirty"; else { cout << "thirty-"; Units(n); } } if (t == '4') { if (6u == '0') cout << "forty"; else { cout << "forty- "; Units(n); } } if (t == '5') { if (u == '0') cout << "fifty"; else { cout << "fifty-"; Units(n); } } if (t == '6') { if (u == '0') cout << "sixty"; else { cout << "sixty-"; Units(n); } } if (t == '7') { if (u == '0') cout << "seventy"; else { cout << "seventy-"; Units(n); } } if (t == '8') { if (u == '0') cout << "eighty"; else { cout << "eighty-"; Units(n); } } if (t == '9') { if (u == '0') cout << "ninety"; else { cout << "ninety-"; Units(n); } } } //-------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Hundreds(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); n = n.substr(1, 2); if (t == '0') { Tens(n); } if (t == '1') { if (u == '0'&& v == '0') cout << "One hundred"; else { cout << "One hundred and "; Tens(n); } } if (t == '2') { if (u == '0'&& v == '0') cout << "Two hundred"; else { cout << "Two hundred and "; Tens(n); } } if (t == '3') { if (u == '0'&& v == '0') cout << "Three hundred"; else { cout << "Three hundred and "; Tens(n); } } if (t == '4') { if (u == '0'&& v == '0') cout << "Four hundred"; else { cout << "Four hundred and "; Tens(n); } } if (t == '5') { if (u == '0'&& v == '0') cout << "Five hundred"; else { cout << "Five hundred and "; Tens(n); } } if (t == '6') { if (u == '0'&& v == '0') cout << "Six hundred"; else { cout << "Six hundred and "; Tens(n); } } if (t == '7') { if (u == '0'&& v == '0') cout << "Seven hundred"; else { cout << "Seven hundred and "; Tens(n); } } if (t == '8') { if (u == '0'&& v == '0') cout << "Eight hundred"; else { cout << "Eight hundred and "; Tens(n); } } if (t == '9') { if (u == '0'&& v == '0') cout << "Nine hundred"; else { cout << "Nine hundred and "; Tens(n); } } } //--------------------------------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------------------------------- void Thousands(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); n = n.substr(1, 3); if (t == '0') { Hundreds(n); } if (t == '1') { if (u == '0' && v == '0'&& w == '0') cout << "One thousand"; else { cout << "One thousand "; Hundreds(n); } } if (t == '2') { if (u == '0' && v == '0'&& w == '0') cout << "Two thousand"; else { cout << "Two thousand "; Hundreds(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0') cout << "three thousand"; else { cout << "three thousand "; Hundreds(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0') cout << "four thousand"; else { cout << "four thousand "; Hundreds(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0') cout << "five thousand"; else { cout << "five thousand "; Hundreds(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0') cout << "six thousand"; else { cout << "six thousand "; Hundreds(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0') cout << "seven thousand"; else { cout << "seven thousand "; Hundreds(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0') cout << "eight thousand"; else { cout << "eight thousand "; Hundreds(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0') cout << "nine thousand"; else { cout << "nine thousand "; Hundreds(n); } } } //-------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Thousands2(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); char x = n.at(4); n = n.substr(1, 4); if (t == '0') { Thousands(n); } if (t == '1') { string temp = ""; temp.resize(10); temp[0] = t; temp[1] = u; temp[2] = v; temp[3] = w; temp[4] = x; Tens(temp); cout << " thousand "; temp = &temp[2]; Hundreds(temp); return; } if (t == '2') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "twenty thousand"; else { cout << "twenty-"; Thousands(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "thirty thousand"; else { cout << "thirty-"; Thousands(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "forty thousand"; else { cout << "forty-"; Thousands(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "fifty thousand"; else { cout << "fifty-"; Thousands(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "sixty thousand"; else { cout << "sixty-"; Thousands(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "seventy thousand"; else { cout << "seventy-"; Thousands(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "eighty thousand"; else { cout << "eighty-"; Thousands(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0' && x == '0') cout << "ninety thousand"; else { cout << "ninety-"; Thousands(n); } } } //----------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Thousands3(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); char x = n.at(4); char y = n.at(5); n = n.substr(1, 5); if (t == '0') { Thousands2(n); } if (t == '1') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "one hundred thousand "; else { cout << "one hundred and "; Thousands2(n); } } if (t == '2') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "two hundred thousand "; else { cout << "two hundred and "; Thousands2(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "three hundred thousand"; else { cout << "three hundred and "; Thousands2(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "four hundred thousand"; else { cout << "four hundred and "; Thousands2(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "five hundred thousand"; else { cout << "five hundred and "; Thousands2(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "six hundred thousand"; else { cout << "six hundred and "; Thousands2(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "seven hundred thousand"; else { cout << "seven hundred and "; Thousands2(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "eight hundred thousand"; else { cout << "eight hundred and "; Thousands2(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0') cout << "nine hundred thousand"; else { cout << "nine hundred and "; Thousands2(n); } } } //----------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Millions(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); char x = n.at(4); char y = n.at(5); char z = n.at(6); n = n.substr(1, 6); if (t == '0') { Thousands3(n); } if (t == '1') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "one million "; else { cout << "one million, "; Thousands3(n); } } if (t == '2') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "two million "; else { cout << "two million, "; Thousands3(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "three million"; else { cout << "three million, "; Thousands3(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "four million"; else { cout << "four million, "; Thousands3(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "five million"; else { cout << "five million, "; Thousands3(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "six million"; else { cout << "six million, "; Thousands3(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "seven million"; else { cout << "seven million, "; Thousands3(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "eight million"; else { cout << "eight million, "; Thousands3(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0') cout << "nine million"; else { cout << "nine million, "; Thousands3(n); } } } //----------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Millions2(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); char x = n.at(4); char y = n.at(5); char z = n.at(6); char z2 = n.at(7); n = n.substr(1, 7); if (t == '0') { Millions(n); } if (t == '1') {//11 string temp = "";//112000 temp.resize(10); temp[0] = t; temp[1] = u; temp[2] = v; temp[3] = w; temp[4] = x; temp[5] = y; temp[6] = z; temp[7] = z2; Tens(temp); cout << " million "; temp = &temp[2];//12000000 Thousands3(temp); return; } if (t == '2') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "twenty million "; else { cout << "twenty million "; Millions(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "thirty million"; else { cout << "thirty million "; Millions(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "forty million"; else { cout << "forty million "; Millions(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "fifty million"; else { cout << "fifty million "; Millions(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "sixty million"; else { cout << "sixty million "; Millions(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "seventy million"; else { cout << "seventy million "; Millions(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "eighty million"; else { cout << "eighty million "; Millions(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0') cout << "ninety million"; else { cout << "ninety million "; Millions(n); } } } //----------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Millions3(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); char x = n.at(4); char y = n.at(5); char z = n.at(6); char z2 = n.at(7); char z3 = n.at(8); n = n.substr(1, 8); if (t == '0') { Millions2(n); } if (t == '1') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "one hundred million "; else { cout << "one hundred and "; Millions2(n); } } if (t == '2') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "two hundred million "; else { cout << "two hundred and "; Millions2(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "three hundred million"; else { cout << "three hundred and "; Millions2(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "four hundred million"; else { cout << "four hundred and "; Millions2(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "five hundred million"; else { cout << "five hundred and "; Millions2(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "six hundred million"; else { cout << "six hundred and "; Millions2(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "seven hundred million"; else { cout << "seven hundred and "; Millions2(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "eight hundred million"; else { cout << "eight hundred and "; Millions2(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0') cout << "nine hundred million"; else { cout << "nine hundred and "; Millions2(n); } } } //----------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------------------- void Millions4(string n) { char t = n.at(0); char u = n.at(1); char v = n.at(2); char w = n.at(3); char x = n.at(4); char y = n.at(5); char z = n.at(6); char z2 = n.at(7); char z3 = n.at(8); char z4 = n.at(8); n = n.substr(1, 9); if (t == '0') { Millions3(n); } if (t == '1') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "one billion "; else { cout << "one billion, "; Millions3(n); } } if (t == '2') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "two billion "; else { cout << "two billion, "; Millions3(n); } } if (t == '3') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "three billion"; else { cout << "three billion, "; Millions3(n); } } if (t == '4') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "four billion"; else { cout << "four billion, "; Millions3(n); } } if (t == '5') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "five billion"; else { cout << "five billion, "; Millions3(n); } } if (t == '6') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "six billion"; else { cout << "six billion, "; Millions3(n); } } if (t == '7') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "seven billion"; else { cout << "seven billion, "; Millions3(n); } } if (t == '8') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "eight billion"; else { cout << "eight billion, "; Millions3(n); } } if (t == '9') { if (u == '0' && v == '0'&& w == '0' && x == '0' && y == '0' && z == '0' && z2 == '0' && z3 == '0' && z4 == '0') cout << "nine billion"; else { cout << "nine billion, "; Millions3(n); } } } //----------------------------------------------------------------------------------------------------------------------- int main() { int i, c; string num, n, r, g; // cout << "===============================\n"; cout << "= Convert numbers to letters =\n"; cout << "===============================\n\n"; do { do { cout << "Type a number up to 10 digits to convert in Written English: "; getline(cin, n); c = n.size(); if (c == NULL) g = "0"; else g = val(n); } while (c > 10 || g == "0"); switch (n.length()) { case 1: Units(n); break; case 2: Tens(n); break; case 3: Hundreds(n); break; case 4: Thousands(n); break; case 5: Thousands2(n); break; case 6: Thousands3(n); break; case 7: Millions(n); break; case 8: Millions2(n); break; case 9: Millions3(n); break; case 10: Millions4(n); break; } cout << endl; cout << "Another number?:(Y/N) "; getline(cin, r); } while (r == "y" || r == "Y"); cout << "\n\nHOMEWORK # 2\nAuthor: Giancarlo Rosero Portillo\nto: Jesus Insuasti\n"; system("pause"); return 0; }