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

📄 基于c语言的猜数字游戏源码.c.txt

📁 机遇C语言的猜字游戏源代码
💻 TXT
字号:
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
main() 
{ char c; clock_t start,end;
  time_t a,b; double var;
  int i,guess; srand(time(NULL));
  printf("do you want to play it.('y' or 'n') \n");
  loop: while((c=getchar())=='y') 
  { i=rand()%100; printf("\npleaseinput number you guess:\n"); 
    start=clock(); 
    a=time(NULL);
    scanf("%d",&guess); while(guess!=i) 
    { if(guess>i) 
      { printf("please input a little smaller.\n"); 
        scanf("%d",&guess); }
      else 
      { printf("please input a little bigger.\n"); 
        scanf("%d",&guess); 
      }
    }
  end=clock(); 
  b=time(NULL); 
  printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
  printf("\1: it took you %6.3f seconds\n\n",difftime(b,a)); 
  if(var<15) printf("\1\1 You are very clever! \1\1\n\n"); 
  else if(var<25) printf("\1\1 you are normal! \1\1\n\n"); 
  else printf("\1\1 you are stupid! \1\1\n\n");
  printf("\1\1 Congradulations \1\1\n\n");
  printf("The number you guess is %d",i); 
 }
 printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
 if((c=getch())=='y') goto loop; 
} 


#include<iostream.h> 
#include<stdio.h> 
void initial(int *area);
 int issame(int a, int b, int c, int d);
 int getandprt(int *area, int count); 
void change(int *area, int t, int x, int y); 
void main() 
{ int i, area[10000], temp;
 int x, y; int count; char ctrl;
 restart:count=1; 
for(i=0; i<10000; i++)
 area[i]=i; initial(area);
 if((temp=getandprt(area, count))==0) goto restart; 
count++; 
cout<<"A="; cin>>x; 
if(x==4) goto start; 
cout<<"B="; cin>>y; 
cout<<"\n"; 
if(((x+y)>4)||((x==3)&&(y==1))||(x<0)||(y<0)||(x>4)||(y>4))
{ cout<<"Input error!\nNow replay.\n"; goto restart; } while(x!=4){ change(area, temp, x, y); if((temp=getandprt(area, count))==0) goto restart; count++; cout<<"A="; cin>>x; if(x==4) goto start; cout<<"B="; cin>>y; cout<<"\n"; if(((x+y)>4)||((x==3)&&(y==1))||(x<0)||(y<0)||(x>4)||(y>4)){ cout<<"Input error!\nNow replay.\n"; goto restart; } } start: cout<<"Yes!! I'm very clever.\nPress '1' to replay or else to exit.\n"; cin>>ctrl; if(ctrl=='1') goto restart; } void initial(int *area) { int i, temp[4]; for(i=0; i<10000; i++){ temp[3]=area[i]%10; temp[2]=(area[i]/10)%10; temp[1]=(area[i]/100)%10; temp[0]=(area[i]/1000)%10; if(issame(temp[0], temp[1], temp[2], temp[3])) area[i]=0; } } int issame(int a, int b, int c, int d) { if((a==b)||(a==c)||(a==d)||(b==c)||(b==d)||(c==d)) return 1; else return 0; } int getandprt(int *area, int count) { int i=0; for(i=0; i<10000; i++) { if(area[i]==0) continue; else break; } if(i==10000){ cout<<"You inputs have error!\nNow replay.\n"; return 0; } cout<<"this is the "<<count<<" time:"<<endl; if(area[i]<1000) cout<<'0'<<area[i]<<endl; else cout<<area[i]<<endl; return area[i]; } void change(int *area, int t, int x, int y)//area为数组,t为已输出的那个做根据的数,x为A的值,y为B的值 { int temp[5], base[5]; int flagA, flagB; int i, j, k; base[4]=t%10; base[3]=(t/10)%10; base[2]=(t/100)%10; base[1]=(t/1000)%10; for(i=0; i<10000; i++){ if(area[i]==0) continue; temp[4]=area[i]%10; temp[3]=(area[i]/10)%10; temp[2]=(area[i]/100)%10; temp[1]=(area[i]/1000)%10; flagA=0; flagB=0; for(j=1; j<=4; j++){ if(temp[j]==base[j]) flagA++; } for(j=1; j<=4; j++){ for(k=1; (k<=4); k++){ if(k==j) continue; if(base[j]==temp[k]) flagB++; } } if((flagA==x)&&(flagB==y)) continue; else area[i]=0; } }

⌨️ 快捷键说明

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