?? 4061014.c
字號:
/*頭文件*/
#include <stdio.h>
#include<dos.h>
#include<stdlib.h> /*其它說明*/
#include<string.h> /*字符串函數(shù)*/
#include<mem.h> /*內(nèi)存操作函數(shù)*/
#include<ctype.h> /*字符操作函數(shù)*/
#include<alloc.h> /*動態(tài)地址分配函數(shù)*/
#define LEN sizeof(BOOK)
typedef struct book /*定義結(jié)構(gòu)體數(shù)組用于緩存數(shù)據(jù)*/
{char bno[8];
char bname[21];
char pdate[10];
char state[4];
char author[9];
char press[11];
char digest[30];
int order;
struct book *next;
}BOOK;
/*函數(shù)原型*/
BOOK *init(); /*初始化函數(shù)*/
int menu_select(); /*菜單函數(shù)*/
BOOK *create(); /*創(chuàng)建鏈表*/
void print(BOOK *head); /* 顯示全部記錄*/
void search(BOOK *head); /*查找記錄*/
BOOK *delete(BOOK *head); /*刪除記錄*/
BOOK *sort(BOOK *head); /*排序*/
BOOK *insert(BOOK *head,BOOK *new); /*插入記錄*/
void save(BOOK *head); /*保存文件*/
BOOK *load(); /*讀文件*/
/*主函數(shù)界面*/
main()
{
BOOK *head,new;
head=init(); /*鏈表初始化,使head的值為NULL*/
for(;;) /*循環(huán)無限次*/
{switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=insert(head,&new);break; /*&new表示返回地址*/
case 6:save(head);break;
case 7:head=load(); break;
case 0:exit(0); /*如菜單返回值為0則程序結(jié)束*/
}
}
}
/*初始化函數(shù)*/
BOOK *init()
{
return NULL; /*返回空指針*/
}
/*菜單選擇函數(shù)*/
menu_select()
{int n;
struct date d; /*定義時間結(jié)構(gòu)體*/
getdate(&d); /*讀取系統(tǒng)日期并把它放到結(jié)構(gòu)體d中*/
printf("press any key to enter the menu......"); /*按任一鍵進入主菜單*/
getch(); /*從鍵盤讀取一個字符,但不顯示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The book information manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*輸入圖書信息記錄*/
printf("\t\t\t2. Print the record\n"); /*顯示*/
printf("\t\t\t3. Search a record on name\n"); /*尋找*/
printf("\t\t\t4. Delete a record\n"); /*刪除*/
printf("\t\t\t5. Insert a record to list\n"); /*插入*/
printf("\t\t\t6. Save the file\n"); /*保存*/
printf("\t\t\t7. Load the file\n"); /*讀取*/
printf("\t\t\t0. Quit\n"); /*退出*/
printf("\n\t\t 4061014 \n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*顯示當前系統(tǒng)日期*/
do{
printf("\n\t\t\tEnter your choice(0~7):");
scanf("%d",&n);
}while(n<0||n>7); /*如果選擇項不在0~7之間則重輸*/
return(n); /*返回選擇項,主函數(shù)根據(jù)該數(shù)調(diào)用相應(yīng)的函數(shù)*/
}
/*輸入函數(shù)*/
BOOK *create()
{int i,s;
BOOK *head=NULL,*p; /* 定義函數(shù).此函數(shù)帶回一個指向鏈表頭的指針*/
clrscr();
for(;;)
{p=(BOOK*)malloc(LEN); /*開辟一個新的單元*/
if(!p) /*如果指針p為空*/
{printf("\nOut of memory."); /*輸出內(nèi)存溢出*/
return (head); /*返回頭指針,下同*/
}
printf("Enter the book No(press \".\" to end):");
scanf("%s",p->bno);
printf("\n");
if(p->bno[0]=='.') break; /*如果書號首字符為“.”則結(jié)束輸入*/
printf("\n");
printf("Enter the book name:");
scanf("%s",p->bname);
printf("Please enter the publishdate:"); /*提示開始輸入出版日期*/
scanf("%s",p->pdate);
printf("Please enter the state:"); /*提示開始輸入狀態(tài)*/
scanf("%s",p->state);
printf("Please enter the author :"); /*提示開始輸入作者*/
scanf("%s",p->author);
printf("Please enter the press:"); /*提示開始輸入出版社*/
scanf("%s",p->press);
printf("Please enter the digest:"); /*提示開始輸入文摘*/
scanf("%s",p->digest);
p->order=0; /*未排序前此值為0*/
p->next=head; /*將頭結(jié)點做為新輸入結(jié)點的后繼結(jié)點*/
head=p; /*新輸入結(jié)點為新的頭結(jié)點*/
}
return(head);
}
/* 顯示全部記錄函數(shù)*/
void print(BOOK *head)
{int i=0; /* 統(tǒng)計記錄條數(shù)*/
BOOK *p; /*移動指針*/
clrscr();
p=head; /*初值為頭指針*/
printf("\n************************************BOOK************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("Rec: %d\n",i);
printf(" Book No: %s\n", p->bno);
printf(" Book name :%s\n", p->bname);
printf(" Publish date: %s\n",p->pdate);
printf(" State: %s\n", p->state);
printf(" Author: %s\n", p->author);
printf(" Press: %s\n", p->press);
printf(" Digest: %s\n\n", p->digest);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*查找記錄函數(shù)*/
void search(BOOK *head)
{BOOK *p; /* 移動指針*/
char s[21]; /*存放書名用的字符數(shù)組*/
clrscr();
printf("Please enter book name for searching.\n");
scanf("%s",s);
p=head; /*將頭指針賦給p*/
while(strcmp(p->bname,s) && p != NULL) /*當記錄的姓名不是要找的,或指針不為空時*/
p=p->next; /*移動指針,指向下一結(jié)點*/
if(p!=NULL) /*如果指針不為空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("-------------------------------------------------------------------------------\n");
printf(" Book No: %s\n", p->bno);
printf(" Book name :%s\n", p->bname);
printf(" Publish date: %s\n",p->pdate);
printf(" State: %s\n", p->state);
printf(" Author: %s\n", p->author);
printf(" Press: %s\n", p->press);
printf(" Digest: %s\n\n", p->digest);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no book No %s book on the list.\n",s); /*顯示沒有該書*/
}
/*刪除記錄函數(shù)*/
BOOK *delete(BOOK *head)
{int n;
BOOK *p1,*p2; /*p1為查找到要刪除的結(jié)點指針,p2為其前驅(qū)指針*/
char c,s[8]; /*s[8]用來存放書號,c用來輸入字母*/
clrscr();
printf("Please enter the deleted book No: ");
scanf("%s",s);
p1=p2=head; /*給p1和p2賦初值頭指針*/
while(strcmp(p1->bno,s) && p1 != NULL) /*當記錄的書號不是要找的,或指針不為空時*/
{p2=p1; /*將p1指針值賦給p2作為p1的前驅(qū)指針*/
p1=p1->next; /*將p1指針指向下一條記錄*/
}
if(strcmp(p1->bno,s)==0) /*書號找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("-------------------------------------------------------------------------------\n");
printf(" book No: %s\n", p1->bno);
printf(" book name :%s\n", p1->bname);
printf(" publishdate: %s\n",p1->pdate);
printf(" state: %s\n", p1->state);
printf(" author: %s\n", p1->author);
printf(" press: %s\n", p1->press);
printf(" digest: %s\n\n", p1->digest);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the book Y/N ?"); /*提示是否要刪除,輸入Y刪除,N則退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不刪除,則跳出本循環(huán)*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,說明被刪結(jié)點是首結(jié)點*/
head=p1->next; /*把第二個結(jié)點地址賦予head*/
else
p2->next=p1->next; /*否則將一下結(jié)點地址賦給前一結(jié)點地址*/
n=n-1;
printf("\nBno %s book has been deleted.\n",s);
printf("Don't forget to save.\n");break; /*刪除后就跳出循環(huán)*/
}
}
}
else
printf("\nThere is no book No %s book on the list.\n",s); /*找不到該結(jié)點*/
return(head);
}
/*插入記錄函數(shù)*/
BOOK *insert(BOOK *head,BOOK *new)
{BOOK *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一個結(jié)點*/
p0=new; /*p0指向要插入的結(jié)點*/
printf("\nPlease enter a new record.\n"); /*提示輸入記錄信息*/
printf("Enter the book No:");
scanf("%s",new->bno);
printf("Enter the book name:");
scanf("%s",new->bname);
printf("Please enter the publishdate:"); /*提示開始輸入出版日期*/
scanf("%s",new->pdate);
printf("Please enter the state:"); /*提示開始輸入狀態(tài)*/
scanf("%s",new->state);
printf("Please enter the author:"); /*提示開始輸入作者*/
scanf("%s",new->author);
printf("Please enter the press:"); /*提示開始輸入出版社*/
scanf("%s",new->press);
printf("Please enter the digest:"); /*提示開始輸入文摘*/
scanf("%s",new->digest);
new->order=0;
if(head==NULL) /*原來的鏈表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的結(jié)點作為頭結(jié)點*/
else
{p1->next=p0;p0->next=NULL;} /*插到最后的結(jié)點之后*/
n=n+1; /*結(jié)點數(shù)加1*/
printf("\nBook %s has been inserted.\n",new->bname);
printf("Don't forget to save the new file.\n");
return(head);
}
/*保存數(shù)據(jù)到文件函數(shù)*/
void save(BOOK *head)
{FILE *fp; /*定義指向文件的指針*/
BOOK *p; /* 定義移動指針*/
char outfile[10];
printf("Enter outfile name,for example c:\\books\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*為輸出打開一個二進制文件,為只寫方式*/
{
printf("Cannot open the file\n");
return; /*若打不開則返回菜單*/
}
printf("\nSaving the file......\n");
p=head; /*移動指針從頭指針開始*/
while(p!=NULL) /*如p不為空*/
{
fwrite(p,LEN,1,fp); /*寫入一條記錄*/
p=p->next; /*指針后移*/
}
fclose(fp); /*關(guān)閉文件*/
printf("Save the file successfully!\n");
}
/* 從文件讀數(shù)據(jù)函數(shù)*/
BOOK *load()
{BOOK *p1,*p2,*head=NULL; /*定義記錄指針變量*/
FILE *fp; /* 定義指向文件的指針*/
char infile[10];
printf("Enter infile name,for example c:\\books.txt\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打開一個二進制文件,為只讀方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(BOOK *)malloc(LEN); /*開辟一個新單元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環(huán)讀數(shù)據(jù)直到文件尾結(jié)束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果沒讀到數(shù)據(jù),跳出循環(huán)*/
p1->next=(BOOK *)malloc(LEN); /*為下一個結(jié)點開辟空間*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向剛才p1指向的結(jié)點*/
p1=p1->next; /*指針后移,新讀入數(shù)據(jù)鏈到當前表尾*/
}
p2->next=NULL; /*最后一個結(jié)點的后繼指針為空*/
fclose(fp);
printf("You have success to read the data from the file!\n");
return (head);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -