#include
#include
#include
#define
NULL
0
int
compare(char
sum1,char
sum2)
{
if(sum1>=sum2)
return
1
else
return
0
}
int
breakal(int
sum)
{
if(sum>21)
return
0
else
return
1
}
char
change(int
card)
{
char
ch,n,m,x
switch(card)
{
case
1:
ch='A'
break
case
10:
{
n=1+int(rand()/(RAND_MAX+1.0))
if(n==1)
ch='K'
else
{
m=1+int(rand()/(RAND_MAX+1.0))
if(m==1)
ch='Q'
else
{
x=1+int(rand()/(RAND_MAX+1.0))
if(x==1)
ch='J'
}
}
break
}
}
//以上switch语句用于将数字转换成字符
return
ch
}
void
game()
{
int
i,k,n
char
host_ch,player_ch,host_card[15],player_card[15]
int
host_sum=0,player_sum=0
srand(time(NULL))
for(i=0i<2i++)
{
int
(10.0*rand()/(RAND_MAX+1.0))
player_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
player_ch=change(player_card[i])
if(player_card[i]>1
&&
player_card[i]<10)
//输出玩家抽到的牌的点数
printf("您抽到的第%d张牌是%d\n",i+1,player_card[i])
else
if(player_card[i]==10)
printf("您要到的第%d张牌是%c\n",i+1,player_ch)
else
printf("您要到的第%d张牌是A\n",i+1)
if(player_ch=='A')
//询问判断玩家的A的点值为1或11
{
printf("
您有一张A,您想要A作1还是11?\n")
printf("
1.
1\n"
)
printf("
2.
11\n")
if(scanf("%d",&n)
&&
n==1)
player_card[i]=1
else
player_card[i]=11
}
host_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
host_ch=change(host_card[i])
if(host_ch=='A')
//电脑自动判断庄家的A的点值为1或11
{
k=1+int(rand()/(RAND_MAX+1.0))
if(k==1)
host_card[i]=11
else
host_card[i]=1
}
player_sum+=player_card[i]
//分别对庄家和玩家的点数求和
host_sum+=host_card[i]
}
printf("您现在的总面值为%d\n",player_sum)//输出玩家的总面值
for(i=2i<5i++)
{
if(host_sum<18
&&
host_sum>0)//电脑自动判断庄家是否要牌
{
host_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
host_ch=change(host_card[i])
if(host_ch=='A')
//电脑自动判断庄家的A的点值为1或11
{
k=1+int(rand()/(RAND_MAX+1.0))
if(k==1)
host_card[i]=11
else
host_card[i]=1
}
host_sum+=host_card[i]
}
printf("您还要继续要牌吗?\n")
printf("
1.要\n")
printf("
2.不要\n")
scanf("%d",&k)
if(k==1)
{
{
player_card[i]=1+int
(10.0*rand()/(RAND_MAX+1.0))
player_ch=change(player_card[i])
if(player_ch=='A')
//询问判断玩家的A的点值为1或11
{
printf("您有一张A,您想要A作1还是11?\n")
printf("
1.
1\n"
)
printf("
2.
11\n")
if(scanf("%d",&n)
&&
n==1)
player_card[i]=1
else
player_card[i]=11
printf("您要到的第%d张牌是A\n",i+1)
}
else
printf("您要到的第%d张牌是%d\n",i+1,player_card[i])
player_sum+=player_card[i]
if(i==4)
printf("您手中已有5张牌,一决胜负的时刻到啦!\n")
if(breakal(host_sum)==0)
{
if(breakal(player_sum)==0)
{
printf("庄家爆牌了,您也爆牌了!庄家的牌面值是%d,您的牌面值是%d\n",host_sum,player_sum)
printf("\nYou
lose..\n")
break
}
}
else
if(breakal(player_sum)==0)
{
printf("您爆牌了!您的牌面值是%d\n
\nYou
lose..\n",player_sum)
break
}
}
if(breakal(host_sum)==0)
{
printf("庄家爆牌了!庄家的牌面值是%d\n",host_sum)
printf("\nCongratulations!
YOu
win!\n")
break
}
}
else
{
printf("庄家的牌面值是%d,您的牌面值是%d\n",host_sum,player_sum)
if(compare(host_sum,player_sum)==1)
printf("\nYou
lose..\n")
else
printf("\nCongratulations!
You
win!\n")
break
}
}
}
int
main()
{
kaishi:
int
n
printf("
欢迎进入21点游戏!\n")
printf("
************************************\n")
printf("
1.开始游戏\n")
printf("
2.退出游戏\n")
printf("
************************************\n")
if(scanf("%d",&n)&&n==1)
game()
else
if(n==2)
{
printf("
******************************\n")
printf("
游戏结束!祝您愉快!\n")
printf("\n\n")
printf("
再见!\n")
printf("
******************************\n")
return
0
}
goto
kaishi
}
#include <stdio.h>#include <string.h>
int z(char c) return(c==' '?0:c-'0')
void main() {
char a[81],b[81],s[81]
int n,flag
scanf("%s",s)
sprintf(a,"%80s",s)
scanf("%s",s)
sprintf(b,"%80s",s)
for(flag=0,n=79n>=0n--, flag/=10)
s[n]=(flag=z(a[n])+z(b[n])+flag)%10
for(s[80]=flag=n=0n<80n++)
if(flag || s[n]) {
s[n]+='0'
flag=1
} else s[n]=' '
printf("%s\n",s)
}
#include<stdio.h>#define MAXLINE 1000
int getline(char line[],int maxline)
void copy(char to[],char from[])
int main()
{
int len
int max
char line[MAXLINE]
char longest[MAXLINE]
max=0
while((len=getline(line,MAXLINE))>0)
{
if(len>max)
{
max=len
copy(longest,line)
}
}
if(max>0)
printf("%s",longest)
return 0
}
int getline(char s[],int lim)
{
int c,i
for(i=0i<lim-1&&(c=getchar())!=EOF&&c!='\n'++i)
s[i]=c
if(c=='\n')
{
s[i]=c
i++
}
s[i]='\0'
return i
}
void copy(char to[],char from[])
{
int i
i=0
while((to[i]=from[i])!='\0')
i++
}
程序基本没问题。。。。
最后按Ctrl+C结束即可。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)