日记本系统

作者在 2013-01-07 19:18:52 发布以下内容

这个是小菜鸟我大一时候,用c写的的日记本系统.
基础的知识都基本涉及到,如文件操作和目录操作.

运行都没问题,也没什么太严重的bug.想请路过的大神对鄙人的风格或其他不足方面给些建议,在此谢过.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#define MAXSIZE 1024
#define LENTH 50


typedef struct node{
    char date[LENTH]; //yyyy-mm-dd
    char content[MAXSIZE];
    char weather[LENTH];
    struct node *next;
}dairy;

typedef struct user_node{
    char dir[LENTH];
    char userName[LENTH];
    char pwd[LENTH];
    struct user_node *next;
}userInfo;


void prtByKeyWord(dairy *h);
void modify_pwd(userInfo *curUser) ; //密码修改
void IU() ;                  //系统界面设计
void SysLoad() ;             //系统启动
void useUI();    //用户功能界面
void regis();  //注册用户
void saveUserInfo(userInfo *H);  //永久化保存用户信息
void useSys(userInfo *curUser,int *flag);  //使用系统
void prtByDate(dairy *h);
void prtOrder(dairy *h);
void writeDairy(dairy *h,userInfo *curUser);  //写日记
dairy *loadAllDairy(userInfo *curUser);
userInfo *getUserInfo();     //获得所有用户账号和密码
userInfo *login(userInfo *H);   //登录界面
int trave_dir(char* path,char filename[][LENTH]);  //遍历文件夹,获取其中的所有txt文件(一篇日志一个txt文件)


int main()
{
    SysLoad();
    return 0;
}


void IU()   //系统界面设计
{
    system("CLS");
    printf("\t\t             ****************             \n");
    printf("\t\t              ** 欢迎使用 **              \n");
    printf("\t\t             ****************             \n");
    printf("\t\t                                          \n");
    printf("\t\t                                          \n");
    printf("\t\tAuthor:\t\t薛云腾                         \n");
    printf("\t\tClass:\t\t计算机二班                      \n");
    printf("\t\tStuPayroll:\t3110307212                   \n");
    printf("\t\t                                          \n");
    printf("\t\t                                          \n");
    printf("\t\t====== *** ===小型日记本系统=== *** ======\n");
    printf("\t\t==                                      ==\n");
    printf("\t\t==                                      ==\n");
    printf("\t\t==     l/L、用户登录;                  ==\n");
    printf("\t\t==     r/R、注册新用户;                ==\n");
    printf("\t\t==     q/Q、退出;                      ==\n");
    printf("\t\t==                                      ==\n");
    printf("\t\t=======****************************=======\n\n\n");
    printf("您的选择:");
}


void SysLoad()     //系统启动
{
    char choose;
    int flag=1;
    userInfo *H,*curUser;

    while(1)
    {
        IU();
        fflush(stdin);
        scanf("%c",&choose);
        getchar();

        switch (choose)
        {
           case 'l':
           case 'L':
                H=getUserInfo();
                if(H->next!=NULL)
                {
                    curUser=login(H);
                    flag=1;
                    if(curUser!=NULL)
                    {
                       while(flag){
                          useUI();
                          useSys(curUser,&flag);
                       }

                    }
                }
                else
                    printf("尚未有任何用户注册!请先注册...\n\n\n");

                printf("\n\n\n");
                system("PAUSE");   //按任意键继续
                break;

           case 'r':
           case 'R':
                regis();
                printf("\n\n\n");
                system("PAUSE");   //按任意键继续
                break;

           case 'q':
           case 'Q':
               exit(0);

           default:
               printf("\n\n输入错误,请重选...\n\n\n");
               system("PAUSE");
               break;
        }
    }
}


userInfo *login(userInfo *H)  //登录界面
{
    userInfo *p;
    char userName[LENTH];
    char pwd[LENTH];

    system("CLS");

        printf("请输入用户名:");
        scanf("%s",userName);
        printf("请输入密码:");
        scanf("%s",pwd);

        p=H->next;
        while(p){
            if(strcmp(p->userName,userName)==0){
                if(strcmp(p->pwd,pwd)==0)
                    return p;
                else
                {
                    printf("\n\n密码错误!返回主菜单...\n\n\n");
                    return NULL;
                }
            }
            p=p->next;

        }
    printf("\n\n该用户名不存在!返回主菜单...\n\n\n");
    return NULL;

}

userInfo *getUserInfo()  //获得所有用户账号和密码
{
    FILE *fp =NULL;
    userInfo *H=NULL,*p,*r;
    H=(userInfo *)malloc(sizeof(struct user_node));
    H->next=NULL;
    r=H;

    if((fp=fopen("userInfo.txt","r"))!=NULL)
    {
        while( !(feof(fp)) )
        {
            p=(userInfo *)malloc(sizeof(struct user_node));
            fscanf(fp,"%s%s%s",p->userName,p->pwd,p->dir);
            p->next=r->next;
            r->next=p;
            r=p;
        }
    }

    fclose(fp);

    return H;

}


void regis()  //注册用户
{
    userInfo *H,*r,*p,*q;
    char fileDir[LENTH]="md c:\\";   //默认创建路径为c盘

    int flag=0;
    char userName[LENTH];
    char pwd[LENTH];
    char dir[LENTH];

    H=getUserInfo();

    while(1)
    {
        if(flag!=0)
            break;
        system("CLS");
        printf("请输入注册的用户名:");
        scanf("%s",userName);
        printf("请输入密码:");
        scanf("%s",pwd);
        printf("请输入您的个人文件夹名:");
        scanf("%s",dir);

        p=H->next;
        r=H;
        while(p)
        {
            if(strcmp(p->userName,userName)==0)
            {
                printf("\n\n该用户名已经存在!请重新输入...\n\n\n");
                system("PAUSE");
                break;
            }
            r=p;
            p=p->next;
        }

        if(p==NULL)
            flag=1;
    }

    q=(userInfo *)malloc(sizeof(struct user_node));
    strcpy(q->userName,userName);
    strcpy(q->pwd,pwd);
    strcpy(q->dir,dir);

    q->next=NULL;
    r->next=q;

    strcat(fileDir,q->dir);
    system(fileDir);
    saveUserInfo(H);

    printf("\n\n注册成功!\n\n\n");

}


void saveUserInfo(userInfo *H)  //永久化保存用户信息
{
    FILE *fp;
    userInfo *p;

    if((fp=fopen("userInfo.txt","w"))==NULL)
    {
        printf("\n文件打开失败!\n\n");
        exit(0);
    }

    p=H->next;
    while(p)
    {
        if(p->next==NULL)
            fprintf(fp,"%s %s %s",p->userName,p->pwd,p->dir);
        else
            fprintf(fp,"%s %s %s\n",p->userName,p->pwd,p->dir);
        p=p->next;
    }

    fclose(fp);

}


void useUI()
{
    system("CLS");
    printf("1、创建日志;\n");
    printf("2、查阅指定日期的日记;\n");
    printf("3、按顺序查阅日记;\n");
    printf("4、按关键字查阅;\n");
    printf("5、修改密码;\n");
    printf("按任意键返回登录界面;\n\n\n");
    printf("您的选择:");
}


void useSys(userInfo *curUser,int *flag)  //使用系统
{
    char choose;
    dairy *h,*p;

    fflush(stdin);
    scanf("%c",&choose);
    fflush(stdin);

    switch (choose)
    {
       case '1':
        system("CLS");
        h=loadAllDairy(curUser);
        writeDairy(h,curUser);
        break;

      case '2':
        system("CLS");
        h=loadAllDairy(curUser);
        prtByDate(h);
        printf("\n\n\n");
        system("PAUSE");
        break;

      case '3':
        system("CLS");
        h=loadAllDairy(curUser);
        prtOrder(h);
        printf("\n\n\n");
        system("PAUSE");
        break;

      case '4':
        system("CLS");
        h=loadAllDairy(curUser);
        prtByKeyWord(h);
        break;

      case '5':
        system("CLS");
        modify_pwd(curUser);
        *flag=0;
        break;

      default:
        printf("\n\n您确定要注销,离开本系统?(n/y)");
        scanf("%c",&choose);
        if(choose!='n' && choose!='N')
            *flag=0;

        return;
    }

}


void writeDairy(dairy *h,userInfo *curUser)  //写日记
{
    dairy *p=NULL,*q;
    FILE *fp;
    char path[256];
    char n[LENTH],no[LENTH];
    char ch;

    memset(path,0,256);
    strcpy(path,"c:/");
    p=(dairy *)malloc(sizeof(struct node));
    p->next=NULL;
    printf("文件名:");
    scanf("%s",no);
    printf("日记日期(YYYY-MM-dd):");
    scanf("%s",p->date);
    printf("天气:");
    scanf("%s",p->weather);
    getchar();
    printf("日记内容:");
    gets(p->content);

    printf("\n\n保存该日记?(y/n):");
    fflush(stdin);
    scanf("%c",&ch);


    if(ch!='n' && ch!='N')
    {
        strcat(path,curUser->dir);   //c:/yun 其中yun是dir的一个例子
        strcat(path,"/");         //c:/yun/
        strcat(path,no);       //c:/yun/no
        strcat(path,".txt");      //c:/yun/no.txt
        printf(" %s ",path);

        if((fp=fopen(path,"w"))==NULL)
        {
            printf("\n文件打开失败!日志未保存...\n");
            exit(0);
        }

        fprintf(fp,"Date:%s\tWeather:%s\n",p->date,p->weather);
        fprintf(fp,"Content:\t%s",p->content);
        printf("\n\n日记保存成功!\n\n\n");

        fclose(fp);
    }

    else
        printf("\n\n日记未保存!\n\n");

    system("PAUSE");
    system("CLS");

}


dairy *loadAllDairy(userInfo *curUser)
{
    int cnt=0,len = 0 ,i =0;
    dairy *H=NULL,*p,*r;
    char filename[50][50];
    char path[256]="c:/";
    FILE *fp;

    H=(dairy *)malloc(sizeof(struct node));
    H->next=NULL;
    r=H;

    strcat(path,curUser->dir);
    len=trave_dir(path,filename);

    for(i=0;i<len;i++)
    {
        char filePath[LENTH];
        strcpy(filePath,path);
        strcat(filePath,"/");
        strcat(filePath,filename[i]);

        if((fp=fopen(filePath,"r"))==NULL)
        {
            printf("\n文件打开失败!系统退出...\n");
            exit(0);
        }

        p=(dairy *)malloc(sizeof(struct node));
        p->next=NULL;

        fscanf(fp,"%s %s",p->date,p->weather);
        fgetc(fp);
        fgets(p->content,1000,fp);

        p->next=r->next;
        r->next=p;
        r=p;

        fclose(fp);

    }

    return H;
}


int trave_dir(char* path,char filename[][LENTH])  //遍历文件夹,获取其中的所有txt文件(一篇日志一个txt文件)
{
     DIR *d; //声明一个句柄
     struct dirent *file; //readdir函数的返回值就存放在这个结构体中
     int len=0;
     int length;

     if(!(d = opendir(path)))
     {
         printf("error opendir: %s!!!\n",path);
         return -1;
     }

     while((file = readdir(d)) != NULL)
     {
         length=0;
         length=strlen(file->d_name);
         //把当前目录.,上一级目录..及隐藏文件都去掉,避免死循环遍历目录
         if(strncmp(file->d_name, ".", 1) == 0)
             continue;
         else if( length >=4 && file->d_name[length-1]=='t' && file->d_name[length-2]=='x' && file->d_name[length-3]=='t')
              strcpy(filename[len++], file->d_name); //若是txt文件则保存遍历到的文件名

     }

     closedir(d);

     return len;
}


void prtOrder(dairy *h)  //顺序查阅
{
    int chose,cnt=0;
    dairy *p=h->next;

    printf("您想从第几篇日志开始查看:");
    scanf("%d",&chose);

    if(p==NULL)
    {
       printf("\n用户未有过任何日记记录!\n\n");
       return ;
    }

    while(p)
    {
        cnt++;
        if(cnt>=chose)
        {
            printf("%s\t%s\n%s\n\n",p->weather,p->date,p->content);
        }

        p=p->next;
    }

    if(cnt<chose)
        printf("\n输入范围有误,您的日志总共只有%d篇\n\n",cnt);

}


void prtByDate(dairy *h)  //按指定日期查看日记
{
    dairy *p=h->next;
    char date[LENTH];
    int l,L,i=0,j=1;

    if(p==NULL)
    {
        printf("\n\n您未写过任何日志...\n\n");
        return ;
    }

    printf("输入查询日期(yyyy-MM-dd):");
    scanf("%s",date);

    while(1)
    {

        if(strlen(date)==10)
            break;
        else
        {
            printf("\n\n日期格式错误!请重新输入\n");
            system("PAUSE");
            system("CLS");
            printf("输入查询日期(yyyy-MM-dd):");
            scanf("%s",date);
        }
    }

    while(p)
    {
        L=strlen(p->date);
        l=strlen(date);
        for(i=l-1;i>=0;i--,j++)
        {
            if(p->date[L-j]!=date[i])
                break;
        }

        if(i<0)  //日期匹配成功,退出遍历
        {
            printf("%s %s\n%s\n",p->date,p->weather,p->content);
            break;
        }
        p=p->next;
    }

    if(p==NULL)
      printf("\n\n未找到该日期相关日记!\n\n");

}


void prtByKeyWord(dairy *h)
{
    char key[LENTH],*str;
    dairy *p=h->next;
    int flag=1;

    printf("请输入您的关键字:");
    gets(key);

    while(p)
    {
        str=strstr(p->content,key);
        if(str)
        {
            printf("%s %s\n%s\n",p->date,p->weather,p->content);
            flag=0;
        }
        p=p->next;
    }

    if(flag)
        printf("\n\n未找到关键字匹配的日志!...");
    printf("\n\n\n");
    system("PAUSE");
}


void modify_pwd(userInfo *curUser)   //密码修改
{
    char pwd[LENTH];
    userInfo *H=getUserInfo(),*p;

    printf("请输入新密码:");
    scanf("%s",pwd);

    p=H->next;
    while(p)
    {
        if(strcmp(p->userName,curUser->userName)==0)
        {
            strcpy(p->pwd,pwd);
            break;
        }
        p=p->next;
    }

    saveUserInfo(H);
    printf("\n修改密码成功!请重新登录...\n\n\n");
}

c/c++ | 阅读 875 次
文章评论,共3条
kkpearmoon
2013-01-21 22:48
1
运行不到,仍需努力
虚拟情缘
2013-04-10 20:59
2
这位大哥,你QQ多少??
虚拟情缘
2013-04-10 21:04
3
你的这个日记系统我没运行成功。提示这个#include <dirent.h>文件或者目录不存在,我QQ是1140614269,我想加你Q请教下
游客请输入验证码
浏览1809次
文章分类
文章归档