質問

Cには複数桁の整数があるとしましょう。私はそれを1桁の整数に分割したいと思います。

123 に変わります 1, 2, 、 と 3.

特に整数が何桁であるかわからない場合、どうすればこれを行うことができますか?

役に立ちましたか?

解決

int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}

他のヒント

まず、数字を数えます。

unsigned int count(unsigned int i) {
 unsigned int ret=1;
 while (i/=10) ret++;
 return ret;
}

次に、配列に保存できます。

unsigned int num=123; //for example
unsigned int dig=count(num);
char arr[dig];
while (dig--) {
 arr[dig]=num%10;
 num/=10;
}

ヒントとして、数字にn桁を取得するのは非常に簡単です。 10 n回、次にmod 10、またはcで割る:

int nthdig(int n, int k){
     while(n--)
         k/=10;
     return k%10;
}

123の最後の数字は123%です。123/10を実行することで123の最後の桁を落とすことができます。整数部門を使用して、これにより12を与えます。 - 上記のようにやってみると、いつ停止するかを知る方法がわかります。

以下のコードが役立つと思います。

temp = num;
while(temp)
{
    temp=temp/10;
    factor = factor*10;
}

printf("\n%d\n", factor);
printf("Each digits of given number are:\n");

while(factor>1)
{
    factor = factor/10;
    printf("%d\t",num/factor);
    i++;
    num = num % factor;
}

このプログラムを3つの引数を持つ関数として使用できます。「while(a ++ <2)」では、2は必要な数字(1つの引数として与えることができます)2を、必要な数字のない数字を置き換えます。ここでは、「z/= pow(10,6)」を使用できます。特定の数字を持続する必要がない場合は、必要ありません(別の引数として与えることができます)の数字を6つに置き換えます。3番目の引数はです。あなたが壊す必要がある数。

int main(){
long signed c=0,z,a=0,b=1,d=1;
scanf("%ld",&z);
while(a++<2){
       if(d++==1) 
       z/=pow(10,6);
       c+=(z%10)*b; 
       z/=10;
       b*=10;}
        return c;}
//Based on Tony's answer
#include <stdio.h> 
int nthdig(int n, int k){
    while(n--)
        k/=10;
    return k%10;
}

int main() {
    int numberToSplit = 987;
    printf("Hundreds = %i\n",nthdig(2, numberToSplit));
    printf("Tens     = %i\n",nthdig(1, numberToSplit));
    printf("Units    = %i\n",nthdig(0, numberToSplit));
}

これにより、次の印刷が行われます。

数百= 9

TENS = 8

単位= 7

@asaelrのコードに基づいてこれを作成しました:

typedef struct digitsArrayPlusNumber {
    uint32_t *baseAddress;
    uint32_t number;
} digitsArrayPlusNumber;

digitsArrayPlusNumber *splitDigits (uint32_t inValue) {
    // based on code from asaelr@stackoverflow.com

    uint32_t inputValue = inValue;

    //Count digits

    uint32_t theCount = 1;
    while (inputValue /= 10)
        theCount++;

    // put in array
    uint32_t *arr = malloc(sizeof(uint32_t) * theCount);
    uint32_t dig = theCount;
    while (dig--) {
        arr[dig]=inValue % 10;
        inValue /= 10;
        //  printf ("%d\n", arr[dig]);
    }

    digitsArrayPlusNumber *dandn = malloc (sizeof(digitsArrayPlusNumber));

    dandn->baseAddress = arr;
    dandn->number = theCount;

    return dandn;

}

int main(int argc, const char * argv[]) {


    for (int d = 0; d < splitDigits(12345678)->number; d++)
        printf ("%u\n", (splitDigits(12345678)->baseAddress)[d]);

}

それは非常にうまく機能します、ありがとう!

配列を使用せずに同じ順序で個別に分離する場合は、このコードを試してください。

//Separate number digits
#include <stdio.h>
#include <math.h>

void main()
{
    int x, y, n = 0;

    scanf("%d", &x);

    //counting digits
    y = x;
    while (y != 0)
    {
        n += 1;
        y /= 10;
    }

    //printing separated digits
    int i;
    for (i = ceil(pow(10, (n - 1))); i != 0; i /= 10)
        printf("%d  ", (x / i) % 10);
}

%10を使用できます。これは、分割後の数が残りを意味することを意味します。そう 123 % 10 残りは3であるため、3は123から3をサブトレクトし、120で次に120で12で除算し、同じプロセスを実行します。

分割して征服することはできますが、すべての算術ライブラリを書き直しています。マルチエシジョンライブラリを使用することをお勧めします https://gmplib.org しかし、もちろんそれは良い習慣です

int l1; //123456 for example
scanf("%d",&l1);
char s[sizeof(l1)];
sprintf(s,"%5d",l1);'

//This will give you separate digits of the number in char format inside s[0],s[1] 
//and so on.

//If you want them in int format, declare a int array say int i[sizeof(l1)] and add 
//the following code

for(int c=1;c<=sizeof(l1);c++){
i[c] = s[c] - '0';
} 

//Now i[0], i[1] etc will have the digits in int format
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top