是否可以在整数中存储前导零? [英] Is it possible to store a leading zero in an int?

查看:74
本文介绍了是否可以在整数中存储前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编程任务,需要加密用户输入的4位整数。我已经将int分为四个单独的值,并且加密和解密功能正常工作。我的问题是,当我将四个单独的int重新放在一起时,一些数字被加密为零(例如in:1234 out:0189),并且我想将输出存储到int以与其他函数一起使用。

I have a programming assignment where I need to encrypt a 4 digit int, input by user. I have split the int into four separate values and the encrypt and decrypt functions work. My problem is when I put the four separate ints back together, some numbers encrypt to zero (eg. in:1234 out:0189) and I want to store the output into an int for use with other functions.

现在,我有一个半熟的解决方案,如果第一个int为0,则首先打印0。

Right now I have a half-baked solution that prints 0 first if the first int is 0.

void joinInt(){
    if(int1 == 0) {cout << 0;}
    joined = int1 * 1000;
    joined += int2 * 100;
    joined += int3 * 10;
    joined += int4;
    cout << joined << endl;
    }

我的目标是返回联接(前导零)而不是仅仅打印

My goal is to return joined (with the leading zero) rather than just print it within the function.

推荐答案

一个int基本存储前导零。您遇到的问题是您没有打印在那里的前导零。

An int basically stores leading zeros. The problem that you are running into is that you are not printing the leading zeros that are there.

另一种不同的方法是创建一个将接受四个int的函数。

Another, different approach is to create a function that will accept the four int values along with a string and to then return a string with the numbers.

通过这种方法,您将获得一个辅助函数,具有很好的内聚力,没有副作用,可在您需要的地方重复使用需要做类似的事情。

With this approach you have a helper function with very good cohesion, no side effects, reusable where you need something similar to be done.

例如:

char *joinedIntString (char *pBuff, int int1, int int2, int int3, int int4)
{
    pBuff[0] = (int1 % 10) + '0';
    pBuff[1] = (int2 % 10) + '0';
    pBuff[2] = (int3 % 10) + '0';
    pBuff[3] = (int4 % 10) + '0';
    pBuff[4] = 0;                    // end of string needed.
    return pBuff;
}

然后在需要打印值的地方,只需调用

Then in the place where you need to print the value you can just call the function with the arguments and the provided character buffer and then just print the character buffer.

使用这种方法,如果您有一些不合理的数字,而这些数字最终以一个前导零开头,则使用该方法。 ,您将得到所有的零。

With this approach if you have some unreasonable numbers that end up have more than one leading zero, you will get all of the zeros.

或者您可能想拥有一个将四个int组合为一个int的函数,然后另一个函数将组合的打印出来

Or you may want to have a function that combines the four ints into a single int and then another function that will print the combined int with leading zeros.

int createJoinedInt (int int1, int int2, int int3, int int4)
{
    return (int1 % 10) * 1000 + (int2 % 10) * 100 + (int 3 % 10) * 10 + (int4 % 10);
}

char *joinedIntString (char *pBuff, int joinedInt)
{
    pBuff[0] = ((joinedInt / 1000) % 10) + '0';
    pBuff[1] = ((joinedInt / 100) % 10) + '0';
    pBuff[2] = ((joinedInt / 10) % 10) + '0';
    pBuff[3] = (joinedInt % 10) + '0';
    pBuff[4] = 0;                    // end of string needed.
    return pBuff;
}

这篇关于是否可以在整数中存储前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆