55kai( 2022-05-24 18:04 采纳率: 100%
浏览 84
已结题

C语言报错运行不了,不知道怎么改

题目
世界杯小组赛分组题目描述:世界杯是每四年的一个热门话题,请用C语言实现世界杯的小组分组。接下来才是你应该关注的重点:参赛队:英格兰、法国、德国、意大利、西班牙、荷兰、葡萄牙、克罗地亚、土耳其、俄罗斯、瑞典、捷克、塞尔维亚、加纳、科特迪瓦、突尼斯、尼日利亚、喀麦隆、日本、韩国、澳大利亚、伊朗、美国、墨西哥、哥斯达黎加、洪都拉斯、巴西、阿根廷、巴拉圭、智利、乌拉圭、厄瓜多尔。世界杯的小组分组规则如下:有八只种子球队,分别是:乌拉圭队、西班牙队、德国队、阿根廷队、哥伦比亚队、比利时队、瑞士队以及东道主巴西队。这八只球队一定分别在A——H八个组中(八只球队不能碰面)。小组分为A——H一共八个小组。分组结果按行输出。每个小组四个球队。(参考循环赛)。

提示:1.可以用链表指针或者结构体数组;2.随机数;3.也许你能做到我没有想到的

目前代码

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

#define T League
typedef struct T *T;
#define TreeHeigh 5

char *Other[] = {"英格兰","意大利","葡萄牙","克罗地亚","土耳其","瑞典","捷克","塞尔维亚","加纳","科特迪瓦","突尼斯","尼日利亚","喀麦隆","日本","韩国","澳大利亚","伊朗","美国","墨西哥","哥斯达黎加","洪都拉斯","巴拉圭","智利","厄瓜多尔"};

char *Send[] = { "乌拉圭","西班牙","德国","阿根廷","荷兰","法国","巴西","俄罗斯"};
int F_Send[ 8 ];
int F_Other[ 24 ];

struct T{
int Score;
int Heigh;
T Left;
T Right;
char *Name;
};

void INIT( T *Root, int Heigh );

void Print( T Root );

int main( void )
{
T Root;
int heigh;

heigh = 0;

Root = NULL;
INIT( &Root, heigh );
Print( Root );


return 0;

}

void INIT( T *Root, int Heigh )
{
int f;
static int g = -1;
srand( ( unsigned )time( NULL ) );
if( NULL == *Root )
{
T new = ( T )malloc( sizeof( struct T ) );
if( NULL == new )
exit( EXIT_FAILURE );
new->Left = NULL;
new->Right = NULL;
new->Name = NULL;
new->Score = -1;
new->Heigh = Heigh;
*Root = new;
}

if( TreeHeigh == ( *Root )->Heigh )
{
    ++g;
    if( 0 == g % 4 )
    {
        while( 1 )
        {        
            f = rand( ) % 8;
            if( 0 == F_Send[ f ] )
            {
                ( *Root )->Name = Send[ f ];
                F_Send[ f ] = 1;
                break;
            }
        }
    }
    else
    {
        while( 1 )
        {
            f = rand() % 24;
            if( 0 == F_Other[ f ] )
            {
                ( *Root )->Name = Other[ f ];
                F_Other[ f ] = 1;
                break;
            }
        }
    }

}

if( TreeHeigh == Heigh )
    return;

INIT( &(*Root)->Left, Heigh + 1 );
INIT( &(*Root)->Right, Heigh + 1 );

}

void Print( T Root )
{
static int g = 0;
static char ch = 'A';

if( NULL == Root )
    return;
if( NULL != Root->Name )
{
    if( 0 == g % 4 )
        printf( "%c\n", ch++);
    printf( "%s ", Root->Name );
    ++g;
    if( 0 == g % 2 )
        printf( "\n" );
}
Print( Root->Left );
Print( Root->Right );

}

  • 写回答

4条回答 默认 最新

  • 张世争 2022-05-24 19:45
    关注
    • 我整理了一下代码,编译通过,运行正常
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define TreeHeigh       5
    
    typedef struct tree_s tree_t;
    struct tree_s {
        int Score;
        int Heigh;
        tree_t *Left;
        tree_t *Right;
        const char *Name;
    };
    
    const char *Other[] =
    {
        "英格兰","意大利","葡萄牙","克罗地亚","土耳其","瑞典","捷克","塞尔维亚","加纳",
        "科特迪瓦","突尼斯","尼日利亚","喀麦隆","日本","韩国","澳大利亚","伊朗","美国",
        "墨西哥","哥斯达黎加","洪都拉斯","巴拉圭","智利","厄瓜多尔" 
    };
    const char *Send[] = { "乌拉圭","西班牙","德国","阿根廷","荷兰","法国","巴西","俄罗斯" };
    int F_Send[8];
    int F_Other[24];
    
    void tree_init(tree_t **root, int heigh);
    void tree_print(tree_t *root);
    
    int main(void)
    {
        tree_t *root;
        int heigh;
    
        heigh = 0;
    
        root = NULL;
        tree_init(&root, heigh);
        tree_print(root);
        return 0;
    }
    
    void tree_init(tree_t **root, int Heigh)
    {
        int f;
        static int g = -1;
        srand((unsigned)time(NULL));
        if (NULL == *root)
        {
            tree_t *item = (tree_t *)malloc(sizeof(tree_t));
            if (NULL == item)
                exit(EXIT_FAILURE);
            item->Left = NULL;
            item->Right = NULL;
            item->Name = NULL;
            item->Score = -1;
            item->Heigh = Heigh;
            *root = item;
        }
    
        if (TreeHeigh == (*root)->Heigh)
        {
            ++g;
            if (0 == g % 4)
            {
                while (1)
                {
                    f = rand() % 8;
                    if (0 == F_Send[f])
                    {
                        (*root)->Name = Send[f];
                        F_Send[f] = 1;
                        break;
                    }
                }
            }
            else
            {
                while (1)
                {
                    f = rand() % 24;
                    if (0 == F_Other[f])
                    {
                        (*root)->Name = Other[f];
                        F_Other[f] = 1;
                        break;
                    }
                }
            }
    
        }
    
        if (TreeHeigh == Heigh)
            return;
    
        tree_init(&(*root)->Left, Heigh + 1);
        tree_init(&(*root)->Right, Heigh + 1);
    }
    void tree_print(tree_t *root)
    {
        static int g = 0;
        static char ch = 'A';
    
        if (NULL == root)
            return;
        if (NULL != root->Name)
        {
            if (0 == g % 4)
                printf("%c\n", ch++);
            printf("%s ", root->Name);
            ++g;
            if (0 == g % 2)
                printf("\n");
        }
        tree_print(root->Left);
        tree_print(root->Right);
    }
    
    • 运行效果(VS 2022)

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月27日
  • 已采纳回答 5月24日
  • 创建了问题 5月24日

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算