鱼C论坛

 找回密码
 立即注册
查看: 791|回复: 6

发牌程序问题

[复制链接]
发表于 2023-5-25 15:36:29 | 显示全部楼层 |阅读模式
30鱼币
问题:.h文件的第48行总是报  Segmentation fault   错误
代码清单:
.c文件如下:
#include "S1E31T0.h"
#define MAX 1024

int main()
{
    int i;
    char name1[MAX], name2[MAX], name3[MAX];
    int played_cards[54] = {0};//初始化已消耗牌牌组
    int* playing_cards1;
    int* playing_cards2;
    int* playing_cards3;

    printf("请输入第一个参与打牌的人的名字:");
    fgets(name1, MAX, stdin);
    printf("请输入第二个参与打牌的人的名字:");
    fgets(name2, MAX, stdin);
    printf("请输入第三个参与打牌的人的名字:");
    fgets(name3, MAX, stdin);

    //发牌
    playing_cards1 = deal_cards(played_cards, 0);
    for(i = 0; i < 18; i++)
    {
        played_cards[i] = playing_cards1[i];
    }

    playing_cards2 = deal_cards(played_cards, 1);
    for(i = 19; i < 37; i++)
    {
        played_cards[i] = playing_cards1[i];
    }

    playing_cards3 = deal_cards(played_cards, 2);
   
    //打印每个人手上的牌
    printf("%s手里的牌为:\n", name1);
    printf_cards(playing_cards1);
    printf("\n");

    printf("%s手里的牌为:\n", name2);
    printf_cards(playing_cards2);
    printf("\n");

    printf("%s手里的牌为:\n", name3);
    printf_cards(playing_cards3);
    printf("\n");

    return 0;
}


.h文件如下:
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"   
//#define playing_cards[54] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54}
//其中1-13表示黑桃,14-26表示红桃,27-39表示梅花,40-52表示方片,51表示小王(joker),52表示大王(JOKER)

int random_num(int num);//用于生成随机数的函数
int* deal_cards(int sum, int played_cards[54]);//用于发牌的函数
int judge(int num, int played_cards[13]);//判断num是否是之前已经随机出来的数
void printf_cards(int played_cards[18]);//用来输出每个人手牌
//int sum = 0;

int random_num(int num)
{
    int result;
    time_t t;//使用time函数返回标准计时点到当前时间的秒数用于作为初始化伪随机数的种子序列

    srand((unsigned int)time(&t));//使用当前时间值作为初始化伪随机数种子序列

    result = rand() % num + 1;//rand() % (n-m+1)+m --->   产生m到n之间的随机数
}

int judge(int num, int played_cards[18])
{
    int i;

    for(i = 0; i < 18; i++)
    {
        if(num == played_cards[i])
        {
            return 1;
        }
    }

    return 0;
}

int* deal_cards(int sum, int played_cards[54])
{
    int i, j,num, len;
    static int playing_cards[18];
    int played_card[54];

    len = 19;
    for(j = 0; j < 54; j++)
    {
        played_card[i] = played_cards[i];
    }

    for(i = 0 + len * sum; i < (18 + len * sum); i++)
    {
        while(judge(num, played_card) == 1)
        {
            num = random_num(54);
        }
        played_card[i] = num;
        playing_cards[i - len * sum] = num;
        sum++;
    }
   
    return playing_cards;
}

void printf_cards(int played_cards[18])
{
    int i;

    for(i = 0; i < 18; i++)
    {
        if(played_cards[i] > 0 && played_cards[i] < 14)
        {
            if(played_cards[i] == 11)
            {
                printf("黑桃J ");
            }
            else if(played_cards[i] == 12)
            {
                printf("黑桃Q ");
            }
            else if(played_cards[i] == 13)
            {
                printf("黑桃K ");
            }
            else
            {
                printf("黑桃%d ", played_cards[i]);
            }
        }
        else if(played_cards[i] > 13 && played_cards[i] < 27)
        {
            if(played_cards[i] - 13 == 11)
            {
                printf("红桃J ");
            }
            else if(played_cards[i] - 13 == 12)
            {
                printf("红桃Q ");
            }
            else if(played_cards[i] - 13 == 13)
            {
                printf("红桃K ");
            }
            else
            {
                printf("红桃%d ", played_cards[i]);
            }
        }
        else if(played_cards[i] > 26 && played_cards[i] < 40)
        {
            if(played_cards[i] - 26 == 11)
            {
                printf("梅花J ");
            }
            else if(played_cards[i] - 26 == 12)
            {
                printf("梅花Q ");
            }
            else if(played_cards[i] - 26 == 13)
            {
                printf("梅花K ");
            }
            else
            {
                printf("梅花%d ", played_cards[i]);
            }
        }
        else
        {
            if(played_cards[i] - 39 == 11)
            {
                printf("方片J ");
            }
            else if(played_cards[i] - 39 == 12)
            {
                printf("方片Q ");
            }
            else if(played_cards[i] - 39 == 13)
            {
                printf("方片K ");
            }
            else
            {
                printf("方片%d ", played_cards[i]);
            }
        }
    }
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-25 15:39:31 | 显示全部楼层
从你的代码来看,你在.h文件中声明了函数,并在.c文件中调用这些函数。然而,你并没有提供这些函数的实现,这可能导致程序在运行时出错。以下是我对你的代码中可能存在问题的地方的分析:

1.  random_num 函数在声明时返回值为 int 类型,但实现时没有明确的 return 语句。应该将 result 的值返回。

2. 在 deal_cards 函数中, played_card[i] = played_cards[i]; 这一行代码使用了 i 作为索引,但 i 在此时并未被初始化,这可能会引起未定义的行为,包括段错误。你可能想使用 j 作为索引。

3. 在 deal_cards 函数中, num 变量在 while 循环中被使用前并未被初始化,这可能导致随机出现的数字无法预测。

4. 在.c文件中,当你在第二次和第三次调用 deal_cards 函数时,你试图将 playing_cards2 和 playing_cards3 的值赋给 played_cards ,但你使用的索引可能出了问题。第二次你应该从索引18开始,而不是19,第三次你应该从索引36开始。

在修改这些问题后,你的代码可能会看起来像这样:

.h文件
  1. // 其他的代码保持不变

  2. int random_num(int num)
  3. {
  4.     int result;
  5.     time_t t;//使用time函数返回标准计时点到当前时间的秒数用于作为初始化伪随机数的种子序列

  6.     srand((unsigned int)time(&t));//使用当前时间值作为初始化伪随机数种子序列

  7.     result = rand() % num + 1;//rand() % (n-m+1)+m --->   产生m到n之间的随机数
  8.     return result;
  9. }

  10. int* deal_cards(int sum, int played_cards[54])
  11. {
  12.     int i, j,num = 0, len;
  13.     static int playing_cards[18];
  14.     int played_card[54];

  15.     len = 18;
  16.     for(j = 0; j < 54; j++)
  17.     {
  18.         played_card[j] = played_cards[j];
  19.     }

  20.     for(i = 0 + len * sum; i < (18 + len * sum); i++)
  21.     {
  22.         while(judge(num, played_card) == 1)
  23.         {
  24.             num = random_num(54);
  25.         }
  26.         played_card[i] = num;
  27.         playing_cards[i - len * sum] = num;
  28.         sum++;
  29.     }
  30.    
  31.     return playing_cards;
  32. }
复制代码


.c文件
  1. // 其他的代码保持不变

  2.     //发牌
  3.     playing_cards1 = deal_cards(played_cards, 0);
  4.     for(i = 0; i < 18; i++)
  5.     {
  6.         played_cards[i] = playing_cards1[i];
  7.     }

  8.     playing_cards2 = deal_cards(played_cards, 1);
  9.     for(i = 18; i < 36; i++)
  10.     {
  11.         played_cards[i] = playing_cards2[i - 18];
  12.     }

  13.     playing_cards3 = deal_cards

  14. (played_cards, 2);
  15.     for(i = 36; i < 54; i++)
  16.     {
  17.         played_cards[i] = playing_cards3[i - 36];
  18.     }
复制代码


请注意,这些修改只解决了一部分问题,还有其他的问题可能会影响到程序的正确性。例如,在发牌的算法中,你可能会遇到同一张牌被发给多个玩家的情况。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-5-25 15:49:12 | 显示全部楼层
isdkz 发表于 2023-5-25 15:39
从你的代码来看,你在.h文件中声明了函数,并在.c文件中调用这些函数。然而,你并没有提供这些函数的实现, ...

按照您的意见修改了代码,但是依然在同样的位置报Segmentation fault错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-25 20:59:12 | 显示全部楼层
本帖最后由 sfqxx 于 2023-5-25 21:02 编辑

问题解答:

根据代码,可以看出第48行没有代码,应该是注释掉了。但是在不注释的情况下,可能会发生段错误,因为 `playing_cards1`数组、`playing_cards2`数组和`playing_cards3`数组都是通过`deal_cards`函数返回的指针,而这个指针指向的内存空间是在函数中动态分配的。但是,在函数中并没有释放这些动态分配的内存空间,所以可能会导致内存泄漏和段错误等问题。建议在使用完这些指针之后,再通过`free`函数释放掉这些内存空间,以避免内存泄漏和其他问题的发生。

另外,`random_num`函数没有声明返回类型,应该将其返回类型设为`int`类型。另外,在`deal_cards`函数中,定义了一个`played_card`数组,但是在赋值的时候,使用了不存在的变量`i`作为下标,应该改为使用`j`作为下标,即将`played_card[ i] = played_cards[ i];`改为`played_card[j] = played_cards[j];`。

代码清单如下:
  1. #include "S1E31T0.h"
  2. #include <stdlib.h> // 添加头文件
  3. #define MAX 1024

  4. int main()
  5. {
  6.     int i;
  7.     char name1[MAX], name2[MAX], name3[MAX];
  8.     int played_cards[54] = {0};
  9.     int* playing_cards1;
  10.     int* playing_cards2;
  11.     int* playing_cards3;

  12.     printf("请输入第一个参与打牌的人的名字:");
  13.     fgets(name1, MAX, stdin);
  14.     printf("请输入第二个参与打牌的人的名字:");
  15.     fgets(name2, MAX, stdin);
  16.     printf("请输入第三个参与打牌的人的名字:");
  17.     fgets(name3, MAX, stdin);

  18.     playing_cards1 = deal_cards(played_cards, 0);
  19.     for(i = 0; i < 18; i++)
  20.     {
  21.         played_cards[i] = playing_cards1[i];
  22.     }

  23.     playing_cards2 = deal_cards(played_cards, 1);
  24.     for(i = 19; i < 37; i++)
  25.     {
  26.         played_cards[i] = playing_cards2[i - 19]; // 修改下标
  27.     }

  28.     playing_cards3 = deal_cards(played_cards, 2);

  29.     printf("%s手里的牌为:\n", name1);
  30.     printf_cards(playing_cards1);
  31.     printf("\n");

  32.     printf("%s手里的牌为:\n", name2);
  33.     printf_cards(playing_cards2);
  34.     printf("\n");

  35.     printf("%s手里的牌为:\n", name3);
  36.     printf_cards(playing_cards3);
  37.     printf("\n");

  38.     free(playing_cards1); // 释放动态分配的内存空间
  39.     free(playing_cards2);
  40.     free(playing_cards3);

  41.     return 0;
  42. }

  43. int random_num(int num)
  44. {
  45.     int result;
  46.     time_t t;

  47.     srand((unsigned int)time(&t));

  48.     result = rand() % num + 1;
  49.     return result; // 添加返回类型
  50. }

  51. int judge(int num, int played_cards[18])
  52. {
  53.     int i;

  54.     for(i = 0; i < 18; i++)
  55.     {
  56.         if(num == played_cards[i])
  57.         {
  58.             return 1;
  59.         }
  60.     }

  61.     return 0;
  62. }

  63. int* deal_cards(int sum, int played_cards[54])
  64. {
  65.     int i, j,num, len;
  66.     int* playing_cards = (int*)malloc(sizeof(int) * 18); // 动态分配内存空间
  67.     int played_card[54];

  68.     len = 19;
  69.     for(j = 0; j < 54; j++)
  70.     {
  71.         played_card[j] = played_cards[j]; // 修改下标
  72.     }

  73.     for(i = 0 + len * sum; i < (18 + len * sum); i++)
  74.     {
  75.         while(judge(num, played_card) == 1)
  76.         {
  77.             num = random_num(54);
  78.         }
  79.         played_card[i] = num;
  80.         playing_cards[i - len * sum] = num;
  81.         sum++;
  82.     }

  83.     return playing_cards;
  84. }

  85. void printf_cards(int played_cards[18])
  86. {
  87.     int i;

  88.     for(i = 0; i < 18; i++)
  89.     {
  90.         if(played_cards[i] > 0 && played_cards[i] < 14)
  91.         {
  92.             if(played_cards[i] == 11)
  93.             {
  94.                 printf("黑桃J ");
  95.             }
  96.             else if(played_cards[i] == 12)
  97.             {
  98.                 printf("黑桃Q ");
  99.             }
  100.             else if(played_cards[i] == 13)
  101.             {
  102.                 printf("黑桃K ");
  103.             }
  104.             else
  105.             {
  106.                 printf("黑桃%d ", played_cards[i]);
  107.             }
  108.         }
  109.         else if(played_cards[i] > 13 && played_cards[i] < 27)
  110.         {
  111.             if(played_cards[i] - 13 == 11)
  112.             {
  113.                 printf("红桃J ");
  114.             }
  115.             else if(played_cards[i] - 13 == 12)
  116.             {
  117.                 printf("红桃Q ");
  118.             }
  119.             else if(played_cards[i] - 13 == 13)
  120.             {
  121.                 printf("红桃K ");
  122.             }
  123.             else
  124.             {
  125.                 printf("红桃%d ", played_cards[i]);
  126.             }
  127.         }
  128.         else if(played_cards[i] > 26 && played_cards[i] < 40)
  129.         {
  130.             if(played_cards[i] - 26 == 11)
  131.             {
  132.                 printf("梅花J ");
  133.             }
  134.             else if(played_cards[i] - 26 == 12)
  135.             {
  136.                 printf("梅花Q ");
  137.             }
  138.             else if(played_cards[i] - 26 == 13)
  139.             {
  140.                 printf("梅花K ");
  141.             }
  142.             else
  143.             {
  144.                 printf("梅花%d ", played_cards[i]);
  145.             }
  146.         }
  147.         else
  148.         {
  149.             if(played_cards[i] - 39 == 11)
  150.             {
  151.                 printf("方片J ");
  152.             }
  153.             else if(played_cards[i] - 39 == 12)
  154.             {
  155.                 printf("方片Q ");
  156.             }
  157.             else if(played_cards[i] - 39 == 13)
  158.             {
  159.                 printf("方片K ");
  160.             }
  161.             else
  162.             {
  163.                 printf("方片%d ", played_cards[i]);
  164.             }
  165.         }
  166.     }
  167. }
复制代码


有用请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-5-29 10:16:01 | 显示全部楼层
sfqxx 发表于 2023-5-25 20:59
问题解答:

根据代码,可以看出第48行没有代码,应该是注释掉了。但是在不注释的情况下,可能会发生段错 ...

谢谢您的回复,但是还是在您代码的第99行报了  Segentation fault 的错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-29 16:31:50 | 显示全部楼层
在函数deal_cards中,数组played_card在复制时使用了错误的变量名i,正确的应该是j:
```
for(j = 0; j < 54; j++)
{
    played_card[j] = played_cards[j];
}
```
另外,在函数random_num中,虽然生成了随机数,但是没有返回结果,应该加入返回语句:
```
int random_num(int num)
{
    int result;
    time_t t;

    srand((unsigned int)time(&t));

    result = rand() % num + 1;

    return result;  //加入返回语句
}
```
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-29 16:34:14 | 显示全部楼层
sfqxx 发表于 2023-5-25 20:59
问题解答:

根据代码,可以看出第48行没有代码,应该是注释掉了。但是在不注释的情况下,可能会发生段错 ...

使用动态分配内存空间来存储每个人手里的牌。其中,使用了随机数函数来模拟洗牌和发牌的过程,同时也使用了函数来进行多个操作。代码的错误主要集中在数组下标的处理上,特别是使用了循环变量来代替数组下标,导致程序无法正确处理数组内元素的顺序。

针对这些问题,本文主要对代码进行如下改进:

1. 添加头文件#include ;

2. 修正函数random_num的返回类型,应该是int而不是void;

3. 修改变量名played_cards的定义,应该是一个指向int的指针int* played_cards;

4. 修改deal_cards函数中的循环体,将循环变量i作为数组下标修改为i-len*sum,同时对played_cards数组的下标也进行调整;

5. 在函数deal_cards中增加变量的定义num,用于代替i作为洗牌过程中产生的随机数;

6. 在主函数中增加对动态分配内存空间的释放,使用free()函数进行释放。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 11:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表