Untitled
unknown
plain_text
a year ago
3.6 kB
15
Indexable
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
string helper(int num) {
vector<string> belowTwenty = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
vector<string> tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
if (num == 0) return "";
else if (num < 20) return belowTwenty[num] + " ";
else if (num < 100) return tens[num / 10] + " " + helper(num % 10);
else return belowTwenty[num / 100] + " hundred " + helper(num % 100);
}
string numberToWords(int num) {
vector<string> belowTwenty = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
vector<string> tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
vector<string> thousands = {"", "thousand", "million", "billion"};
if (num == 0) return "zero";
string result;
int i = 0;
while (num > 0) {
if (num % 1000 != 0) {
result = helper(num % 1000) + thousands[i] + " " + result;
}
num /= 1000;
i++;
}
while (!result.empty() && result.back() == ' ') result.pop_back(); // Trim trailing space
return result;
}
string convertCurrency(string numStr) {
// Remove the leading '$'
numStr.erase(0, 1);
// Split into dollar and cent parts
size_t dotPos = numStr.find('.');
string dollarPart = numStr.substr(0, dotPos);
string centPart = dotPos == string::npos ? "" : numStr.substr(dotPos + 1);
// Convert the dollar part
int dollarNum = stoi(dollarPart);
string dollarWords = numberToWords(dollarNum);
if (dollarNum == 1) dollarWords += " dollar";
else dollarWords += " dollars";
// Convert the cent part
string centWords;
if (!centPart.empty()) {
int centNum = stoi(centPart);
if (centNum > 0) {
centWords = " and " + numberToWords(centNum);
if (centNum == 1) centWords += " cent";
else centWords += " cents";
}
}
return dollarWords + centWords;
}
// To execute C++, please define "int main()"
void validate(string input, string expectedOutput) {
string actual = convertCurrency(input);
if(!expectedOutput.compare(actual)) {
cout << "SUCCESS" << endl;
} else {
cout << "FAILED\n\tExpected: " << expectedOutput << "\n\tActual: "<< actual << endl;
}
}
int main() {
// Do not modify the test cases
validate("$0", "zero dollars");
validate("$1", "one dollar");
validate("$4", "four dollars");
validate("$12.01", "twelve dollars and one cent");
validate("$30", "thirty dollars");
validate("$71", "seventy one dollars");
validate("$56", "fifty six dollars");
validate("$90.00", "ninety dollars");
validate("$100", "one hundred dollars");
validate("$217.84", "two hundred seventeen dollars and eighty four cents");
validate("$320", "three hundred twenty dollars");
validate("$350.21", "three hundred fifty dollars and twenty one cents");
validate("$701.82", "seven hundred one dollars and eighty two cents");
return 0;
}
Editor is loading...
Leave a Comment