资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C语言--图书管理项目-创新互联

C语言图书管理系统项目

第一节 C 语言基础以及基本数据类型
第二节 C 语言运算符
第三节 C 语言控制语句
第四节 C 语言自定义函数
第五节 C 语言修饰变量的关键字
第六节 C 语言构造数据类型–数组
第七节 C 语言字符串
第八节 C 语言指针
第九节 指针与函数、指针函数、函数指针
第十节 数组与指针、二级指针&NULL&void 指针
第十一节 结构体
第十二节 链表
第十三节 共用体union,枚举enum,宏定义#define,条件编译,const与指针
C语言–图书管理项目

成都创新互联公司是一家专业提供淮滨企业网站建设,专注与网站设计、网站制作、H5网站设计、小程序制作等业务。10年已为淮滨众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

文章目录
  • C语言图书管理系统项目
  • 前言
  • 一、登录功能
  • 二、主要功能
  • 总结


前言

图书馆项目主要运用到了链表,以及文件操作。


一、登录功能

账号是admin。
默认密码是123456
在这里插入图片描述

二、主要功能

在这里插入图片描述

搜索功能展示如下:
在这里插入图片描述

整体代码如下:

#include#include#includechar pwd[10]="123456";	//系统密码 
char password[10];	//密码全局变量
char username[10];	
//判断输入的用户名和密码是否正确,正确返回1,否则返回0;
int login(char* username,char* password){char u[] = "admin";
	int i = strlen(username);
	if(i< 3 || i>5){return -1;
	}   //对比字符串
	if(strcmp(u,username)==0 && strcmp(pwd,password)==0){return 1;
	} 
	return 0;
}

void getPwd(){//输入密码 
	int i = 0;
	char ch;
	while(1)
	{ch = getch();	//暗文 
		if(ch == '\b')//  \b 回退键 	
		{	// 删除i-1位上的字符 ,如果i == 0,什么都不做
			// 如果i不等于0,先清除password[i-1]里面的数据 '\0';  i = i - 1;  
			if(i != 0)
			{		password[i-1] = '\0';
				printf("\b");	//回退 
				printf(" ");	//把位置上的改为空格 
				printf("\b");	//回退 
				i--;				
			}			
		}else if(ch == '\r'){// 字符串的结束符号\0
			password[i] = '\0';
			break;		 // 结束循环
		}else{	password[i] = ch;
			printf("*");
			i++;
		}	
	}
}
int update(){printf("请输入你的原密码:");
	char temp[20];
	scanf("%s",temp);
	int f = strcmp(temp,pwd);
	if(f!=0){printf("输入的密码错误,请重新重来\n");
		return 0; 
	}
	printf("请输入你的新密码:");
	scanf("%s",&temp);
	printf("再确认你的新密码:");
	char temp2[20];
	scanf("%s",&temp2);
	f = strcmp(temp,temp2);
	if(f!=0){printf("两次输入的密码不一致,请重新重来\n");
		return 0; 
	}
	strcpy(pwd,temp); 
}
void logon(){printf("===========欢迎进入图书管理系统===========\n");
	printf("==========================================\n");
	while(1){printf("请登录账号密码。\n");
		printf("账号:");
		scanf("%s",username);
		printf("密码:");
		getPwd();	//获取密码 
		int res = login(username,password);
		if(res == 1){	printf("\n登陆成功\n");
			break;;
		}else if(res == -1){	printf("\n请输入3~5位数账号\n"); 
		}else{	printf("\n账号密码错误\n"); 
		}
	}
}
	
typedef struct book{char name[41];
	char author[21];
	int num;
	float price;
	float hot;
	struct book *next;
}book;
int hotRank(book* *h){book *head = *h;
	book *pre,*p,*q;
	book *tail = NULL;
	book *touZhiZhen = (book* )malloc(sizeof(book));
	touZhiZhen->next = *h;	//让头节点为空,方便排序后找到头节点 
	if(head == NULL) return 0;
	while(touZhiZhen->next != tail)
	{
		pre = touZhiZhen;  		//指向头节点 
		p = touZhiZhen->next;	//头节点
		q = p->next;		//next 
		while (p->next != tail)
		{if (p->hot< q->hot)//升序排列
			{//交换节点
				pre->next = q;
				p->next = q->next;
				q->next = p;
			}else{		p = p->next;
			}
			q = p->next;
			pre = pre->next;
		}
		tail = p;//一次循环后,最后一个数已大,tail前移
	}
	*h = touZhiZhen->next;
	p = *h;	//从新指向头节点
	printf("书名\t作者\t库存\t价格\t热度\n");
	while(p!=NULL){int l = strlen(p->name);
		if(l< 10){	printf("%s\t%s\t%d\t%.2f\t%.2f\n",p->name,p->author,p->num,p->price,p->hot);
		}else{	printf("%s  %s  %d\t%.2f\t%.2f\n",p->name,p->author,p->num,p->price,p->hot);
		}
		p = p->next;
	}
	return 1;
}

int borrowBook(book* *h){printf("请输入要借阅书名:");
	char str[35];
	scanf("%s",str);
	
	book *p = *h;
	while(p != NULL){if(strcmp(str,p->name) == 0){	p->num = p->num-1;
			printf("借阅成功\n\n"); 
			return 1; 
		}
		p = p->next;
	}
	printf("查找不到该书籍\n\n"); 
	return 0;
}
int returnBook(book* *h){printf("请输入要归还书名:");
	char str[35];
	scanf("%s",str);
	
	book *p = *h;
	while(p != NULL){if(strcmp(str,p->name) == 0){	p->num = p->num+1;
			printf("归还成功\n\n"); 
			return 1; 
		}
		p = p->next;
	}
	printf("没有该书籍,归还失败\n\n"); 
	return 0;
}
int initLink(book* *h){//以读的方式打开文件
	FILE *fp = fopen("./book.txt","r"); 
	if(fp == NULL) 
	{	    printf("打开文件失败"); 
	    return 0; 
	} 
	int count = 0;
	char ch;
	while((ch = fgetc(fp)) != EOF){if(ch == '\n'){	count++;
		}//判断有多少个行(即多少个结构体)
	}
	rewind(fp);	//将文件指针指向文件头
	book *p = *h;
	int i;
	for(i=0; ibook *node = (book *)malloc(sizeof(book));
		fscanf(fp,"%s %s %d %f %f\n",node->name,node->author,&node->num,&node->price,&node->hot);
	    node->next = NULL;
		//把创面新建的节点加入链表
		if(p == NULL){	p=node;
			*h = node;
			continue;
		}
		p->next = node;		
		p = node;		
	}
	fscanf(fp,"%s",pwd);
	// 关闭 
	fclose(fp);
}
int save(book* *h){book *p = *h;
	//以写的方式打开文件
	FILE *fp = fopen("./book.txt","w"); 
	if(fp == NULL) 
	{	    printf("打开文件失败"); 
	    return 0; 
	} 
	while(p != NULL){fprintf(fp,"%s %s %d %.2f %.2f\n",p->name,p->author,p->num,p->price,p->hot);
	    p = p->next;
	}	
	fprintf(fp,"%s",pwd);
	// 关闭 
	fclose(fp);
}
int searchAll(book* *h){book* p = *h;
	printf("书名\t作者\t库存\t价格\t热度\n");
	while(p != NULL){int l = strlen(p->name);
		if(l< 8){	printf("%s\t%s\t%d\t%.2f\t%.2f\n",p->name,p->author,p->num,p->price,p->hot);
		}else{	printf("%s  %s  %d\t%.2f\t%.2f\n",p->name,p->author,p->num,p->price,p->hot);
		}
		p = p->next;
	}
	printf("搜索完成\n\n");  
	return 0;
}
int searchName(book* *h){printf("请输入要搜索书名:");
	char str[35];
	scanf("%s",str);
	printf("书名\t作者\t库存\t价格\t热度\n");
	
	book* p = *h;
	while(p != NULL){if(strcmp(str,p->name) ==0){	printf("%s\t%s\t%d\t%.2f\t%.2f\n",(p)->name,(p)->author,(p)->num,(p)->price,(p)->hot);
		}	
		(p) = (p)->next;
	}
	printf("搜索完成\n\n"); 
	return 0;
}
int searchAuthor(book* *h){printf("请输入要搜索作者:");
	char str[35];
	scanf("%s",str);
	printf("书名\t作者\t库存\t价格\t热度\n");
	
	book* p = *h;
	while(p != NULL){if(strcmp(str,p->author) ==0){	printf("%s\t%s\t%d\t%.2f\t%.2f\n",(p)->name,(p)->author,(p)->num,(p)->price,(p)->hot);
		}	
		(p) = (p)->next;
	}
	printf("搜索完成\n\n"); 
	return 0;
}
void serch(book* *h){AA:printf("[1]全部查询   [2]按书名查找   [3]按作者查找 其余数字退出\n");
	printf("请输入你的选择:");
	short temp;
	scanf(" %hd",&temp);
	switch(temp){case 1: searchAll(h); goto AA;
		case 2: searchName(h);goto AA;
		case 3:	searchAuthor(h);goto AA;
		default: 	printf("退出搜索界面\n\n"); break;
	}
}
int addbook(book* *h){book *node = (book *)malloc(sizeof(book));
	printf("请输入书名:");
	scanf("%s",node->name); 
	printf("请输入书的作者:");
	scanf("%s",node->author); 
	printf("请输入库存数量:");
	scanf("%d",&node->num); 
	printf("请输入书的价格:");
	scanf("%f",&node->price);
	printf("请输入书的热度:");
	scanf("%f",&node->hot);
	node->next = NULL;
	book* p = *h;	
	if(p == NULL){*h = node;
		printf("添加完成\n\n"); 	
		return 1;
	}
	while(p->next != NULL){//找到链表上最后一个节点 
		p = p->next;
	}
	p->next = node;		//添加节点
	printf("添加完成\n\n"); 
	return 1;
}
void UI(){printf("===========欢迎进入图书管理系统===========\n");
	printf("==========================================\n");
	printf("=======[1]检索\t\t[2]借书 还书======\n");
	printf("=======[3]热度排行\t[4]添加新书=======\n");
	printf("=======[5]修改密码\t[6]保存数据=======\n");
	printf("=======[0]退出程序\n");
	printf("==========================================\n");
	printf("请输入相应的数字选择:");
}

int main()
{	book *h=NULL;
	initLink(&h);
	logon();
	while(1){		UI();
		int num; 
		scanf("%d",&num); 
		switch(num){	case 1:serch(&h);break; 
			case 2:{printf("[1]借阅 [2]归还  其余退出\n");
					short temp;
					scanf("%hd",&temp);
					if(temp == 1){borrowBook(&h); 
					}else if(temp == 2){returnBook(&h); 
					}
					break;
			}
			case 3:hotRank(&h);break; 
			case 4:addbook(&h); break; 
			case 5:update();;break;
			case 6:save(&h);break;
			case 0:	return 0;
			default: printf("请输入功能对应的数字1~6/n");break;
		}
	}
	return 0;
}


总结

主要运用到了链表的增删改查,文件的保存以及初始化操作。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


本文名称:C语言--图书管理项目-创新互联
链接地址:http://cdkjz.cn/article/doiegj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220