日记管理系统

/* 日记管理系统*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define ESC 27        //退出键
#define Enter 13      //回车键
#define BackSpace 8
/*定义日记结构体*/
typedef struct Link1
{   char date[10];          //日期    格式2009-12-30
    char title[40];         //标题
    char content[1000];     //日记内容
    char keyword[20];       //关键字,可用空格隔开
    int    tag;             //用来标记该日记是否满足查阅要求,是为0,否为-1,初始值为0;
    struct Link1 *nextd;
} Diary;
/*定义用户结构体*/
typedef struct Link2
{   char  username[25];       //用户名
    char  password[16];       //登录密码
    Diary *diarys_list;       //该用户拥有的日记链表
    struct Link2 *nextu;
} User;
/* 函数声明*/
   int Change_Password(User *U2);   /*更改用户密码*/
   int Create_NewUser(User *U1);   /*创建用户链表*/
   int Delete_Diary(Diary *D);    /*删除日记*/
   int Diary_Operation(User *U2,Diary *D1);  /*日记操作*/
   int Encrypt_Password(char password[]);/* 将登录口令加密*/
   int Enter_Password(char password[]);/* 获取登录口令*/
   User *Find_User(User *U1,char username[]);/*查找用户*/
   Diary *Init_Diary(Diary *D1);/*初始化日记表*/
   void Initial_Tag(Diary *D1,int tag);
   User *Init_User(User *U1);/*初始化用户表*/
   int Input_Choose();
   int Open_Diary(Diary *D1);
   Diary *Open_DiaryFile(Diary *D1,char FileName[]);/* 打开并导入日记文件*/
   int Printf_DiaryList(Diary *D1);/*显示日记列表*/
   void Print_Menu1(int *choose1);/*进入菜单*/
   void Print_Menu2(int *choose2);/*进入日记菜单*/
   void Print_Menu3(int *choose3);/*进入日记查阅菜单*/
   int Printf_users(User *U1);/*导入用户信息*/
   int Save_Diary(Diary *D1);/*保存日记*/
   int Save_Users_info(User *U1);/*保存用户信息*/
   int Search_Diary(Diary *D1);/*日记查询*/
   Diary *Filter_word(Diary *D1,char cmpword[],int n);/*按标题、日期、关键字查阅*/
   User *User_Login(User *U1);/* 登录个人用户*/
   int Write_Diary(Diary *D1);/* 写新日记*/

/* 更改用户密码*/
int Change_Password(User *U2)
{
      char oldpw[16],newpw1[16],newpw2[16];
     do
     {
        printf("\n\t请输入旧密码:\t");
        Enter_Password(oldpw);  //调用输入密码函数
        Encrypt_Password(oldpw);  //调用对密码加密函数
        if(strcmp(U2->password,oldpw)!=0)   //如果输入的密码与各个用户中的密码不相等
           printf("\n\t旧密码输入错误!....\n\n");
     }while(strcmp(U2->password,oldpw)!=0);//一直到输入的密码与用户密码相等的时候才退出循环
   do
      {
        printf("\n\t请输入新密码:\t");
        Enter_Password(newpw1);
        printf("\n\t请确认新密码:\t");
        Enter_Password(newpw2);
        if(strcmp(newpw1,newpw2)!=0)
           printf("\n\t密码确认错误!....\n\n");
      }while(strcmp(newpw1,newpw2)!=0);  //到输入的新密码与确认的新密码一样的时候退出循环
    Encrypt_Password(newpw1);   //对新密码进行加密
    strcpy(U2->password,newpw1);  //讲新密码复制给旧密码,就把旧密码覆盖掉了,密码就修改完毕
    printf("\n\n\t密码修改成功!......");
    return 1;
}
/* 创建用户链表*/
int Create_NewUser(User *U1)
{   int flag=0;
 char password2[16];
    User *s,*temp;
    Diary *D2=NULL;
    D2=Init_Diary(D2);
    do
    {
       s=(User *)malloc(sizeof(User));
        printf("\n\t请输入用户名:\t");
        scanf("%s",&s->username);
        temp=Find_User(U1,s->username);
      if(temp==NULL)
      {
        do
        {
        printf("\n\t请输入密码:\t");
        Enter_Password(s->password);
        printf("\n\t请确认密码:\t");
        Enter_Password(password2);
        if(strcmp(s->password,password2)!=0)
           printf("\n\t密码确认错误!....\n\n");
        }while(strcmp(s->password,password2)!=0);
        Encrypt_Password(s->password);
        s->diarys_list=D2;
        s->nextu=U1->nextu;
        U1->nextu=s;
        flag=0;
        printf("\n\t用户创建成功!......\n");
      }
      else
        {printf("\n\n\t***对不起**该用户名已存在!!!!\n");
         printf("\n\t是否继续创建用户:1/0\t");
         scanf("%d",&flag);
        }
    } while(flag);
    return 1;
}
/* 删除日记*/
int Delete_Diary(Diary *D)
{
      Diary *p;
      p=D->nextd;
      D->nextd=p->nextd;
      free(p);
      printf("\n\t日记删除成功......");
      return 1;
}
/* 日记操作*/
int Diary_Operation(User *U2,Diary *D1)
{ char tempch='1';
  int choose2;
  char FileName[40];
  Diary *p=NULL;
  while(tempch!=ESC)
  {
    printf("\n\t\t\t欢迎用户---“%s”的到来!......\n\n",U2->username);
    Print_Menu2(&choose2);
    switch(choose2)
      { case 0:return 0;
        case 1:{
              Write_Diary(D1);
              tempch=getch();  //用来每次做完一项指令后按回车之后返回选择操作
              }break;
        case 2:{
              printf("\n\t请输入文件路径及文件名:");
                 scanf("%s",FileName);
              p=Open_DiaryFile(D1,FileName); //调用该函数返回该文件名里面的内容并赋值给p
              if(p!=NULL)
                Open_Diary(p);   //如果该文件夹里面不为空,就调用打开日记函数,将文件夹里面的内容打印出来
              tempch=getch();
               };break;
        case 5:
              { printf("\n\n\t该用户有以下日记:\n\n");
                Printf_DiaryList(D1);
                tempch=getch();
              }break;
        case 4: {
                  if(Search_Diary(D1)==1)
                   tempch=getch();
                }break;    //打开帮助文件
        case 3: {
                  Change_Password(U2);
                  tempch=getch();
                }break;
      }
  }
   return 1;
}
/* 将登录口令加密*/
int Encrypt_Password(char password[])
{  int i=0;
 while(password[i]!='\0')
 {
  if((password[i]>='a'&&password[i]<='z')||(password[i]>='A'&&password[i]<='Z'))
  //当password[i]为a~z或A~Z时
  {
      password[i]=password[i]+4;  //将每个字符进行加4,
    if(password[i]>'Z'&&password[i]<='Z'+4||password[i]>'z')
         password[i]=password[i]-26;
  }
  else if(password[i]>='0'&&password[i]<='9')
   { password[i]=password[i]+4;
     if(password[i]>'9'&&password[i]<='9'+4)
       password[i]=password[i]-10;
   }
  else
     password[i]=password[i]+4;
  i++;
 }
 return 1;
}
/* 获取登录口令*/
int Enter_Password(char password[])
{ char password1[50];
  int i=0;
  while((password1[i]=getch())!=Enter)  //将手动输入的字符读入password1[i]中,Enter为终结符
      {
         password[i]=password1[i];       //将得到的password1中的数赋给password;
          password1[i]='*';
          printf("%c",password1[i]);   //打印出出*号,即将密码隐藏只打印出*号
          i++;
      }
      password[i]='\0';
      rewind(stdin);
  return 1;
}
/*查找用户*/
User *Find_User(User *U1,char username[])
{
    User *q;
    q=U1->nextu;
    while(q!=NULL&&strcmp(q->username,username)!=0)
        q=q->nextu;  //一直循环到q等于NULL或者找到与username一样的用户名的用户
    if(q!=NULL)
        return q;   //找到就返回该用户
    else
        return NULL;   //否则就返回NULL
}
/*初始化日记表*/
Diary *Init_Diary(Diary *D1)
{   D1=(Diary *)malloc(sizeof(Diary));  //为用户D1申请空间
        D1->nextd=NULL;
        return D1;
}
/* 将所有日记记录的标记tag值置为0或-1*/
void Initial_Tag(Diary *D1,int tag)
{
    Diary *q;
    q=D1->nextd;
    while(q!=NULL)
       {q->tag=tag;
        q=q->nextd;
       }
}
/*初始化用户表*/
User *Init_User(User *U1)
{   U1=(User *)malloc(sizeof(User));
    if(U1!=NULL)
    {   U1->nextu=NULL;
        return U1;
    }
    else
        return NULL;
}

/* 打开日记*/
int Open_Diary(Diary *D1)
{
     printf("\n\t--------------------------------------------------\n");
     printf("\n\t日记日期==>%s",D1->date);
     printf("\n\t日记标题==>%s",D1->title);
     printf("\n\n\t关键字==>%s",D1->keyword);
     printf("\n\t--------------------------------------------------\n");
     printf("\n\n\t日记正文如下:\n");
     printf("\n\t%s",D1->content);
  return 1;
}
/* 打开并导入日记文件*/
Diary *Open_DiaryFile(Diary *D1,char FileName[])
{FILE *fp;
 Diary *p=NULL;
 if((fp=fopen(FileName,"r"))==NULL)  //将文件名为FileName的文件赋给文件名fp并打开
    {
      printf("\t文件打开失败!");
      return NULL;
    }
  else
  {
       p=(Diary *)malloc(sizeof(Diary));

      fscanf(fp,"\t日期: %s\n",p->date);   //将文件fp中的内容赋给p
      fscanf(fp,"\t标题: %s\n",p->title);
      fscanf(fp,"\t关键字: %s\n",p->keyword);
      fscanf(fp,"\t正文如下:\n\t%s\n",p->content);
      p->tag=0;
       p->nextd=D1->nextd;   //将p接到D1后面,后插法
       D1->nextd=p;
       fclose(fp);  //对于文件的操作,最后都要对打开的文件进行关闭操作
     return p;  //返回p
  }
 }
/*导入用户信息*/
int Open_Users_info(User *U1)
{
 FILE *fp;
 User *U2=NULL,*U3=NULL;
 Diary *D1=NULL,*D2=NULL;
 char filepn[20],tempstr[80];/*用来存放文件保存路径以及文件名,tempstr是用来保存文件的保存路径*/
  strcpy(filepn,"Users_info.txt");//只是为了省去写文件名的麻烦而用filepn来替换Users_info.txt
  if((fp=fopen(filepn,"r"))==NULL)
    {
     printf("不能打开文件!\n");
     return 0;
    }
  else
    {  fscanf(fp,"%s\n",tempstr); //将文件fp写入字符型tempstr中,这样才可以分别与字符串“User”和"Diary"进行复制
       while(strcmp(tempstr,"User:")==0&&(!feof(fp)))//循环用来一条一条的读出文件中的每一个用户以及用户的信息
         {
           U2=(User *)malloc(sizeof(User));//申请一个空间用来存放每一条的用户信息
               fscanf(fp,"用户名:%80s\n",U2->username);//将文件中的每一个用户名写到U2->username中
               fscanf(fp,"密码:%80s\n",U2->password);
               fscanf(fp,"%80s",tempstr);
               D1=(Diary *)malloc(sizeof(Diary));
               D1=Init_Diary(D1);
               U2->diarys_list=D1;
               while(strcmp(tempstr,"Diary:")==0&&(!feof(fp)))//将属于该用户的日记信息一条一条的全部读出
                  {
                    D2=(Diary *)malloc(sizeof(Diary));

                     fscanf(fp,"\t日期: %s\n",D2->date);
                     fscanf(fp,"\t关键字: %s\n",D2->keyword);
                     fscanf(fp,"\t标题: %s\n",D2->title);
                     fscanf(fp,"\t日记内容: \n%s\n\n",D2->content);
                     fscanf(fp,"%s\n",tempstr);
                     D2->tag=0;
                     D2->nextd=D1->nextd;
                     D1->nextd=D2;
                         //D2->nextd=NULL;  //由前往后进行导入
                         //D1->nextd=D2;
                         //D1=D1->nextd;
                }

               U2->nextu=U1->nextu;
               U1->nextu=U2;//跟上面的插入方法是一样的
             }
           fscanf(fp,"\n\n");
           }
  fclose(fp);
  return 0;
}
/*显示日记列表*/
int Printf_DiaryList(Diary *D1)
{  int count=0;
   Diary *D;
   D=D1;
   printf("\n");
   if(D->nextd==NULL)
      printf("\t没有日记信息!.....\n");
   else
      {  while(D->nextd!=NULL)
         { D=D->nextd;
          if(D->tag==0)
          {
          count++;
          printf("\t第%2d篇==>时间:%-10s   日记标题:%-20s  \t关键字:%-s\n",count,D->date,D->title);
          printf("\t关键字:%-s\n",D->keyword);
          }
         }
      }
    return count;
}
/*进入菜单并选择操作*/
 void Print_Menu1(int *choose1)
{
 printf("\n\t\t           “日记管理系统”---(0退出)\n");
    printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
    printf("\t| 1.用户登录  2.新建用户  3.显示用户列表          |\n");
    printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
   do {
   printf("\n\t请选择操作:\t");
     scanf("%d",choose1);
     }while(*choose1<0||*choose1>3);
    rewind(stdin);         /*清空键盘缓冲区里的无用字符*/
}
/*进入日记菜单并选择操作*/
 void Print_Menu2(int *choose2)
{
 printf("\n\t\t\t           “日记操作”---------(0返回上级)\n");
    printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
    printf("\t|  1.写日记  2.导入日记  3.更改用户密码     4.查阅与删除|\n");
    printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
   do {
   printf("\n\t请选择操作:\t");
    scanf("%d",choose2);
     }while(*choose2<0||*choose2>5);
    rewind(stdin);         /*清空键盘缓冲区里的无用字符*/
}
/*进入日记查阅菜单*/
 void Print_Menu3(int *choose3)
{
 printf("\n\t\t                      “日记查阅与删除”-----(0返回上级)\n");
    printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
    printf("\t|   1.日记列表   2.按标题查阅  3.按日期查阅    4.按关键字查阅  |\n");
    printf("\t|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n");
   do {
   printf("\n\t请选择操作:\t");
     scanf("%d",choose3);
     }while(*choose3<0||*choose3>5);
    rewind(stdin);         /*清空键盘缓冲区里的无用字符*/
}
/* 显示用户列表*/
int Printf_users(User *U1)
{  int count=0;
   User *p;
   p=U1;
   printf("\n\n\t用户信息列表如下:\n\n");
   if(U1->nextu==NULL)
      printf("\n\t没有用户信息!.....\n");
   else
      {  while(p->nextu!=NULL)
         { p=p->nextu;
          count++;
          printf("\t第%d个用户==>用户名:%s\n",count,p->username);
         }
      }
    return 1;
}
/* 日记保存*/
int Save_Diary(Diary *D1)
{FILE *fp;
 char FileName[40],postfix[6]=".txt\0";
 printf("\t请输入文件路径及文件名:");
 scanf("%s",FileName);
 if((fp=fopen(FileName,"w+"))==NULL)
    {
      printf("\t打开文件失败!");
      return 0;
    }
  fprintf(fp,"\t日期: %s\n",D1->date);
  fprintf(fp,"\t标题: %s\n",D1->title);
  fprintf(fp,"\t关键字: %s\n",D1->keyword);
  fprintf(fp,"\t正文如下:\n\t%s\n",D1->content);
  fclose(fp);
  printf("\t日记文件保存成功!");
  return 1;
}
/* 保存用户信息*/
int Save_Users_info(User *U1)
{
  FILE *fp;
  Diary *D1;
  int temp;
  char filepn[20];/*用来存放文件保存路径以及文件名*/
  printf("\n\n\t--------------------------------------------------\n");
  printf("\t是否保存本次使用所做的所有操作?...\t1-是  0--否\t");
  printf("\n\t--------------------------------------------------\n\t");
  printf("请选择操作:\t");
  scanf("%d",&temp);
  if(temp==1)
  {
  strcpy(filepn,"Users_info.txt");
  if((fp=fopen(filepn,"w+"))==NULL)
    {
     printf("不能打开文件!\n");
     return 0;
    }
  while(U1->nextu!=NULL)
    { fprintf(fp,"User:\n");
      U1=U1->nextu;/*下移一个结点*/
      fprintf(fp,"用户名:%s\n密码:%s\n",U1->username,U1->password);
      D1=U1->diarys_list;
      while(D1->nextd!=NULL)
         {
          D1=D1->nextd;
          fprintf(fp,"Diary:\n");
          fprintf(fp,"\t日期:%s\n\t关键字: %s\n",D1->date,D1->keyword);
            fprintf(fp,"\t标题:%s\n",D1->title);
            fprintf(fp,"\t日记内容:\n%s\n",D1->content);
         }
    }
  fclose(fp);
  }
  return 0;
}
/* 日记查阅*/
int Search_Diary(Diary *D1)
{    char tempch='1';
     char cmpword[20];
  int choose3;
  Diary *p;
  while(tempch!=ESC)
   {
     Print_Menu3(&choose3);
	 if(choose3==0)
	 {
		 return 0;
	 }
     p=D1;
     if(p->nextd==NULL)
        printf("\n\t没有日记......\n");
     else
     {
        switch(choose3)
         { case 0: return 0;
           case 1:
                  { printf("\n\t该用户有以下日记:\n\n");
                    Printf_DiaryList(D1);
                   }break;
           //case 2:Search_order(D1);break;
           case 2:{
                    printf("\n\t请输入要查阅的日记标题:\t");
                 scanf("%s",cmpword);
                 Filter_word(D1,cmpword,1);
                  }break;     //按标题查阅
           case 3:{ printf("\n\t请输入要查阅的日记日期(2009-4-11):\t");
                 scanf("%s",cmpword);
                    Filter_word(D1,cmpword,2);
                  } break;     //按日期查阅
           case 4:{ printf("\n\t请输入要查阅的关键字(如:想你):\t");
                 scanf("%s",cmpword);
                    Filter_word(D1,cmpword,3);
           }break;     //按关键字查阅
         }

     }
    Initial_Tag(D1,0);
    tempch=getch();
    rewind(stdin);                    /*清空键盘缓冲区里的无用字符*/
   }
 return 1;
}

/*按标题、日期、关键字查阅*/
Diary *Filter_word(Diary *D1,char cmpword[],int n)    //采用的是筛选算法
{   char *temp=NULL;
 Diary *D,*D2;
 Initial_Tag(D1,0);   //将每一篇日记都编辑成可查找状态
    D=D1;
    while(D->nextd!=NULL)  //对没有都进行比较
        {   if(n==1)
              temp=strstr(D->nextd->title,cmpword);  //strstr函数是对D->nextd->title和cmpword进行模糊比较,并将找到的赋给指针temp
            else if(n==2)
              temp=strstr(D->nextd->date,cmpword);
            else if(n==3)
              temp=strstr(D->nextd->keyword,cmpword);
            if(temp==NULL)  //如果找不到相似的标题,日期,关键字,就将日记编辑成不可查找状态
               D->nextd->tag=-1;
            D=D->nextd; //比较下一个
         }
    printf("\n\n\t有以下的日记符合查阅要求:\n");
    //D2=Search_order(D1);
 Initial_Tag(D1,0);
 return D1;
}
/* 登录个人用户*/
User *User_Login(User *U1)    //登录成功返回改用户的日记链表指针
{
     int i=0,temp;
   User *s=NULL,*tempU=NULL;
   printf("\n\t--------------------------------------------------\n");
   printf("\t是否要显示用户列表?\t1-是  0--否\t");
   printf("\n\t--------------------------------------------------\n\t");
   scanf("%d",&temp);
   if(temp==1)
         Printf_users(U1);
   s=Init_User(s);
   tempU=Init_User(tempU);
   printf("\t请输入用户名:\t");
   scanf("%s",&s->username);
   tempU=Find_User(U1,s->username);
   if(tempU==NULL)
     {printf("\n\t该用户不存在!......\n");
      getch();
     }
   else
     {
       printf("\t请输入密码:\t");
       rewind(stdin);
       Enter_Password(s->password);
         Encrypt_Password(s->password);
        if(strcmp(tempU->password,s->password)==0)
           return tempU;
        else
           printf("\n\t密码错误!.....\n\n");
           getch();
     }
    return NULL;
}
/* 写新的日记*/
int Write_Diary(Diary *D1)
{  int temp;
    Diary *p=NULL;
    p=Init_Diary(p);
    p=(Diary *)malloc(sizeof(Diary));
    printf("\n\n\t请输入日期(2008-12-31):\t");
    scanf("%s",&p->date);
    printf("\n\t请输入标题:\t");
    scanf("%s",&p->title);
    printf("\n\t请开始写日记:......\n\n\t");
    scanf("%s",&p->content);
    printf("\n\t请为该日记写关键字:\t");
    scanf("%s",&p->keyword);
    p->tag=0;
    p->nextd=D1->nextd;
    D1->nextd=p;
    printf("\n\t--------------------------------------------------\n");
    printf("\t是否要以文件方式保存日记?\t1-是  0--否\t");
    printf("\n\t--------------------------------------------------\n\t");
    scanf("%d",&temp);
    if(temp==1)
         Save_Diary(p);
 return 1;
}

int main()
{   User *U1=NULL;
 User *U2=NULL;
 Diary *D1=NULL;
 int choose1;
 char tempch='1',FileName[20];
    U1=Init_User(U1);
    D1=Init_Diary(D1);
    Open_Users_info(U1);
    while(tempch!=ESC)
   {
      Print_Menu1(&choose1);            //输出菜单,并输入choose的值
      switch(choose1)
      {
	  case 0:
		  Save_Users_info(U1);
		  return 0;
		  break;
      case 1:
         {
             U2=User_Login(U1);
          if(U2!=NULL)
          {
            Diary_Operation(U2,D1);
               tempch=getch();
          }
         }break;
      case 2: { Create_NewUser(U1);
               tempch=getch();
               }break;
      case 3: { Printf_users(U1);
                tempch=getch();
               }break;

      }

   }
    Save_Users_info(U1);
    system("PAUSE");
    return 1;
}

猜你喜欢

转载自blog.csdn.net/u011256974/article/details/89212447