首页 > 学院 > 开发设计 > 正文

c程序设计教程7-16号习题,见笑了

2019-11-17 05:46:50
字体:
来源:转载
供稿:网友
#include "stdio.h"
#include "stdlib.h"
#include "time.h"int  Avg1=0;
int  Avg2=0;
void _Shuffle (int[][13]); /*洗牌*/  void    _Deal(const int [][13], /*发牌*/
    const char *[],
    const char *[],int [],int [],int,int);void _JustPair(int [],const int); /*判定'对子牌'*/void _JustSuit(int [],int ); /*判定 '花色' */
int  _PRintResult(int,const char *[], /*打印结果*/
     const char *[],const int *,int *,int);void _FirstHandDesk(const int *Desk,const char *Suit,
     const char * Face,int *FaceArray,
     int *SuitArray);void _SecondHandDesk(const int *Desk,const char *Suit,
     const char * Face,int *FaceArray,
     int *SuitArray);void _CompareResult(void); int main()
; /*花色指针数组*/
 char *Face[13]={"A","2","3","4","5",
                 "6","7","8","9","10",
                 "J","Q","K"};    /*面值指针数组*/ int  Desk[4][13]=; /*一副牌,行表示花色,
        列表示面值*/
 
 int  FaceArray[13]=; /*牌面值数组*/ int     SuitArray[4]=; /*牌花色数组*/ int  i,j,N=1;
 
 
 srand(time(NULL));   /*随机种子函数*/ 
 printf("The desk do not be shuffled.");   for (i=0;i<4;i++)
 {
  for (j=0;j<13;j++)
   {
   Desk[i][j]=N;
   N++;
   printf(" %2d",Desk[i][j]);
   }   printf(""); }
 
  _Shuffle(Desk); /*高效洗牌,存入Desk数组中*/ _Shuffle(Desk); printf("The desk had been shuffled."); for (i=0;i<4;i++)
 {
  for (j=0;j<13;j++)
   {
   
   printf(" %2d",Desk[i][j]);
   }   printf(""); }
 _FirstHandDesk(Desk,Suit,Face,FaceArray,SuitArray); /**/ _SecondHandDesk(Desk,Suit,Face,FaceArray,SuitArray);/* _Deal(Desk,Suit,Face,FaceArray,SuitArray); */ /*发牌,处理*/ 
/* _PrintResult(13,Face,Suit,FaceArray,SuitArray,4);   */  /*打印结果*/ 
  
 _CompareResult();
 
 return 0;
}
void _Shuffle(int _wDesk[][13])
{
 
 int  i,j;
 int  _Row,_Column;
 int  _Temp; /*让Desk数组中顺序初始化的发牌号,同随机产生的数组下标确定的发牌号互换,
 所以一共只需要互换52次即可洗牌,原来的算法可能是无限次*/ /*for (_Card=1;_Card <= 52;_Card++)
 {
  _Row = rand() % 4;  
  _Column = rand() % 13;      while (_wDesk[_Row][_Column] != 0)
   
  {
   _Row = rand() % 4;
   _Column = rand() % 13;     
  }  _wDesk[_Row][_Column] = _Card; */
 
 
 for (i=0;i<4;i++ )
  {
  for (j=0;j<13;j++)
   {
   _Row = rand() % 4;
   _Column = rand() % 13;
   
   _Temp = _wDesk[i][j];   _wDesk[i][j] = _wDesk[_Row][_Column];   _wDesk[_Row][_Column] = _Temp;   
   }
  }
}void _FirstHandDesk(const int *Desk,const char *Suit,
     const char * Face,int *FaceArray,
     int *SuitArray){
 int  _InitNum = 1; /*第一手牌的初始发牌顺序号,即1,3,5,...*/
 int  _Step=2;  /*求奇数的步长*/
 int  i;  for (i=0;i<13;i++)  /*初始化FaceArray,SuitArray数组,由于第一,二手牌都用*/
  FaceArray[i]=0;
 
   for (i=0;i<13;i++)
  SuitArray[i]=0;
 printf("The FirstHand Desk:"); _Deal(Desk,Suit,Face,FaceArray,SuitArray,_InitNum,_Step); /*在Desk中查找顺序号代表的牌,并且统计面值,花色*/ Avg1 = _PrintResult(13,Face,Suit,FaceArray,SuitArray,4);
 
 /*将第一手牌打印,并且返回积分平均值,用来和第二手牌比较输赢*/
}
void _SecondHandDesk(const int *Desk,const char *Suit,
     const char * Face,int *FaceArray,
     int *SuitArray){
 int  _InitNum = 2; /*第二手牌的初始发牌顺序号,即2,4,6,...*/
 int  _Step=2;  /*求偶数的步长*/
 int  i;  for (i=0;i<13;i++)
  FaceArray[i]=0;
 
  for (i=0;i<13;i++)
  SuitArray[i]=0;
 printf("The SecondHand Desk:"); _Deal(Desk,Suit,Face,FaceArray,SuitArray,_InitNum,_Step); Avg2 = _PrintResult(13,Face,Suit,FaceArray,SuitArray,4); /*将第二手牌打印,并且返回积分平均值,用来和第一手牌比较输赢*/
 
}
void _Deal(const int _wDeck[][13],const char * _wSuit[],
   const char * _wFace[],int _FaceArray[],
   int _SuitArray[],int _InitNum,int _Step)
{
 int  _Card=0, _Row,_Column;
  for (_Card = _InitNum;_Card <= 10;_Card += _Step)
 
 /*查找发牌序号对应的牌*/ {
 
  for (_Row = 0;_Row < 4 ;_Row++ )
  
   for (_Column = 0;_Column < 13 ;_Column++)
       if (_wDeck[_Row][_Column] == _Card)
    {
     _JustPair(_FaceArray,_Column); 
     /*判定发的牌数是否有对子,
     传入面值数组中*/     _JustSuit(_SuitArray,_Row);     /*判定发的牌是否为同色
     传入花色数组中*/     printf("%5s of %-8s%c",_wFace[_Column],_wSuit[_Row]
         ,(_Card % 2==(_InitNum % 2)) ? '' : ' ');     /*打印发的牌*/
    }  
 }  }
void _JustPair(int _FaceArray[],const int _Column)
void _JustSuit(int _SuitArray[],int _Row)
{
  _SuitArray[_Row]++;/*给传入的_Row(花色)记数*/
}int  _PrintResult(int _Size, const char *_Face[],
                   const char *_Suit[],const int *_FaceArray,
     int *_SuitArray,int _RowSize)
{
 int  i;
 int  Temp=0;
 
 int  Sum=0;  /*每手牌的积分变量*/
 printf("");  for (i=0;i<_Size ;i++)
 {
  /*根据_FaceArray[]判定相同面值的牌有几张*/     switch (_FaceArray[i])
    {
    case 1: printf(" %s ", _Face[i]);Sum += i;break;    /* 积分变量加上牌面值 */    case 2: printf(" 一对 %s ", _Face[i]);Sum += i * 2 + 10;break;   /*积分变量加上牌面值 * 2 表示两张, + 10 可以大于大面值单牌的积分*/    case 3: printf(" 三张 %s ", _Face[i]);Sum += i * 3 + 30;break;    /*积分变量加上牌面值 * 3 表示三张, + 30 可以大于大面值对子牌的积分*/    case 4: printf(" 四张 %s ", _Face[i]);Sum += i * 4 + 50;break;
   
    }
  
  if (i >= 0 && i <= 11)
    {      if (_FaceArray[i] == _FaceArray[i+1]  && _FaceArray[i] == 1)
        /*判定发的几张牌是不是顺子,
    前一张和后一张面值记数都相等且为1*/      Temp++;   /*类加器应该等于发的牌数*/      
     
    }
  
 } printf("");   for (i=0;i<_RowSize;i++)
 {
    if (_SuitArray[i] == 5)
   {
     printf("%s  同花",_Suit[i]);     Sum += 150;    /*假如是同花应该再加150分*/     break;     /*判定每一种花色的牌的记数个数,为5即为同花*/
   }   else  if (_SuitArray[i]>=1 _SuitArray[i]<=4)
  
   {
   printf("不是同花");
   break;
   }   else  if (Temp==4)
   {
    printf("顺子");    Sum += 100;   /*顺子应该再加100分*/
   
   }
 } return Sum / 5; /*返回积分的平均值*/
}void _CompareResult(void)
{
 
  if ((Avg1 / 2 ) > Avg2) /*假如第一手牌的平均积分/2 > 第二手牌的平均积分,可以断定第一手牌必胜*/
 {
  printf(
   "The Firsthand desk must win.FirstHand's average is %d SecondHand's average is %d",Avg1,Avg2);
 } else  if (Avg1 > Avg2)
 {
  printf(
   "The Firsthand desk would win.FirstHand's average is %d SecondHand's average is %d",Avg1,Avg2);
 }   if (Avg1 < (Avg2 / 2))
 {
  printf(
   "The Secondhand desk must win.FirstHand's average is %d SecondHand's average is %d",Avg1,Avg2);
 } else  if (Avg1 < Avg2 )
 {
   printf(
   "The Secondhand desk would win.FirstHand's average is %d SecondHand's average is %d",Avg1,Avg2);
 }} 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表