个人图书管理系统——c语言版

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include<stdio.h>
#include<string.h>
#include<stdlib.h> //支持system()函数
#include<conio.h> //支持getch()函数
#include<ctype.h> //支持isdigit()函数(判断是否为整形)

#define B_INFO_URL "E:\\图书信息表.csv"

//结构体为图书信息
typedef struct Book{
int number; //图书编号
char name[40]; //图书名字
char auth[20]; //作者
char publish[50]; //出版社
struct Book *next;
}Book_info;

//定义的机动变量,整个程序中的所有中间变量都存放在此结构体里边
struct temp{
char value[50];
int max_number;
}T[3],max;

/*主要函数介绍*/

void SetEnum(); //显示导航页面
void JumpPage(int number); //控制主页面跳转函数
int DeleteInfo(); //删除图书信息函数
int AddInfo(); //添加图书信息函数
int CheckInfo(); //查看图书信息函数
int UpdateInfo(); //修改图书信息函数
Book_info* ShowBookInfo(); //读取文件信息存放在结构体中
void print(); // 输出图书信息

Book_info* ShowBookInfo(){
FILE *fp = fopen(B_INFO_URL,"r");
if(fp==NULL){
printf("文件打开失败!");
exit(0);
}
fseek(fp,31,SEEK_SET);
int n = 0,v = 0;
Book_info *p,*q,*head;
p=q=(struct Book*)malloc(sizeof(struct Book));
while(feof(fp)==0){
v++;
fscanf(fp,"%d,%s\n",&p->number,&p->name);
char *result = NULL;
result = strtok(p->name,","); //此方法是把一个字符串按规则分割成几部分
int m = 0;
while(result!=NULL){
strcpy(T[m].value,result);
result = strtok(NULL,",");
m++;
}
strcpy(p->name,T[0].value);
strcpy(p->auth,T[1].value);
strcpy(p->publish,T[2].value);
if(v==1){
head = p;
}
q->next = p;
q = p;
q->next = NULL;
p = (struct Book*)malloc(sizeof(struct Book));
}
fclose(fp);
return head;
}

void print(){
printf("\n ");
FILE *fp = fopen(B_INFO_URL,"r");
if(fp==NULL){
printf("\n\t文件打开失败!");
exit(0);
}
char header[30] = "";
fgets(header,sizeof(header),fp);
char *result = NULL;
result = strtok(header,","); //此方法是把一个字符串按规则分割成几部分
while(result!=NULL){
printf("%15s",result);
result = strtok(NULL,",");
}
fclose(fp);
printf("\n\n");

Book_info *B;
B = ShowBookInfo(); //显示文件中的图书信息方法
while(B!=NULL){
if(B->name==NULL) break;
printf("\t %-6d %-17s %-11s %-20s\n",B->number,B->name,B->auth,B->publish);
if(B->number>max.max_number){
max.max_number = B->number; //保存最大的编号
}
B = B->next;
}
fclose(fp);
printf("\n ***********************************************************************");
}

//去掉data[0]
char setData(char *data

){
int number;
for(number = 1;number<=30;number++){
data[number-1] = data[number];
}
return *data;
}

//设置检索方式
//以每个字符为匹配条件
void setSearch(char *sea
rch,Book_info *bk,const char or){
while(bk!=NULL){
int j=0,m=0;
for(j;j<30;j++){
if(or=='a'){
if(search[j]==bk->auth[j]) m++;
else break;
}else if(or=='n'){
if(search[j]==bk->name[j]) m++;
else break;
}

}
if(m>=2){
printf("\n\t%-6d %-17s %-11s %-20s\n",bk->number,bk->name,bk->auth,bk->publish);
}
bk = bk->next;
}
}


int CheckInfo(){
print();
printf("\n\n\t提示:1、默认以图书名称为检索条件;\n\t 2、首写字符为‘@’表示以作者为检索条件;\n\t 3、首写字符为‘#’表示以图书编号为检索条件;");
printf("\n\n ***********************************************************************");
printf("\n\n\t查看图书信息:");
go:
printf("\n\n\t按Enter继续,按Esc退出:");
if(getch()==0x1b) return 0;
to:
printf("\n\n\t请输入检索条件:");
char search[30];
scanf("%s",search);
Book_info *bk;
bk = ShowBookInfo();
switch(search[0]){
case '@':
search[30] = setData(search);
setSearch(search,bk,'a');
break;
case '#':
int number;
search[30] = setData(search);
number = atoi(search);
if(number==0){
printf("\t输入不合法!");
goto to;
}
while(bk!=NULL){
if(bk->number==number)
printf("\n\t%-6d %-17s %-11s %-20s\n",bk->number,bk->name,bk->auth,bk->publish);
bk = bk->next;
}
break;
default:
setSearch(search,bk,'n');
}
goto go;
return 0;
}

int UpdataInfo(){
print();
printf("\n\n\t修改图书信息:");
Book_info *bk;
bk = ShowBookInfo();
while(1){
printf("\n\n\t请根据提示数据数据(按Enter继续,按Esc退出):");
if(getch()==0x1b) return 0;
go:
printf("\n\n\t请输入图书编号:");
char temp[5];
scanf("%s",&temp);
int number = atoi(temp);
if(number==0) {
printf("\t输入有误!");
goto go;
}
FILE *fp = fopen(B_INFO_URL,"w+");
const char *header = "图书编号,图书名称,作者,出版社\n";
fputs(header,fp);
fclose(fp);
while(bk!=NULL){
if(bk->number==number){
printf("\n\t%-6d %-17s %-11s %-20s\n",bk->number,bk->name,bk->auth,bk->publish);
printf("\n\n\t新图书名称:");
scanf("%s",&bk->name);
printf("\n\t新作者:");
scanf("%s",&bk->auth);
printf("\n\t新出版社:");
scanf("%s",&bk->publish);
}
fp = fopen(B_INFO_URL,"a+");
fprintf(fp,"%d,%s,%s,%s\n",bk->number,bk->name,bk->auth,bk->publish);
fclose(fp);
bk = bk->next

;
}

}
return 0;
}

int AddInfo(){
FILE *fp = fopen(B_INFO_URL,"a+");
if(fp==NULL){
printf("\n\t文件打开失败!");
return 0;
}
char header[30] = "";
fgets(header,sizeof(header),fp);
if(header[0]==NULL){
const char *header = "图书编号,图书名称,作者,出版社\n";
fputs(header,fp);
fclose(fp);
printf("\n\t数据为空...\n\n ***************************************************
*****************");
}else{
print();
}
fclose(fp);

printf("\n\n\t添加图书信息:\n\n\t请根据提示输入图书信息(按enter继续,按esc退出):");
Book B_info;
while(getch()!=0x1b){
printf("\n\n\t图书名称:");
scanf("%s",&B_);
printf("\t图书作者:");
scanf("%s",&B_info.auth);
printf("\t 出版社:");
scanf("%s",&B_info.publish);
B_info.number = ++max.max_number;
fp = fopen(B_INFO_URL,"a+");
fprintf(fp,"%d,%s,%s,%s\n",B_info.number,B_,B_info.auth,B_info.publish);
fclose(fp);
printf("\n\tEnter继续,Esc退出?");
}
return 0;
}

int DeleteInfo(){
print(); //打印出文件中的图书信息
printf("\n\n\t删除图书信息:\n");
go:
printf("\n\t请输入图书编号(按Esc退出):");
char n[4];
int m = 0;
while((n[m]=getch())!=0x1b){ //控制输入字符
printf("%c",n[m]);
if((int)n[m]<48||(int)n[m]>57){
if((int)n[m]==13){
if(m==0) goto go;
break;
}
printf(" 数据类型出错!");
goto go;
}
if(++m==5){
printf(" 数据溢出!");
goto go;
}
}

int number = atoi(n); //把字符串转换成数字
if(number==0) return 0; //如果为0认为是用户操作了esc键

Book_info *bk,*prev;
prev = bk = ShowBookInfo(); //显示文件中的图书信息方法

FILE *fp = fopen(B_INFO_URL,"w+");
const char *header = "图书编号,图书名称,作者,出版社\n";
fputs(header,fp);
fclose(fp);
while(bk!=NULL){
if(bk->number==number){
prev->next = bk->next;
}else{
fp = fopen(B_INFO_URL,"a+");
fprintf(fp,"%d,%s,%s,%s\n",bk->number,bk->name,bk->auth,bk->publish);
fclose(fp);
prev = bk;
}
bk = bk->next;
}
goto go;
return 0;
}

void SetEnum(){
printf("\n\t\t\t\t个人图书管理系统");
printf("\n\n\t ********************************************************\n");
printf("\n\t\t\t1:查看图书信息\t2:修改图书信息");
printf("\n\n\t\t\t3:添加图书信息\t4:删除图书信息\n");
printf("\n\t ********************************************************\n");

int number = 1; //根据编号确定程序要跳转的方法
printf("\n\t 请输入操作编号:");
while(1){
printf("%c",number = getch());
if(num

ber<=52&&number>=49){
break;
}
printf("\n\t 输入正确的编号:");
}
JumpPage(number);
}

void JumpPage(int number){
system("cls");
switch(number){
case 49:CheckInfo();break;
case 50:UpdataInfo();break;
case 51:AddInfo();break;
case 52:DeleteInfo();break;
default:break;
}
system("cls");
SetEnum(); //回到主界面
}

int main(){
system("color 8F"); //灰色背景,浅蓝色前景色
SetEnum();
return 0;
}

相关文档
最新文档