c - 如何从数组中删除 0 并将值分配给 C 中的锯齿状数组

标签 c arrays jagged-arrays

我的代码最初用全零填充多维 int 数组 ([20][20])。然后它从文件中读取数字并将其放回该数组中。它成功地从文件中输入了数字。但是,有些 0 是我无法摆脱的。

我在网上查了锯齿状数组的解法,还是不行。


//fills the array
for( i = 0; i < 20 ; i++){
        for( j = 0; j < 20; j++){
                newString[i][j] = 0;
        }
}




j = 0;
ctr = 0;   //line number 
int m = 1,num = 1;
int spaceCounter = 0;
bool isMultiDigit = false;
while (fgets(line, sizeof(line), fp) != NULL){
        for(i=0;i<=(strlen(line));i++){
                if(line[i]==' '){
                        spaceCounter++;
                }
                else if(line[i]=='\n'){
                        j = 0;

                }
                else{
                        if(line[i] != '\0'){
                                if(line[i+1] == ' ' || line[i+1] == '\n' || line[i+1] == '\0'){
                                        if(isMultiDigit){
                                                newString[ctr][j] = num;
                                                num = 1;
                                                isMultiDigit = false;
                                        }
                                        else{
                                                printf("line[i] = %c. \n", line[i]);
                                                newString[ctr][j] = line[i] - '0';
                                                printf("newString[%d][%d] = %d\n", i,j, newString[ctr][j]);
                                        }

                                        j++;
                                }
                                else{
                                        if(!isMultiDigit) num = (line[i]-'0') * 10 + (line[i+1] - '0') ;
                                        else num = num * 10 + (line[i+1] - '0') ;
                                        isMultiDigit = true;
                                }
                        }
                }
                spaceCounter = 0;
        }

        ctr++;

}


for( i = 0; i < 20 ; i++){
        for( j = 0; j < 20; j++){
                printf("%d ", newString[i][j]);
        }
        printf("-------\n");
}

这是文件的内容

3
9 3 34 4 12 5 2
6 3 2 7 1
256 3 5 7 8 9 1

这是填充零然后从文件读取后的输出

3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
9 3 34 4 12 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
6 3 2 7 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
256 3 5 7 8 9 1 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -------

所以在这种情况下,新数组在去掉 0 之后将是

newArray[0] = {3} 
newArray[1] = {9, 3, 34, 4, 12, 5, 2} 
newArray[2] = {6, 3, 2, 7, 1} 
newArray[3] = {256, 3, 5, 7, 8, 9, 1} 

请提供代码片段来解决我的问题。 谢谢你

最佳答案

修复您的代码后,我得到了类似下面的内容。我希望我得到的东西几乎是正确的。尽管如此,您的代码有几点。要为所有数组索引分配零值,您不需要构建该“for”循环。你可以这样做:

int newString[20][20] = { 0 };

对于您的情况,您构建了一个 20 x 20 的两级 int 数组。C 的数组长度不灵活。因此,所有这些数组索引将保留,无论它们的值是否为零。因此,如果您使用该类型的数组,则无法删除这些零。在您的情况下,如果您想保留该样式,则必须在打印时验证值。我改为更改了打印 block 。

您将来可以考虑的其他选项是指向由 malloc、hashmap 和类似数据结构定位的数组的指针数组。

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

int main(void) {
    int newString[20][20] = { 0 };
    int j = 0;
    int m = 1,num = 1;
    int ctr = 0;   //line number
    int spaceCounter = 0;
    char line[999];
    bool isMultiDigit = false;
    FILE *fp;

    if ( (fp = fopen( "test.txt", "r" )) == NULL ){
        printf( "Could not open file" ) ;
        return 1;
    }

    while (fgets(line, sizeof(line), fp) != NULL){
        for( int i=0; i<=(strlen(line));i++ ){
            if(line[i]==' '){
                spaceCounter++;
            } else if(line[i]=='\n'){
                j = 0;
            } else{
                if(line[i] != '\0'){
                    if(line[i+1] == ' ' || line[i+1] == '\n' || line[i+1] == '\0'){
                        if(isMultiDigit){
                            newString[ctr][j] = num;
                            num = 1;
                            isMultiDigit = false;
                        } else{
                            printf("line[i] = %c. \n", line[i]);
                            newString[ctr][j] = line[i] - '0';
                             printf("newString[%d][%d] = %d\n", i,j, newString[ctr][j]);
                        }

                        j++;
                    } else {
                        if(!isMultiDigit) num = (line[i]-'0') * 10 + (line[i+1] - '0');
                        else num = num * 10 + (line[i+1] - '0') ;
                        isMultiDigit = true;
                    }
                }
            }
            spaceCounter = 0;
        }
        ctr++;
    }

    for( int i = 0; i < 20 ; i++){
        if ( newString[i][0] == 0 ) continue;
        for( int j = 0; j < 20; j++){
            if ( newString[i][j] != 0 ) printf("%d ", newString[i][j]);
        }
        printf("-------\n");
    }
}

我有一些空闲时间。因此,我包含了第二个代码,其中使用了动态长度 int 数组。该代码几乎与您的代码完全相同,只是数据类型结构有所不同。此外,当您将一个您确定是数字的 ASCII 代码的 char 转换为一个 int 值时,您可以执行“char ^ 48”而不是“char - '0'”。

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

/*
 * An example of a data structure
 * that can hold dynamic length
 * int arrays. 
 */
struct arrayInt {
    int *value;
    int length;
    int size;
};

/*
 * Locating and return an array int structure
 * with its size and its value array to the de
 * defined size.
 */
struct arrayInt newArrayInt( const int size ){
    struct arrayInt output;
    output.value = (int*) malloc(size * sizeof(int));
    output.length = 0;
    output.size = size;
    return output;
}

int main(void) {
    struct arrayInt newString[20];
    int j = 0;
    int m = 1,num = 1;
    int ctr = 0;   //line number
    int spaceCounter = 0;
    char line[999];
    bool isMultiDigit = false;
    FILE *fp;

    if ( (fp = fopen( "test.txt", "r" )) == NULL ){
        printf( "Could not open file" ) ;
        return 1;
    }

    // Locating dynamic length int arrays
    for ( int i = 0; i < 20; i++ ){
        newString[i] = newArrayInt(20);
    }

    while (fgets(line, sizeof(line), fp) != NULL){
        for( int i=0; i<=(strlen(line));i++ ){
            if(line[i]==' '){
                spaceCounter++;
            } else if(line[i]=='\n'){
                j = 0;
            } else{
                if(line[i] != '\0'){
                    if(line[i+1] == ' ' || line[i+1] == '\n' || line[i+1] == '\0'){
                        if(isMultiDigit){
                            newString[ctr].value[j] = num;
                            // Keep track of length
                            newString[ctr].length++;
                            num = 1;
                            isMultiDigit = false;
                        } else{
                            printf("line[i] = %c. \n", line[i]);
                            newString[ctr].value[j] = line[i] - '0';
                            // Keep track of length
                            newString[ctr].length++;
                             printf("newString[%d].value[%d] = %d\n", i,j, newString[ctr].value[j]);
                        }

                        j++;
                    } else {
                        if(!isMultiDigit) num = (line[i]-'0') * 10 + (line[i+1] - '0');
                        else num = num * 10 + (line[i+1] - '0') ;
                        isMultiDigit = true;
                    }
                }
            }
            spaceCounter = 0;
        }
        ctr++;
    }

    for( int i = 0; i < 20 ; i++){
        if ( newString[i].length == 0 ) continue;
        for( int j = 0; j < newString[i].length; j++){
            printf("%d ", newString[i].value[j]);
        }
        printf("-------\n");
    }

    // free memory
    // Probably unnecessary.
    for ( int i = 0; i < 20; i++ ){
        free(newString[i].value);
        newString[i].length = 0;
        newString[i].size = 0;
    }
}

关于c - 如何从数组中删除 0 并将值分配给 C 中的锯齿状数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58229605/

相关文章:

javascript - 如何在 JavaScript 中初始化锯齿状数组?

c - 获取错误 : lvalue required while trying to implement a Jagged Array in C

c - 在 C/Go 中从 Google Chrome 获取标签页信息

c - execv 到 .c prog 使用相同的管道

c# - 使用字符串数组作为对象参数

sql - 不同于排列的 PostgreSQL 组合

将 char 指针数组转换为小写 C

python - 将锯齿状数组转换为 Pandas 数据框

c - 我的 Strlen 语法写得不好?

c - 我知道以下代码的输出将是 1000 4 但为什么是 1000 4 而不是 4 1000?