【问题标题】:Currency denomination货币面额
【发布时间】:2013-03-27 08:30:40
【问题描述】:

尝试为我的游戏使用正确的货币面额。货币存储为字符串(即由于我的教授而无法更改),并且按铂金、黄金、白银和铜的顺序排列。例如,如果我将我的货币初始化为“0.1.23.15”,这意味着我有 0 白金、1 黄金、23 银和 15 铜。

不过,我需要能够转换到更高的教派。那是什么意思?例如,如果我有 105 个银币(即 0.0.105.0),它应该显示为 1 个金币和 5 个银币(即 0.1.5.0)。

我在 setCost 方法中加粗了我的问题。我正在检查一个数字是否大于 100,如果是 - 我将该列设为 0,返回到前一个元素并在 ASCII 值上加一,给出正确的进位。不幸的是,调试器显示“/x4”被转储到元素中,而不仅仅是“4”。有谁知道这是为什么以及如何改变它??

编辑:编辑了代码,只要您不输入超过 100 的数字,它就可以工作。在如何使它适用于大于 100 的数字上出现问题。

这是我写过的最草率的代码。请温柔一点。 :(

void Potion::setCost(std::string cost)
{
    char buffer[256];
    std::string currencyBuffer [4];
    int integerBuffer[4];
    int * integerPointer = nullptr;
    int temp = 0;
    int i = 0;
    char * tokenPtr;
    //Convert string to cString
    strcpy(buffer, cost.c_str() );

    //Tokenize cString
    tokenPtr = strtok(buffer, ".");

    while(tokenPtr != nullptr)
    {
        //Convert ASCII to integer
        temp = atoi(tokenPtr);

        //Store temp into currency buffer
        integerBuffer[i] = temp;

        //Make pointer point to integer buffer
        integerPointer = &integerBuffer[i];

        if(*integerPointer < 100)
            currencyBuffer[i] = tokenPtr;
        else
        {
            //Store zero in column if number is 
            //greater than 100
            temp2 = temp % 100;
            itoa(temp2, temp3, 10);
            currencyBuffer[i] = temp3;

            //Go back and add one to currency buffer
            temp = atoi(currencyBuffer[i-1].c_str());
            temp += 1;
            itoa(temp, temp3, 10);
            currencyBuffer[i - 1] = temp3;
        }

        i++;

        //Get next token
        tokenPtr = strtok(nullptr, ".");
    }
    NewLine();

    std::string tempBuffer;

    //Store entire worth of potions
    tempBuffer = "Platinum: ";
    tempBuffer += currencyBuffer[0];
    tempBuffer += "\nGold: ";
    tempBuffer += currencyBuffer[1];
    tempBuffer += "\nSilver: ";
    tempBuffer += currencyBuffer[2];
    tempBuffer += "\nCopper: ";
    tempBuffer += currencyBuffer[3];

    mCost = tempBuffer;
}

【问题讨论】:

  • 呃...这里不确定,但为什么是 0?如果是150S,应该是1G50S吧?您需要查看除法和余数...这应该会有所帮助:daniweb.com/software-development/cpp/threads/9349/… 请注意最后一篇非常重要的帖子...
  • 这绝对是我的错。它应该是取余数的取模运算符(即,如果它是 105,则余数将是 5,一个作为进位)。

标签: c++ string parsing


【解决方案1】:

我认为问题出在这一行:

currencyBuffer[i - 1] = temp;

您将 int (temp) 分配给字符串 (currencyBuffer[i-1]),这会导致写入垃圾字符。显然,这是允许的: (Why does C++ allow an integer to be assigned to a string?) 因为 int 可以隐式转换为 chars,而 chars 可以分配给字符串。

您想使用 itoa 或类似函数将 temp 转换为 char(当您从字符串中获取 int 时,您已经正确地执行了相反的操作,atoi)。

由于您使用的是 C++,因此一个简单的方法是:

std::stringstream itos;
itos << temp;
currencyBuffer[i-1] = itos.c_str();

【讨论】:

  • 非常感谢。我使用了 itoa,现在它可以完美运行了。不幸的是,我不知道如何修复“temp2 = temp % 100;”线。如果用户输入 200,则应该有两个进位,但现在我只是将其设置为进行一个进位(即,如果数字大于 100)。
  • 是的,因为您有命令“返回并在货币缓冲区中添加一个”。 :) 你想把temp += 1; 改成temp += atoi(tokenPtr)/100;
  • % 得到余数。你应该有另一个变量,我们称之为quotient,它得到商:quotient = temp / 100;。将一个 int 除以另一个会得到商,因此除以例如205 x 100 会给你 2。然后你说temp += quotient;。在之前的评论中,我没有将东西放在一个名为quotient 的单独变量中,而是使用atoi 重新获得了数字,因为我太懒了。
  • 哇。非常感谢大家的帮助!!
【解决方案2】:

不确定这是否只有我在这里(我的 C++ 时代可以追溯到大约 13 年前),你的老师最适合回答你这个问题,但感觉就像你在做什么/你是怎么做的是处理器密集型的。从技术上讲,您最好将整个字符串拆分为一个字符串数组,然后使用它们来确定您的最终计数:

std::string str = "I.have.a.dog";
//replace all DOTS with SPACES for next use
for (int i = 0; i < str.length(); ++i) {
    if (str[i] == '.')
      str[i] = ' ';
}
std::istringstream stm(str) ;
string word ;
while( stm >> word ) // read white-space delimited tokens one by one 
{
   // put word into array
}

从那里,你有一个数组,带有正确的文本/单词,进入一个数组,你可以用它来做你的计算……不过只是一个想法……不要引用我的话;)

【讨论】:

    【解决方案3】:

    这是我创建的用于解析您的号码的函数。大于...的数字没有问题,如果您愿意,请使用它 =)

    unsigned long int str2cur(std::string cost)
    {
        unsigned long int money = 0;
        //given that there are always 4 segments
        int end;
        do
        {
            end = cost.find('.', 0);
            money = money * 100 + atoi(cost.substr(0, end).c_str());
            cost.erase(0, end + 1);         
        }
        while (end != std::string::npos);
        return money;
    }
    

    【讨论】:

    • 这个函数的输出是最低面额的总金额
    猜你喜欢
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2020-12-20
    相关资源
    最近更新 更多