|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 奥普瓯江 于 2017-9-22 11:08 编辑
题目是
为什么我写得程序执行不了,第一个问题是,洗牌时执行选项失败没有反馈,还有就是洗牌“ad”指针数组第1个是空字符我也不知道问什么,还麻烦大神给予解答谢谢
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 1024
#define NAX 14
char *sp[54] ={\
"方A","方2","方3","方4","方5","方6","方7","方8","方9","方10",\
"梅A","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10",\
"红A","红2","红3","红4","红5","红6","红7","红8","红9","红10",\
"黑A","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10",\
"方J","方Q","方K","梅J","梅Q","梅K","红J","红Q","红K","黑J",\
"黑Q","黑K","大王","小王"\
};
char *ssp[54]; //接收字符串
char *ad[NAX], *bd[NAX], *cd[NAX]; //每个人最可以拿多少张牌
char fapai();
char xipai();
char dayin();
char fapai() //发牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp[i] = sp[j];
}
}
char xipai() //洗牌函数
{
srand(time(NULL)); //初始化rand随机变量的值
for (int i = 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在1-54内
ssp[i] = sp[j];
}
}
char dayin() //打印函数程序
{
int d = 0, k = 0, g = 0;
for(int i = 0; i < 42; i++)
{
if (i >= 0 && i < 14)
{
d++;
ad[d]= ssp[i];
}
else if(i >= 14 && i < 28)
{
k++;
bd[k]= ssp[i];
}
else if(i >= 28 && i < 42)
{
g++;
cd[g]= ssp[i];
}
}
}
int main()
{
char *a[MAX], *b[MAX], *c[MAX];
printf("请输入1号玩家的名字:");
scanf("%s",&a);
printf("请输入2号玩家的名字:");
scanf("%s",&b);
printf("请输入3号玩家的名字:");
scanf("%s",&c);
printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
fapai();
dayin();
printf("%s手上的牌是: ", a);
for (int i = 0; i < NAX; i++)
{
printf("%s", ad[i]);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (int i = 0; i < NAX; i++)
{
printf("%s", bd[i]);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (int i = 0; i < NAX; i++)
{
printf("%s", cd[i]);
}
putchar('\n');
printf("重新洗牌(Y/N)? ");
char ch;
while(1)
{
scanf("%c", &ch);
getchar();
if ( ch == 'Y' )
{
xipai();
dayin();
printf("%s手上的牌是: ", a);
for (int i = 0; i < NAX; i++)
{
printf("%s", ad[i]);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (int i = 0; i < NAX; i++)
{
printf("%s", bd[i]);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (int i = 0; i < NAX; i++)
{
printf("%s", cd[i]);
}
putchar('\n');
printf("\n重新洗牌(Y/N)? ");
}
else if ( ch == 'N' )
{
break;
}
else
{
printf("\n您输入的字符有误\n");
printf("\n重新洗牌(Y/N)? ");
}
}
return 0;
}
本帖最后由 ba21 于 2017-9-22 13:15 编辑
1,d++ i++ g++;请放到后面;
2, Y/N 再次接收时
while(getchar()!='\n') //#清空键盘缓冲区数据
continue;
3,fapai() ; xipai(); 就合成一个洗牌就好了。
4,随机数有重复(我这里也没有解决)
参数代码: #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 1024
#define NAX 14
char *sp[54] ={\
"方A","方2","方3","方4","方5","方6","方7","方8","方9","方10",\
"梅A","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10",\
"红A","红2","红3","红4","红5","红6","红7","红8","红9","红10",\
"黑A","黑2","黑3","黑4","黑5","黑6","黑7","黑8","黑9","黑10",\
"方J","方Q","方K","梅J","梅Q","梅K","红J","红Q","红K","黑J",\
"黑Q","黑K","大王","小王"\
};
char *ssp[54]; //接收字符串
char *ad[NAX], *bd[NAX], *cd[NAX]; //每个人最可以拿多少张牌
void xipai();
void dayin();
void xipai() //洗牌函数
{
int i;
srand((unsigned)time(NULL)); //初始化rand随机变量的值
for (i= 0; i < 54; i++)
{
int j;
j = rand()%54; //随机限定rand函数在0-53内
ssp[i] = sp[j];
}
}
void dayin() //打印函数程序
{
int d = 0, k = 0, g = 0;
int i;
for(i = 0; i < 42; i++)
{
if (i >= 0 && i < 14)
{
ad[d]= ssp[i];
d++;
}
else if(i >= 14 && i < 28)
{
bd[k]= ssp[i];
k++;
}
else if(i >= 28 && i < 42)
{
cd[g]= ssp[i];
g++;
}
}
}
int main(void)
{
char *a[MAX], *b[MAX], *c[MAX];
int i;
char ch;
printf("请输入1号玩家的名字:");
scanf("%s",&a);
printf("请输入2号玩家的名字:");
scanf("%s",&b);
printf("请输入3号玩家的名字:");
scanf("%s",&c);
printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
xipai();
dayin();
printf("%s手上的牌是: ", a);
for (i = 0; i < NAX; i++)
{
printf("%s ", ad[i]);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (i = 0; i < NAX; i++)
{
printf("%s ", bd[i]);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (i = 0; i < NAX; i++)
{
printf("%s ", cd[i]);
}
putchar('\n');
printf("重新洗牌(Y/N)? ");
while(getchar()!='\n') //#清空键盘缓冲区数据
continue;
while(1)
{
scanf("%c", &ch);
while(getchar()!='\n') //#清空键盘缓冲区数据
continue;
if ( ch == 'Y' )
{
xipai();
dayin();
printf("%s手上的牌是: ", a);
for (i = 0; i < NAX; i++)
{
printf("%s ", ad[i]);
}
putchar('\n');
printf("%s手上的牌是: ", b);
for (i = 0; i < NAX; i++)
{
printf("%s ", bd[i]);
}
putchar('\n');
printf("%s手上的牌是: ", c);
for (i = 0; i < NAX; i++)
{
printf("%s ", cd[i]);
}
putchar('\n');
printf("\n重新洗牌(Y/N)? ");
}
else if ( ch == 'N' )
{
break;
}
else
{
printf("\n您输入的字符有误\n");
printf("\n重新洗牌(Y/N)? ");
}
}
return 0;
}
|
|