货币面值

Currency denomination

本文关键字:货币      更新时间:2023-10-16

尝试在游戏中使用适当的货币面额。货币以字符串的形式存储(即由于我的教授不能更改),并按白金,黄金,白银和铜的顺序排列。例如,如果我初始化我的货币为"0.1.23.15",这意味着我有0个白金,1个黄金,23个白银和15个铜。

但是,我需要能够转换为更高的面额。这是什么意思?例如,如果我有105个银币(即0.0.105.0),它应该显示为1金5银(即0.1.5.0)。

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

EDIT:编辑代码,只要你不输入超过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;
}

我认为问题出在这一行:

currencyBuffer[i - 1] = temp;

你将一个int (temp)赋值给一个string (currencyBuffer[i-1]),这会导致写入垃圾字符。显然,这是允许的:(为什么c++允许将整数赋值给字符串?)因为整型可以隐式转换为字符,而字符可以赋值给字符串,所以。

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

因为你使用的是c++,一个简单的方法是:

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

不确定这是否只是我的问题(我使用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
}

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

这是我创建的用于解析您的号码的函数。对于大于……的数字没有问题。如果你愿意的话就用它=)

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;
}