学生图书管理系统大作业

代码如下:

#include 
using namespace std;

struct book
{
	char id[20];
	char name[15];
	char person[30][20];
	int number;
	int tot; 
	struct book *next;

};
struct student
{
	char name[20];
	int id;
	char book[20];
	bool state=true; 
	struct student *next;
};

int start()
{
	printf("***********************************\n");
	printf("请输入你的身份:\n");
	printf("1.图书管理员\n2.借阅者\n3.查询借阅者情况\n4.查询图书的信息\n5.退出\n");
	printf("***********************************\n");
	int n;
	scanf("%d",&n);
	return n;

}
int borrow_start(void)
{
	printf("************************************\n");
	printf("请输入你要进行的操作:\n");
	printf("1.借书\n2.还书\n3.退出\n");
	printf("************************************\n");
	int choose;
	scanf("%d",&choose);
	return choose;
}

int operate_start(void)
{
    printf("************************************\n");
	printf("请输入你要进行的操作:\n");
	printf("1.读者录入\n2.图书信息的录入\n3.修改图书信息\n4.修改读者的信息\n5.删除图书信息\n6.删除读者的信息\n7.浏览已经录入的图书信息\n8.浏览读者的情况\n9.退出\n");
	printf("************************************\n");
	int choose;
	scanf("%d",&choose);
	return choose;
}
struct student* operate1(struct student* head)
{
	struct student *p1=new struct student;
	struct student *p2=new struct student;
	p1->next=head->next;
	while (p1->next!=NULL)
	{
		  p1=p1->next;
	}
	printf("请输入你要录入的读者信息:\n");
	printf("格式:读者的名字 读者的编号(数字)\n");
	scanf("%s%d",p2->name,&p2->id);
	p2->book[0]='\0';
	//printf("%s %s %d\n",p2->id,p2->name,p2->number);
	p2->next=NULL;
	if (head->next==NULL) head->next = p2;
	p1->next=p2;

	return head;
}
struct book* operate2(struct book* head)
{
	struct book *p1=new struct book;
	struct book *p2=new struct book;
	p1->next=head->next;
	while (p1->next!=NULL)
	{
		  p1=p1->next;
	}
	printf("请输入你要录入的图书信息:\n");
	printf("格式:图书的编号(字符串) 图书的名字 图书的数量\n");
	scanf("%s%s%d",p2->id,p2->name,&p2->number);
	p2->tot=0; 
	//printf("%s %s %d\n",p2->id,p2->name,p2->number);
	p2->next=NULL;
	if (head->next==NULL) head->next = p2;
	p1->next=p2;
    //free(p1);
    //free(p2);
	return head;
}
struct book* operate3(struct book* head)
{
    struct book *p1=new struct book;
	//p1=head->next;
	p1 = head; 
	strcpy(p1->name,"\0"); 
	printf("请输入你要修改的图书的名字:\n");
	char name1[20];
	scanf("%s",name1);
	while(strcmp(p1->name,name1)&&p1->next!=NULL)
    {
        p1=p1->next;
    }
    if (p1->next!=NULL){
        printf("请重新输入你要写入的图书信息:\n");
        printf("请输入你要录入的图书信息:\n");
        printf("格式:图书的编号(字符串) 图书的名字 图书的数量\n");
        scanf("%s%s%d",p1->id,p1->name,&p1->number);
    }
    else if (p1->next==NULL&!strcmp(p1->name,name1))
    {
        printf("请重新输入你要写入的图书信息:\n");
        printf("请输入你要录入的图书信息:\n");
        printf("格式:图书的编号(字符串) 图书的名字 图书的数量\n");
        scanf("%s%s%d",p1->id,p1->name,&p1->number);
    }
    else{
        printf("未找到你要输入修改的图书信息\n");
        printf("按回车键返回\n"); 
        getchar();
        getchar();

    }
    return head;
}
struct student* operate4(struct student* head)
{
    struct student* p1=new struct student;
	p1=head;
	printf("请输入你要修改的借阅者的名字:\n");
	char name1[20];
	scanf("%s",name1);
	while(strcmp(p1->name,name1)&&p1->next!=NULL)
    {
        p1=p1->next;
    }
    if (p1->next!=NULL){
        printf("请重新输入你要写入的用户信息:\n");
        printf("请输入你要录入的读者信息:\n");
        printf("格式:读者的名字 读者的编号(数字)\n");
        scanf("%s%d",p1->name,&p1->id);
    }
    else if (p1->next==NULL&!strcmp(p1->name,name1))
    {
        printf("请重新输入你要写入的用户信息:\n");
        printf("请输入你要录入的读者信息:\n");
        printf("格式:读者的名字 读者的编号(数字)\n");
        scanf("%s%d",p1->name,&p1->id);
    }
    else{
        printf("未找到你要输入修改的用户信息\n");
        printf("按回车键继续!\n"); 
        getchar(); 
        getchar(); 
    }
    return head;
}
struct book* operate5(struct book* head)
{
    struct book* p1=new struct book;
    struct book* p2=new struct book;
    p2=head;
	p1=head;
	printf("请输入你要删除书的名字:\n");
	char name1[20];
	scanf("%s",name1);
	while(strcmp(p1->name,name1)&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if (p1->next!=NULL){
        p2->next=p1->next;
        //free(p1);
        return head;
    }
    else if (p1->next==NULL&!strcmp(p1->name,name1))
    {
       p2->next=NULL;
       return head ;
    }
    else{
        printf("未找到你要输入删除的图书信息\n");
        getchar();
        getchar();
        return head;
    }
}
struct student* operate6(struct student* head)
{
    struct student* p1=new struct student;
    struct student* p2=new struct student;
    p2=head;
	p1=head;
	printf("请输入你要删除书的借阅者名字:\n");
	char name1[20];
	scanf("%s",name1);
	while(strcmp(p1->name,name1)&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if (p1->next!=NULL){
        p2->next=p1->next;
        //free(p1);
        return head;
    }
    else if (p1->next==NULL&!strcmp(p1->name,name1))
    {
       p2->next=NULL;
       return head ;
    }
    else{
        printf("未找到你要输入删除的借阅者信息\n");
        getchar();
        getchar();
        return head;
    }
}
void operate7(struct book* head)
{
	system("cls");
	struct book* p1=new struct book;
	p1->next=head->next;
	//if (p1->next==NULL) printf("你永远也不会看到图书的录入信息的\n");
	while (p1->next!=NULL)
	{
		p1=p1->next;
		printf("***********************************\n");
		printf("%s %s %d\n",p1->id,p1->name,p1->number);
	}
	printf("***********************************\n");
	getchar();
	printf("按任意键返回");
	getchar();
}
void operate8(struct student* head)
{
	system("cls");
	struct student* p1=new struct student;
	p1->next=head->next;
	//if (p1->next==NULL) printf("你永远也不会看到图书的录入信息的\n");
	while (p1->next!=NULL)
	{
		p1=p1->next;
		printf("***********************************\n");
		printf("%s %d %s\n",p1->name,p1->id,p1->book);
	}
	printf("***********************************\n");
	getchar();
	printf("按任意键返回...");
	getchar();
}
void  operate(struct book* head_book,struct student* head_student)
{
    bool flag=true;
    while(flag) {
        system("cls");
        int choose1=operate_start();
        switch(choose1)
        {
            case 1:{operate1(head_student);break;}
            case 2:{operate2(head_book);break;}
            case 3:{operate3(head_book);break;}
            case 4:{operate4(head_student);break;}
            case 5:{operate5(head_book);break;}
            case 6:{operate6(head_student);break;}
            case 7:{operate7(head_book);break;}
            case 8:{operate8(head_student);break;}
            default:{flag=false;break;}
        }
         
    }

	return ;
}
void borrow1(struct book* head_book,struct student* head_student)
{
	char borrow_book[20];
	char borrow_name[20]; 
	printf("请输入您的名字:\n");
	struct student* p1 = new struct student;
	p1 = head_student;
	scanf("%s",borrow_name);
	//printf("%d ",p1->next); 
	while (strcmp(borrow_name,p1->name)&&p1->next!=NULL){//为什么不可以 while (strcmp(borrow_name,p1->name)&&p1!=NULL),是当p1=NULL后就不能进行比较了吗? 
		p1=p1->next; 
	} 
	if (!strcmp(borrow_name,p1->name)){
		if (p1->state == false){
			printf("很抱歉你已经借过书了。人不能贪得无厌!"); 
			printf("按任意键返回\n");
			getchar();
			getchar(); 
			return; 
		} 
		printf("你已经登录成功了!\n");
		printf("请输入你要借的图书的名字:\n");
		printf("温馨提醒,由于本系统比较的智商捉急,所以请输入对的名字,并且每位读者只能借阅一本!O(∩_∩)O\n");
		scanf("%s",borrow_book);
		struct book* p2=new struct book;
		p2= head_book;
		//printf("%d %d %d",p2,head_book,&head_book->next);
		
		while  (strcmp(borrow_book,p2->name)&&p2->next!=NULL){
		p2=p2->next; 
		} 
		if (!strcmp(borrow_book,p2->name)){
			strcpy(p1->book,p2->name);
			strcpy(p2->person[p2->tot],p1->name);
			p2->tot++; 
			//printf("%d %s",p2->tot,p2->person[0]); 
			printf("借书成功了!\n");
			p1->state=false; 
			printf("请按回车键退出\n");
			getchar();
			getchar(); 
		} 
		else {
			printf("没有找到你想要借的图书!\n");
			printf("请按回车键推出!\n");
			getchar();
			getchar(); 
		}  
	} 
	else {
		printf("对不起,你的信息未被录入管理系统,无法借书\n"); 
		printf("按任意键退出\n");
		getchar();
		getchar(); 
	} 
	return ;
}
void borrow2(struct book* head_book,struct student* head_student) 
{
	char borrow_book[20];
	char borrow_name[20]; 
	printf("请输入您的名字:\n");
	struct student* p1 = new struct student;
	p1 = head_student;
	scanf("%s",borrow_name);
	//printf("%d ",p1->next); 
	while (strcmp(borrow_name,p1->name)&&p1->next!=NULL){//为什么不可以 while (strcmp(borrow_name,p1->name)&&p1!=NULL),是当p1=NULL后就不能进行比较了吗? 
		p1=p1->next; 
	} 
	if (!strcmp(borrow_name,p1->name)){
		if (p1->state == true){
			printf("很抱歉你还未借书。你的忘记好大啊!"); 
			printf("按任意键返回\n");
			getchar();
			getchar(); 
			return; 
		} 
		printf("你已经登录成功了!\n");
		printf("请输入你要还的图书的名字:\n");
		//printf("温馨提醒,由于本系统比较的智商捉急,所以请输入对的名字,并且每位读者只能借阅一本!O(∩_∩)O\n");
		scanf("%s",borrow_book);
		struct book* p2=new struct book;
		p2= head_book;
		//printf("%d %d %d",p2,head_book,&head_book->next);
		
		while  (strcmp(borrow_book,p2->name)&&p2->next!=NULL){
		p2=p2->next; 
		} 
		if (!strcmp(borrow_book,p2->name)){
			for (int i=0;itot;i++)
			{
				if (!strcmp(p2->person[i],borrow_name))
				{
					for (int j=i+1;jtot;j++)
					{
						strcpy(p2->person[j-1],p2->person[j]); 
					} 
					p2->tot--;
					 
				} 
			} 
			//printf("%d %s",p2->tot,p2->person[0]); 
			printf("还书成功了!\n");
			p1->state=true;  
			printf("请按回车键退出\n");
			getchar();
			getchar(); 
		} 
		else {
			printf("没有找到你想要还的图书!\n");
			printf("请按回车键推出!\n");
			getchar();
			getchar(); 
		}  
	} 
	else {
		printf("对不起,你的信息未被录入管理系统,无法还书\n"); 
		printf("按任意键退出\n");
		getchar();
		getchar(); 
	} 
	return ;
} 
void borrow(struct book* head_book,struct student* head_student)
{
    bool flag=true;
    while (flag){
        system("cls");
        int choose=borrow_start();
        switch (choose)
        {
            case 1:{borrow1(head_book,head_student);break;}
            case 2:{borrow2(head_book,head_student);break;}
            //case 3:{borrow3(head_book,head_student);break;}
            default :{flag=false;break;}

        }
    }
}

void dict_student(struct student *head_student)
{
	struct student* p1 = new struct student;
	p1=head_student;
	while (p1->next!=NULL)
	{
		p1=p1->next;
		printf("************************\n");
		printf("ID:%d\nName:%s\nBorrowed Book:%s\n",p1->id,p1->name,p1->book);
		printf("************************\n");
		 
	} 
	printf("数据已经全部列出来了,请查看!\n按任意键结束\n");
	getchar();
	getchar(); 
	 
} 
void dict_book(struct book *head_book)
{
	struct book* p1= new struct book;
	p1 = head_book;
	while (p1->next!=NULL) 
	{
		p1=p1->next;
		printf("************************\n");
		printf("ID:%d\nName:%s\n总的图书数量:%d\n被借阅过的数量:%d\n剩余数量:%d\n已经借阅的名单:\n",p1->id,p1->name,p1->number,p1->tot,p1->number-p1->tot);
		for (int i=0;itot;i++)
		printf("%s\n",p1->person[i]); 
		printf("************************\n");
	} 
	printf("数据已经全部列出来了,请查看!\n按任意键结束\n");
	getchar();
	getchar(); 
} 
int main()
{
	int choose;
	//choose=start();
	//system("cls");
	bool flag=true;
	struct student *head_student=new struct student;
	struct book *head_book=new struct book;
	head_student->next=NULL;
	head_book->next=NULL; //这个初始化要写在外面,修改的时候我已经对世界充满了怀疑。。。
	while (flag)
    {
    	system("cls"); 
    	choose=start();
		switch(choose)
		{
			case 1:{operate(head_book,head_student);break;}
			case 2:{borrow(head_book,head_student);break;}
			case 3:{dict_student(head_student);break;} 
			case 4:{dict_book(head_book);break;} 
			default :{flag=false;break;}
		}
	}


	return 0;
}


你可能感兴趣的:(作业代码)