首页 > 编程语言 >C++网上购书系统项目的二次开发 2252416 hzx

C++网上购书系统项目的二次开发 2252416 hzx

时间:2024-03-04 21:25:03浏览次数:33  
标签:购书 cout int 50 C++ choice char file 二次开发

1、来源:同学大二下的期末大作业:网上购书系统项目。
2、运行环境:VisualStudio 2019

代码:

点击查看代码
#include"StdAfx.h"
#include<iostream>
#include"person.h"
#include<string.h>
#include"globalfunction.h"
#include"book.h"
#include"adm.h"
using namespace std;
extern book BOOK[1000];
extern int Booksum;
bool adm::check(char a[50], char b[50])
{
	{if (!strcmp(ID, a) && !strcmp(key, b))
		return true;
	else
		return false;
	}
}
adm::adm()
{
	strcpy_s(ID, "hansumer");
	strcpy_s(key, "2252416");
}

void adm::ls()
{
	load();
	int k = 0;
	while (k != Booksum)
	{
		cout << "ID:" << k << endl;
		cout << "书名:" << BOOK[k].name << endl << "价格:" << BOOK[k].price << endl << "总数:"<<BOOK[k].sum<<endl;
			cout << "作者:" << BOOK[k].author << endl << "出版社:" << BOOK[k].publish << endl << "简介 : "<<BOOK[k].brief<<endl;
			if (BOOK[k].sum == 0)
				cout << "该书库存为 0,请尽快补充库存!!!" << endl;
		k++;
	}
	cout << "共计:" << Booksum << endl;
}
void adm::way()
{
start:
	system("pause");
	system("cls");
	cout << "1:遍历图书" << endl;
	cout << "2:添加图书" << endl;
	cout << "3:修改图书" << endl;
	cout << "4:删除图书" << endl;
	cout << "5:遍历用户" << endl;
	cout << "6:制表" << endl;
	cout << "7:订单" << endl;
	cout << "8:退出" << endl;
	cout << "choice:";
	int choice;
	cin >> choice;
	if (choice == 8)
		exit(0);
	if (choice == 1)
		ls();
	else if (choice == 2)
		add();
	else if (choice == 3)
		change();
	else if (choice == 4)
		del();
	else if (choice == 5)
		userlook();
	else if (choice == 6)
		tab();
	else if (choice == 7)
		vieworder();
	goto start;
}#include"StdAfx.h"
#include<iostream>
#include<fstream>
#include<string.h>
#include"adm.h"
#include"book.h"
#include"order.h"
#include"user.h"
#include"globalfunction.h"
using namespace std;
adm A;
book BOOK[1000];
user B[1000];
int Booksum = 0;
int Usersum = 0;
void load()
{
	fstream file;
	Booksum = 0;
	file.open("book.txt", ios::in);
	if (!file.is_open())
	{
		cout << "打开失败!" << endl;
		A.way();
	}
	while (file.peek() != EOF)
	{
		file.read((char*)&BOOK[Booksum].price, sizeof(int));
		file.read((char*)&BOOK[Booksum].sum, sizeof(int));
		file.read(BOOK[Booksum].name, 50 * sizeof(char));
		file.read(BOOK[Booksum].author, 50 * sizeof(char));
		file.read(BOOK[Booksum].publish, 50 * sizeof(char));
		file.read(BOOK[Booksum++].brief, 50 * sizeof(char));
	}
	file.close();
}
void userls(void)
{
	load();
	fstream file;
	fstream com;
	com.open("comment.txt", ios::in);
	file.open("book.txt", ios::in);
	int k = 0;
	while (k != Booksum)
	{
		cout << "ID:" << k << endl;
		cout << "书名:" << BOOK[k].name << endl << "价格:" << BOOK[k].price << endl << "总数:"<<BOOK[k].sum<<endl;
			cout << "作者:" << BOOK[k].author << endl << "出版社:" << BOOK[k].publish << endl << "简介 : "<<BOOK[k].brief<<endl;
			k++;
	}
	cout << "共计:" << Booksum << endl;
	file.close();
}
void add(void)
{
	fstream a;
	a.open("book.txt", ios::out | ios::app);
	int price, sum;
	char name[50], author[50], publish[50], biref[50];
	cout << "单价:";
	cin >> price;
	cout << "数量:";
	cin >> sum;
	cout << "名字:";
	cin.get();
	cin.getline(name, 50 * sizeof(char));
	cout << "作者:";
	cin.getline(author, 50 * sizeof(char));
	cout << "出版社:";
	cin.getline(publish, 50 * sizeof(char));
	cout << "简介:";
	cin.getline(biref, 50 * sizeof(char));

	a.write((char*)&price, sizeof(price));
	a.write((char*)&sum, sizeof(sum));
	a.write(name, 50 * sizeof(char));
	a.write(author, 50 * sizeof(char));
	a.write(publish, 50 * sizeof(char));
	a.write(biref, 50 * sizeof(char));
	a.close();
	cout << "add success!\n";
	load();
}
void change(void)
{
	load();
	char name[50], author[50];
	int ID, k;
	char choice;
	cout << "1)书名搜索\n2)作者搜索\n3)ID 搜索:";
	cin >> k;
	if (k == 1)
	{
		cout << "书名:";
		cin.get();
		cin.getline(name, 50 * sizeof(char));
		ID = search(name, k);
	}
	if (k == 2)
	{
		cout << "作者:";
		cin.get();
		cin.getline(author, 50 * sizeof(char));
		ID = search(author, k);
	}
	if (k == 3)
	{
		cout << "ID:";
		cin >> k;
		ID = search(k);
	}
	if (ID == -1)
	{
		cout << "没有找到!" << endl;
		A.way();
	}
	else
	{
		cout << "名字:" << BOOK[ID].name << endl << "价格:" << BOOK[ID].price << endl << "总数:"<<BOOK[ID].sum<<endl;
			cout << "是否修改名字?(Y or N):";
		cin >> choice;
		if (choice == 'y' || choice == 'Y')
		{
			cout << "修改后的名字:";
			cin.get();
			cin >> BOOK[ID].name;
		}
		cout << "修改后的价格:";
		cin >> BOOK[ID].price;
		cout << "修改后的总数:";
		cin >> BOOK[ID].sum;
	}
	fstream file;
	file.open("book.txt", ios::out);
	for (int j = 0; j < Booksum; j++)
	{
		file.write((char*)&BOOK[j].price, sizeof(int));
		file.write((char*)&BOOK[j].sum, sizeof(int));
		file.write(BOOK[j].name, 50 * sizeof(char));
		file.write(BOOK[j].author, 50 * sizeof(char));
		file.write(BOOK[j].publish, 50 * sizeof(char));
		file.write(BOOK[j].brief, 50 * sizeof(char));
	}
	file.close();
	cout << "change success!" << endl;
	load();

}
void bookwrite(void)
{
	fstream file;
	file.open("book.txt", ios::out);
	for (int j = 0; j < Booksum; j++)
	{
		file.write((char*)&BOOK[j].price, sizeof(int));
		file.write((char*)&BOOK[j].sum, sizeof(int));
		file.write(BOOK[j].name, 50 * sizeof(char));
		file.write(BOOK[j].author, 50 * sizeof(char));
		file.write(BOOK[j].publish, 50 * sizeof(char));
		file.write(BOOK[j].brief, 50 * sizeof(char));
	}
	file.close();
}

void del(void)
{
	load();
	int ID;
	fstream file;
	int k;
	char name[50], author[50];
	cout << "全部删除按 1,部分删除按 2:";
	int choice;
	cin >> choice;
	if (choice == 1)
	{
		cout << "输入 1 确认全部删除!!:";
		cin >> choice;
		if (choice == 1)
		{
			file.open("book.txt", ios::out);
			file.close();
		}
	}
	if (choice == 2)
	{
		cout << "1)书名搜索\n2)作者搜索\n3)ID 搜索:";
		cin >> k;
		if (k == 1)
		{
			cout << "书名:";
			cin.get();
			cin.getline(name, 50 * sizeof(char));
			ID = search(name, k);
		}
		if (k == 2)
		{
			cout << "作者:";
			cin.get();
			cin.getline(author, 50 * sizeof(char));
			ID = search(author, k);
		}
		if (k == 3)
		{
			cout << "ID:";
			cin >> k;
			ID = search(k);
		}
		if (ID == -1)
			cout << "没有找到!" << endl;
		else
		{
			cout << BOOK[ID].name << endl;
			BOOK[ID].sum = -1;
			file.open("book.txt", ios::out);
			k = 0;
			while (k != Booksum)
			{
				if (BOOK[k].sum != -1)
				{
					file.write((char*)&BOOK[k].price, sizeof(int));
					file.write((char*)&BOOK[k].sum, sizeof(int));
					file.write(BOOK[k].name, 50 * sizeof(char));
					file.write(BOOK[k].author, 50 * sizeof(char));
					file.write(BOOK[k].publish, 50 * sizeof(char));
					file.write(BOOK[k].brief, 50 * sizeof(char));
				}
				k++;
			}
			cout << "del success" << endl;
		}
	} file.close();
	load();
}
int search(char* a, int choice)
{
	load();
	if (choice == 1)
	{
		for (int j = 0; j < Booksum; j++)
		{
			if (!strcmp(BOOK[j].name, a))
				return j;
		}
		return -1;
	}
	if (choice == 2)
	{
		for (int j = 0; j < Booksum; j++)
		{
			if (!strcmp(BOOK[j].author, a))
				return j;
		}
		return -1;
	}
	else
		return -1;
}
int search(int choice)
{
	load();
	if (choice >= Booksum || choice < 0)
		return -1;
	else
		return choice;
}
void userload()
{
	fstream file;
	Usersum = 0;
	file.open("user.txt", ios::in);
	if (file.is_open())
	{
		while (file.peek() != EOF)
		{
			file.read((char*)&B[Usersum].ID, 50 * sizeof(char));
			file.read((char*)&B[Usersum].key, 50 * sizeof(char));
			file.read((char*)&B[Usersum].history, 2500 * sizeof(char));
			file.read((char*)&B[Usersum].buy, 2500 * sizeof(char));
			file.read((char*)&B[Usersum].buyprice, 50 * sizeof(int));
			file.read((char*)&B[Usersum].buysum, 50 * sizeof(int));
			file.read((char*)&B[Usersum].number, sizeof(int));
			Usersum++;
		}
	}
	file.close();
}
void useradd()
{
	fstream file;
	file.open("user.txt", ios::out | ios::app);
	file.write((char*)&B[Usersum].ID, 50 * sizeof(char));
	file.write((char*)&B[Usersum].key, 50 * sizeof(char));
	file.write((char*)&B[Usersum].history, 2500 * sizeof(char));
	file.write((char*)&B[Usersum].buy, 2500 * sizeof(char));
	file.write((char*)&B[Usersum].buyprice, 50 * sizeof(int));
	file.write((char*)&B[Usersum].buysum, 50 * sizeof(int));
	file.write((char*)&B[Usersum].number, sizeof(int));
	Usersum++;
	file.close();
}
void userwrite()
{
	fstream file;
	file.open("user.txt", ios::out);
	for (int k = 0; k < Usersum; k++)
	{
		file.write((char*)&B[k].ID, 50 * sizeof(char));
		file.write((char*)&B[k].key, 50 * sizeof(char));
		file.write((char*)&B[k].history, 2500 * sizeof(char));
		file.write((char*)&B[k].buy, 2500 * sizeof(char));
		file.write((char*)&B[k].buyprice, 50 * sizeof(int));
		file.write((char*)&B[k].buysum, 50 * sizeof(int));
		file.write((char*)&B[k].number, sizeof(int));
	}
	file.close();
	userload();
}
void userlook()
{
	userload();
	for (int j = 0; j < Usersum; j++)
		cout << "ID:" << B[j].ID << endl;
	cout << "共计" << Usersum << "个用户" << endl;
}
bool same(char ID[50])
{
	userload();
	for (int j = 0; j < Usersum; j++)
		if (!strcmp(ID, B[j].ID))
			return true;
	return false;
}
void comment(int ID)
{
	fstream file;
	file.open("comment.txt", ios::in);
	char name[50];
	char comment[1000];
	cout << "评论区:" << endl;
	while (file.peek() != EOF)
	{
		file.read(name, 50 * sizeof(char));
		file.read(comment, 1000 * sizeof(char));
		if (!strcmp(name, BOOK[ID].name))
			cout << comment << endl;
	}
}
void seek(string a)
{
	load();
	string b;
	cout << "在书名中查找中..." << endl;
	for (int k = 0; k < Booksum; k++)
	{
		b = BOOK[k].name;
		if (b.find(a) != string::npos)
			cout << "ID:" << k << endl << "书名:" << BOOK[k].name << endl << "作者:"<<BOOK[k].author<<endl<<endl;
	}
	cout << "在作者中查找..." << endl;
	for (int l = 0; l < Booksum; l++)
	{
		b = BOOK[l].author;
		if (b.find(a) != string::npos)
			cout << "ID:" << l << endl << "书名:" << BOOK[l].name << endl << "作者:"<<BOOK[l].author<<endl<<endl;
	}
	cout << "在出版社中查找..." << endl;
	for (int m = 0; m < Booksum; m++)
	{
		b = BOOK[m].publish;
		if (b.find(a) != string::npos)
			cout << "ID:" << m << endl << "书名:" << BOOK[m].name << endl << "作者:"<<BOOK[m].author<<endl<<endl;
	}
}
void tab()
{
	load();
	cout << "1)售价制表 2)数量制表 3)销量制表" << endl;
	int choice;
	cin >> choice;
	if (choice == 1)
	{
		int temp[1000][2];
		int t;
		for (int k = 0; k < Booksum; k++)
		{
			temp[k][0] = k;
			temp[k][1] = BOOK[k].price;
		}
		for (int i = 0; i < Booksum; i++)
			for (int j = 0; j < Booksum -j- 1; j++)
			{
				if (temp[j][1] < temp[j + 1][1])
				{
					t = temp[j][1];
					temp[j][1] = temp[j + 1][1];
					temp[j + 1][1] = t;

					t = temp[j][0];
					temp[j][0] = temp[j + 1][0];
					temp[j + 1][0] = t;
				}
			}
		for (int l = 0; l < Booksum; l++)
			for (int j = 0; j < Booksum; j++)
			{
				if (j == temp[l][0])
				{
					cout << l + 1 << ":" << BOOK[j].name << " 价格:" << BOOK[j].price << endl;
					break;
				}
			}
	}
	else if (choice == 2)
	{
		int temp[1000][2];
		int t;
		for (int k = 0; k < Booksum; k++)
		{
			temp[k][0] = k;
			temp[k][1] = BOOK[k].sum;
		}
		for (int m = 0; m < Booksum; m++)
			for (int j = 0; j < Booksum - m - 1; j++)
			{
				if (temp[j][1] < temp[j + 1][1])
				{
					t = temp[j][1];
					temp[j][1] = temp[j + 1][1];
					temp[j + 1][1] = t;

					t = temp[j][0];
					temp[j][0] = temp[j + 1][0];
					temp[j + 1][0] = t;
				}
			}
		for (int n = 0; n < Booksum; n++)
			for (int j = 0; j < Booksum; j++)
			{
				if (j == temp[n][0])
					cout << n + 1 << ":" << BOOK[j].name << " 数量:" << BOOK[j].sum << endl;
			}
	}
	else if (choice == 3)
	{
		fstream file;
		file.open("order.txt", ios::in);
		int sum[1000][2];
		int ID, temp, num = 0;
		int k;
		for (int l = 0; l < 10; l++)
		{
			sum[l][0] = -1;
			sum[l][1] = 0;
		}
		bool flag = false;
		char b[50], a[50];
		while (file.peek() != -1)
		{
			file.read(b, 50 * sizeof(char));
			file.read(a, 50 * sizeof(char));
			for (k = 0; k < 10; k++)
			{
				if (sum[k][0] == search(a, 1))
				{
					flag = true;
					break;
				}
			}
			file.read((char*)&temp, sizeof(int));
			if (flag)
				sum[k][1] += temp;
			else
			{
				sum[num][0] = search(a, 1);
				sum[num][1] += temp;
				num++;
			}
			flag = false;
		}
		for (k = 0; k < num; k++)
			for (int j = 0; j < num - k - 1; j++)
			{
				if (sum[j][1] < sum[j + 1][1])
				{
					temp = sum[j][0];
					sum[j][0] = sum[j + 1][0];
					sum[j + 1][0] = temp;
					temp = sum[j][1];
					sum[j][1] = sum[j + 1][1];
					sum[j + 1][1] = temp;
				}
			}
		for (int n = 0; n < num; n++)
		{
			ID = search(sum[n][0]);
			strcpy_s(a, BOOK[ID].name);
			cout << a << " " << sum[n][1] << "本" << endl;
		}
		file.close();
	}
}
void vieworder()
{
	fstream file;
	file.open("order.txt", ios::in);
	char ID[50], name[50];
	int sum;
	while (file.peek() != -1)
	{
		file.read(ID, 50 * sizeof(char));
		file.read(name, 50 * sizeof(char));
		file.read((char*)&sum, sizeof(int));
		cout << "ID:" << ID << " 书名:" << name << " 数量" << sum << endl;
	}
	file.close();
}#include"StdAfx.h"
#include <iostream>
#include<fstream>
#include<string.h>
#include"globalfunction.h"
#include"adm.h"
#include"book.h"
#include"order.h"
#include"user.h"
extern user B[1000];
extern int Usersum;
extern adm A;
int main(int argc, char** argv) {
	int choice, k;
	char ID[50];
	char key[50];
	char userID[50];
	char userkey[50];
	cout << "1)顾客\n" << "2)管理员" << endl << "登录为:";
	cin >> choice;
	cin.get();
	if (choice == 1)
	{
		userload();
		cout << "1)登陆 2)注册:";
		cin >> choice;
		cin.get();
		if (choice == 1)
		{
		step1:
			cout << "账号:";
			cin >> userID;
			cout << "密码:";
			cin >> userkey;
			for (k = 0; k < Usersum; k++)
			{
				if (B[k].check(userID, userkey))
				{
					cout << "登陆成功!" << endl;
					system("pause");
					system("cls");
					break;
				}
			}
			if (k == Usersum)
			{
				cout << "账号与密码不匹配!" << endl;
				goto step1;
			}
			B[k].way();
		}
		else if (choice == 2)
		{
		step2:
			cout << "账号:";
			cin >> userID;
			if (userID[0] <= '9' && userID[0] >= '0')
			{
				cout << "首位不能为数字!" << endl;
				goto step2;
			}
			if (same(userID))
			{
				cout << "该账号已经注册!" << endl;
				goto step2;
			}
			cout << "密码:";
			cin >> userkey;
			B[Usersum].create(userID, userkey);
			useradd();
			cout << "注册成功!" << endl;
			B[Usersum - 1].way();
		}
	}
	else if (choice == 2)
	{
	step3:
		cout << "账号:";
		cin >> ID;
		cout << "密码:";
		cin >> key;
		if (A.check(ID, key))
			A.way();
		else
		{
			cout << "账号与密码不匹配!";
			goto step3;
		}
	}
	return 0;
}#include"StdAfx.h"
#include"order.h"
#include<string.h>
void order::set(char a[50], char b[50], int c)
{
	strcpy_s(ID, a);
	strcpy_s(name, b);
	sum = c;
}#include"StdAfx.h"
#include<iostream>
#include<string.h>
using namespace std;
#include"person.h"
bool person::check(char a[50], char b[50])
{
	{if (!strcmp(ID, a) && !strcmp(key, b))
		return true;
	else
		return false;
	}
}#include"StdAfx.h"
#include"globalfunction.h"
#include<string>
#include<string.h>
#include"book.h"
#include<fstream>
#include"adm.h"
#include"stdlib.h"
extern adm A;
extern book BOOK[1000];
extern user B[1000];
extern int Booksum;
extern int Usersum;
bool user::check(char a[50], char b[50])
{
	if (!strcmp(ID, a) && !strcmp(key, b))
		return true;
	else
		return false;
}
void user::create(char a[50], char b[50])
{
	strcpy_s(ID, a);
	strcpy_s(key, b);
	memset(history, 0, 2500 * sizeof(char));
	memset(buy, 0, 2500 * sizeof(char));
	for (int k = 0; k < 50; k++)
	{
		buyprice[k] = 0;
		buysum[k] = 0;
	}
	number = 0;
}
void user::way()
{
	int choice;
	int k;
start:
	system("pause");
	system("cls");
	cout << "1)遍历图书 2)购买图书 3)我的购物车 4)我的订单 5)评价图书 6)退出 : ";
		cin >> choice;
	cin.get();
	if (choice != 0)
	{
		if (choice == 1)
		{
			userls();
		}
		else if (choice == 2)
		{
			char name[50], author[50];
			int ID;
		step:
			cout << "1)精准查找 2)模糊查找:";
			cin >> k;
			if (k == 1)
			{
				cout << "1)书名搜索\n2)作者搜索\n3)ID 搜索:";
				cin >> k;
				if (k == 1)
				{
					cout << "书名:";
					cin.get();
					cin.getline(name, 50 * sizeof(char));
					ID = search(name, k);
				}
				if (k == 2)
				{
					cout << "作者:";
					cin.get();
					cin.getline(author, 50 * sizeof(char));
					ID = search(author, k);
				}
				if (k == 3)
				{
					cout << "ID:";
					cin >> k;
					ID = search(k);
				}
				if (ID == -1)
				{
					cout << "没有找到!" << endl;
					goto step;
				}
				else
				{
					cout << "书名:" << BOOK[ID].name << endl << "价格:" << BOOK[ID].price << endl << "总数:"<<BOOK[ID].sum<<endl;
						cout << "作者:" << BOOK[ID].author << endl << "出版社 : "<<BOOK[ID].publish<<endl<<"简介 : "<<BOOK[ID].brief<<endl;
						comment(ID);
					cout << "您是否想购买此书\n1)把此书添加到购物车 2)离开:";
					cin >> choice;
					if (choice == 1)
					{
						int a;
						cout << "购买数量:";
						cin >> a;
						if (a > BOOK[ID].sum)
						{
							cout << "您购买的数量大于库存,无法购买!" << endl;
							goto start;
						}
						if (a < 0)
						{
							cout << "错误的数量!" << endl;
							goto start;
						}
						strcpy_s(buy[number], BOOK[ID].name);
						buyprice[number] = BOOK[ID].price;
						buysum[number] = a;
						number++;
						cout << "添加成功!" << endl;

					}
					userwrite();
					goto start;

				}
			}
			else if (k == 2)
			{
				cout << "输入查找内容:";
				string enter;
				cin >> enter;
				seek(enter);
				goto start;
			}
		}
		else if (choice == 3)
		{
			int b = 0;
			for (int k = 0; k < number; k++)
			{
				cout << buy[k] << " 单价:" << buyprice[k] << " 数量:" << buysum[k] << endl;
				b += buyprice[k] * buysum[k];
			}
			cout << "小计:" << b << "元" << endl;
			if (b != 0)
			{
				cout << "购买?(Y/N):";
				char choice;
				cin >> choice;
				if (choice == 'y' || choice == 'Y')
				{
					fstream file;
					file.open("order.txt", ios::out | ios::app);
					int historynum = 0;
					for (historynum; history[historynum][0] != 0; historynum++)
						;

					for (int k = historynum; k < historynum + number; k++)
						strcpy_s(history[k], buy[k - historynum]);


					for (int l = 0; l < number; l++)
					{
						file.write(ID, 50 * sizeof(char));
						file.write(buy[l], 50 * sizeof(char));
						file.write((char*)&buysum[l], sizeof(int));
					}

					file.close();
					int ID;
					for (int m = 0; m < number; m++)
					{
						ID = search(buy[m], 1);
						BOOK[ID].sum -= buysum[m];
						buyprice[m] = 0;
						buysum[m] = 0;
					}
					memset(buy, 0, 2500 * sizeof(int));
					number = 0;
					cout << "感谢您的购买!" << endl;
					bookwrite();
				}
				userwrite();
			}
			goto start;
		}
		else if (choice == 4)
		{
			for (int k = 0; history[k][0] != 0; k++)
				cout << k + 1 << ": " << history[k] << endl;
			if (history[0][0] == 0)
				cout << "您的购买历史为空!" << endl;
			goto start;
		}
		else if (choice == 5)
}

项目结构:
运行结果截图:




3、主要问题
1)书名查找功能未完善,有时会出现乱码
2)评价系统还未完成

4、改进新代码
1)对存放数据的文件的编码格式进行了更改
2)

点击查看代码
		{
			char name[50], author[50];
			int ID;

			cout << "1)书名搜索\n2)作者搜索\n3)ID 搜索:";
			cin >> k;
			if (k == 1)
			{
				cout << "书名:";
				cin.get();
				cin.getline(name, 50 * sizeof(char));
				ID = search(name, k);
			}
			if (k == 2)
			{
				cout << "作者:";
				cin.get();
				cin.getline(author, 50 * sizeof(char));
				ID = search(author, k);
			}
			if (k == 3)
			{
				cout << "ID:";
				cin >> k;
				ID = search(k);
			}
			if (ID == -1)
			{
				cout << "没有找到!" << endl;
				way();
			}
			else
			{
				cout << "您要评价的书是:" << BOOK[ID].name << endl;
				int power = 0;
				for (int k = 0; history[k][0] != 0; k++)
					if (!strcmp(history[k], BOOK[ID].name))
					{
						power = 1;
						break;
					}
				if (power == 0)
				{
					cout << "您没有购买过此书,无权评论!" << endl;
					goto start;
				}
				else
				{
					cout << "请输入您的评论:";
					char comment[1000];
					cin >> comment;
					fstream file;
					file.open("comment.txt", ios::out | ios::app);
					file.write(BOOK[ID].name, 50 * sizeof(char));
					file.write(comment, 1000 * sizeof(char));
					file.close();
					goto start;
				}
			}
		}
		else if (choice == 6)
			exit(0);
		goto start;
	}

5、重构的软件测试截图

6、总结
在这次代码的二次开发中,我深刻的明白了在写代码之前先理清楚结构的重要性,同时我也明白了代码注释的重要性。不然代码的可读性会大大下降,以至于找到同学本人也一时半会读不懂自己写的代码。完成体量稍大的代码前要有一个清晰的规划,而不是写到哪里就是哪里。

标签:购书,cout,int,50,C++,choice,char,file,二次开发
From: https://www.cnblogs.com/hansumer/p/18052710

相关文章

  • c++在类外是不能直接调用私有成员函数的
    c++在类外是不能直接调用私有成员函数的,比如#include<iostream>usingnamespacestd;classA3{voidshow3(){cout<<"classA3"<<endl;//注意,类内部默认是私有}};intmain(){A3obj3;obj3.show3();//这里出错return0;}......
  • C++U6-06 - 一维线性动态规划
    上节课作业:链接:https://pan.baidu.com/s/17Fei1SuGEk5pnSspf_hprg?pwd=hq04提取码:hq04 动态规划  [最长上升子序列]  本题采用动态规划。数据储存,设定数组a[]用于存储数字序列,设定dp[]数组用于统计上升的序列个数;遍历组数a[],在遍历的过程中如果出现了数......
  • C++ mySQL数据库连接池(windows平台)
    C++MySQL数据库连接池新手学了C++多线程,看了些资料练手写了C++数据库连接池小项目,自己的源码地址关键技术点MySQL数据库编程、单例模式、queue队列容器、C++11多线程编程、线程互斥、线程同步通信和unique_lock、基于CAS的原子整形、智能指针shared_ptr、lambda表达式、生产......
  • C++ 简易STL 教程 与 C++ 标准库
    C++STL(标准模板库)是一套功能强大的C++模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量、链表、队列、栈。C++标准模板库的核心包括以下三个组件:组件描述容器(Containers)容器是用来管理某一类对象的集合。C++提供了各种不......
  • C++U5-第06课-广度优先搜索3
    温故知新广搜的概念,编程实现基本流程 二进制矩阵中的最短路径]    【题意分析】找到一个从(0,0)到达(n-1,n-1)的路径并且路径上每一个数字都为0【思路分析】首先如果grid[0][0]=1,那么显然不存在最短路径,因此输出-1。使用dist[x][y]保存左上角单......
  • 【C++】【OpenCV-4.9.0】灰度图取反(Mat属性的使用)
    此次我们将一张图像转灰度后再进行灰度取反,即黑的变白的,白的变黑的,所以我们需要获取每个像素点上的灰度级,cv中提供了一个函数at,但是这个函数还有11个重载函数,太多了,我们只用这次需要用到的,即通过读取像素点的位置来获取灰度级。◆ at() [3/12]template<typename_Tp>c......
  • 提高C++编译速度
    提高C++编译速度BuildPerformanceInsights-Crascit如何分析和提高大型项目(C/C++)的编译速度?-知乎(zhihu.com)以上链接提供了提高编译速度的方案,以及如何检查是编译哪个文件花的时间最长。实践下来,我采用的方案是直接换用ninja来替代make,结合CMake计时参数,成功将......
  • C语言“贪吃蛇”小游戏二次开发
    引言当谈到经典的电子游戏时,贪吃蛇(Snake)无疑是首选之一。贪吃蛇游戏以简单却具有挑战性的玩法深受玩家喜爱。在游戏中,玩家需要操控一条蛇在游戏界面上不断移动,吃掉食物并尽可能长大,同时避免撞到墙壁或者自己的身体。其简单却耐玩的特点使得贪吃蛇成为了许多人童年时的美好回忆。......
  • C++ 命名空间
    在C++应用程序中。例如,您可能会写一个名为xyz()的函数,在另一个可用的库中也存在一个相同的函数xyz()。这样,编译器就无法判断您所使用的是哪一个xyz()函数。因此,引入了命名空间这个概念,专门用于解决上面的问题,它可作为附加信息来区分不同库中相同名称的函数、类、变量等。......
  • C++系列:const关键字
    前言在学习C++时,const关键字的知识点分散在书的各个章节。当我们尝试在编程时使用const时,总会感觉有一些细节被遗忘,因而不能得心应手地使用const关键字。因此,本篇文章尝试着对const关键字的做一些总结。参考书籍《C++PrimerPlus》const总结这里是我做的关于const关键字的一些......