这个原本是c语言程序设计书里面的习题,老师布置的作业,既然写好了,就在网上分享下,相信肯定有很多和我一样使用的是这本教材,大家可以借鉴下答案,在dev c++上运行是没有任何错误的。文末已经贴了源码,还有什么不了解的可以在评论里讲下!!!
输入1添加数据
输入2
输入3 查询
输入4
再输入2,查看改变后的数据
输入5(注意此程序只能从后面删除,切不可删前面的id,否则就是复制后面的数据)
输入6(借书)
可看到存数减一
输入7归还图书
至此整个功能介绍完毕,输入8退出系统
下面贴出源代码
#include<stdio.h> //预处理
#include<string.h>
#include<stdlib.h>
#include<conio.h>//定义结构体
struct book
{int id;char name[31];char author[27];char publish[31];int store;int total;int user[10];int days[10];
}books[100];//显示整体标题标题函数
void page_title(char *menu_item)
{printf("\t\t********欢迎大家使用阜阳师范大学图书馆管理系统********\n\n-%s-\n\n",menu_item);}
//等待返回函数
void return_confirm()
{printf("\n请按任意键返回……\n");getch();
} book_add()
{int i;system("cls"); //清屏 page_title("注册新书"); for(i=0;i<2;i++){printf("序号:");scanf("%d",&books[i].id);printf("书名:");scanf("%s",&books[i].name); printf("作者:");scanf("%s",&books[i].author); printf("出版社:");scanf("%s",&books[i].publish); printf("数量:");scanf("%d",&books[i].total);books[i].store=books[i].total;printf("\n"); }
}book_show()
{int i,flag=0;system("cls");for(i=0;i<100;i++){ if(strlen(books[i].publish)!=0){printf("序号:%d\t ",books[i].id);printf("书名:%s\t ",books[i].name);printf("作者:%s\t ",books[i].author);printf("出版社:%s\t ",books[i].publish);printf("存数:%d\t",books[i].store);printf("总数:%d\n\n",books[i].total);flag=1;} } if(flag==0){printf("没有找到相关书籍\n");}getch();return i;
}int book_search()
{int n,i,flag=0;char nam[30];system("cls");printf("请输入书名:");scanf("%s",&nam);for(i=0;i<100;i++){if(strcmp(books[i].name,nam)==0){printf("序号:%d\t ",books[i].id);printf("书名:%s\t ",books[i].name);printf("作者:%s\t ",books[i].author);printf("出版社:%s\t ",books[i].publish);printf("存数:%d\t",books[i].store);printf("总数:%d\n",books[i].total);n=i;flag=1;getch();break;} } if(flag==0){printf("\n没有找到相关书籍\n");getch();return -1;}return n;
}book_edit()
{int i,id_change;system("cls");page_title("修改图书信息:");printf("输入要修改的图书id:");scanf("%d",&id_change);for(i=0;i<100;i++){if(books[i].id==id_change){printf("修改书名:");scanf("%s",&books[i].name); printf("修改作者:");scanf("%s",&books[i].author); printf("修改出版社:");scanf("%s",&books[i].publish); printf("修改数量:");scanf("%d",&books[i].total);printf("修改图书信息成功\n");books[i].store=books[i].total;return i; } } printf("\n没有找到相关信息\n");getch();return -1;
}int book_del()
{int i,num,j;system("cls");page_title("删除图书");printf("输入所要删除的书的id:");scanf("%d",&num);for(i=0;i<100;i++){if(books[i].id==num){for(j=i;j<100;j++){books[j].id=books[j+1].id;strcpy(books[j].name,books[j+1].name);strcpy(books[j].publish,books[j+1].publish);printf("该书已经删除了!!!\n");getch();return i;}} } printf("未找到相关记录\n");getch();return -1;
}//借书
book_out()
{int n,s,i,d;system("cls");page_title("借阅图书");n=book_search();if(n!=-1&&books[n].store>0){//赋初值,全为零 for(i=0;i<10;i++){books[n].user[i]=0;books[n].days[i]=0;} printf("借书证序号:");scanf("%d",&s);printf("输入可借天数:");scanf("%d",&d); books[n].user[0]=s;books[n].days[0]=d; books[n].store--; // 书上这有何意义
// for(i=0;i<10;i++)
// {
// if(books[n].user[i]==0)
// {
// books[n].user[i]=0;
// books[n].days[i]=0;
// break;
// }
// books[n].store--;
// } }else if(n!=-1&&books[n].store==0){printf("此书已经完全借完\n\n");getch();}else;return_confirm();
}//归还图书
void book_in()
{int n,s,i;page_title("归还图书");n=book_search();printf("%d",n);if(n!=-1&&books[n].store<books[n].total){printf("借阅图书证列表\n");for(i=0;i<10;i++){if(books[n].user!=0){printf("[%d]--%d天\n",books[n].user[i],books[n].days[i]);}printf("输入借书证号:");scanf("%d",&s);for(i=0;i<10;i++){if(books[n].user[i]==s){books[n].store++;}}} }if(n!=-1&&books[n].store==books[n].total){printf("全部归还\n");}else if(n!=-1&&books[n].store<books[n].total){printf("归还成功\n");}else ;return_confirm();} main()
{menu:page_title("操作选单");printf("用数字选择操作\n\n");printf("\t\t1注册新书\t\t2显示图书\n");printf("\t\t3查询图书信息\t\t4修改图书信息\n");printf("\t\t5删除图书\t\t6借阅图书\n");printf("\t\t7归还图书\t\t8退出系统\n");printf("请按数字键:\n");switch(getchar()){case'1': book_add();break;case'2': book_show();break;case'3': book_search();break;case'4': book_edit();break;case'5': book_del();break;case'6': book_out();break;case'7': book_in();break;case'8': exit(0);} goto menu;}
代码与书上不一样的点:
书上的struct结构体错误,未在后面添加数组
借书里面有一段都等于0看不懂
这个项目主要训练了我调试项目的能力,受教了
带有多名学生的动态链表
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)struct Student
{long num;int score;struct Student *next;
};int n;struct Student *creat()
{struct Student *head;struct Student *p1,*p2;n=0;printf("请输入学号和成绩以逗号隔开:\n");printf("结束请按911+回车键:\n");printf("第1个学生学号和成绩:\n");p1=p2=(struct Student *)malloc(LEN);scanf("%d,%d",&p1->num,&p1->score);head=NULL;while(p1->num!=911){n=n+1;if(n==1)head=p1;elsep2->next=p1;p2=p1;p1=(struct Student *)malloc(LEN);printf("第%d个学生学号和成绩:\n",n+1);scanf("%ld,%d",&p1->num,&p1->score);}p2->next=NULL;return head;
}struct Student *del(struct Student *head)
{struct Student *p1,*p2;long n;printf("输入删除的学号:");scanf("%ld",&n);p1=head;if(p1->num==n)head=p1->next;else{do{p2=p1;p1=p1->next;} while(p1!=NULL&&p1->num!=n);if(p1->num==n)p2->next=p1->next;elseprintf("没找到\n");}free(p1);return(head);
}void print(struct Student *head)
{struct Student *p;p=head;n=0;if(head!=NULL)do{n=n+1;printf("第%d个学生的学号:%ld,成绩:%d\n",n,p->num,p->score);p=p->next;}while(p!=NULL);
}int main()
{struct Student *pHead;pHead=creat();print(pHead);pHead=del(pHead);print(pHead);return 0;
}