简体   繁体   English

如何在C中将整数拆分为单独的多位整数?

[英]How do I split an integer into separate multi digit integers in C?

I am attempting to learn C by myself. 我试图自己学习C语言。 I was trying to split an integer into multiple separate integers, eg 12345 into 12 and 34 and 5, but I'm unable to find a way to do it. 我试图将一个整数分成多个单独的整数,例如12345分为12和34和5,但我无法找到办法。

I already know some Java and programming basics. 我已经了解了一些Java和编程基础知识。 Would I need to use some kind of do-while or for loop to do this, or can I use an array of an integer? 我需要使用某种do-while或for循环来执行此操作,还是可以使用整数数组?

This dynamically accomplishes what you are looking for! 这动态地完成了你正在寻找的东西! You can set any type of split you want. 您可以设置所需的任何类型的拆分。 2-2-4 or 3-4-5 or anything. 2-2-4或3-4-5或任何东西。 (you can basically get a number string from the user and accomplish this task, and turn the temporary string into an integer if you would like later on) : (您基本上可以从用户获取一个数字字符串并完成此任务,如果您希望以后将临时字符串转换为整数):

#include <stdio.h>
#include <string.h>

int main() {
    int i; //Counter

    char getStr[100];
    int NumberToSplit1, NumberToSplit2, NumberToSplit3;

    printf("Enter a number: ");
    scanf("%s", getStr);    //get it as a string
    //or you can use scanf("%[^\n], getStr);

    printf("How many number splits you want: ");
    scanf("%d %d %d", &NumberToSplit1, &NumberToSplit2, &NumberToSplit3);

    printf("\n%d-%d-%d split: \n", NumberToSplit1, NumberToSplit2, NumberToSplit3);

    for (i = 0; i < NumberToSplit1; i++) {
        printf("%c", getStr[i]);
    }
    printf("\n");
    for (i = NumberToSplit1; i < (NumberToSplit1+NumberToSplit2); i++) {
        printf("%c", getStr[i]);
    }
    printf("\n");
    for (i = (NumberToSplit1+NumberToSplit2); i < (NumberToSplit1+NumberToSplit2+NumberToSplit3); i++) {
        printf("%c", getStr[i]);
    }

    //If you want to save it in an integer, you can use strcat function to save the temp 2 numbers in a string convert that to integer

    //or use http://stackoverflow.com/questions/7021725/converting-string-to-integer-c

    printf("\n");
    printf("\n");

}

Output: 输出:

Enter a number: 12345
How many number splits you want: 2 2 4

    2-2-4 split: 
    12
    34
    5

Program ended with exit code: 0

Otherwise, first, to convert the int to a string: 否则,首先,将int转换为字符串:

 #include <stdio.h>
 int n = 12345678;
 int len = snprintf(NULL, NULL, "%d", n);
 char *digits = malloc(len);
 sprintf(digits, "%d", n);

Then you could split the string up various ways, such as: 然后你可以用各种方式拆分字符串,例如:

 int a, b, c;
 sscanf(digits, "%2d%2d%4d", &a, &b, &c);

Or: 要么:

 char sa[2], sb[2], sc[4];
 char *cp = digits;
 sa[0] = *cp++;
 sa[1] = *cp++;
 sb[0] = *cp++;
 sb[1] = *cp++;
 sc[0] = *cp++;
 sc[1] = *cp++;
 sc[2] = *cp++;
 sc[3] = *cp++;

 printf("%2c %2c %4c\n", sa, sb, sc);

Or: 要么:

 // Create 3 buffers to hold null-terminated ('\0' terminated) strings
 char sa[3] = { 0 } , sb[3] = { 0 }, sc[4] = { 0 };

 char *cp = digits;
 sa[0] = *cp++;
 sa[1] = *cp++;
 sb[0] = *cp++;
 sb[1] = *cp++;
 sc[0] = *cp++;
 sc[1] = *cp++;
 sc[2] = *cp++;
 sc[3] = *cp++;

 printf("%s %s %s\n", sa, sb, sc);

Then free your memory: 然后释放你的记忆:

 free(digits);

Etc... Etc... Etc... 等等......等......等等......

Since you want to split an eight digit number in a 2-2-4 shape you can just use integer division and modulo. 由于您想要以2-2-4形状分割八位数字,您可以使用整数除法和模数。
Assuming you don't want the negative sign if any : 假设你不想要负号,如果有的话:

void split( int input, int output[3] )
{
  if ( input<0 )
    input = -input;
  output[0] = input % 10000;
  output[1] = ( input / 10000 ) % 100;
  output[2] = input / 1000000;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM