如何在c中的数组中存储像002567这样的数字? [英] how to store digit like 002567 in an array in c?

查看:62
本文介绍了如何在c中的数组中存储像002567这样的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究c中的一个项目,我需要在数组中存储数字。

我在存储以0开头的数字时遇到问题。我有数字像00123 04567 000012等我希望使用代码块将这些数字存储在c..im中的数组中。但是以零开头的数字被输出中的其他数字替换.i将数组声明为整数...我使用任何其他数据类型?

I'm working on a project in c where I need to store digits in an array.
I'm facing a problem in storing a digit which begins with a 0. i have digits like 00123 04567 000012 etc i want to store these digits in an array in c..im using code blocks..but the digits that begin with zero are replaced by some other digits in the output..i declared the array as integer..can i use any other data type?

推荐答案

如果我错了,请纠正我,但这听起来像是在尝试将数字存储在数组中,每个元素代表一个不同的数字。



例如,01234看起来像



Correct me if I'm wrong, but it sounds like your trying to store a number in an array, with each element representing a different digit.

So for instance, 01234 would look something like

int DigitArr[] = 
{ 
    0, 1, 2, 3, 4
};





Her这是我的问题,你如何为数组分配数字?我想这可能是你遇到问题的地方。



另外,当你打印数字时,请确保将它们打印为ASCII字符。尝试将它们转换为字符,即



Here's my question, how are you assigning the digits to the array? I think this might be where you problem is.

Also, when you print the digits out, make sure you're printing them as ASCII characters. Try either casting them to chars, i.e.

(char)DigitArr[0];





希望这会有所帮助。

Jacob



Hope this helps a little.
Jacob


那是( 00123 04567 000012 )而不是数字。在计算意义上前面的数字不能有 0 s(尝试插入 001 进入一个计算器)。



这是一个数字序列(我猜想是固定的长度)。您不能将其视为数字,但可以将其视为数字数组。我想你想存储多个这些序列。所以这样做:



That is (00123 04567 000012) not a number. In computational sense a number cannot have 0s in the front (Try inserting 001 into a calculator).

So that is a sequence of digits (In a fixed length I suppose). You cannot treat it as a number but you can treat it as an array of digits. I suppose you want to store a multiple number of these sequences. So do it this way :

const size_t length = 16; //number of digits in a one sequence

int* numbersArray[10]; // an array of ten integer pointers
size_t numUsed = 0; //number of uesd elements in the numbersArray

//a few sequences of 16 digits
int one[length] = {0,0,0,0,2,3,4,5,6,7,8,9,0,1,2,4};
int two[length] = {0,0,5,6,7,8,3,2,1,6,3,4,5,3,2,7};
int three[length] = {0,2,3,5,3,5,6,7,3,2,6,3,5,3,5,3};

numbersArray[0] = one;
numbersArray[1] = two;
numbersArray[2] = three;

numUsed = 3;

//you can iterate through numbersArray like this:
int* current;
for(int i=0; i < numUsed; i++)
{
   current = numbersArray[i];

   for(int j=0; j < length; j++)
   {
      std::cout << current[j];
   }

   std::cout << endl;
}







如果您的数字没有固定,那会有点复杂长度。无论如何,我希望你得到你想要的东西!




It gets somewhat complicated if your "numbers" are not in a fixed length. Anyway, I hope you got what you wanted !


这篇关于如何在c中的数组中存储像002567这样的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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