关于扑克牌洗牌的程序---Ubuntu LinuxC++程序/g++编写但会导致Ubuntu12.04出现Core Dumps(小心使用)

转自http://blog.csdn.net/warrioralex/article/details/8547066

#include <cstdlib>
#include <iostream>

using namespace std;
struct Poker{
     char pt;//点数 
     char st;//花色 
    };
 char Point(int i){
     switch(i)
     {
      case 0: return 'K';
      case 1: return 'A';
      case 11: return 'J';
      case 10: return '!';
      case 12: return 'Q';
      default : return i+48; 
     }
     }
 char Style(int j)
 {
  switch(j)
  {
   case 0: return '@';//Spade黑桃
   case 1: return '#';//Heart红桃
   case 2: return '$';//Diamond方片
   case 3: return '&';//Ciub 梅花 
  } 
 }
void shuffle ( int a[], int n ) 
{
    int tmp = 0, p1, p2;
    int flag = rand()%54;
    while (flag--)
    {
           p1 = rand() % n;
           p2 = rand() % n;           
           tmp = a[p1];
           a[p1] = a[p2];
           a[p2] = tmp;
    }
}
int main(int argc, char *argv[])
{
 Poker  P[54];
 int temp[54],n;
 for(int i=0;i<54;i++)
 temp[i]=i+1;
 cout<<"=========说明:'!'代表10,黑桃@,红桃$,方片#,梅花&========="<<endl; 
 cout<<"请输入要洗牌的次数 N= ";
 cin>>n; 
 shuffle (temp,n); 
 for(int i=0;i<54;i++)
 {
  if(temp[i]==54)
  {
   P[i].pt='R',P[i].st=0;//大王的花色和点数
  } 
  else 
  if(temp[i]==53)
  {
   P[i].pt='r',P[i].st=0;//小王的花色和点数 
  }
  else
  {
   P[i].pt=Point(temp[i]%13);
   P[i].st=Style((temp[i]-1)/13); 
  }      
 }
 for (int j=0;j<54;j++)
 {
  cout<<P[j].st<<P[j].pt<<" "; 
 }
 system("pause");
 return 0;
}


你可能感兴趣的:(关于扑克牌洗牌的程序---Ubuntu LinuxC++程序/g++编写但会导致Ubuntu12.04出现Core Dumps(小心使用))