首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个C编的洗牌程序的有关问题,请高手来帮一下

2012-02-14 
一个C编的洗牌程序的问题,请高手来帮一下.我用C编一个洗牌程序,我的思路是这样的:牌是随机生成的,不是先就

一个C编的洗牌程序的问题,请高手来帮一下.
我用C编一个洗牌程序,我的思路是这样的:牌是随机生成的,不是先就定义好所有的牌,用5个链表来放牌,前面四个分别放四种花色的牌,以确定那些牌有了,那些没有.每生成一个牌就放到这四个中对应的一个并比较大小,如果没有和刚生成的牌相等的(既是一张新牌)就按生成顺序放到第5个链表中,这样生成的第5个中的牌就是随机的.输出的时候就输出第5个就行了.
程序基本上写好了,不过有个错误,总是找不出原因,请大家帮一下我.程序如下:


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

struct   reuslt       //用来返回结果  
{
    int   hua;
    int   num;              
}r1,t;

struct   pai
{
    int   hua;             //花色  
    int   num;             //牌面值  
    struct   pai   *next;            
};

int   Y;               //牌是否全部生成       等于0全生成  
struct   sc
{
    char   hua1;
    int   num1;
    struct   sc   *next1;              
}*print,*temp;

struct   reuslt   creat_pai(int   Y)       //随机创建牌  
{
    while(Y)
    {
          r1.hua=rand()%6+3;             //产生3-6方便在数据类型转换时不要在变值.  
          r1.num=rand()%13+1;
          return   r1;                
    }                      
}

int   Insert_pai(struct   reuslt   r1)         //把牌插入相应位置  
{
    struct   pai   *p1,*q1,*p2,*q2,*p3,*q3,*p4,*q4,*p;
    struct   pai   *head_hei,*head_hong,*head_cao,*head_fang;
    int   hei,hong,cao,fang;               //记录每种花色的牌有多少个  
    struct   sc   *pt;
    Randomize();

    print=(struct   sc   *)malloc(sizeof(struct   sc));
    head_hei=(struct   pai   *)malloc(sizeof(struct   pai));
    head_hong=(struct   pai   *)malloc(sizeof(struct   pai));
    head_cao=(struct   pai   *)malloc(sizeof(struct   pai));
    head_fang=(struct   pai   *)malloc(sizeof(struct   pai));
   
//     q1=(struct   pai   *)malloc(sizeof(struct   pai));       //申请空间给它赋初值,方便后面比较.  
//     q2=(struct   pai   *)malloc(sizeof(struct   pai));
//     q3=(struct   pai   *)malloc(sizeof(struct   pai));
//     q4=(struct   pai   *)malloc(sizeof(struct   pai));
   
    head_hei-> next=NULL;
    head_hong-> next=NULL;
    head_cao-> next=NULL;
    head_fang-> next=NULL;
    print-> next1=NULL;
    pt=print;
   
    switch(r1.hua)
    {
        case   3:
                        if(head_hei==NULL)
                        {
                            p=(struct   pai   *)malloc(sizeof(struct   pai));


                            p-> hua=r1-> hua;     //这里有这个错误:invalid   type   argument   of   '-> '  
                            p-> num=r1-> num;     //这里有这个错误:invalid   type   argument   of   '-> '  
                            head_hei-> next=p;
                            head_hei=q1;
                            head_hei-> next=p1;
                            p-> next=NULL;
                        }
                        else
                        {
                            while(hei <14)
                            {
                                  if(p-> num==p1-> num)
                                      break;
                                  else
                                  {
                                      if(p-> num <p1-> num)           //第一次就找到插入位置        
                                      {
                                          p-> next=p1;         //插入  
                                          q1-> next=p;
                                          head_hei=q1;                 //还原指针的位置,以便下次比较  
                                          head_hei-> next=p1;
                                          hei=hei+1;       //记录加1
                                          break;
                                      }


                                      else
                                      {  
                                          if(p1-> next==NULL)
                                          {
                                              p1-> next=p;
                                              head_hei=q1;                 //还原指针的位置,以便下次比较  
                                              head_hei-> next=p1;
                                              hei=hei+1;       //记录加1
                                              break;
                                          }
                                          else
                                          {  
                                              q1=p1;
                                              p1=p1-> next;
                                          }
                                      }
                                  }
                            }  
                        //写到输出链表当中去.            
                        temp-> hua1=(char)p-> hua;            
                        temp-> num1=p-> num;                    
                        pt-> next1=temp;          


                        pt=temp;
                        pt-> next1=NULL;
                        }
                        break;
        //其它的三种情况暂时不写,等一种花色没错了在补上,应该不会有应响  
        case   4:break;
        case   5:break;
        case   6:break;
//然后在这个位置写输出链表  
                                 
    }        
}

int   main(int   argc,   char   *argv[])
{  

    int   i;
    for(i=0;i <=i+1;i++)
    {
        if(Y==0)
            break;
        t=creat_pai(Y);
        Y=Insert_pai(t);
    }

    system( "PAUSE ");
    return   0;
}


//这是那个随机函数.我看了有些的程序不写这个也能生成随机数,这是怎么回事??????  
Randomize()
{
        srand((unsigned)time(NULL));          
}


20分不知道少了不......

[解决办法]
int Insert_pai(struct reuslt r1) //把牌插入相应位置
{
............
p-> hua=r1-> hua; //这里有这个错误:invalid type argument of '-> '
p-> num=r1-> num; //这里有这个错误:invalid type argument of '-> '
............
}

你函数中r1是结构体变量,而非指针,怎么能用-〉呢。
修改一下:
int Insert_pai(struct reuslt r1) //把牌插入相应位置
{
............
p-> hua=r1.hua;
p-> num=r1.num;
............
}

热点排行