⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 另一个.txt

📁 猜数字的C语言编写出来的
💻 TXT
字号:
一.算法及步骤: 

(一):游戏规则: 

                输入四个不重复的数字,按“enter”确认,结果将输在幕上,其形式为“?A?B”。 

                四个数字中位置和数字都正确的为A,数字相同而位置不同为B,当4A时为猜对,游戏结束。当8次仍未猜对,游戏结束。 

结束时,系统自动给出是否继续的提示,按“Y”+“enter”将继续;按“N”+“enter”将结束游戏。 

(二):算法及实现过程: 

首先利用库函数给出一个四位均不相同且首位数字不为0的随机数赋值给z,并将其个位,十位,百位,千位拆开并分别赋值给a,b,c,d。 

  printf(" Please enter four numbers,then press 'enter' key "); 

        do 

        { 

               srand(time(0)); 

               z=rand()%10000; 

               a=z%10000; 

               b=z%1000; 

               c=z%100; 

               d=z%10; 

               a/=1000; 

               b/=100; 

               c/=10; 

        } 

        while (a==b||a==c||a==d||b==c||b==d||c==d||z<1000&&z>=10000||a==0);

  

而后游戏开始,游戏者通过键盘给出一个四位均不相同且首位数字不为0的数,赋值给Z,系统将其个位,十位,百位,千位拆开并分别赋值给A,B,C,D。之后比较a,b,c,d与A,B,C,D的相同关系,并用e记录位置和数字都正确的数,用f记录数字相同而位置不同的数。最后以“eAfB ”的形式输出猜测结果,以便于游戏者继续游戏。在此期间,用一个变量h记录游戏次数。 

猜测完毕后,无论结果如何,都将用提示是否继续,如果是, 用do-while语句循环整个程序;如果否,将跳出程序。 

  do 

        { 

               int e=0,f=0; 

               h--; 

               printf("\Please enter four numbers:"); 

               scanf("%d",&Z); 

               A=Z/1000; 

               B=(Z%1000)/100; 

               C=(Z%100)/10; 

               D=Z%10; 

if (a==A) e++; 

               if (b==B) e++; 

               if (c==C) e++; 

               if (d==D) e++; 

               if (a==B||a==C||a==D) f++; 

               if (b==A||b==C||b==D) f++; 

               if (c==A||c==B||c==D) f++; 

               if (d==A||d==B||d==C) f++; 

               printf("%dA%dB\n",e,f); 

               if (h!=0) 

               printf("You have %d changes\n",h); 

        } 

        while((a!=A||b!=B||c!=C||d!=D)&&(h!=0)); 

        if (h==0) 

      { 

               printf("Sorry,you are wrong!"); 

        printf("The key is%d\n  ",z); 

        } 

        else printf("You are cleaver,the answer is right!~ ~\n"); 

        getchar(); 

        printf("continue(Y/N)?"); 

        scanf("%c",&g); 

} 

while(g=='y'||g=='Y'); 

}












新闻:玩火者必自焚 | 举报此页(违法内容) | 51EA.COM培训搜索 | 虎翼即时通,沟通无极限(免费) | 51EA.COM兼职招聘 
back

四.源程序 

  

#include <stdio.h> 

#include <stdlib.h> 

#include <time.h> 

void main() 

{ 

      int a,b,c,d,A,B,C,D,z,Z,i,j; 

      char g; 

      clrscr(); 

      textbackground(1); 

  

      do 

      { 

  

            int h=1; 

        textcolor(6); 

        clrscr(); 

        gotoxy(2,2); 

                  putch(0xc9); 

                  for(i=0;i<76;i++) 

                  putch(0xcd); 

                  putch(0xbb); 

                  for(i=3;i<24;i++) 

                 { 

                 gotoxy(2,i); 

                 putch(0xba); 

                 gotoxy(79,i); 

                 putch(0xba); 

                  } 

                 gotoxy(2,24); 

                 putch(0xc8); 

                 for(i=0;i<76;i++) 

                 putch(0xcd); 

                 putch(0xbc); 

                 gotoxy(3,18); 

                 for(i=0;i<76;i++) 

                 putch(0xc4); 

                  window(1,1,80,25); 

      gotoxy(23,1); 

 printf("~~~~~~~ Welcome To Play! ~~~~~~");

gotoxy(15,2); 

        printf(" Please enter four numbers,then press 'enter' key "); 

           do 

           { 

                 srand(time(0)); 

                 z=rand()%10000; 

                 a=z%10000; 

                 b=z%1000; 

                 c=z%100; 

                 d=z%10; 

                 a/=1000; 

                 b/=100; 

                 c/=10; 

           } 

            while (a==b||a==c||a==d||b==c||b==d||c==d||z<1000&&z>=10000||a==0); 

           do 

           { 

                 int e=0,f=0; 

                 h--; 

                 gotoxy(20,13-h); 

                 scanf("%d",&Z); 

                            A=Z/1000; 

                 B=(Z%1000)/100; 

                 C=(Z%100)/10; 

                 D=Z%10; 

                     if((A==B||A==C||A==D||B==C||B==D||C==D)||(10000<Z||Z<1000)) 

                     { 

                     textcolor(4); 

                     gotoxy(60,13-h); 

                     cprintf("wrong"); 

                     h++; 

                     continue; 

                     } 

                 if (a==A) e++; 

                 if (b==B) e++; 

                 if (c==C) e++; 

                 if (d==D) e++; 

                 if (a==B||a==C||a==D) f++; 

           if (b==A||b==C||b==D) f++; 

                 if (c==A||c==B||c==D) f++; 

                 if (d==A||d==B||d==C) f++; 

                 gotoxy(35,13-h); 

                 printf("%dA%dB",e,f); 

                 if (h>=0) 

                        gotoxy(50,13-h); 

                     printf("%d",8-h); 

           } 

            while((a!=A||b!=B||c!=C||d!=D)&&(h!=0)); 

           if (h<=0) 

           { 

             gotoxy(27,20); 

            printf(" Sorry,the key is %d.",z); 

           } 

           else printf(" Congratulations!"); 

            getchar(); 

                gotoxy(30,22); 

            printf(" continue(Y/N)?"); 

            scanf("%c",&g); 

      } 

      while(g=='y'||g=='Y'); 

      clrscr(); 

} 













#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
	int a,b,c,d,A,B,C,D,z,Z,i,j;
	char g;
	clrscr();
	textbackground(1);

	do
	{

	      int h=8;
	  textcolor(2);
	  clrscr();
	  gotoxy(2,2);
		       putch(0xc9);
		       for(i=0;i<76;i++)
			 putch(0xcd);
			 putch(0xbb);
		       for(i=3;i<24;i++)
		      {
		      gotoxy(2,i);
			putch(0xba);
		      gotoxy(79,i);
			putch(0xba);
		      }
		      gotoxy(2,24);
			putch(0xc8);
		      for(i=0;i<76;i++)
			putch(0xcd);
			putch(0xbc);
		      gotoxy(3,18);
		      for(i=0;i<76;i++)
		      putch(0xc4);
		       window(1,1,80,25);
	gotoxy(23,1);
 printf("~~~~~~~ Welcome To Play! ~~~~~~~~");
           
gotoxy(15,2);
        printf(" Please enter four numbers,then press 'enter' key ");
		do
		{
			srand(time(0));
			z=rand()%10000;
			a=z%10000;
			b=z%1000;
			c=z%100;
			d=z%10;
			a/=1000;
			b/=100;
			c/=10;
		}
		while (a==b||a==c||a==d||b==c||b==d||c==d||z<1000&&z>=10000||a==0);
		do
		{
			int e=0,f=0;
			h--;
			gotoxy(20,13-h);
			scanf("%d",&Z);
			A=Z/1000;
			B=(Z%1000)/100;
			C=(Z%100)/10;
			D=Z%10;
			    if((A==B||A==C||A==D||B==C||B==D||C==D)||(10000<Z||Z<1000))
			    {
			    textcolor(4);
			    gotoxy(60,13-h);
			    cprintf("wrong");
			    h++;
			    continue;
			    }
			if (a==A) e++;
			if (b==B) e++;
			if (c==C) e++;
			if (d==D) e++;
			if (a==B||a==C||a==D) f++;

		if (b==A||b==C||b==D) f++;
			if (c==A||c==B||c==D) f++;
			if (d==A||d==B||d==C) f++;
			gotoxy(35,13-h);
			printf("%dA%dB",e,f);
			if (h>=0)
                        gotoxy(50,13-h);
			printf("%d",8-h);
		}
		while((a!=A||b!=B||c!=C||d!=D)&&(h!=0));
		if (h<=0)
		{
		  gotoxy(27,20);
		printf(" Sorry,the key is %d.",z);
		}
		else printf(" Congratulations!");
		getchar();
                gotoxy(30,22);
		printf(" continue(Y/N)?");
		scanf("%c",&g);
	}
	while(g=='y'||g=='Y');
	clrscr();
}












⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -