当前位置: 代码迷 >> 综合 >> C语言,Math Dash的账户登录程序(二叉排序树)
  详细解决方案

C语言,Math Dash的账户登录程序(二叉排序树)

热度:50   发布时间:2023-12-02 09:32:12.0

如果有任何问题可以私信

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <stdbool.h>
#include <dirent.h>
typedef char *KeyType;
typedef struct
{KeyType id;char *password,*file_name;unsigned long long int score;
}Elemtype;
typedef struct BSTNode
{Elemtype data;int _Depth;struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
char *hui_fu_data(char *a,int mods)
{int b;for(b=0;b<strlen(a);b++){if(a[b]!='\n'){if(mods==1){a[b]=a[b]+100;}else if(mods==2){a[b]=a[b]-100;}}}return a;
}
int bi_(char *a,char *b)
{if(strcmp(a,b)==0){return 2;}else if(strlen(a)>strlen(b)){return 1;}else if(strlen(a)<strlen(b)){return 0;}else{int c;for(c=0;c<strlen(a);c++){if(a[c]!=b[c]){break;}}if(a[c]>b[c]){return 1;}else{return 0;}}
}
void InsertBFS(BSTree *a,Elemtype b)
{if(!(*a)){BSTree c;c=(BSTree)malloc(sizeof(BSTNode));c->lchild=c->rchild=NULL;c->data=b;*a=c;}else if(!bi_(b.id,(*a)->data.id)){InsertBFS(&(*a)->lchild,b);}else{InsertBFS(&(*a)->rchild,b);}
}
BSTree BSTree_Data(BSTree a)
{if((!a)||((!a->lchild)&&(!a->rchild))){return NULL;}else if((a->lchild)&&(a->rchild)){return a->lchild->_Depth>a->rchild->_Depth?a->lchild:a->rchild;}else{return a->lchild?a->lchild:a->rchild;}
}
void BSTree_Depth_Data(BSTree *a)
{BSTree b;b=BSTree_Data(*a);if(b){(*a)->_Depth=b->_Depth+1;}else if(*a){(*a)->_Depth=1;}
}
void _Depth_Data(BSTree *a,KeyType key)
{if(*a){int mods;if((mods=bi_(key,(*a)->data.id))==0){_Depth_Data(&(*a)->lchild,key);}else if(mods==2){return;}else{_Depth_Data(&(*a)->rchild,key);}BSTree_Depth_Data(a);}
}
void BSTree_Data2(BSTree *a,BSTree *b1,BSTree *c1,KeyType key)
{BSTree b,c=NULL;b=*a;while(b){int mods;if((mods=bi_(key,b->data.id))==2){break;}c=b;if(!mods){b=b->lchild;}else{b=b->rchild;}}*c1=b;*b1=c;
}
int jian_(BSTree a)
{if((!a)||(!a->lchild)&&(!a->rchild)){return 0;}else if((a->lchild)&&(a->rchild)){int w;w=a->lchild->_Depth-a->rchild->_Depth;if(w<-1){return 1;}else if(w>1){return 2;}else{return 0;}}else if(!a->lchild){if(a->rchild->_Depth>1){return 1;}}else if(!a->rchild){if(a->lchild->_Depth>1){return 2;}}return 0;
}
int jian_ce2(BSTree a)
{if((!a)||(!a->lchild)&&(!a->rchild)){return 0;}else if((a->lchild)&&(a->rchild)){int w;w=a->lchild->_Depth-a->rchild->_Depth;if(w==-1){return 1;}else if(w==1){return 2;}else{return 0;}}else if(!a->lchild){if(a->rchild->_Depth==1){return 1;}}else if(!a->rchild){if(a->lchild->_Depth==1){return 2;}}return 0;
}
void BSTree_Data3(BSTree *a,BSTree *b1,KeyType key)
{BSTree b,c=NULL;b=*a;while(b){if(jian_(b)){c=b;}int mods;if((mods=bi_(key,b->data.id))==2){break;}if(!mods){b=b->lchild;}else{b=b->rchild;}}*b1=c;
}
void BSTree_AVL_LL(BSTree *a,BSTree b1)
{if(!b1){return;}BSTree b,c,d,e;BSTree_Data2(a,&b,&c,b1->data.id);d=c->lchild;e=d->rchild;d->rchild=c;c->lchild=e;if(!b){*a=d;}else if(b->lchild==c){b->lchild=d;}else{b->rchild=d;}_Depth_Data(a,c->data.id);
}
void BSTree_AVL_RR(BSTree *a,BSTree b1)
{if(!b1){return;}BSTree b,c,d,e;BSTree_Data2(a,&b,&c,b1->data.id);d=c->rchild;e=d->lchild;d->lchild=c;c->rchild=e;if(!b){*a=d;}else if(b->lchild==c){b->lchild=d;}else{b->rchild=d;}_Depth_Data(a,c->data.id);
}
void BSTree_AVL(BSTree *a,KeyType key)
{BSTree b,c;BSTree_Data3(a,&b,key);int mods;if(!b){return;}else if((mods=jian_(b))==2){c=b->lchild;if(jian_ce2(c)==1){BSTree_AVL_RR(a,c);}BSTree_AVL_LL(a,b);}else{c=b->rchild;if(jian_ce2(c)==2){BSTree_AVL_RR(a,c);}BSTree_AVL_LL(a,b);}
}
char *input_char(char *a)
{fgets(a,1500000,stdin);a[strlen(a)-1]='\0';return a;
}
void BSTree_Data_FPrint(Elemtype a)
{char *Data;Data=(char*)malloc(sizeof(char)*1500000);sprintf(Data,"id:\n%s\npassword:\n%s\nscore:\n%lld\n",a.id,a.password,a.score);Data=hui_fu_data(Data,1);FILE*fp=fopen(a.file_name,"wt+");fputs(Data,fp);free(Data);fclose(fp);
}
void Elemtype_data(Elemtype *a,char *file,char mods,bool *mods2)
{printf("请输入用户名:");char *b;b=(char*)malloc(sizeof(char)*1500000);do{b=input_char(b);}while(strlen(b)==0);a->id=(char*)malloc(sizeof(char)*(strlen(b)+1));strcpy(a->id,b);printf("请输入用户密码:");b=(char*)malloc(sizeof(char)*1500000);do{b=input_char(b);}while((strlen(b)>20)||(strlen(b)<6));a->password=(char*)malloc(sizeof(char)*(strlen(b)+1));strcpy(a->password,b);sprintf(b,"%s\\%s.txt",file,a->id);a->file_name=(char*)malloc(sizeof(char)*(strlen(b)+2));strcpy(a->file_name,b);free(b);a->score=0;*mods2=true;if(mods=='1'){if(_access(a->file_name,F_OK)==-1){BSTree_Data_FPrint(*a);*mods2=false;}}
}
void Elemtype_FREE(Elemtype *a)
{free(a->file_name);free(a->id);free(a->password);
}
void FREE(BSTree *a)
{BSTree b;b=*a;if(b){Elemtype_FREE( b->data ;}
}
bool DeleteBFS(BSTree *a,KeyType key)
{BSTree b,c,d,e;BSTree_Data2(a,&b,&c,key);if(!c){return false;}remove(c->data.file_name);char *file_data_tmp;file_data_tmp=(char*)malloc(sizeof(char)*(strlen(c->data.file_name)+6));sprintf(file_data_tmp,"%s_.tmp",c->data.file_name);remove(file_data_tmp);free(file_data_tmp);d=c;if((c->lchild)&&(c->rchild)){e=c->lchild;while(e->rchild){d=e;e=e->rchild;}FREE(&c);c->data=e->data;if(d!=e){d->rchild=e->lchild;}else{d->lchild=e->lchild;}free(e);_Depth_Data(a,d->data.id);BSTree_AVL(a,d->data.id);return true;}else if(!c->lchild){c=c->rchild;}else if(!c->rchild){c=c->lchild;}if(!b){*a=c;}else if(b->lchild==d){b->lchild=c;}else{b->rchild=c;}FREE(&d);if(c){_Depth_Data(a,c->data.id);BSTree_AVL(a,c->data.id);}else if(b){_Depth_Data(a,b->data.id);BSTree_AVL(a,b->data.id);}return true;
}
char *file_data_xv(char *a)
{int b,c;for(b=strlen(a)-1;b>=0;){if(a[b]=='\\'){break;}else{b--;}}b=b+1;char *d;d=(char*)malloc(sizeof(char)*b);for(c=0;c<b-1;c++){d[c]=a[c];}d[c]='\0';char *e;e=(char*)malloc(sizeof(char)*(strlen(d)+9));sprintf(e,"%s\\numbers",d);if(_access(e,F_OK)==-1){_mkdir(e);}free(d);return e;
}
bool FILE_Data4(char *data,Elemtype *U)
{char *mods;if(_access(data,F_OK)==-1){mods="wt+";}else{mods="rt";}FILE*fp=fopen(data,mods);char **w;w=(char**)malloc(sizeof(char*)*6);int e;for(e=0;e<6;e++){w[e]=(char*)malloc(sizeof(char)*1500000);fgets(w[e],1500000,fp);w[e][strlen(w[e])-1]='\0';w[e]=hui_fu_data(w[e],2);}bool mods2;mods2=true;if((bi_(w[0],"id:")==2)&&(bi_(w[2],"password:")==2)&&(bi_(w[4],"score:")==2)){U->file_name=data;U->id=(char*)malloc(sizeof(char)*(strlen(w[1])+1));strcpy(U->id,w[1]);U->password=(char*)malloc(sizeof(char)*(strlen(w[3])+1));strcpy(U->password,w[3]);U->score=atoll(w[5]);mods2=false;}for(e=0;e<6;e++){free(w[e]);}free(w);fclose(fp);return mods2;
}
bool BSTree_jian_ce(BSTree *a,Elemtype data,BSTree *DATA)
{BSTree b,c;BSTree_Data2(a,&b,&c,data.id);if(!c){*DATA=NULL;return true;}if(bi_(data.id,c->data.id)==2){if(_access(c->data.file_name,F_OK)==-1){DeleteBFS(a,c->data.id);return true;}char *Data;Data=(char*)malloc(sizeof(char)*(strlen(c->data.file_name)+6));sprintf(Data,"%s_.tmp",c->data.file_name);remove(Data);if(_access(Data,F_OK)!=-1){*DATA=NULL;free(Data);return true;}else{*DATA=c;free(Data);return false;}}*DATA=NULL;return true;
}
bool _FILE_DATA(char *file_name,char *name,Elemtype *U)
{char *data,*mods;data=(char*)malloc(sizeof(char)*(strlen(file_name)+strlen(name)+3));sprintf(data,"%s\\%s",file_name,name);return FILE_Data4(data,U);
}
void Insert(BSTree *a,Elemtype data)
{InsertBFS(a,data);_Depth_Data(a,data.id);BSTree_AVL(a,data.id);
}
void _DATA(char *file_name,BSTree *y)
{DIR *dir;dir=NULL;if((dir=opendir(file_name))==NULL){printf("数据读取失败!\n");system("pause");exit(0);}else{struct dirent *entry;while(entry=readdir(dir)){bool mods;Elemtype data;mods=_FILE_DATA(file_name,entry->d_name,&data);if(!mods){BSTree e,r;BSTree_Data2(y,&e,&r,data.id);if(!r){Insert(y,data);}}}closedir(dir);}
}
int input_int()
{char *a;a=(char*)malloc(sizeof(char)*1500000);int b;do{a=input_char(a);for(b=0;b<strlen(a);b++){if(!isdigit(a[b])){break;}}}while((b==0)||(b!=strlen(a)));int c;c=atoi(a);free(a);return c;
}
void game_data_1(BSTree *d)
{int len,t;printf("请输入算式数量:");t=input_int();for(len=0;len<t;len++){int a,b,c;do{a=rand()%101;b=rand()%101;}while(a+b>100);printf("%d:%d+%d=",len+1,a,b);c=input_int();if(c==a+b){Elemtype t;t=(*d)->data;free(t.file_name);char *e;e=t.file_name;free(t.password);free(t.id);FILE_Data4((*d)->data.file_name,&t);t.file_name=e;(*d)->data=t;(*d)->data.score=(*d)->data.score+(unsigned long long int)500;BSTree_Data_FPrint((*d)->data);printf("回答正确!\n");}else{printf("回答错误!\n");}}
}
void Deng_Data(BSTree *a,BSTree b)
{char *Data;Data=(char*)malloc(sizeof(char)*(strlen(b->data.file_name)+6));sprintf(Data,"%s_.tmp",b->data.file_name);FILE*fp=fopen(Data,"wt+");free(Data);int mods=0;for(;;){printf("1.注销\n2.100以内加法\n3.查询成绩\n按其它键退出\n");char mod=getch();switch(mod){case '1':fclose(fp);DeleteBFS(a,b->data.id);mods=1;break;case '2':game_data_1(&b);break;case '3':printf("score:%lld\n",b->data.score);break;default:mods=1;fclose(fp);break;}if(mods){break;}system("pause");system("cls");}
}
bool Windows_mods(char *argv)
{int b,c,d,e,f=0;for(b=strlen(argv)-1;b>=1;b--){if(argv[b-1]=='\\'){break;}}for(c=1;c<strlen(argv);c++){if((argv[c-1]=='\\')&&(c!=b)){f=c;}}char *Data;Data=(char*)malloc(sizeof(char)*(b-f+1));for(d=f,e=0;d<b;d++,e++){Data[e]=argv[d];}Data[e-1]='\0';bool mods;if(strcmp(Data,"numberData")==0){mods=false;}else{mods=true;}free(Data);return mods;
}
void FREE_BSTree(BSTree *a)
{if(*a){FREE_BSTree(&(*a)->lchild);FREE_BSTree(&(*a)->rchild);FREE(a);*a=NULL;}
}
void tre_Data(BSTree *a,Elemtype data)
{BSTree b,c;BSTree_Data2(a,&b,&c,data.id);if(c){return;}else if(_access(data.file_name,F_OK)!=-1){Elemtype data2;data2.file_name=(char*)malloc(sizeof(char)*(strlen(data.file_name)+1));strcpy(data2.file_name,data.file_name);FILE_Data4(data2.file_name,&data2);Insert(a,data2);}
}
void Windows_system(char *argv)
{char *a;a=file_data_xv(argv);BSTree f=NULL;_DATA(a,&f);for(;;){printf("1.注册\n2.登录\n其他键退出\n");char c=getch();system("cls");if((c=='1')||(c=='2')){Elemtype data;bool mods;Elemtype_data(&data,a,c,&mods);system("cls");switch(c){case '1':{BSTree e,r;BSTree_Data2(&f,&e,&r,data.id);if(r){printf("账户已存在!\n");Elemtype_FREE(&data);}else{if(!mods){Insert(&f,data);}else{printf("账户已存在!\n");Elemtype_FREE(&data);}}}break;case '2':{tre_Data(&f,data);BSTree Data;bool mods=BSTree_jian_ce(&f,data,&Data);Elemtype_FREE(&data);if(!mods){Deng_Data(&f,Data);}else{printf("登录失败!\n");}}break;}}else{free(a);FREE_BSTree(&f);return;}system("pause");system("cls");}
}
void Windows(char *argv)
{system("title Math Dash的账户登录程序");system("color f0");if(Windows_mods(argv)){printf("本程序不在文件夹\"numberData\"之中!请将本程序移动到文件夹\"numberData\"之中。\n");system("pause");}else{Windows_system(argv);}
}
int main(int argc, char *argv[])
{Windows(argv[0]);return 0;
}

程序运行如下:

  相关解决方案