对于图书管理系统的完善————管理员权限的添加

来源:同学在大一下的期末大作业
运行环境:VS
运行结果截图






程序代码
//book类

点击查看代码
#define _CRT_SECURE_NO_WARNINGS
#include "book.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<fstream>
#include<iostream>
using namespace std;
book::book()
{
    int i = 0;
    char* result = NULL;
    FILE* fpbook;
    fpbook = fopen("book.txt", "r");
    while (fgets(str, 1020, fpbook) != NULL) {
        sscanf(str, "%s %s %s %s %s", &number[i], &book_name[i], &author[i], &pub[i], &price[i]);
        i += 1;
    }
    read();
    book_num = i;
}

int book::BookNum()
{
    return book_num;
}

void book::show_book()
{
    for (int i = 0; i < book_num; i++)
    {
        printf("%s %s %s %s %s\n", &number[i], &book_name[i], &author[i], &pub[i], &price[i]);
    }
}

void book::searchBook()
{
    char name_book[100];
    printf("请输入要查找书名:\n");
    scanf_s("%s", name_book, 100);
    for (int i = 0; i < book_num; i++)
    {
        if (strcmp(name_book, book_name[i]) == 0)
            printf("%s %s %s %s\n", &number[i], &book_name[i], &author[i], &pub[i]);
    }
}

int book::buy_book(char* str)
{
    int m;
    int price;
    printf("请输入购书序号:");
    scanf_s("%d", &m);
    m = m + 1;
    fstream fp;
    fp.open("buy.txt", ios::out | ios::app);
    //&buy_number[m], &buy_book_name[m], &buy_author[m], &buy_pub[m], &buy_price[m], &buy_name[m]);
    fp << buy_number[m] << " " << buy_book_name[m] << " " << buy_author[m] << " " << buy_pub[m] << " " << buy_price[m] << " " << str << endl;
    printf("购买成功");
    fp.close();
    price = atoi(buy_price[m]);
    return price;
}

void book::shou_buy()
{
    read();
    for (int m = 0; m < buy_num; m++)
    {
        printf("%s %s %s %s %s %s\n", &buy_number[m], &buy_book_name[m], &buy_author[m], &buy_pub[m], &buy_price[m], &buy_name[m]);
    }
}

void book::read()
{
    FILE* fp_buy;
    fp_buy = fopen("buy.txt", "r");
    int m = 0;
    while (fgets(str, 1020, fp_buy) != NULL) {
        sscanf(str, "%s %s %s %s %s %s", &buy_number[m], &buy_book_name[m], &buy_author[m], &buy_pub[m], &buy_price[m], &buy_name[m]);
        m += 1;
    }
    buy_num = m;
}

//buyer类

点击查看代码
#define _CRT_SECURE_NO_WARNINGS
#include "buyer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;

buyer::buyer()
{
	int i = 0;
	FILE* fp_buyer;
	fp_buyer = fopen("buyer.txt", "r");
	while (fgets(str, 1024, fp_buyer) != NULL)
	{
		sscanf(str, "%s  %s  %s  %s", &name[i], &num[i],  &address[i], yonghuclass[i]);
		i += 1;
	}//读入文本数据
	buyer_num = i;
	person_num = i;//记录长度
}

void buyer::show_buyer(int i)
{
	char rbuyer[100];
	ifstream infile_buyer;
	infile_buyer.open("buyer.txt", ios::in);
	if (!infile_buyer.is_open())
	{
		cout << "读取文件失败" << endl;
		return;
	}
	infile_buyer.getline(rbuyer, 100);
	cout << "姓名" << " " << "序号" << " " << "地址" << " " << "用户级别" << endl;
	cout << rbuyer<<endl;
	infile_buyer.close();
}



void buyer::show_person()
{
	ifstream infile;
	infile.open("person.txt", ios::in);
	if (!infile.is_open())
	{
		cout << "读取文件失败" << endl;
		return;
	}
	char buf[1024];
	while (infile.getline(buf, sizeof(buf)))
	{
		cout << buf << endl;
	}
}

void buyer::getpay_buyer(int price)
{
	cout << "购书人需要付费: " << price << endl;
}

//honoured guest类

点击查看代码
#define _CRT_SECURE_NO_WARNINGS
#include "honoured_guest.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;

honoured_guest::honoured_guest()
{
    int i = 0;
    FILE* fp_hguest;
    fp_hguest = fopen("hguest.txt", "r");
    while (fgets(str, 1024, fp_hguest) != NULL)
    {
        sscanf(str, "%s %s %s %s %s", &name[i], &num[i], &hguest[i], &address[i], yonghuclass[i]);
        i += 1;
    }//读入文本数据
    hguest_num = i;//记录长度
}

void honoured_guest::show_hguest(int i)
{
	char rhguest[100];
	ifstream infile_hguest;
	infile_hguest.open("hguest.txt", ios::in);
	if (!infile_hguest.is_open())
	{
		cout << "读取文件失败" << endl;
		return;
	}
	infile_hguest.getline(rhguest, 100);
	cout << "姓名" << " " << "序号" << " " << "地址" << " " << "用户级别" << endl;
	cout << rhguest << endl;
	infile_hguest.close();
}

void honoured_guest::getpay_hguest(int price)
{
    int a = 0.3;
	price = (1 - a) * price;
    cout << "购书人需要付费: " << price << endl;
}

//主函数

点击查看代码
#define _CRT_SECURE_NO_WARNINGS//防止函数报错
#include <iostream>
#include "login.h"
using namespace std;

void face()//面板函数
{
	cout << "-------------------------------------" << endl;
	cout << "###########网上购书平台###########" << endl;
	cout << "           1.登录" << endl;
	cout << "           2.注册" << endl;
	cout << "           3.退出" << endl;
}
int main()
{
	int key;//选择输入
	login log;//定义login类的对象log
	while (1)
	{
		face();//显示面板
		cin >> key;//输入选项
		switch (key)
		{
		case 1:
		{
			log.log();//登录函数
			break;
		}
		case 2:
		{
			log.getlogin();//注册函数
			break;
		}
		case 3:
		{
			goto exit;//跳出循环
		}
		}
	}
exit:

	printf("系统退出");//退出系统
	return 0;
}

//member类

点击查看代码
#define _CRT_SECURE_NO_WARNINGS
#include "member.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;

member::member()
{
	int i = 0;
	FILE* fp_member;
	fp_member = fopen("member.txt", "r");
	while (fgets(str, 1024, fp_member) != NULL)
	{
		sscanf(str, "%s %s %s %s %s", &name[i], &num[i],yonghuclass[i], &address[i], &members[i]);
		i += 1;
	}//读入文本数据
	member_num = i;//记录长度
}

void member::show_member(int i)
{
    char rmember[100];
    ifstream infile_member;
    infile_member.open("member.txt", ios::in);
    if (!infile_member.is_open())
    {
        cout << "读取文件失败" << endl;
        return;
    }
    infile_member.getline(rmember, 100);
    cout << "姓名" << " " << "序号" << " " << "地址" << " " << "用户级别" << endl;
    cout << rmember << endl;
    infile_member.close();
}

void member::getpay_member(int price,int i)
{
    if (strcmp("1", members[i]) == 0)
    {
        price = price * 0.9;
    }
    else if (strcmp("2", members[i]) == 0)
    {
        price = price * 0.8;
    }
    else if (strcmp("3", members[i]) == 0)
    {
        price = price * 0.7;
    }
    else if (strcmp("4", members[i]) == 0)
    {
        price = price * 0.6;
    }
    else if (strcmp("5", members[i]) == 0)
    {
        price = price * 0.5;
    }
    cout << "购书人需要付费: " << price << endl;
}

主要问题列表
作为一个图书管理系统,只有用户端的使用界面,没有面向管理员的界面。因而选择添加管理员权限,可以调用其他的类,查看图书买卖入库记录
更改后代码



重构后界面展示





总结
更改别人的代码确实不是一件简单的事,最为关键的是要搞清楚对方写代码时的逻辑,这样才能可以进行一定程度上的更改;除此之外,我也意识到了代码格式的重要性,只有比较好的代码格式,才能保证可读性,这样才能方便以后的修改和观看,这次的经历会使我以后更加注重这方面的问题

posted on 2024-03-06 22:23  盛宇伟  阅读(10)  评论(0编辑  收藏  举报