忙了一个下午,终于把我以前编用VB写的逻辑推数作弊程序,改造成C++语言了,这次的改造,让我体会到了指针的作用,也让我明白了编程中的一个道理:差之毫厘,失之千里。刚开始的时候,由于clear()清空AB值的函数不小心放错了行,结果导致程序输出错误,为了找到这个错误,不知道编写了多少测试代码,让后又用/* */杠掉了,心痛啊!!后来看了一下以前用vb编写的代码,方法很傻,这次编写时,灵活运用c++运算符,免去了很多代码,同时也加入了回溯计算。顺便贴一下VB代码。
#include <iostream.h>
#include <stdlib.h>
short thousand(short n){return (short)(n/1000);}
short hundred(short n){return (short)((n-(thousand(n)*1000))/100);}
short ten(short n){return (short)((n-(thousand(n)*1000)-(hundred(n)*100))/10);}
short one(short n){return (short)(n-(thousand(n)*1000)-(hundred(n)*100)-(ten(n)*10));}
struct number{short numbers;short A;short B;short S;};
number all[4537];
void makeall()
{
short i,j,l,m,k;
k=0;
for (i=1;i<=9;i++)
{
for (j=0;j<=9;j++)
{
for (l=0;l<=9;l++)
{
for (m=0;m<=9;m++)
{
if (i!=j&&i!=l&&i!=m&&j!=l&&j!=m&&l!=m)
{k=k+1;
all[k].numbers=i*1000+j*100+l*10+m;
all[k].A =0;all[k].B=0;all[k].S =1;}
// cout<<thousand(all[k].numbers);
}
}
}
}//cout<<k;
}
void clear()
{short i;
for(i=0;i<=4536;i++)
{all[i].A =0;all[i].B=0;}
}
void compare(short input,number *in,short A,short B)
{
short i[4],j[4],m,n;
i[0]=thousand(input);j[0]=thousand((*in).numbers );
i[1]=hundred(input);j[1]=hundred((*in).numbers );
i[2]=ten(input);j[2]=ten((*in).numbers );
i[3]=one(input);j[3]=one((*in).numbers );
//for (k=0;k<=3;k++) {if (i[k]==j[k]) in.A++;}
clear();
for (m=0;m<=3;m++)
{
for (n=0;n<=3;n++)
{
// if ((((*in).numbers)>=1300)&&(((*in).numbers)<=1399)) cout<<(*in).numbers<<(*in).A<<(*in).B<<(*in).S<<endl;
if (i[m]==j[n]&&m!=n) (*in).B++;
if (i[m]==j[n]&&m==n) (*in).A++;
}
}
if (((*in).A==A)&&((*in).B==B)) ; else (*in).S =0;
// if ((((*in).numbers)>=1300)&&(((*in).numbers)<=1399)) cout<<(*in).numbers<<(*in).A<<(*in).B<<(*in).S<<endl;
//cout<<(*in).A <<(*in).B <<(*in).S <<(*in).numbers<<endl ;
}
/*void input(short _in,short a,short b)
{
;
};
};*/
void input(short _in,short a,short b)
{
short i;
for (i=0;i<=4536;i++)
{ compare(_in,&all[i],a,b);
//if (((all[i].numbers)>=1300)&&((all[i].numbers)<=1399)) cout<<all[i].numbers <<all[i].S <<endl;
}
}
void show()
{short i;
for (i=0;i<=4536;i++)
{ if (all[i].S ==1) cout<<all[i].numbers<<endl ;}
}
void main()
{
//cout<<ten(01)<<endl;
makeall(); //初始化,创建所有可能数字
short in[7],a[7],b[7],i,j,r,text,t;
i=0;
s: while(all[r].S !=1)
{
r=rand()%4536;
};
t=all[r].numbers;
cout<<"请输入:"<<t<<endl;
cin>>text;
for (j=0;j<=i;j++)
input(t,ten(text),one(text));
show();
i=i+1;
goto s;
}
#include <stdlib.h>
short thousand(short n){return (short)(n/1000);}
short hundred(short n){return (short)((n-(thousand(n)*1000))/100);}
short ten(short n){return (short)((n-(thousand(n)*1000)-(hundred(n)*100))/10);}
short one(short n){return (short)(n-(thousand(n)*1000)-(hundred(n)*100)-(ten(n)*10));}
struct number{short numbers;short A;short B;short S;};
number all[4537];
void makeall()
{
short i,j,l,m,k;
k=0;
for (i=1;i<=9;i++)
{
for (j=0;j<=9;j++)
{
for (l=0;l<=9;l++)
{
for (m=0;m<=9;m++)
{
if (i!=j&&i!=l&&i!=m&&j!=l&&j!=m&&l!=m)
{k=k+1;
all[k].numbers=i*1000+j*100+l*10+m;
all[k].A =0;all[k].B=0;all[k].S =1;}
// cout<<thousand(all[k].numbers);
}
}
}
}//cout<<k;
}
void clear()
{short i;
for(i=0;i<=4536;i++)
{all[i].A =0;all[i].B=0;}
}
void compare(short input,number *in,short A,short B)
{
short i[4],j[4],m,n;
i[0]=thousand(input);j[0]=thousand((*in).numbers );
i[1]=hundred(input);j[1]=hundred((*in).numbers );
i[2]=ten(input);j[2]=ten((*in).numbers );
i[3]=one(input);j[3]=one((*in).numbers );
//for (k=0;k<=3;k++) {if (i[k]==j[k]) in.A++;}
clear();
for (m=0;m<=3;m++)
{
for (n=0;n<=3;n++)
{
// if ((((*in).numbers)>=1300)&&(((*in).numbers)<=1399)) cout<<(*in).numbers<<(*in).A<<(*in).B<<(*in).S<<endl;
if (i[m]==j[n]&&m!=n) (*in).B++;
if (i[m]==j[n]&&m==n) (*in).A++;
}
}
if (((*in).A==A)&&((*in).B==B)) ; else (*in).S =0;
// if ((((*in).numbers)>=1300)&&(((*in).numbers)<=1399)) cout<<(*in).numbers<<(*in).A<<(*in).B<<(*in).S<<endl;
//cout<<(*in).A <<(*in).B <<(*in).S <<(*in).numbers<<endl ;
}
/*void input(short _in,short a,short b)
{
;
};
};*/
void input(short _in,short a,short b)
{
short i;
for (i=0;i<=4536;i++)
{ compare(_in,&all[i],a,b);
//if (((all[i].numbers)>=1300)&&((all[i].numbers)<=1399)) cout<<all[i].numbers <<all[i].S <<endl;
}
}
void show()
{short i;
for (i=0;i<=4536;i++)
{ if (all[i].S ==1) cout<<all[i].numbers<<endl ;}
}
void main()
{
//cout<<ten(01)<<endl;
makeall(); //初始化,创建所有可能数字
short in[7],a[7],b[7],i,j,r,text,t;
i=0;
s: while(all[r].S !=1)
{
r=rand()%4536;
};
t=all[r].numbers;
cout<<"请输入:"<<t<<endl;
cin>>text;
for (j=0;j<=i;j++)
input(t,ten(text),one(text));
show();
i=i+1;
goto s;
}