写段代码沉淀一下


// booklistOfLibrary.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h>
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
const int  size = 20;

struct bookList
{
	string identifier;//编号
	string bookName;//书名
	string publisher;//出版社
	float bookPrice;//单本书价格
	int bookNum;//书的数量
	float totalPrice;//总价格
};
void storage(bookList list[], int row);//功能1:将文件中的信息存储到内存单元
void countBook(bookList list[], int row);//功能2:统计清华大学出版社和高等教育出版社出版读物的数量。
void findBook(bookList list[], int row);//功能3:寻找总价最低 和最高的书种,并输出价格。
void findBookName(string nameOfbook);//功能4:寻找书籍,并输出其全部信息,若找不到则提示找不到。
double calculateAve(bookList list[], int row);//计算所有种类书籍总价的平均值;

int main()

{
	using namespace std;
	int row = 11;
	bookList list[11];
	storage(list, row);
	countBook(list, row);
	findBook(list, row);
	int ave = calculateAve(list, row);
	cout << "所有种类书籍总价的平均值是:" << ave << endl;
	cout << "请输入需要查询的馆藏读物:" << endl;
	string str;
	cin >> str;
	findBookName(str);
	system("pause");
	return 0;

}
void storage(bookList list[], int row)
{
	using namespace std;
	ifstream inFile;
	int n = 0;
	string str;
	inFile.open("1.txt");
	if (!inFile.is_open())
	{
		cout << "打开文件1.txt失败!!!" << endl;
		exit(EXIT_FAILURE);
	}
	else {
		
		while (!inFile.eof() && n < 11)
		{

			inFile >> str;  list[n].identifier = str;
			inFile >> str;  list[n].bookName = str;
			inFile >> str;  list[n].publisher = str;
			inFile >> str; float a = atof(str.c_str()); list[n].bookPrice = a;
			inFile >> str; int b = atoi(str.c_str()); list[n].bookNum = b;
			inFile >> str; float c = atof(str.c_str()); list[n].totalPrice = c;
			cout << endl;
			cout << list[n].identifier << "\t" << list[n].bookName << "\t" << list[n].publisher << "\t" << list[n].bookPrice << "\t" << list[n].bookNum << endl;

			n++;

		}
	}
	inFile.close();
}
void countBook(bookList list[], int row)
{
	using namespace std;
	//string publisherOne = "清华大学出版社";
	//string publisherTwo = "高等教育出版社";
	int qhCount = 0;
	int gdCount = 0;
	for (int i = 0; i < row; i++)
	{
		if (list[i].publisher.compare("清华大学出版社") == 0) qhCount++;
		else if (list[i].publisher.compare("高等教育出版社") == 0) gdCount++;
	}
	cout << "图书馆中共有清华大学出版社出版的读物" << qhCount << "种" << endl;
	cout << "图书馆中共有高等教育出版社出版的读物" << gdCount << "种" << endl;
}
void findBook(bookList list[], int row)
{
	using namespace std;
	float temp = list[0].totalPrice;
	for (int i = 1; i < row; i++)
	{
		if (temp < list[i].totalPrice)
		{
			temp = list[i].totalPrice;
		}
	}
	cout << "总价最高:" << temp << endl;
	cout << "总价最低:" << list[6].totalPrice << endl;
}
void findBookName(string nameOfbook)
{
	using namespace std;
	ifstream in("1.txt");
	string line, str;
	while (!in.eof())
	{
		in >> str;
		if (nameOfbook.compare(str) == 0)
		{
			getline(in, line);
			cout << line << endl;
		}
			
	}

}double calculateAve(bookList list[], int row)
{
	float sum = 0.0;
	for (int i = 2; i < row; i++)
	{
		sum += list[i].totalPrice;
	}
	return sum/10 ;
}
posted @ 2018-05-26 15:03  20155329胡佩伦  阅读(253)  评论(2编辑  收藏  举报