
image.png
需求設(shè)計:
1、登錄功能 密碼錯誤三次機會 全錯提示退出 自定義函數(shù)
2、登錄成功后 學(xué)生信息錄入(姓名 學(xué)號 性別 成績) 用戶可以選擇是否繼續(xù)錄入
3、學(xué)生錄入完成后可以查詢遍歷及其增刪改查
4、實現(xiàn)存儲功能 保存至文件中
退出
#include<stdio.h>
#include<windows.h>
struct stds
{
int id;
char name[20];
char sex[10];
int ach;
struct stds *next;
};
struct stds *head;
struct stds *std;
struct stds *jl;
//讀取文件
int read()
{
int n;
FILE *rp;
struct stds *rd;
rp=fopen("./cundang.txt","a+");
rd=(struct stds *)malloc(sizeof(struct stds));
n=fscanf(rp,"%d%s%s%d",&rd->id,rd->name,rd->sex,&rd->ach);
if(n==-1)
{
printf("數(shù)據(jù)庫內(nèi)無數(shù)據(jù)\n");
return 0;
}
while(1)
{
if(!feof(rp))
{
rd->next=NULL;
std->next=rd;
std=std->next;
}
else
{
break;
}
rd=(struct stds *)malloc(sizeof(struct stds));
fscanf(rp,"%d%s%s%d",&rd->id,rd->name,rd->sex,&rd->ach);
}
fclose(rp);
printf("數(shù)據(jù)庫加載完成\n");
jl=std;
return 0;
}
//寫入文件
int write()
{
FILE *wp;
std=head->next; //從頭遍歷
if(std==NULL)
{
printf("內(nèi)容為空,無法保存\n");
return 0;
}
wp=fopen("./cundang.txt","w");
while(std!=NULL)
{
fprintf(wp,"%d %s %s %d\n",std->id,std->name,std->sex,std->ach);
std=std->next;
}
fclose(wp);
printf("保存成功\n");
return 0;
}
//輸入內(nèi)容實現(xiàn)
int input()
{
char n;
struct stds *ed;
std=jl;
while(n=getchar())
{
if(n=='n')
{
break;
}
ed=(struct stds *)malloc(sizeof(struct stds));
printf("請輸入學(xué)號,姓名,性別,成績:\n");
scanf("%d%s%s%d",&ed->id,ed->name,ed->sex,&ed->ach);
getchar(); //吸收回車符
ed->next=NULL;
std->next=ed;
std=std->next;
printf("n退出,回車?yán)^續(xù)輸入...\n");
}
jl=std;
return 0;
}
//顯示內(nèi)容實現(xiàn)
int output()
{
int a=0; //判斷數(shù)據(jù)是否有輸入
std=head->next; //從頭遍歷
printf("內(nèi)容如下:\n");
while(std!=NULL)
{
if(std->next==NULL)
{
jl=std;
}
printf("%d\t%s\t%s\t%d\n",std->id,std->name,std->sex,std->ach);
std=std->next;
a=1;
}
if(a==0) //未輸入數(shù)據(jù)情況
printf("內(nèi)容為空\n");
return 0;
}
//查找內(nèi)容實現(xiàn)
int find(int n)
{
std=head->next;
while(std!=NULL)
{
if(std->id==n)
{
printf("%d\t%s\t%s\t%d\n",std->id,std->name,std->sex,std->ach);
std=std->next;
}
else
{
std=std->next;
}
}
return 0;
}
//修改內(nèi)容實現(xiàn)
int gai(int n)
{
find(n);
std=head->next;
while(std!=NULL)
{
if(std->id==n)
{
printf("輸入新信息:\n");
scanf("%d%s%s%d",&std->id,std->name,std->sex,&std->ach);
printf("修改完成");
break;
}
else
{
std=std->next;
}
}
output();
return 0;
}
//刪除內(nèi)容實現(xiàn)
int del(int num)
{
struct stds *dp;
int n;
dp=head;
std=head->next;
n=0;
while(std!=NULL)
{
if(std->id==num)
{
dp->next=std->next;
free(std);
printf("刪除后");
n=1;
output();
break;
}
n=2;
dp=std;
std=std->next;
}
if(n==0)
{
printf("數(shù)據(jù)為空\n");
}
else if(n==2)
{
printf("沒找著呀\n");
}
return 0;
}
//登錄實現(xiàn)
int login()
{
int i,j;
char username[20],password[20];
char lun[20]="123";
char lpw[20]="123";
for(i=0;i<3;i++)
{
printf("用戶名(20字符以內(nèi)):");
scanf("%s",username);
printf("密 碼(20字符以內(nèi)):");
scanf("%s",password);
if((j=strcmp(lun,username))==0)
{
if((j=strcmp(lpw,password))==0)
{
system("cls");
printf("登陸成功...\n");
return 1;
}
else
{
printf("用戶名或密碼錯誤...\n");
}
}
else
{
printf("用戶名不存在...\n");
}
}
printf("已輸入錯誤三次,系統(tǒng)自動退出...\n");
return 0;
}
int main()
{
int lg,i,n,gb;
char menu;
head=(struct stds *)malloc(sizeof(struct stds));
std=(struct stds *)malloc(sizeof(struct stds));
gb=0;
head->next=NULL;
jl=std=head;
lg=login();
if(lg)
{
getchar();
read();
printf("歡迎使用\n");
printf("輸入內(nèi)容按1,顯示內(nèi)容按2,查找內(nèi)容按3,修改內(nèi)容按4,刪除內(nèi)容按5,6保存,其他退出\n");
while(menu=getchar())
{
printf("繼續(xù)輸入按1,顯示內(nèi)容按2,查找內(nèi)容按3,修改內(nèi)容按4,刪除內(nèi)容按5,6保存,其他退出\n");
switch(menu)
{
case '1':system("cls");input();break;
case '2':system("cls");output();break;
case '3':system("cls");printf("輸入要查找的ID:\n");scanf("%d",&n);find(n);break;
case '4':system("cls");output();printf("輸入要修改的ID:\n");scanf("%d",&n);gai(n);break;
case '5':system("cls");output();printf("輸入要刪除的ID:\n");scanf("%d",&n);del(n);break;
case '6':write();getchar();break;
case '\n':gb=2;break;
default:system("cls");gb=1;break;
}
if(gb==1)
{
write();
printf("退出成功\n");
break;
}
else if(gb==2)
{
continue;
}
}
}
else
{
printf("歡迎下次使用,再見...\n");
}
printf("感謝使用\n");
system("pause");
return 0;
}