zoukankan      html  css  js  c++  java
  • first program

    图书馆借书代码

    #define _CRT_SECURE_NO_WARNINGS
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int book_num = 0;
    
    struct Book  //书籍数据库
    {
    	int id;
    	char name[20];
    	int state;
    }book[50];
    
    void book_panel(void)  //书籍显示
    {
    	printf("=======网上图书馆=======
    
    
    书籍编号    书籍名字    状态(1可借阅/2已借阅)
    
    ");
    	for (int i = 0; i < book_num; i++)
    	{
    		printf("%d		%s		%d
    ", book[i].id, book[i].name, book[i].state);
    	}
    	printf("
    
    =====================
    
    ");
    }
    
    int if_book_id(int id) //判断编号
    {
    	for (int i = 0; i < book_num; i++)
    	{
    		if (id == book[i].id)
    		{
    			return 1;
    		}
    	}
    	return 0;
    }
    
    int if_book_name(char name[]) //判断书籍名称
    {
    	int ret = 0;
    	for (int i = 0; i < book_num; i++)
    	{
    		ret = strcmp(name, book[i].name);
    		if (ret == 0)
    		{
    			return 1;
    		}
    	}
    	return 0;
    }
    
    void panel(void)  //开始界面
    {
    	printf("=======网上图书馆=======
    
    
      请选择你的身份:
    
    (1)管理员(2)学生
     (3) 退出
    
    
    ");
    } 
    
    void drop(void) //还书界面
    {
    	int num = 0;
    	printf("
    
    =======还书界面=======
    
    请输入归还书籍编号:
    
    ");
    	scanf("%d", &num);
    	for (int i = 0; i < book_num; i++)
    	{
    		if (num == book[i].id)
    		{
    			book[i].state = 1;
    			break;
    		}
    	}
    }
    
    void lend(void) //借书界面
    {
    	int num = 0;
    	printf("
    
    =======借书界面=======
    
    请输入借书籍编号:
    
    ");
    	scanf("%d", &num);
    	for (int i = 0; i < book_num; i++)
    	{
    		if (num == book[i].id)
    		{
    			book[i].state = 2;
    			break;
    		}
    	}
    }
    
    int add(void)  //添加书籍界面
    {
    	if (book_num == 50)
    	{
    		printf("添加失败
    已达最大图书储存数量
    
    ");
    		system("pause");
    		return 0;
    	}
    	printf("==请输入书籍编号==
    ");
    	scanf("%d", &book[book_num].id);
    	if (if_book_id(book[book_num].id) == 1)
    	{
    		printf("添加失败
    该编号已存在
    
    ");
    		system("pause");
    		return 0;
    	}
    	printf("==请输入书籍名字==
    ");
    	scanf("%s", &book[book_num].name);
    	getchar();
    	if (if_book_name(book[book_num].name) == 1)
    	{
    		printf("该书已存在
    
    ");
    		system("pause");
    		return 0;
    	}
    	printf("==请选择书籍状态==
    (1)未借阅 (2)已借阅
    ");
    	scanf("%d", &book[book_num].state);
    	if (book[book_num].state != 1 && book[book_num].state != 2)
    	{
    		printf("错误操作
    ");
    		system("pause");
    		return 0;
    	}
    	else
    	{
        printf("===添加已完成===
    ");
    	book_num++;
    	system("pause");
    	return 0;
    	}
    
    }
    
    int del2(int id) //删除操作
    {
    	int num = 0;
    	if (if_book_id(id) != 1)
    	{
    		printf("
    
    编号不存在
    ");
    		system("pause");
    		return 0;
    	}
    	for (int i = 0; i < book_num; i++)
    	{
    		if (id == book[i].id)
    		{
    			num = i;
    			break;
    		}
    	}
    	for (int i = num + 1; i < book_num; i++)
    	{
    		book[i - 1] = book[i];
    	}
    	book_num--;
    	return 1;
    }
    
    int del(void)  //删除界面
    {
    	int num = 0;
    	printf("==请选择你要删除编号(-1退出)==
    ");
    	scanf("%d", &num);
    	if (num == -1)
    	{
    		printf("
    
    ===退出成功===
    ");
    		system("pause");
    		return 0;
    	}
    	else
    	{
    		if (del2(num) == 1) 
    		{
    		    printf("
    
    ===删除成功===
    ");
    	        system("pause");
    	    	return 0;
    		}
    		printf("
    
    ===删除失败===
    ");
    		system("pause");
    		return 0;
    	}
    
    }
    
    int lookup(void) //查找界面
    {
    	int num = 0,num1=0;
    	char num3[20];
    	printf("请选择查找方式(-1 退出):
    
    (1) 编号 (2)书名 (3)状态 
    
    ");
    	scanf("%d", &num);
    	if (num == -1)
    	{
    		printf("
    
    ===退出成功===
    ");
    		system("pause");
    		return 0;
    	}
    	if (num == 1)
    	{
    
    		printf("请输入书籍编号:");
    		scanf("%d", &num1);
    		for (int i = 0; i < book_num; i++)
    		{
    			if (num1 == book[i].id)
    			{
    				printf("%d 		%s 		%d
    ", book[i].id, book[i].name, book[i].state);
    				system("pause");
    				return 0;
    			}
    			else
    			{
    				printf("此书暂时无法借阅
    ");
    				system("pause");
    				return 0;
    			}
    		}
    	}
    	else if (num == 2)
    	{
    
    		printf("请输入书名:");
    		scanf("%s", &num3);
    		for (int i = 0; i < book_num; i++)
    		{
    			if (strcmp(num3, book[i].name)==0)
    			{
    				printf("%d 		%s 		%d
    ", book[i].id, book[i].name, book[i].state);
    				system("pause");
    				return 0;
    			}
    			else
    			{
    				printf("此书暂时无法借阅
    ");
    				system("pause");
    				return 0;
    			}
    		}
    	}
    	else if(num==3)
    	{
    		printf("请输入书籍状态(1未借阅/2已借阅):");
    		scanf("%d", &num1);
    		for (int i = 0; i < book_num; i++)
    		{
    			if (num1 == book[i].state)
    			{
    				printf("%d 		%s 		%d
    ", book[i].id, book[i].name, book[i].state);
    			}
    		}
    		system("pause");
    		return 0;
    	}
    	else
    	{
    		printf("没有此操作
    ");
    		system("pause");
    		return 0;
    	}
    
    }
    
    int change(void) //修改界面
    {
    	int num1 = 0;
    	printf("==请选择你要修改编号(-1退出)==
    ");
    	scanf("%d", &num1);
    	if (num1 == -1)
    	{
    		printf("
    
    ===退出成功===
    ");
    		system("pause");
    		return 0;
    	}
    		if (del2(num1) == 1)
    		{
    			add();
    			return 0;
    		}
    }
    
    void sort(void) //排序操作
    {
    	int num = 0;
    	for (int j = 1; j < book_num; j++) 
    	{
    	    for (int i = 1;i <book_num; i++)
    	    {
    			if (book[i - 1].id > book[i].id)
    			{
    				num = book[i - 1].id;
    				book[i - 1].id = book[i].id;
    				book[i].id = num;
    		    }
    	    }
    	}
    	printf("
    
    ===完成排序===
    ");
    	system("pause");
    }
    
    void administrator2(void)
    {
    	printf("=========管理界面=========
    
    
    请选择你的操作
    
    (1)添加 (2)删除 (3)查找
    (4) 修改 (5)排序(6)退出
     ");
    }
    
    void administrator(void)  //管理界面
    {
    	book_panel();
    	int num = 0;
    	administrator2();
    	while (num != 6)
    	{
    		scanf("%d", &num);
    		switch (num)
    		{
    		case 1:add(); system("cls"); book_panel(); administrator2(); break;
    		case 2:del();  system("cls"); book_panel(); administrator2(); break;
    		case 3:lookup(); system("cls"); book_panel(); administrator2(); break;
    		case 4:change(); system("cls"); book_panel(); administrator2(); break;
    		case 5:sort(); system("cls"); book_panel(); administrator2(); break;
    		case 6:printf("退出程序中
    "); system("pause"); break;
    		default:
    			printf("没有此操作
    "); system("pause"); system("cls"); book_panel(); administrator2(); break;
    		}
    	}
    
    }
    
    void student2(void)
    {
    	printf("========学生界面=======
    
    
    请选择你的操作
    
    (1) 还书 (2)借书 (3)退出
    ");
    }
    
    void student(void) //学生界面
    {
    	book_panel();
    	int num = 0;
    	student2();
    	while (num != 3)
    	{
    		scanf("%d", &num);
    		switch (num)
    		{
    		case 1:drop(); system("pause"); system("cls"); book_panel(); student2(); break;
    		case 2:lend(); system("pause"); system("cls"); book_panel(); student2(); break;
    		case 3:printf("退出程序中
    "); system("pause"); break;
    		default:
    			printf("没有此操作
    "); system("pause"); system("cls"); book_panel(); student2(); break;
    		}
    	}
    }
    
    int main()
    {
    	int num = 0;
    	panel();
    	while (num != 3)
    	{
    		scanf("%d", &num);
    	    switch (num)
    	    {
    		case 1:system("cls"); administrator(); system("cls"); panel(); break;
    	     case 2:system("cls");student(); system("cls"); panel(); break;
    		 case 3:printf("退出程序中
    "); system("pause"); break;
    		 default:printf("没有此操作
    "); system("pause"); system("cls"); panel(); break;
    	    }
    	}
    
    }
    
    
    

    总结:

    1.该代码的个个界面不是很好,可以再次优化;

    2.在写代码时多次卡壳,思路不是很清晰;

    3.做为所写的第一个程序,我对于他的表现不是很好,可能是写之前对于他的期待太高;

    4.下次再写代码之前一定要想好主要的功能,在写的时候再去想每个功能要如何去写。我在写这个程序之前花了一天时间去想如何写,这是不好的,下次要改正。

  • 相关阅读:
    filter()函数
    递归算法
    日志模块nb_log
    sys.argv[]简单阐述
    DB2中字符、数字和日期类型之间的转换
    Java 连接 Hive的样例程序及解析
    对hadoop namenode -format执行过程的探究
    想要成为牛人、大佬,那请至少拥有这12项技能!
    形象决定你的收入,别问为什么!
    年轻人,能用钱解决的,绝不要花时间(转)
  • 原文地址:https://www.cnblogs.com/love0017/p/14144967.html
Copyright © 2011-2022 走看看