#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE sizeof(nDate)
typedef struct nPasseword
{
char userName[10];
char userPassword[10];
struct nPasseword* next;
}nDate;
void administrator()
{
}
void teacherUser()
{
}
void normalUser()
{
}
void regUser(nDate* head)
{
//nDate* p = (nDate*)malloc(SIZE);
//p->next = NULL;
nDate* p = head;
while (1)
{
printf("请输入你的新用户名(用户名为10位)\n");
scanf("%s",p->userName);
if (strlen(p->userName)==10)
{
while(1)
{
printf("请输入你的新密码(密码长度为10位)\n");
scanf("%s",p->userPassword);
if (strlen(p->userPassword)==10)
{
printf("注册成功,您的用户名是%s,密码是%s\n",p->userName,p->userPassword);
break;
}
else if(strlen(p->userPassword)>10)
{
printf("您的密码过长,请重新输入:\n");
}
else
{
printf("您的密码过短,请重新输入:\n");
}
}
break;
}
else if ( strlen(p->userName) > 10 )
{
printf("您的用户名过长,请重新输入:\n");
}
else
{
printf("您的用户名过短,请重新输入: \n");
}
}
}
void writeDate(nDate* head)
{
FILE *fp;
if((fp=fopen("passerword.dat","w+"))==NULL)
{
printf("can"t open the file.");
exit(0);
}
else
{
nDate* curnode=(nDate*)malloc(SIZE);
curnode = head;
while(curnode!=NULL)
{
fwrite(curnode,sizeof(nDate),1,fp);
curnode = curnode->next;
}
}
fclose(fp);
}
nDate* read(nDate* head)
{
FILE* fp;
nDate* curnode = head;
if((fp=fopen("passerword.dat","r"))==NULL)
{
printf("file passerword.dat can"t be opened");
}
else
{
while (!feof(fp))
{
nDate* nextnode=(nDate*)malloc(SIZE);
nextnode->next = NULL;
fread(nextnode,sizeof(nDate),1,fp);
if (!feof(fp))
{
curnode->next = nextnode;
curnode = nextnode;
}
}
fclose(fp);
}
return head;
}
int main()
{
nDate* head = (nDate*)malloc(SIZE);
head->next = NULL;
//int administratorName = 16175211;
//int administratorPassword = 16175211;
int n;
printf("请选择:\n");
printf("1.超级权限登陆 2.教师用户登陆 3.普通用户登陆 4.用户注册 0.退出客户端\n");
scanf("%d",&n);
switch (n)
{
case 0:
{
writeDate(head);
exit(1);
}
case 1:
{
administrator();
break;
}
case 2:
{
teacherUser();
break;
}
case 3:
{
normalUser();
break;
}
case 4:
{
regUser(head);
writeDate(head);
break;
}
}
return 0;
}
解决方案
40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE sizeof(nDate)
#pragma pack(push)
#pragma pack(1)
typedef struct nPasseword
{
char userName[11];
char userPassword[11];
struct nPasseword* next;
}nDate;
#pragma pack(pop)
void administrator()
{
}
void teacherUser()
{
}
void normalUser()
{
}
void regUser(nDate* head)
{
//nDate* p = (nDate*)calloc(1,SIZE);
//p->next = NULL;
nDate* p = head;
while (1)
{
printf("请输入你的新用户名(用户名为10位)\n");
scanf("%10s",p->userName);
if (strlen(p->userName)==10)
{
while(1)
{
printf("请输入你的新密码(密码长度为10位)\n");
scanf("%10s",p->userPassword);
if (strlen(p->userPassword)==10)
{
printf("注册成功,您的用户名是%s,密码是%s\n",p->userName,p->userPassword);
break;
}
else if(strlen(p->userPassword)>10)
{
printf("您的密码过长,请重新输入:\n");
}
else
{
printf("您的密码过短,请重新输入:\n");
}
}
break;
}
else if ( strlen(p->userName) > 10 )
{
printf("您的用户名过长,请重新输入:\n");
}
else
{
printf("您的用户名过短,请重新输入: \n");
}
}
}
void writeDate(nDate* head)
{
FILE *fp;
if((fp=fopen("passerword.dat","wb"))==NULL)
{
printf("can"t open the file.");
exit(0);
}
else
{
nDate* curnode=head;
while(curnode!=NULL)
{
fwrite(curnode,sizeof(nDate),1,fp);
curnode = curnode->next;
}
}
fclose(fp);
}
nDate* read(nDate* head)
{
FILE* fp;
nDate* curnode = head;
if((fp=fopen("passerword.dat","rb"))==NULL)
{
printf("file passerword.dat can"t be opened");
}
else
{
while (!feof(fp))
{
nDate* nextnode=(nDate*)calloc(1,SIZE);
nextnode->next = NULL;
fread(nextnode,sizeof(nDate),1,fp);
if (!feof(fp))
{
curnode->next = nextnode;
curnode = nextnode;
}
}
fclose(fp);
}
return head;
}
int main()
{
nDate* head = (nDate*)calloc(1,SIZE);
head->next = NULL;
//int administratorName = 16175211;
//int administratorPassword = 16175211;
int n;
printf("请选择:\n");
printf("1.超级权限登陆 2.教师用户登陆 3.普通用户登陆 4.用户注册 0.退出客户端\n");
scanf("%d",&n);
switch (n)
{
case 0:
{
writeDate(head);
exit(1);
}
case 1:
{
administrator();
break;
}
case 2:
{
teacherUser();
break;
}
case 3:
{
normalUser();
break;
}
case 4:
{
regUser(head);
writeDate(head);
break;
}
}
return 0;
}