Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中n-集的笛卡尔积_C_Cartesian - Fatal编程技术网

C中n-集的笛卡尔积

C中n-集的笛卡尔积,c,cartesian,C,Cartesian,我正在编写一个C程序,它应该打印n个集合的笛卡尔积,其中每个集合的元素是一个文件中的文本行,n个文件的名称作为命令行参数传递。到目前为止,我设法把每一行都读入字符串矩阵。然而,我无法思考如何编写打印产品的算法 以下是我到目前为止的情况: #include <stdio.h> #include <string.h> #include <stdlib.h> #define LSIZ 128 #define RSIZ 10 int main(int argc, ch

我正在编写一个C程序,它应该打印n个集合的笛卡尔积,其中每个集合的元素是一个文件中的文本行,n个文件的名称作为命令行参数传递。到目前为止,我设法把每一行都读入字符串矩阵。然而,我无法思考如何编写打印产品的算法

以下是我到目前为止的情况:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LSIZ 128
#define RSIZ 10
int main(int argc, char *argv[])
{
    char input[LSIZ];
    int n = argc;
    char *sets[argc - 1][RSIZ];
    int i = 0;
    int j = 0;
    int y = 1;
    FILE *file = NULL;
    if (argc == 1)
    {
        printf("nofiles");
    }
    else
    {
        while (--argc > 0)
        {
            if ((file = fopen(argv[y], "r")) == NULL)
            {
                printf("cat: failed to open %s\n", *argv);
                return 1;
            }
            else
            {

                for (j = 0; j < RSIZ && fgets(input, sizeof(input), file); ++j)
                {
                    int lineLen = strlen(input) + 1;
                    sets[i][j] = strncpy(malloc(lineLen), input, lineLen);
                }

                fclose(file);
                j = 0;
            }
            i++;
            y++;
        }
    }
    return 0;
 }

您可以使用类似里程表的计数器实现笛卡尔乘积:

将所有当前项目设置为每个列表中的第一个项目。 处理当前状态,例如打印它。 推进第一个列表。如果到达末尾,请将状态重置为该列表的开头,并前进到下一个列表,依此类推。如果你到达最后一个列表的末尾,你就完成了。 因此,假设您已将文件中的信息读取到以NULL结尾的字符串列表中,您可以执行以下操作:

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

int main(int argc, char *argv[])
{
    const char *adj1[] = {"little", "big", "huge", NULL};
    const char *adj2[] = {"red", "yellow", "grey", "orange", NULL};
    const char *noun[] = {"house", "car", NULL};
    
    const char **list[3] = {adj1, adj2, noun};
    const char **curr[3];
    
    unsigned n = sizeof(list) / sizeof(*list);
    unsigned count = 0;
    bool done = false;
    
    for (unsigned i = 0; i < n; i++) {
        curr[i] = list[i];
    }
    
    while (!done) {
        unsigned i = 0;

        printf("%s %s %s\n", *curr[0], *curr[1], *curr[2]);
        count++;

        curr[i]++;

        while (*curr[i] == NULL) {
            curr[i] = list[i];      // back to beginning
            i++;                    // move to next list
            
            if (i == n) {           // stop when the last list is exhausted
                done = true;
                break;
            }

            curr[i]++;              // ... and increment that
        }      
    }
    
    printf("%u combinations.\n", count);

    return 0;
}

这种方法不限于三个列表。如果您确切知道有多少个列表,当然可以使用嵌套循环。

这是否回答了您的问题?不幸的是,我已经搜索过了,但没有找到任何与此问题有关的内容。请将您正在使用的输入文件的内容编辑到您的帖子示例中。您可以按照我的回答实现里程表。对不起,我很抱歉,但是对于如何为n个项目实现笛卡尔集的问题,提议的复制是如何一个好的答案?