日记管理系统

by admin on 2020年1月22日

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TRUE 1
#define FLASE -1
typedef struct student
{
 char name[10];
 char sex[10];
 char Class[10];
 float mark;
 int age;
 struct student *prev;
 struct student *next;
}node;/*定义结构体*/
char strbuf[40];
char strmark[10];
FILE *fp;/*定义只想文件的指针*/
//将链表中的学生信息导入到文本当中
void save(node *h)
{
 char
strbuf[40];//定义一个字符串来储存链表学生信息中的姓名,性别,班级。
    char strmark[10];//用来储存链表中的学生的年龄,成绩。
 if((fp=fopen(“D:\student.txt”,”wb”))==NULL)//判断是否打开文本。!!!!注意你要储存信息的文本最好添加在除C盘之外的地方
 {
  printf(“不能打开该文件”);
 }
 node *p=h;
 while(p)//通过判断p是否为空
 {
  strcpy(strbuf,p->name);//将链表中的名字赋值给字符串。
  strcat(strbuf,”.”);
  strcat(strbuf,p->sex );
  strcat(strbuf,”.”);
  strcat(strbuf,p->Class );
  strcat(strbuf,”.”);
  itoa(p->age ,strmark,10);//将int类型装换为字符串类型
  strcat(strbuf,strmark);
  strcat(strbuf,”.”);
  itoa(p->mark ,strmark,10);
  strcat(strbuf,strmark);
  fwrite(strbuf,1,strlen(strbuf),fp);//写入到文本当中
  fwrite(“rn”,1,strlen(“rn”),fp);//换行
  p=p->next ;
 }
  fclose(fp); //一定要关闭文本否则将保存失败
  printf(“保存成功n”);
}
//打印链表中的信息
澳门新葡亰平台官网,void print(node *h)
{
 if (h == NULL)
 {
  return ;
 }
 else
 {
  node *p = h;
  while (p)
  {
   printf(“姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0fn”,
p->name, p->age,p->sex,p->Class ,p->mark );
   p = p->next;
  }
  printf(“n”);
 }
}
//用来将文本中的信息重新插入到链表当中
node *creat3(node *h,char strstuname[10],char sex[10],char
class1[10] ,int age,float mark)
{
 node *p = (node *)malloc(sizeof(node));
 memset(p, 0, sizeof(node));
 strcpy(p->name ,strstuname);
 strcpy(p->sex ,sex);
 strcpy(p->Class ,class1);
 p->age=age;
 p->mark =mark;
 p->next = NULL;
 p->prev = NULL;
 if (h == NULL)
  {
   h = p;
  }
   else
  {
   p->next = h;
   h->prev = p;
   h = p;
  }
  /*printf(“姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%fn”,
p->name, p->age,p->sex,p->Class ,p->mark );*/
 /* return h; */
}
//将文本中的信息添加到链表当中
node* load(node *h)
{
 char strbuf[50]={0};
    char strstuname[10]={0};
    char strstusex[10]={0};//为了来储存文本中的信息。
    char strstuage[10]={0};
    char strstuclass[10]={0};
    char strstumark[10]={0};
if((fp=fopen(“D:\student.txt”,”rb”))==NULL)
 {
  printf(“不能打开该文件”);
  exit(0);
 } 
 node *p=h;
 int ncount=0;
 int j=0;
 while(NULL !=fgets(strbuf,50,fp))
 {
  int i=0;
  ncount=0;
  j=0;
  
  for(i=0;strbuf[i]!=’r’;i++)
  {
   if(0==ncount)
   {
    strstuname[i]=strbuf[i];
    if(strbuf[i]==’.’)
    {
     strstuname[i]=’’;
     ncount++;
    }
   }
   else if(1==ncount)
   { 
    strstusex[j]=strbuf[i];
    j++;
    if(strbuf[i]==’.’)
    {
     strstusex[j-1]=’’;
     ncount++;
     j=0;     
    }
    
   }
   else if(2==ncount)
   {
    strstuclass[j]=strbuf[i];
    j++;
    if(strbuf[i]==’.’)
    {
     strstuclass[j-1]=’’;
     ncount++;
     j=0;
    }
   }
   else if(3==ncount)
   {
    
    strstuage[j]=strbuf[i];
    j++;
    if(strbuf[i]==’.’)
    {
     strstuage[j-1]=’’;
     ncount++;
     j=0;
    }    
   }
   else
   {
    strstumark[j]=strbuf[i];
    j++;
   }
  }
  h=creat3(h,strstuname,strstusex,strstuclass,atoi(strstuage),atoi(strstumark));//调用前面的插入函数来插入到链表当中
 }
  printf(“导入成功n”);
 fclose(fp);//!!!!一定要记得关闭文本
return h;
}
//从头部插入
node  *creat(node *h)

 while (1)
 {
   node *p = (node *)malloc(sizeof(node));
   if (p == NULL)
    return h;
    memset(p, 0, sizeof(node)); 
    printf(“请输入学生的年龄:”);
    int age;
    scanf(“%d”,&age );
    if (age==0)
   {
    break;
   }
   p->age =age;
    printf(“请输入学生的名字:”);
    scanf(“%s”,&p->name);
    printf(“请输入学生的性别:”);
    scanf(“%s”,p->sex );
    printf(“请输入学生的班级:”);
    scanf(“%s”,&p->Class );
    printf(“请输入学生的成绩”);
    scanf(“%f”,&p->mark ) ;
   p->next = NULL;
   p->prev = NULL;
   if (h == NULL)
   {
    h = p;
   }
   else
   {
    p->next = h;
    h->prev = p;
    h = p;
   }  
 }
 return h;
}
//从尾部插入
node *creat1(node *h)
{
 while (1)
 {
   node *p = (node *)malloc(sizeof(node));
   if (p == NULL)
    return h;
   memset(p, 0, sizeof(node));
    printf(“请输入学生的年龄:”);
    int age;
    scanf(“%d”,&age );
    if (age==0)
   {
    break;
   }
   p->age =age;
    printf(“请输入学生的名字:”);
    scanf(“%s”,&p->name);
    printf(“请输入学生的性别:”);
    scanf(“%s”,p->sex );
    printf(“请输入学生的班级:”);
    scanf(“%s”,&p->Class );
    printf(“请输入学生的成绩”);
    scanf(“%f”,&p->mark ) ;
   p->next = NULL;
   p->prev = NULL;
   if (h == NULL)
   {
    h = p;
   }
   else
   {
    node *q = h;
    while (q->next)
    {
     q = q->next;
    }
    q->next = p;
    p->prev = q;
   }
 }
 return h;
}
node* sort(node *h)
{
  int temp;
 if(h==NULL)
 {
  return h;
 }
  else if(h->next==NULL)
 {
  return h;
 }
 else
 {
  node *p=h;
  node *i;
  node *q;
  /*node *p;*/
  while(p)
  {
   for(i=p;i->next!=NULL;i=i->next)
   {
    q=i->next ;
    if(i->age<q->age )
    {
     temp=q->age ;
     q->age =i->age ;
        i->age=temp;  
    }
   }
   p=p->next;
  }
  return h;
 }
}
//从尾部打印
void Lastprint(node *h)
{
 if(h==NULL)
 {
     return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
  printf(“姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0fn”,
p->name, p->age,p->sex,p->Class ,p->mark );
  p=p->prev ; 
  }
  printf(“n”);
 }
}
//通过成绩查询
void select1(node *h,float mark)
{
 if(h==NULL)
 {
  return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
   if(p->mark==mark)
   {
       printf(“姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:.0%fn”,
p->name, p->age,p->sex,p->Class ,p->mark );
      }
   p=p->next ;
  }
  printf(“n”);
 }
}
//通过姓名修改
void update1(node *h,char name[10])
{
 float mark;
 if(h==NULL)
 {
  return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
   if(strcmp(p->name ,name)==0)
   {
    printf(“请输入要修改的成绩”);
    scanf(“%f”,&mark);
       p->mark =mark;
    printf(“姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0fn”,
p->name, p->age,p->sex,p->Class ,p->mark );
   }
   p=p->next;
  }
 }
}
node *Sort(node *h)
{
 float temp;
 if(h==NULL)
 {
  return h;
 }
  else if(h->next==NULL)
 {
  return h;
 }
 else
 {
  node *p=h;
  node *i;
  node *q;
  /*node *p;*/
  while(p)
  {
   for(i=p;i->next!=NULL;i=i->next)
   {
    q=i->next ;
    if(i->mark<q->mark )
    {
     temp=q->mark ;
     q->mark  =i->mark;
        i->mark=temp;  
    }
   }
   p=p->next;
       }
 }
}
//排序之后的成绩打印
void SortMark(node *h)
{
  if (h == NULL)
 {
  return ;
 }
 else
 {
  node *p = h;
  while (p)
  {
   printf(“%fn”,p->mark );
   p = p->next;
  }
  printf(“n”);
 }
}
//通过姓名查找
void select2(node *h,char name[10])
{
 if(h==NULL)
 {
  return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
   if(strcmp(p->name ,name)==0)
   {
     printf(“姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0fn”,
p->name, p->age,p->sex,p->Class ,p->mark );
   }
   p=p->next ;
  }
  printf(“n”);
   printf(“查找无此人”);
 }
}
//销毁链表
void destroy(node *h)
{
 if (h == NULL)
 {
  return;
 }
 else
 {
  node *p=h;
  while (p)
  {
   free(p);
   p = p->next;
  }
 }
}
//通过姓名来删除链表中的信息
int delete1(node **h, char name[10])
{
 if (*h == NULL)
 {
  return -1 ;
 }
 else
 {
  node *p = *h;
  while (p)
  {
   if (strcmp(p->name,name)==0)
   {
    if (p ==*h)
    {
     *h =(*h)->next;
     (*h)->prev = NULL;
     free(p);
    }
    else if (p->next == NULL)
    {
     p->prev->next = NULL;
     free(p);
    }
    else
    {
     p->prev->next = p->next;
     p->next->prev = p->prev;
     free(p);
    }
   }
   p = p->next;
  }
  return -1;
 }
}
int main(int argc , char* argv[])
{
node *h=NULL; 
 int temp;
 int da=1;
 printf(”                      
**************************  开始
学生成绩管理系统  
***********************n”);
 printf(”                      
************************** (1)
从链表的头部插入  
***********************n”);
 printf(”                      
************************** (2)
从链表的尾部插入  
***********************n”);
 printf(”                      
************************** (3)
打印链表中的信息  
***********************n”);
 printf(”                      
************************** (4)
通过姓名查找学生的信息并打印**********************n”);
 printf(”                      
************************** (5)
通过姓名修改学生的成绩:***********************n”);
 printf(”                      
************************** (6)
通过姓名删除学生的信息:***********************n”);
 printf(”                      
************************** (7)
成绩从大到小的排序:***********************n”);
 printf(”                      
************************** (8)      
写入到文本      
:***********************n”);
 printf(”                      
************************** (9)      
从文本中读出      
:***********************n”);
 printf(”                      
************************** (10)      
退出程序       :***********************n”);
 while(da)
 {
 printf(“请输入数字来选择功能n”);
 scanf(“%d”,&temp);
 switch(temp)
 {
  case 1: h=creat(h);
  break; 
  case 2: creat1(h);
  break;  
  case 3:  Print(h);
  break;
  case 4: char name [10];
 printf(“请输入要查询的名字”);
 scanf(“%s”,&name);
 select2(h,name);
 break;
 case 5:
  printf(“请输入要修改的学生成绩的名字”);
 char name1 [10];
 scanf(“%s”,&name1);
 update1(h,name1);
 Print(h);
 break ;
 case 6:
 printf(“请输入要删除学生的信息”);
  char name2[10];
  scanf(“%s”, &name2);
  delete1(&h, name2);
  Print(h);
  break ;
  case 7:
  Sort(h);
  SortMark(h);
  break;
  case 8:
   save(h);
   break;
   case 9:
    h=load(h);
    break;
  case 10:
   da=0;
   break;
 }
}
}

给需要答辩的友友小小参考

typedef struct information
{
int numb;
char starttime[10];
char begin[21];
char end[21];
float lasttime;
int canload;
int alreadyload;
struct information *next;
}INFORMATION,*PINF;             //结构体类型的指针

澳门新葡亰平台官网 1澳门新葡亰平台官网 2

 

INFORMATION *head;             //建立班次头结点
INFORMATION *rear;             //建立班次尾结点

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() //主程序
{
system(“color 1f”);
//system(“mode con: cols=140
lines=130”);

#define N 50
#define LENTH 20

head=rear=(INFORMATION *)malloc(sizeof(INFORMATION));
head->next=NULL;
//函数声明        
void Read_In();        
//文件读入
void Scan();            
//浏览全部信息
void Lookup();          //查询信息

typedef struct book{
    int numBook ;   //图书检索号
    char
name[LENTH],author[LENTH],releaseOrg[LENTH],ISBNnumber[17];   
//书名,作者,出版社,ISBN号
    float price;
    struct book *next;
}books;

void Buyticket();       //预定车票

int countNumber(int choose,books* H);   
//统计指定作者或出版商或ISBN号一样的图书数量
int checkISBN(char ISBN[]);         
//检测ISBN的正确性,对返回1,错的返回0
int checkNumbook(int num_book,books* H);       
//检查图书编号准确性,对返回1,错的返回0
int displayAllBookInfo(books* H);    //显示全部图书信
int enterUI(char passWard[]);   //用户登录
books* createBookInfoSystem();       //数据存储读取操作
books* createBookInfoByFile(FILE *fp) ;        //文件读取数据
books* createBookInfo();    
//用头、尾插法链表创建图书系统图书信息,达到边排序边插入效果
void IU();       //系统界面设计
void SysLoad();       //系统启动
void QueryBySpecialCondition();     
//用于IU界面第四个功能模块的内部模块,查询指定条件图书信息
void displayBookInfoByBook_number(int numbook,books* H);  
//根据图书编号显示图书信息
void deleteBookInfo(books* H);       //根据图书编号删除图书信息
void modifiedByBook_number(books *H);     
//根据图书编号修改图书信息
void saveToFile(books *H);     //将数据保存至文件中
void addBookInfo(books* H);        //增加图书信息
void Passward(char str[]) ;  //初始登录密码
void resetPassward() ;  //修改密码
void sortBySpacialCondition();  //根据不同条件进行排序
void sort(books *H,int sortType,char sortBy);  
//排序通用模块,sortType表示排序方式:1为升序,2为降序排序。sortBy为排序的依据

void Cancelticket();    //退订车票

int main()
{
    char passWard[LENTH];

void
Save_In();            //把数据重新保存到文件中

    Passward(passWard); //获取密码,默认为000000

Read_In();     //读入信息

    if(enterUI(passWard))
    {
       system(“CLS”);
       printf(“ntt登录成功!欢迎使用图书管理系统!nnn”);
       system(“PAUSE”);
       SysLoad();
       system(“PAUSE”);
       return 0;
    }

int i=0;
while(i!=5)
{
    printf(“n”);
    printf(“==========================n”);
    printf(”       车票管理系统n”);
    printf(”       制作:朱俊峰、郎江涛n”);
    printf(”       日期:2010/6/21n”);
    printf(“==========================nnn”);
    printf(“=======车站管理系统=======n”);   //总菜单

    system(“CLS”);
    printf(“nt登录失败!系统退出!n”);
    return 0;
}

    printf(” 1、    浏览信息         n”);
    printf(” 2、    查询信息         n”);
    printf(” 3、    预订车票         n”);
    printf(” 4、    退订车票         n”);
    printf(” 5、    退出系统         n”);
    printf(“==========================n”);
    
    printf(“请选择你所需要的功能:”);
    scanf(“%d”,&i);
    switch(i)
    {  
      
      case 1:Scan();break;
      case 2:Lookup();break;
      case 3:Buyticket();break;
      case 4:Cancelticket();break;
      case 5:break;
      default:printf(“输入错误,请确保你的输入为1–5.n”);break;
    }
}   //while (i!=5)

void IU()   //系统界面设计
{
    system(“CLS”);
    printf(“tt            
****************             n”);
    printf(“tt              ** 欢迎使用 **              n”);
    printf(“tt            
****************             n”);
    printf(“tt                                          n”);
    printf(“tt                                          n”);
    printf(“ttAuthor:tt薛云腾                         n”);
    printf(“ttClass:tt计算机二班                      n”);
    printf(“ttStuPayroll:t3110307212                   n”);
    printf(“tt                                          n”);
    printf(“tt                                          n”);
    printf(“tt======= *** ===图书管理系统=== ***
=======n”);
    printf(“tt==                                      ==n”);
    printf(“tt==                                      ==n”);
    printf(“tt==     1、查看所有图书信息;            ==n”);
    printf(“tt==     2、增加图书信息;                ==n”);
    printf(“tt==     3、删除图书信息;                ==n”);
    printf(“tt==     4、查询相关图书信息;            ==n”);    
//本查询功能还包括统计功能
    printf(“tt==     5、修改图书信息;                ==n”);
    printf(“tt==     6、修改的登录密码;              ==n”);
    printf(“tt==     7、通用排序设计模块;            ==n”);
    printf(“tt==     q/Q、退出;                      ==n”);
    printf(“tt==                                      ==n”);
   
printf(“tt=======****************************=======nnn”);
    printf(“您的选择:”);
}

Save_In();    //保存信息

void SysLoad()     //系统启动
{
    books* H=NULL;
    H=createBookInfoSystem();
    char choose;

return 0;
}

    while(1)
    {
        IU();
        scanf(“%c”,&choose);
        getchar();

void Read_In()       //班次信息文件读入
{
     FILE *fp;
     if((fp=fopen(“information.txt”,”r+”))==0)
     {
        printf(“n打开文件错误!n”);
        exit(0);
     }
     while(!feof(fp))      //读取文件并测试是否抵达文件尾
     {
       INFORMATION *p;
       p=(INFORMATION *)malloc(sizeof(INFORMATION)); //开辟内存空间
      
       fscanf(fp,”%d%s%s%s%f%d%dn”,&p->numb,&p->starttime,&p->begin,&p->end,&p->lasttime,&p->canload,&p->alreadyload);
      
       rear->next=p;      //在INFORMATION单链表中依次插入下一结点

        switch (choose)
        {
           case ‘1’:
               displayAllBookInfo(H);
               printf(“nnn”);
               system(“PAUSE”);   //按任意键继续
               break;

       rear=p;
       rear->next=NULL;    
     }
     fclose(fp);
    
}

           case ‘2’:
               addBookInfo(H);
               break;

void Save_In()     //保存数据

           case ‘3’:
               deleteBookInfo(H);
               break;

{
     FILE *fp;
     if((fp=fp=fopen(“information.txt”, “r+”))==0)
     {
        printf(“n打开文件错误!n”);
        exit(0);
     }
     PINF p=head->next;  
     while(p!=NULL)
     {
       fprintf(fp,”%dt%st%st%st%.2ft%dt%dn”,p->numb,p->starttime,p->begin,p->end,p->lasttime,p->canload,p->alreadyload);
       p=p->next;
     }
     fclose(fp);
}

           case ‘4’:
               QueryBySpecialCondition();
               break;

void Scan()
{
     PINF p=head->next;
     printf(“n各班次信息如下:n”);
     printf(“n班次号t出发时间t起点t终点t行车时间    额定载客量    已订票人数n”);
     printf(“____________________________________________________________________________n”);
     while(p!=NULL)
     {
         printf(“%dt%stt%st%st%.2ftt%dtt%dn”,p->numb,p->starttime,p->begin,p->end,p->lasttime,p->canload,p->alreadyload);
         p=p->next;
     }
     printf(“____________________________________________________________________________n”);
}

           case ‘5’:
               modifiedByBook_number(H);
               break;

void Lookup()       //查询班次信息
{    
     system(“color 1f”);
     //system(“mode con: cols=140
lines=130”);
     void SearchbyNum();  
//函数声明
     void SearchbyEnd();  
//函数声明
     int i,j=0;
     while(j!=1)
     {
        printf(“n”);        
        printf(”       查询子菜单n”);
        printf(“==========================n”);
        printf(”     1.按班次号查询tn”);
        printf(”     2.按终点站查询tn”);
        printf(“==========================n”);
        printf(“请选择查询方式:”);
        scanf(“%d”,
&i);
        switch(i)
        {
           case 1: SearchbyNum(); break;
           case 2: SearchbyEnd(); break;
           default : printf(“n输入错误,请确保你的输入为1–2.n”);
                     break;
        }
        printf(“==========================n”);
        printf(“n1.返回主菜单 2.继续查询 : “);
        scanf(“%d”,&j);
     }
}

           case ‘7’:
               sortBySpacialCondition();
               break;

void SearchbyNum()   //按班次号查询班次
{  
    system(“color 1f”);
   //
system(“mode con: cols=140 lines=130”);
    int GetHour(char *s);
    int GetMinute(char *s);
    
    PINF p,q;   //定义两个结点
    int num;   //要查询的班次号
    int j=0;     //判断班次          
    p=head->next;
    printf(“n请输入需要查询的班次号: “);
    scanf(“%d”,&num);
    while(p!=NULL)
    {
        if(p->numb-num==0)
        {  
            q=p;
            printf(“n”);
            printf(“班次号: t”);printf(“%dn”,q->numb);
            printf(“出发时间:t”);printf(“%sn”,q->starttime);
            printf(“起点: tt”);printf(“%sn”,q->begin);
            printf(“终点: tt”);printf(“%sn”,q->end);
            printf(“行车时间: t”);printf(“%.2fn”,q->lasttime);
            printf(“额定载客量: t”);printf(“%dn”,q->canload);
            printf(“已定票人数: t”);printf(“%dn”,q->alreadyload);
            
            time_t t;
            struct tm *timeinfo;
            time(&t);
            timeinfo = localtime(&t);
            
            if(GetHour(q->starttime)<timeinfo->tm_hour)
               printf(“n此班次车已发出!n”);
            else
               if(GetHour(q->starttime) ==
timeinfo->tm_hour)
               {
                  if(GetMinute(q->starttime)<=timeinfo->tm_min)
                     printf(“n此班次车已发出!n”);
                  else
                     printf(“n此班次车尚未发出!n”);
               }
               else
                  printf(“n此班次车尚未发出!n”);
            j++;
        }
        p=p->next;    
    }     //while(p!=NULL)
    if(j==0)
       printf(“n对不起,不存在此班次!n”);
}

           case ‘6’:
               resetPassward();
               system(“CLS”);
              
printf(“n密码已被修改!请重新登陆…n系统退出…nnn”);
               exit(0);

void SearchbyEnd()    //按终点站查询班次
{
     system(“color 1f”);
     //system(“mode con: cols=140
lines=130”);
     int GetHour(char *s);
     int GetMinute(char *s);
    
     PINF p,q;   //定义两个结点
     char end[21];   //要查询的终点站名
     int j=0;              
     p=head->next;
     printf(“n请输入需要查询的终点站名: “);
     scanf(“%s”,end);
     while(p!=NULL)
     {
        if(strcmp(p->end,end)==0)
        {  
            q=p;
            printf(“n”);
            printf(“班次号: t”);printf(“%dn”,q->numb);
            printf(“出发时间:t”);printf(“%sn”,q->starttime);
            printf(“起点: tt”);printf(“%sn”,q->begin);
            printf(“终点: tt”);printf(“%sn”,q->end);
            printf(“行车时间: t”);printf(“%.2fn”,q->lasttime);
            printf(“额定载客量: t”);printf(“%dn”,q->canload);
            printf(“已定票人数: t”);printf(“%dn”,q->alreadyload);
            
            time_t t;
            struct tm *timeinfo;
            time(&t);
            timeinfo=localtime(&t);
            
            if(GetHour(q->starttime)<timeinfo->tm_hour)
               printf(“n此班已发出!n”);
            else
               if(GetHour(q->starttime)==timeinfo->tm_hour)
               {
                  if(GetMinute(q->starttime)<=timeinfo->tm_min)
                     printf(“n此班已发出!n”);
                  else
                     printf(“n此班次车尚未发出!n”);
               }
               else
                  printf(“n此班次车尚未发出!n”);
            j++;
        }
        p=p->next;    
     }     //while(p!=NULL)
     if(j==0)
       printf(“n对不起,不存在此班次!n”);
}

           case ‘q’:
           case ‘Q’:
               exit(0);

void Buyticket()    //预定车票

           default:
               printf(“输入错误,请重选…nnn”);
               system(“PAUSE”);
               break;
        }
    }
}

{
     system(“color 1f”);
     //system(“mode con: cols=140
lines=130”);
     int GetHour(char *s);
     int GetMinute(char *s);
    
     int i=0;
     while(i!=1)
     {
        int num,j=0;
        PINF p,q;   //定义两个结点
        p=head->next;
        
        time_t t;
        struct tm *timeinfo;
        time(&t);
        timeinfo=localtime(&t);
        
        printf(“n请输入你要购买的班次号:”);
        scanf(“%d”,&num);
        while(p!=NULL)
        {
            if(p->numb-num==0)
            {
               q=p;
               j++;
               if(q->alreadyload<q->canload)
               {
                  if(GetHour(q->starttime)>timeinfo->tm_hour)
                  {
                     printf(“n你已订票成功,请按时上车,谢谢使用!n”);
                     q->alreadyload++;
                  }
                  else
                     if((GetHour(q->starttime)==timeinfo->tm_hour)
&& (GetMinute(q->starttime)>timeinfo->tm_min))
                     {
                        printf(“n你已订票成功,请按时上车,谢谢使用!n”);
                        q->alreadyload++;
                     }
                     else
                        printf(“n对不起,该班次车已经发出,请明天再来!谢谢使用!n”);
               }
               else
                  printf(“n对不起,该班次车已经发出,请明天再来!谢谢使用!n”);
            }
            p=p->next;
        }
        if(j==0)
           printf(“n对不起,不存在此班次!谢谢使用!n”);
          
        printf(“==========================n”);
        printf(“n1. 返回主菜单 2. 继续订票 : “);
        scanf(“%d”,&i);
     }//while(i!=1)
}

void addBookInfo(books* H)    //增加图书信息
{

void Cancelticket()       //退订车票

    books *p,*r,*h;     
//p为当前指针,r为信息存储的地址,h为当前结点的前结点
    int state=1;

{
     system(“color 1f”);
     //system(“mode con: cols=140
lines=130”);
     int GetHour(char *s);
     int GetMinute(char *s);
    
     int i=0;
     while(i!=1)
     {
         PINF p,q;
         p=head->next;
         int num,j=0;
        
         time_t t;
         struct tm *timeinfo;
         time(&t);
         timeinfo=localtime(&t);
        
         printf(“n请输入你要退票的班次号:”);
         scanf(“%d”,&num);
         while(p!=NULL)
         {
             if(p->numb-num==0)
             {
                q=p;
                j++;
                if(GetHour(q->starttime)>timeinfo->tm_hour)
                {
                   printf(“n你已退票成功,谢谢使用!n”);
                   q->alreadyload–;
                }
                else
                   if((GetHour(q->starttime)==timeinfo->tm_hour)
&& (GetMinute(q->starttime)>timeinfo->tm_min))
                   {
                      printf(“n你已退票成功,谢谢使用!n”);
                      q->alreadyload–;
                   }
                   else
                      printf(“n对不起,该班次车已经发出,不能退票!谢谢使用!n”);
             }
             p=p->next;
         }
         if(j==0)
            printf(“n对不起,输入错误,不存在此班次车!谢谢使用!n”);
            
         printf(“==========================n”);  
         printf(“n1. 返回主菜单 2. 继续退票 : “);
         scanf(“%d”,&i);
     }
}

    h=H;
    p=H->next;
    r=(books*)malloc(sizeof(struct book));
    r->next=NULL;

int GetHour(char *s)
{
    char *p,*q;
    p=(char *)malloc(10);
    strcpy(p,s);
    q=p;
    while(*q!=’:’)
    {
       q++;
    }
   *q=’’;
   return
atoi(p);                    //字符串到浮点数的转换
}

    system(“CLS”);
    printf(“开始添加的图书信息:nnn”);

int GetMinute(char *s)
{
    char *p;
    p=(char *)malloc(10);
    strcpy(p,s);
    while(*p!=’:’)
    {
       p++;
    }
    p++;
    return atoi(p);
}

    while(1)
    {
        printf(“输入图书编号:”);
        scanf(“%d”,&(r->numBook));
        if(checkNumbook(r->numBook,H)!=0)
           break;
    }

    printf(“请输入书名:”);
    scanf(“%s”,r->name);
    printf(“请输入作者:”);
    scanf(“%s”,r->author);
    printf(“请输入出版社:”);
    scanf(“%s”,r->releaseOrg);
    printf(“请输入价钱:”);
    scanf(“%f”,&(r->price));
    getchar();
    while(1)
    {
        printf(“请输入ISBN号:”);
        scanf(“%s”,r->ISBNnumber);
        getchar();
        if(checkISBN(r->ISBNnumber))
          break;
    }

    while(1)
    {
         if(p==NULL)
          {
              r->next=p;
              h->next=r;
          }
          else
          {
            while(p)
            {
               if(r->numBook < p->numBook)  
//编号从小到大排序,输入的num小于链表中的图书编号时候,插于
               {
                 r->next=p;
                 h->next=r;
                 state=0;
                 saveToFile(H);
                 printf(“n成功添加图书信息!nnn”);
                 system(“PAUSE”);
                 return ;
               }

               h=p;
               p=p->next;
            }

            if(state!=0)  
//说明插入的图书编号大于所有链表中的图书编号
            {
                h->next=r;
                r->next=p;
                saveToFile(H);
                printf(“n成功添加图书信息!nnn”);
                system(“PAUSE”);
                return ;
            }
          }
    }

}

void deleteBookInfo(books* H)     //根据图书编号删除图书信息
{
    books *p,*pred,*r;   
//pred指针用来指明要删除的图书的前一个指针
    int numbook;
    char ch;

    printf(“输入要删除图书信息的编号:”);
    scanf(“%d”,&numbook);
    getchar();

    pred=H;
    p=H->next;

    while(p!=NULL)
    {
        r=pred;

        if(p->numBook==numbook)
        {
           
printf(“该编号图书信息:n%st%st%st%stt%.2fn”,p->name,p->author,p->releaseOrg,p->ISBNnumber,p->price);
            printf(“确认删除?是(y)/否(n): “);
            scanf(“%c”,&ch);
            getchar();

            if(‘y’==ch|| ‘Y’==ch)
            {
                r->next=p->next;
                p=NULL;
                saveToFile(H);
                printf(“删除成功!nnn”);
                system(“PAUSE”);
                return ;
            }

            printf(“未删除…nnn”);
            system(“PAUSE”);
            return ;
        }
        else
        {
            pred=p;
            p=p->next;
        }
    }

    printf(“该编号不存在!删除失败…nnn”);
    system(“PAUSE”);

}

void modifiedByBook_number(books *H)    //根据图书编号修改图书信息
{
    int bookNumber;
    books *p=NULL;

    while(1)
    {
       system(“CLS”);
       printf(“输入要修改图书的编号:”);
       scanf(“%d”,&bookNumber);
       getchar();

       p=H->next;

       while(p!=NULL)
       {
          if(p->numBook==bookNumber)
          {
             printf(“n开始更新图书信息…nn”);
             printf(“请输入书名:”);
             scanf(“%s”,p->name);
             printf(“请输入作者:”);
             scanf(“%s”,p->author);
             printf(“请输入出版社:”);
             scanf(“%s”,p->releaseOrg);
             printf(“请输入价钱:”);
             scanf(“%f”,&p->price);
             getchar();
             while(1)
             {
                printf(“请输入ISBN号:”);
                scanf(“%s”,p->ISBNnumber);
                getchar();
                if(checkISBN(p->ISBNnumber)==1)
                    break;
             }

             saveToFile(H);
             printf(“nn修改成功!!nnn”);
             system(“PAUSE”);

             return;
          }
          p=p->next;
       }

       printf(“n输入错误,该编号不存在!nnn”);
       system(“PAUSE”);
    }
}

void QueryBySpecialCondition()  
//用于IU界面第四个功能模块的内部模块,查询指定条件图书信息
{
    int bookNumber;
    char choose,AuthOrOrgOrISBN[LENTH];

    books* H=NULL;
    H=createBookInfoSystem();

    while(1)
    {
        system(“CLS”);
        printf(“1、查询指定编号的图书信息;n”);
        printf(“2、查询指定的ISBN号的图书数量;n”);
        printf(“3、查询指定作者的图书数量;n”);
        printf(“4、查询指定出版商的图书数量;n”);
        printf(“按任意键返回上一级;nnn”);
        printf(“您的选择:”);
        scanf(“%c”,&choose);

        switch (choose)
        {
           case ‘1’:
               printf(“输入要查询的图书编号:”);
               scanf(“%d”,&bookNumber);
               getchar();
               system(“CLS”);
               displayBookInfoByBook_number(bookNumber,H);
               printf(“nnn”);
               system(“PAUSE”);
               break;

           case ‘2’:
               system(“CLS”);
               printf(“该ISBN号图书数量为:%d”,countNumber(1,H));
               printf(“nnn”);
               system(“PAUSE”);
               break;

           case ‘3’:
               system(“CLS”);
               printf(“该作者的图书数量为:%d”,countNumber(2,H));
               printf(“nnn”);
               system(“PAUSE”);
               break;

           case ‘4’:
               system(“CLS”);
               printf(“该出版社的图书数量为:%d”,countNumber(3,H));
               printf(“nnn”);
               system(“PAUSE”);
               break;

           default:
               getchar();
               return;
               break;
        }
    }

}

int countNumber(int choose,books* H) 
//统计指定作者或出版商或ISBN号一样的图书数量
{
    books *p;
    int cnt=0;
    char AuthOrOrgOrISBN[LENTH];

    p=H->next;

    switch(choose)
    {
        case 1:
            printf(“输入ISBN号:”);
            scanf(“%s”,AuthOrOrgOrISBN);
            getchar();
            if(checkISBN(AuthOrOrgOrISBN)==0)
            {
                printf(“该ISBN不存在!n”);
                return -1;
            }
            while(p!=NULL)
            {
               if(strcmp(p->ISBNnumber,AuthOrOrgOrISBN)==0)
                    cnt++;
               p=p->next;
            }
            break;

        case 2:
            printf(“输入图书作者:”);
            scanf(“%s”,AuthOrOrgOrISBN);
            getchar();
            while(p!=NULL)
            {
               if(strcmp(p->author,AuthOrOrgOrISBN)==0)
                    cnt++;
               p=p->next;
            }
            break;

        case 3:
            printf(“输入图书出版社名字:”);
            scanf(“%s”,AuthOrOrgOrISBN);
            getchar();
            while(p!=NULL)
            {
               if(strcmp(p->releaseOrg,AuthOrOrgOrISBN)==0)
                    cnt++;
               p=p->next;
            }
            break;

        default:
          printf(“无符合查询的条件!”);
          return -1;
    }

    return cnt;
}

books* createBookInfo()  
//用头、尾插法链表创建图书系统图书信息,达到边排序边插入效果
{
    struct book *head,*p,*r,*h,*q;   
//r为尾指针,h为头结点的指针,用于头插法.q用于循环遍历链表
    int state=1;

    head=(struct book *)malloc(sizeof(struct book));
    r=head;
    h=head;

    r->next=NULL;

    int num;

    system(“CLS”);
    printf(“开始构建图书系统nn”);
    printf(“输入图书编号,以0结束:”);
    scanf(“%d”,&num);
    getchar();
    while(num)  //以检索号输入为0结束输入
    {
      q=head->next;

      if(checkNumbook(num,head)==1)
       {
          p=(struct book *)malloc(sizeof(struct book));
          p->next=NULL;

          p->numBook=num;

          printf(“输入图书名字:”);
          scanf(“%s”,p->name);
          printf(“输入图书作者:”);
          scanf(“%s”,p->author);
          printf(“输入图书出版社:”);
          scanf(“%s”,p->releaseOrg);
          while(1)
          {
             printf(“输入图书ISBN号:”);
             scanf(“%s”,p->ISBNnumber);
             if(checkISBN(p->ISBNnumber))
                break;
          }

          printf(“输入图书单价:”);
          scanf(“%f”,&(p->price));
          getchar();

          if(q==NULL)
          {
              p->next=q;
              h->next=p;
          }
          else
          {
            while(q)
            {
               if(p->numBook < q->numBook)  
//编号从小到大排序,输入的num小于链表中的图书编号时候,插于
               {
                 p->next=q;
                 h->next=p;
                 state=0;
                 break;
               }

               h=q;
               q=q->next;
            }
               if(state)  
//说明插入的图书编号大于所有链表中的图书编号
               {
                   h->next=p;
                   p->next=q;
               }

          }
       }
        printf(“输入图书编号,以0结束:”);
        scanf(“%d”,&num);
        getchar();
    }
    printf(“nn图书系统构建完成!欢迎使用…nnn”);

    system(“PAUSE”);
    saveToFile(head);

    return head;
}

int checkISBN(char ISBN[])   //检测ISBN的正确性,对返回1,错的返回0
{
    int i=0;

    if(ISBN[3]==’-‘ && ISBN[5]==’-‘ && ISBN[9]==’-‘ &&
ISBN[15]==’-‘)
    {
        for(;i<17;i++)  //ISBN为17为字符(其中字符为数字和-)
        {
          if( i!=3 && i!=5 && i!=9 && i!=15 )
          {
             if( ISBN[i]<48 || ISBN[i]>57 )
               break;
          }
        }
        if(i==17)
          return 1;
     }
    printf(“ISBN格式错误!!!n”);

    return 0;
}

int checkNumbook(int num_book,books* H) 
//检查图书编号准确性,对返回1,错的返回0
{
    books *p;
    p=H->next;

    while(p!=NULL)
    {
        if(p->numBook==num_book)
        {
            printf(“图书编号存在,输入错误!n”);
            return 0;
        }
        p=p->next;
    }
    return 1;;
}

int displayAllBookInfo(books* H)   //显示全部图书信息
{
    system(“CLS”);

    books *p=H->next;

    while(p!=NULL)
    {
       
printf(“%dt%st%st%st%.2ft%sn”,p->numBook,p->name,p->author,p->releaseOrg,p->price,p->ISBNnumber);
        p=p->next;
    }
    return 0;
}

void displayBookInfoByBook_number(int numbook,books* H)  
//根据图书编号显示图书信息
{
    books *p;
    p=H->next;

    printf(“按默认的编号升序显示:nn”);
    while(p!=NULL)
    {
        if(p->numBook==numbook){
          
printf(“%st%st%st%st%.2fn”,p->name,p->author,p->releaseOrg,p->ISBNnumber,p->price);
           return ;
        }
        p=p->next;
    }
    printf(“输入错误,该图书编号不存在!n”);
}

books* createBookInfoSystem()     //数据存储读取操作
{
    FILE *fp=NULL;
    if((fp=fopen(“bookInfo.txt”,”rt”))==NULL)  
//如果数据文件存在,从文件读取 否则,手动创建
    {
        if((fp=fopen(“bookInfo.txt.txt”,”wt”))==NULL)
           exit(0);
        else
           return createBookInfo();
    }

    return createBookInfoByFile(fp);

}

books* createBookInfoByFile(FILE *fp)   //文件读取数据
{
    books *head,*p,*r,*h;   
//r为尾指针,h为头结点的指针,用于头插法
    head=(struct book *)malloc(sizeof(struct book));
    r=head;
    h=head;
    r->next=NULL;

    //int num;
    while(!feof(fp))
    {
        p=(struct book *)malloc(sizeof(struct book));
        p->next=NULL;

       
fscanf(fp,”%d%s%s%s%f%s”,&(p->numBook),p->name,p->author,p->releaseOrg,&(p->price),p->ISBNnumber);

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

    fclose(fp);
    printf(“图书系统构建完成!欢迎使用…nn”);

    return head;
}

void saveToFile(books *H)   //将数据保存至文件中
{
    FILE *fp;
    books *point=NULL,*ront;

    if((fp=fopen(“bookInfo.txt”,”w”))==NULL)
    {
        printf(“文件打开失败!”);
        exit(0);
    }

    point=H->next;

    while(point)
    {
        ront=point->next;
        if(ront)
          
fprintf(fp,”%dtt%stt%stt%stt%.2ft%sn”,point->numBook,point->name,point->author,point->releaseOrg,point->price,point->ISBNnumber);
        else
          
fprintf(fp,”%dtt%stt%stt%stt%.2ft%s”,point->numBook,point->name,point->author,point->releaseOrg,point->price,point->ISBNnumber);
        point=point->next;
    }

    fclose(fp);
}

void Passward(char str[])   //登录密码
{
    char passward[LENTH]=”000000″;
    FILE *fp;

    if((fp=fopen(“passward.txt”,”r”))==NULL)
    {
        printf(“密码文件不存在!默认密码创建n”);

        if((fp=fopen(“passward.txt”,”w”))==NULL)
        {
            printf(“默认密码文件创建失败!退出..”);
            exit(0);
        }

        fprintf(fp,”%s”,passward);
        printf(“默认密码文件创建成功!”);
        fclose(fp);
    }

    fscanf(fp,”%s”,passward);
    strcpy(str,passward);
    fclose(fp);
}

int enterUI(char passWard[])   //用户登录
{
    char userEntry[LENTH],ch;

    while(1)
    {
        system(“CLS”);
        printf(“tttwelcome to use BookInfoSystemnn”);
        printf(“用户:administaten”);
        printf(“密码:”);
        scanf(“%s”,userEntry);
        getchar();

        if(strcmp(passWard,userEntry)==0 )  //密码匹配成功,进入系统
          return 1;

        printf(“n密码错误,继续尝试?是(y)/否(n):”);
        scanf(“%c”,&ch);
        getchar();

        if(ch!=’y’ && ch!=’Y’)
           return 0;
    }
}

void resetPassward()   //修改密码
{
    FILE *fp;
    char newPassward[LENTH],temp[LENTH];

    if((fp=fopen(“passward.txt”,”w”))==NULL)
    {
        printf(“n密码文件打开发生错误!暂时不能修改登录密码!n”);
        return ;
    }

    while(1)
    {
        system(“CLS”);
        printf(“请输入新密码:”);
        scanf(“%s”,newPassward);
        getchar();
        printf(“再一次输入新密码:”);
        scanf(“%s”,temp);
        getchar();

        if(strcmp(newPassward,temp)==0)
        {
            fprintf(fp,”%s”,newPassward);
            fclose(fp);
            system(“CLS”);
            printf(“n密码修改成功!nn”);
            system(“PAUSE”);
            return;
        }
        printf(“n两次密码输入不同,请重设…nnn”);
        system(“PAUSE”);
    }

}

void sortBySpacialCondition()  //根据不同条件进行排序
{
    char choose;
    books* H=NULL;

    H=createBookInfoSystem();

    while(1){
      system(“CLS”);
      printf(“升序:n”);
      printf(”   1.按照图书编号升序;n”);
      printf(”   2.按照图书出版商升序;n”);
      printf(”   3.按照图书书名升序;n”);
      printf(”   5.按照图书价格升序;n”);
      printf(“降序:n”);
      printf(”   6.按照图书编号降序;n”);
      printf(”   7.按照图书出版商降序;n”);
      printf(”   8.按照图书书名降序;n”);
      printf(”   9.按照图书作者降序;n”);
      printf(”   A.按照图书价格降序;n”);
      printf(” 按其他任意键返回上一级菜单nn”);
      printf(“您的选择:”);

      scanf(“%c”,&choose);
      getchar();
      switch (choose)
      {
          case ‘1’:
            sort(H,1,choose);
            break;

          case ‘2’:
            sort(H,1,choose);
            break;

          case ‘3’:
            sort(H,1,choose);
            break;

          case ‘4’:
            sort(H,1,choose);
            break;

          case ‘5’:
            sort(H,1,choose);
            break;
          case ‘6’:
            sort(H,2,choose);
            break;

          case ‘7’:
            sort(H,2,choose);
            break;

          case ‘8’:
            sort(H,2,choose);
            break;

          case ‘9’:
            sort(H,2,choose);
            break;

          case ‘A’:
          case ‘a’:
            sort(H,2,choose);
            break;

          default:
            return ;
      }
   }
}

void sort(books *H,int sortType,char sortBy)  
//排序通用模块,sortType表示排序方式:1为升序,2为降序排序。sortBy为排序的依据
{
    books *newBooks,*s,*p,*q;
//newBook用于存储新的链表头,s指代遍历时当前指针

    newBooks=(books*)malloc(sizeof(struct book));
    newBooks->next=NULL;

    if(sortType==1)
    {
        while(H->next)
        {
            p=(books *)malloc(sizeof(struct book));
            (*p)=*H->next;    //copy一份当前指针
            p->next=NULL;
            s=newBooks;

            if(sortBy==’1′)
            {
                newBooks=H;
                break;
            }
            else if(sortBy==’2′)
               while(s->next &&
strcmp(p->releaseOrg,s->next->releaseOrg)>0)
                  s=s->next;
            else if(sortBy==’3′)
               while(s->next &&
strcmp(p->name,s->next->name)>0)
                  s=s->next;
            else if(sortBy==’4′)
               while(s->next &&
strcmp(p->author,s->next->author)>0)
                  s=s->next;
            else if(sortBy==’5′)
               while(s->next &&
abs(p->price-s->next->price)>0)
                  s=s->next;

            p->next=s->next;
            s->next=p;
            H=H->next;
        }
    }

    if(sortType==2)
    {
        while(H->next)
        {
            p=(books *)malloc(sizeof(struct book));
            (*p)=*H->next;    //copy一份当前指针
            p->next=NULL;
            s=newBooks;

            if(sortBy==’6′)
               while(s->next &&
p->numBook<s->next->numBook)
                  s=s->next;
            else if(sortBy==’7′)
               while(s->next &&
strcmp(p->releaseOrg,s->next->releaseOrg)<0)
                  s=s->next;
            else if(sortBy==’8′)
               while(s->next &&
strcmp(p->name,s->next->name)<0)
                  s=s->next;
            else if(sortBy==’9′)
               while(s->next &&
strcmp(p->author,s->next->author)<0)
                  s=s->next;
            else if(sortBy==’A’ || sortBy==’a’)
               while(s->next &&
abs(p->price-s->next->price)<0)
                  s=s->next;

            p->next=s->next;
            s->next=p;
            H=H->next;
        }
    }

    q=newBooks->next;
    printf(“n”);
    while(q)
    {
       
printf(“%dt%st%st%st%.2ft%sn”,q->numBook,q->name,q->author,q->releaseOrg,q->price,q->ISBNnumber);
        q=q->next;
    }
    printf(“nnn”);
    system(“PAUSE”);

}

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图