正好最近离散数学学到了这个,感觉验算起来有点复杂,matlab/wolfram又不会用),就写了个c语言的验算器,仅供参考,希望大佬们多多指正

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

int groupsize();
int *inputgroups(int size);
int count(int size, int *groups, int a, int b);
int checkgroups(int size, int *groups);
int checkmonoid(int size, int *groups);

int main()
{

    int size = groupsize();
    int *groups = inputgroups(size);
    if (checkgroups(size, groups))
    {
        printf("This is a semigroup.\n");
    }
    else
    {
        printf("This is not a semigroup.\n");
    }
    if (checkmonoid(size, groups))
    {
        printf("This is a monoid.\n");
    }
    else
    {
        printf("This is not a monoid.\n");
    }
    system("pause");
    return 0;
}

int groupsize()
{
    printf("Please enter the size of groups ,enter 0 to exit.\n");
    int size;
    scanf("%d", &size);
    return size;
}

int *inputgroups(int size)
{
    int *groups = (int *)malloc(sizeof(int) * size * size);
    printf("Please enter the groups, use 1,2,3... replace a,b,c...:\n");
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            scanf("%d", &groups[i * size + j]);
            groups[i * size + j]--;
        }
    }
    return groups;
}

int count(int size, int *groups, int a, int b)
{
    return groups[a * size + b];
}

int checkgroups(int size, int *groups)
{
    int flag = 1;
    for (int i = 0; i < size && flag; i++)
    {
        for (int j = 0; j < size && flag; j++)
        {
            for (int k = 0; k < size && flag; k++)
            {
                if (count(size, groups, count(size, groups, i, j), k) != count(size, groups, i, count(size, groups, j, k)))
                {
                    flag = 0;
                }
            }
        }
    }
    return flag;
}

int checkmonoid(int size, int *groups)
{
    for (int i = 0; i < size; i++)
    {
        int flag = 1;
        for (int j = 0; j < size && flag; j++)
        {
            if ((count(size, groups, i, j) != count(size, groups, j, i)) || (count(size, groups, i, j) != j))
            {
                flag = 0;
            }
        }
        if (flag == 1)
        {
            return 1;
        }
    }
    return 0;
}

0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注