Dream To Me

总有些什么留下来并被惦记

Guess Program Final


下载程序:猜数字V1.rar

高兴呢~写出来了,4个数字不带重复的,平均5次左右,200行左右



/* 
  Name: Guess Program V1 
  Copyright: None  
  Author: WZ 
  Date: 04-08-09 13:22 
  Description: None 
  Patch: 10-04-03 0:46 
*/
#include <stdio.h> 
#include <stdlib.h> 
#define REGSIZE (Guess *)malloc(sizeof(Guess)) 
#define MAXCOUNT 5040 
int the;//当前有多少种可能性 
int rom;//当前的种子  
/*数据结构*/
typedef struct ABC  
{ 
 char N[5];         //数字  
 int save;          //存活状况,初始化为1  
 struct ABC *next; 
 struct ABC *front; 
}Guess; 
/*计算链表中的剩余数据*/
void Count(Guess *head) 
{ 
     the=0; 
     while(head!=NULL) 
      { 
       the++; 
       head=head->next; 
      } 
} 
/*随机甩出来个数,找到相应的链表位置*/
Guess *Random(Guess *head) 
{ 
    if(the!=1) 
       { 
        srand((unsigned)time( NULL ) );  
        rom=rand()%the; 
        while(rom) 
              { 
               head=head->next; 
               rom--; 
              } 
       } 
    return head; 
} 
/*读入数据文件,构建双向链表*/
void Input(FILE *fp,Guess *head) 
{ 
     Guess *now,*next; 
     now=head; 
     int i;  
     fscanf(fp,"%s",now->N); 
     now->save=1; 
     for(i=0;i<MAXCOUNT-1;i++) 
        { 
          next=REGSIZE;//申请空间 
          next->next=NULL; 
          fscanf(fp,"%s",next->N); 
          next->save=1; 
          next->front=now; 
          now->next=next; 
          now=next;  
        } 
}  
/*对比链表中的所有数据*/
void Find(Guess *head,Guess *now,int a,int b) 
{ 
    /* 
    head头指针  
    now是现在要猜的那个数的位置 
    GUS是移动的  
    */
    Guess *GUS;  
    int A=0,B=0; 
    GUS=head; 
    while(GUS!=NULL)  
      { 
        //A对的  
        if(GUS->N[0]==now->N[0]) 
           A++; 
        if(GUS->N[1]==now->N[1]) 
           A++; 
        if(GUS->N[2]==now->N[2]) 
           A++; 
        if(GUS->N[3]==now->N[3]) 
           A++;    
        //第一个数  
        if(GUS->N[0]==now->N[1]) 
           B++; 
        if(GUS->N[0]==now->N[2]) 
           B++; 
        if(GUS->N[0]==now->N[3]) 
           B++; 
        //第二个数 
        if(GUS->N[1]==now->N[0]) 
           B++;  
        if(GUS->N[1]==now->N[2]) 
           B++; 
        if(GUS->N[1]==now->N[3]) 
           B++; 
        //第三个数 
        if(GUS->N[2]==now->N[0]) 
           B++;  
        if(GUS->N[2]==now->N[1]) 
           B++; 
        if(GUS->N[2]==now->N[3]) 
           B++;   
        //第四个数 
        if(GUS->N[3]==now->N[0]) 
           B++;   
        if(GUS->N[3]==now->N[1]) 
           B++;  
        if(GUS->N[3]==now->N[2]) 
           B++;  
        /*对比且没有0A0B的情况,干掉*/ 
        if((a!=A||b!=B)&&(a+b)) 
            GUS->save=0; 
        /*0A0B的特殊情况,干掉*/
        if(!(a+b)) 
           { 
            if(GUS->N[0]==now->N[0]||GUS->N[0]==now->N[1]||GUS->N[0]==now->N[2]||GUS->N[0]==now->N[3]) 
               GUS->save=0; 
            if(GUS->N[1]==now->N[0]||GUS->N[1]==now->N[1]||GUS->N[1]==now->N[2]||GUS->N[1]==now->N[3]) 
               GUS->save=0; 
            if(GUS->N[2]==now->N[0]||GUS->N[2]==now->N[1]||GUS->N[2]==now->N[2]||GUS->N[2]==now->N[3]) 
               GUS->save=0; 
            if(GUS->N[3]==now->N[0]||GUS->N[3]==now->N[1]||GUS->N[3]==now->N[2]||GUS->N[3]==now->N[3]) 
               GUS->save=0; 
           } 
        GUS=GUS->next; 
        A=B=0; 
      } 
}                 
/*删除当前结点*/
Guess *Delete(Guess *head) 
{ 
    Guess *left,*right,*now; 
    now=head; 
    //考虑空的情况 
    while(now!=NULL) 
        { 
         left=now->front; 
         right=now->next; 
         if(!now->save) 
            { 
             if(left!=NULL&&right!=NULL)//左右都不空  
               { 
                right->front=left; 
                left->next=right; 
               } 
             if(left==NULL&&right!=NULL)//左空右不空  
               { 
                head=right; 
                right->front=NULL; 
               } 
             if(right==NULL&&left!=NULL)//右空左不空  
                left->next=NULL;  
             free(now);//释放结点  
           } 
         now=right;//右移  
        }                  
    return head; 
}  
/*重复猜数*/
void Test(Guess *head) 
{ 
    int a=5,b=6; 
    char t; 
    Guess *now; 
    Count(head);//统计可能的情况  
    printf("猜数\t进度\t?A?B\n");  
    while(the!=1)//如果不止一种情况  
    { 
    now=Random(head);//获得随机后位置  
    printf("%s",now->N); 
    while(a+b>4||a<0||b<0||a==3&&b==1)//判断输入正误  
      { 
        printf("\t%4.2lf%%\t",(((double)1-(double)the/(double)5040))*(double)100); 
        scanf("%d%c%d%c",&a,&t,&b,&t); 
        if(a+b>4||a<0||b<0||a==3&&b==1) 
          printf("Wrong input\n?A?B");  
      } 
    Find(head,now,a,b);//对比数据  
    head=Delete(head);//获取删除结点后的头指针  
    Count(head);//统计可能的情况  
    a=5,b=6;//重计A和B  
    } 
    /*只剩一种情况的时候*/
    printf("\nBingo!\n%s\n",head->N); 
} 
/*读取文件成功*/
int quit(FILE *fp) 
{ 
    if(fp==NULL) 
       return 1; 
    return 0; 
} 
/*主函数*/
int main(int argc, char *argv[]) 
{ 
    FILE *fp=fopen("Guess.dll","r"); 
    Guess *head; 
    head=REGSIZE; 
    head->front=NULL; 
    if(quit(fp))//判断数据文件  
       { 
        printf("出错!请检查程序文件完整性\n");    
        system("PAUSE"); 
        return 0; 
       } 
    Input(fp,head);//读入链表  
    Test(head);//开始猜  
    printf("\n       WZ\n    2009.8.4\n\n");  
    system("PAUSE"); 
    return 0; 
}