花2天时间做的日志系统帮忙看看(支持多用户使用)
程序代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> #define ESC 27 char ff[50]; zhu_ce() {int i=0,k=0,j=0,n=0;char c; struct z_h1 /*用来存放从文件中读取的用户名与密码*/ {char a[15],b[15];}zh1[10]; struct z_h /*用来存放输入的用户名与密码*/ {char a[15],b[15];}zh[10]; FILE *fp; printf("||****Register****||\n"); if(fp=fopen("D:\\zhu_ce.txt","r+")==NULL) /*如果打开失败则创建一个文件只写*/ {fp=fopen("D:\\zhu_ce.txt", "w+"); fclose(fp);} do { printf("Account ID :"); scanf("%s",zh[i].a); printf("Password :"); scanf("%s",zh[i].b); fp=fopen("D:\\zhu_ce.txt","r+"); do {fread(&zh1[j],sizeof(zh1[j]),1,fp);j++;n++; /*读取文件内容负值给结构体*/ }while(!feof(fp)); fclose(fp); for(n=0;n<j-1;n++) /*判断用户名是否已经被注册*/ { if(strcmp(zh1[n].a,zh[i].a)==0) {k=1;break;}} if(k!=1) {fp=fopen(zh[i].a, "w+"); fclose(fp); fp=fopen("D:\\zhu_ce.txt","a"); fwrite(&zh[i],sizeof(zh[i]),1,fp); /*往文件里存储用户名跟密码*/ i++; printf("Done!\n\nDo you want to register one again ?<Y/N>\n\n"); c=getch();} else {puts("User already exists"); puts("Please choose another user name"); getch();} }while(c=='y'||c=='Y'); fclose(fp); logo(); } void deng_lu() /*登录子函数*/ { char c[15],d[15],t; int j,i=0,k=0; struct z_h {char a[15],b[15];}zh[10]; FILE *fp; fp=fopen("D:\\zhu_ce.txt","r"); do {fread(&zh[i],sizeof(zh[i]),1,fp); i++; /*读取文件内容负值给结构体*/ }while(!feof(fp)); fclose(fp); printf("||*******Login*****||\n"); printf("Your ID :"); scanf("%s",c);printf("\n"); printf("Your password :"); scanf("%s",d);printf("\n"); for(j=0;j<i-1;j++) /*判断用户名密码是否正确*/ { if((strcmp(c,zh[j].a)==0)&&(strcmp(d,zh[j].b)==0)) { k=1; break; } } if(k!=0) { printf("Welcome back %s!\n\n",c); printf("Press any key to continue"); strcpy(ff,zh[j].a); getch(); do { logo1(); t=getch(); switch(t) { case '1':printf("\n");cha_kan();break; case '2':printf("\n");xie();break; } }while(t!=27); logo(); } else { printf("Sorry ! Account Error or Password Error"); getch(); logo1(); } } cha_kan() /*查看子函数*/ { char l ,n[11],f[50]; int i,j,k=0,m=0; struct diary /*定义一个结构变量*/ { char mood[8],weather[10],content[50],day[11]; }user[10]; FILE *fp; fp=fopen("D:\\zhu_ce.txt","r"); if(fp=fopen(ff,"r")==NULL) /*如果打开失败则输出说明*/ {puts("Sorry !You have not written a diary");getch();logo2();return;} else fp=fopen(ff,"r"); do {fread(&user[i],sizeof(user[i]),1,fp); i++; /*读取文件内容负值给结构体*/ }while(!feof(fp)); fclose(fp); do { logo2(); l=getch(); switch(l) { case '1': system("CLS");printf("\n"); /*序号查找*/ printf("Serial number : "); scanf("%d",&m); printf("Mood :%5s Data : %2s Wether : %2s\n Content : %s\n",user[m-1].mood,user[m-1].day, user[m-1].weather,user[m-1].content); getch();break; case '2': system("CLS");printf("\n"); /*按日期查找*/ puts("Date(X-X-X):"); gets(n); for(j=0;j<i-1;j++) {if(strcmp(n,user[j].day)==0) {printf("Mood :%5s Data : %2s Wether : %2s\n Content : %s\n",user[j].mood,user[j].day, user[j].weather,user[j].content);k=1;break;}} if(k==0){puts("No find!");}getch();break; case '3': system("CLS");printf("\n"); /*按关键字查找*/ puts("Keyword : "); gets(f); for(j=0;j<i-1;j++) {if(strstr(user[j].content,f)!=NULL) {printf("Mood :%5s Data : %2s Wether : %2s\n Content : %s\n",user[j].mood,user[j].day, user[j].weather,user[j].content);k=1;}} if(k==0){puts("No find !");}getch();break; } }while(l!=27); getch(); logo1(); } xie() { int i=0; char p; struct diary /*定义一个结构变量*/ {char mood[8],weather[10],content[50],day[11];}user[10]; FILE *fp; fp=fopen("D:\\zhu_ce.txt","r"); if(fp=fopen(ff,"a")==NULL) /*如果打开失败则创建一个文件只写*/ {fp=fopen(ff, "w"); fclose(fp);} else fp=fopen(ff,"a"); do { puts("\nThe Mood :"); /*输入心情*/ scanf("%s",user[i].mood); puts("The Data(X-X-X): "); /*出入日期*/ scanf("%s",user[i].day); puts("The Weather : "); /*输入天气*/ scanf("%s",user[i].weather); puts("The Content : "); scanf("%s",user[i].content); fwrite(&user[i],sizeof(user[i]),1,fp); i++; /*结果写入文件*/ puts("\nDo you want to write one again ?(Y/N)\n\n"); p=getch(); }while(p=='Y'||p=='y'); fclose(fp); logo1(); } logo() { system("CLS"); printf("\n\t\t|*******************************|"); printf("\n\t\t|*** Diary System ***|"); printf("\n\t\t|*******************************|"); printf("\n\t\t|* 1.Register new users *|"); printf("\n\t\t|* 2.User login *|"); printf("\n\t\t|* ESC.Exit *|"); printf("\n\t\t|*******************************|"); printf("\n\t\t\tPlease choose :"); } logo1() {system("CLS"); printf("\n\t\t|*******************************|"); printf("\n\t\t|* 1.Cheak *|"); printf("\n\t\t|* 2.Write *|"); printf("\n\t\t|* ESC.Return *|"); printf("\n\t\t|*******************************|"); printf("\n\t\t\tPlease choose :"); } logo2() {system("CLS"); printf("\n\t\t|*******************************|"); printf("\n\t\t|* 1.Search by serial number *|"); printf("\n\t\t|* 2.Search by date *|"); printf("\n\t\t|* 3.Search by keyword *|"); printf("\n\t\t|* ESC.Return *|"); printf("\n\t\t|*******************************|"); printf("\n\t\t\tPlease choose :"); } main() { char c,a[10],b[10]; do { logo(); c=getch(); switch(c) { case '1':printf("\n");zhu_ce();break; case '2':printf("\n");deng_lu();break; } }while(c!=27); exit(0); }看看有什么改进的?
[ 本帖最后由 tonlin 于 2009-12-20 16:31 编辑 ]