哪位好心的大哥或大姐可以帮我用C语言编写一下这道题吗?谢谢啦~!
Contents:Thisassignmentgivesstudentstheopportunitytoworkwitharraysandsorting.Programs...
Contents: This assignment gives students the opportunity to work with arrays and sorting.Programs that simulate card games usually have to simulate the operation of shuffling the deck. Like sorting, shuffling is a process that involves rearranging the elements of an array. Algorithmically, the only difference between sorting and shuffling is how you select elements. When you sort an array using selection sort, you choose the smallest element in the rest of the array on each cycle of the loop. When you shuffle an array, you choose a random element. At last, you should sort the 13 cards in descending order by suit.
Steps: Write a function Shuffle that shuffles an array of strings. To test the Shuffle function, write a program that.
1. Declares an array with 52 elements, each of which are strings.
2. Fills the elements of that array with strings representing standard playing cards. Each card is represented by a string consisting of a rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) concatenated with a single letter representing a suit (C, D, H, S). Thus, the queen of spades is represented by the string "QS". The function IntegerToString described in Chapter 9 (page 302) will probably come in handy here.
3. Shuffles the array using the Shuffle function.
4. Deals a bridge hand by copying the first 13 cards from the deck to a separate array. 展开
Steps: Write a function Shuffle that shuffles an array of strings. To test the Shuffle function, write a program that.
1. Declares an array with 52 elements, each of which are strings.
2. Fills the elements of that array with strings representing standard playing cards. Each card is represented by a string consisting of a rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) concatenated with a single letter representing a suit (C, D, H, S). Thus, the queen of spades is represented by the string "QS". The function IntegerToString described in Chapter 9 (page 302) will probably come in handy here.
3. Shuffles the array using the Shuffle function.
4. Deals a bridge hand by copying the first 13 cards from the deck to a separate array. 展开
2个回答
展开全部
下面实现给你参考,显示前13张牌
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define MAXP 52
#define MAXT 14
#define MAXF 4
#define MAXD 13
typedef struct _p
{
int p1;
int p2;
}PT,*P;
void init(P p[MAXP])
{
int i;
for(i=0;i<MAXP;i++)
{
p[i]->p1 = i % MAXT;
p[i]->p2 = i % MAXF;
}
}
void shuffle(P p[MAXP])
{
int i,k;
PT t[MAXP];
time_t ti;
srand((unsigned) time(&ti));
for(i=0;i<MAXP;i++)t[i].p1=-1;
for(i=0;i<MAXP;i++)
{
k=(int)(((double) rand()/((double) RAND_MAX+1))*(52));
if(-1==t[k].p1)
{
t[k]=*(p[i]);
}
else
{
while(-1!=t[k].p1)
{
(k==(MAXP-1))?k=0:k++;
}
t[k]=*(p[i]);
}
}
for(i=0;i<MAXP;i++)*(p[i])=t[i];
}
void display(P p[MAXP])
{
char* A[14]={"A","1","2","3","4","5","6","7","8","9","10","J","Q","K"};
char* B[4]={"C","D","H","S"};
int k;
for(k=0;k<MAXD;k++)
{
printf("%s%s ",A[p[k]->p1],B[p[k]->p2]);
}
printf("\n");
}
int main(char* a,char** b)
{
int k;
P p[MAXP];
for(k=0;k<MAXP;k++)
{
p[k]=(P)malloc(8);
}
init(p);
shuffle(p);
display(p);
for(k=0;k<MAXP;k++)
{
free(p[k]);
}
return 0;
}
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define MAXP 52
#define MAXT 14
#define MAXF 4
#define MAXD 13
typedef struct _p
{
int p1;
int p2;
}PT,*P;
void init(P p[MAXP])
{
int i;
for(i=0;i<MAXP;i++)
{
p[i]->p1 = i % MAXT;
p[i]->p2 = i % MAXF;
}
}
void shuffle(P p[MAXP])
{
int i,k;
PT t[MAXP];
time_t ti;
srand((unsigned) time(&ti));
for(i=0;i<MAXP;i++)t[i].p1=-1;
for(i=0;i<MAXP;i++)
{
k=(int)(((double) rand()/((double) RAND_MAX+1))*(52));
if(-1==t[k].p1)
{
t[k]=*(p[i]);
}
else
{
while(-1!=t[k].p1)
{
(k==(MAXP-1))?k=0:k++;
}
t[k]=*(p[i]);
}
}
for(i=0;i<MAXP;i++)*(p[i])=t[i];
}
void display(P p[MAXP])
{
char* A[14]={"A","1","2","3","4","5","6","7","8","9","10","J","Q","K"};
char* B[4]={"C","D","H","S"};
int k;
for(k=0;k<MAXD;k++)
{
printf("%s%s ",A[p[k]->p1],B[p[k]->p2]);
}
printf("\n");
}
int main(char* a,char** b)
{
int k;
P p[MAXP];
for(k=0;k<MAXP;k++)
{
p[k]=(P)malloc(8);
}
init(p);
shuffle(p);
display(p);
for(k=0;k<MAXP;k++)
{
free(p[k]);
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询