-----------------本来设计了一个洗牌程序,后来又想用字符串的知识修改成可以在洗牌后发牌的程序。52张牌,洗完后,每人分到13张,并且显示。我企图用strcpy()来完成,可是却遇了错误,请大家指点一下。。谢谢------------------------
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;
enum Suit{clubs,diamonds,hearts,spades};
const int jack=11;
const int queen=12;
const int king=13;
const int ace=14;
class Card
{
private:
int number;
Suit suit;
public:
Card ()
{}
void set(int n,Suit s)
{
number=n;
suit=s;
}
void display();
};
void Card::display()
{
if(number>=2&&number<=10)
cout<<number;
else
switch(number)
{
case jack: cout<<"J";break;
case queen: cout<<"Q";break;
case king: cout<<"K";break;
case ace: cout<<"A";break;
}
switch(suit)
{
case clubs: cout<<static_cast<char>(5);break;
case diamonds: cout<<static_cast<char>(4);break;
case hearts: cout<<static_cast<char>(3);break;
case spades: cout<<static_cast<char>(6);break;
}
}
int main()
{
const MAX=13;
Card deck[52];
Card puck1[MAX];
Card puck2[MAX];
Card puck3[MAX];
Card puck4[MAX];
int j;
string s1;
for(j=0;j<52;j++)
{
Suit su=Suit(j/13);
int nu=(j%13)+2;
deck[j].set(nu,su);
}
cout<<"\nOrdered deck:\n";
for(j=0;j<52;j++)
deck[j].display();
cout<<"Now,swapping...";
srand(time(NULL));
for(j=0;j<52;j++)
{
int k=rand()%52;
Card temp=deck[k];
deck[k]=deck[j];
deck[j]=temp;
}
cout<<"\nShuffled deck:\n";
for(j=0;j<52;j++)
{
deck[j].display();
}
for(j=0;j<13;j++)
{
strcpy(puck1[j],deck[j]);
cout<<"the palyer1:"<<endl;
puck1[j].display();
}
for(j=13;j<26;j++)
{
strcpy(puck2[j],deck[j]);
cout<<"the palyer2:"<<endl;
puck2[j].display();
}
for(j=26;j<39;j++)
{
strcpy(puck3[j],deck[j]);
cout<<"the palyer3:"<<endl;
puck3[j].display();
}
for(j=39;j<53;j++)
{
strcpy(puck4[j],deck[j]);
cout<<"the palyer4:"<<endl;
puck4[j].display();
}
return 0;
}
[此贴子已经被作者于2006-3-9 19:20:41编辑过]