将美元金额转换为字符串

dodit 发布于 6 天前 arrays 最后更新 6 天前 11 浏览

作业是这样的(从网站上获取)

Write a program that displays a simulated paycheck. Theprogram should ask the user to enter the date, the payee's name,and the amount of the check. It should then display a simulated check with he dollar amount spelled out, as shown here. Date: 11/24/2007 Pay to the Order of: JohnPhillips $1920.85 One thousand nine hundred twenty and 85 cents
我已经完成了很多工作,但问题在于如何打印文本美元金额。这是我到目前为止(注意,这只是作为类的一部分的功能)
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "TextVersionOfNumber.h"
using namespace std;
string TextVersionOfNumber::convertNumberToText()
{
string one_19[] = {"", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine", "ten", 
        "eleven", "twelve",
        "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
        "eighteen", "nineteen"};
string twenty_90[] = {"","","twenty","thirty","forty",
        "fifty", "sixty", "seventy", "eighty", "ninety"};
double amount;
    int a = amount/1000;
    int b = (amount/100) - (a*10);
    int c =  (amount/10) - (a*100) - (b*10);
    int d = amount - (a*1000) - (b*100) -  (c*10);
    int cents = (amount*100) - (a*100000) - (b*10000) -  (c*1000) - (d*100);
if (a >= 1)
        amount_string = one_19[a] + " thousand " + one_19[b] + " hundred " 
}
void TextVersionOfNumber::setAmount(double DollarAmount)
{
    DollarAmount = amount;
}
if语句是我要开始做一个嵌套的大块if的地方,但是我的老师说“我不会接受一个带有if语句而不是数组处理的程序!决定结构将需要用来实现这个逻辑;但是使用10-20“如果”陈述不可接受!“ 很明显,我应该使用这种称为“数组处理”的逻辑,但是我对它的内容并没有丝毫的线索,到目前为止,我的搜索只是初始化和各种方式来填充和访问数组。问题是:什么是数组处理,以及如何使用它来完成此代码?不要为我完成代码,我通过例子学习最好。 好吧,我做到了,遇到了一些问题,修复了它,现在一切正常。我打电话给她,她说可以使用几个if语句,但她并不希望我只用if语句来完成这一切。所以,谢谢你们!
已邀请:

hrerum

赞同来自:

那么你可以得到每个数字的第一个数字并通过一些数组运行它:

int thousands = amount / 1000;
int hundreds = amount % 1000 / 100;
int tens = amount % 100 / 10;
int ones = amount % 10;
int decimal = amount - static_cast<int>(amount) * 100; 
//takes out the decimal and multiplies by 100
final_string = th_string[thousands] + hu_string[hundreds] + te_string[tens] + o_string[ones] + to_string(decimal) + "cents";
当然你必须实现_string:我不打算为你做HW

mneque

赞同来自:

"I have quite a lot of it done, but the problem comes with how to print out the text dollar amount."
好吧,你可以尝试这样的事情:
string _0_9[] = { "", "one", "two", ..., "nine" };
string _0x_9x[10] = { "", "", "twenty", "thirty", "forty", ... "ninety" };
string _x0_x9[10][10] = {
    _0_9, // (not sure if you can do this, otherwise you can set it another way)
    { "ten", "eleven", "twelve", ..., "nineteen" },
    _0_9, _0_9, _0_9, _0_9, _0_9, _0_9, _0_9, _0_9
}; 
string tags[] = {"", "hundred", "thousand", "million", ...}
因此,您可以尝试一次将数字减少3位数:
int tagIndex = 0, value = 12345, remainder;
while (value > 0) {
    remainder = value % 1000; // (== 345 first time)
    value = (value - remainder) / 1000; // (move the numbers down, so 12 is left)
    int hundredsIndex = value / 100; // (== 3 first time)
    int tensIndex = value % 100 / 10; // (== 4 first time)
    int onesIndex = value % 10; // (== 5 first time)
    amount_string = _0_9[hundredsIndex] + tags[(hundredsIndex>0?1:0)]
                    + _0x_9x[tensIndex] + _x0_x9[tensIndex][]
                    + tags[tagIndex]
                    + amount_string;
    tagIndex = tagIndex + 1; // "thousand" will be used on next loop pass
}
这只是一个例子 - 我从脑子里拿出C++而没有时间测试任何一个(所以它可能不会编译,你的计算机可能会崩溃成一个小的奇点),但希望如此你明白了;)。此外,我没有处理美分,但你可以搞清楚。 ;)