1. 程式人生 > >基於線性表的圖書管理系統——順序表

基於線性表的圖書管理系統——順序表

第一篇原創尷尬,程式碼寫的不忍直視.....不過還好把它做出來啦偷笑。不多說,上程式碼

#include <iostream>
#include <fstream>
#include <string>
#define MAXSIZE 1000000

using namespace std;

char s0[10],s1[10],s2[10];     //存放3個標題性資訊
int flag = 0;   //圖書資訊讀取標記
int num;     //記錄圖書個數 
float ave;     //記錄所有書平均價格 

typedef struct Book {
	char no[20];
	char name[50];
	float price;
}Book;

typedef struct SqList {
	Book *elem;
	int length;
}Sqlist;

SqList book;


void CreatSqList(SqList &book) {
	book.elem = new Book[MAXSIZE];
	book.length = 0;
}


void Input() {
	int i = 0;

	ifstream inFile("book.txt");
	if(!inFile) {
		cout << "Can't open this file!!" << endl;
		exit(1);
	}
	
	else {
		inFile >> s0 >> s1 >> s2;     //ISBN 書名 定價
		while(!inFile.eof()) {
			inFile >> book.elem[i].no >> book.elem[i].name >> book.elem[i].price;
			i++;
			num = i;
		} 
		inFile.close();
		flag = 1;
		cout << "讀取完畢!" << endl;
	}
}


void Output() {
	int i = 0;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}
	else {
		cout << "書號                 書名               價格:" << '\n';
		while(i<num) {
			cout << book.elem[i].no << "\t" << book.elem[i].name << "\t" << book.elem[i].price << endl;
			i++;
		}
		cout << endl << "資訊顯示完畢!" << endl;
	}
}

void OutputN() {
	cout << "圖書個數為:" << num << endl;
}


void OutputM() {
	int i;
    Book b;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	cout << "價格最高圖書資訊為:" << endl;
	cout << "書號                 書名               價格:" << '\n';
	for(i=0;i<num-1;i++) {
		if(book.elem[i].price>book.elem[i+1].price) {
			b = book.elem[i];
			book.elem[i] = book.elem[i+1];
			book.elem[i+1] = b;
		}
	}

	for(i=0;i<num;i++) {
		if(book.elem[i].price == book.elem[num-1].price) {
			cout << book.elem[i].no << "\t" << book.elem[i].name << "\t" << book.elem[i].price << endl;
		}
	}
}


void Average() {
	int i;
	float sum = 0;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	for(i=0;i<num;i++) {
		sum = sum + book.elem[i].price;
	}

	ave = sum/num;

	cout << "平均價格為:" << ave << endl;
}


void SearchN() {
	int i;
	int n = 0;     //記錄相同書名圖書個數
	char NAME[20];

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	cout << "請輸入需要查詢的書名:" ;
	cin >> NAME;
	cout << endl;

	for(i=0;i<num;i++) {     //字串比較函式strcmp
		if(strcmp(book.elem[i].name,NAME) == 0) {
			cout << "書名            書號             價格:" << endl;
			cout << book.elem[i].name << "\t" << book.elem[i].no << "\t" << book.elem[i].price << endl;
			n = n + 1;
		}
	}
	if(n==0) {
		cout << "沒有您需要的圖書!" << endl;
	}
	while(i<num) {
		cout << book.elem[i].no << "\t" << book.elem[i].name << "\t" << book.elem[i].price << endl;
		i++;
	}
}


void SearchP() {
	int p;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	cout << "請輸入需要查詢圖書的位置:";
	cin >> p;
	cout << endl;

	ifstream inFile("book.txt");
	inFile >> s0 >> s1 >> s2;     //ISBN 書名 定價
	for(int i=0;!inFile.eof();i++) {
		inFile >> book.elem[i].no >> book.elem[i].name >> book.elem[i].price;
		num = i;
	} 
	inFile.close();

	if(p<1 || p>num) {
		cout << "位置輸入錯誤!" << endl;
	}
	else {
		cout << "該圖書資訊為:" << "書號          書名          價格:" << endl;
		cout << book.elem[p-1].no << "\t" << book.elem[p-1].name << "\t" << book.elem[p-1].price << endl;
	}
}


void Insert() {
	int i;  //位置
	int j;
	Book bk;  //插入的圖書

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	cout << "請輸入插入位置:";
	cin >> i;
	cout << endl;
	cout << "請輸入書號:";
	cin >> bk.no;
	cout << "請輸入書名:";
	cin >> bk.name;
	cout << "請輸入價格:";
	cin >> bk.price;

	ifstream inFile("book.txt");
	inFile >> s0 >> s1 >> s2;     //ISBN 書名 定價
	for(i=0;!inFile.eof();i++) {
		inFile >> book.elem[i].no >> book.elem[i].name >> book.elem[i].price;
		num = i;
	} 
	inFile.close();
	
	if(i<1 || i>num+1) {
		cout << "插入位置錯誤!" << endl;
	}
	if(num == MAXSIZE) {
		cout << "沒有位置可以插入!" << endl;
	}
	if(1<i && i<=num+1 && num != MAXSIZE) {
		for(j=num-1;j>=i-1;j--) {
			book.elem[j+1] = book.elem[j];
		}
		book.elem[i-1] = bk;
		++num;
		++book.length;
	}

	ofstream outFile("book.txt");
	outFile << s0 << "\t" << s1 << "\t" << s2 << endl;
	for(j=0;j<num;j++) {
		outFile << book.elem[j].no << "\t" << book.elem[j].name << "\t" << book.elem[j].price << endl; 
	}
}


void Delete() {
	int i;  //位置
	int j;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	ifstream inFile("book.txt");
	inFile >> s0 >> s1 >> s2;     //ISBN 書名 定價
	for(j=0;!inFile.eof();j++) {
		inFile >> book.elem[j].no >> book.elem[j].name >> book.elem[j].price;
		num = j;
	} 
	inFile.close();

	cout << "請輸入將要刪除圖書的位置:";
	cin >> i;

	if(i<1||i>num) {
		cout << "位置輸入錯誤!" << endl;
	}
	else {
		for(j=i;j<=num-1;j++) {
			book.elem[j-1] = book.elem[j];
		}
		--num;
		--book.length;

		ofstream outFile("book.txt");
		outFile << s0 << "\t" << s1 << "\t" << s2 << endl;
		for(j=0;j<num;j++) {
			outFile << book.elem[j].no << "\t" << book.elem[j].name << "\t" << book.elem[j].price << endl; 
		}
	}
}


void Reverse() {   //逆序輸出
	int i,j;
	Book bk;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	ifstream inFile("book.txt");
	inFile >> s0 >> s1 >> s2;     //ISBN 書名 定價
	for(j=0;!inFile.eof();j++) {
		inFile >> book.elem[j].no >> book.elem[j].name >> book.elem[j].price;
		num = j+1;
	} 
	inFile.close();

	for(i=0;i<num/2;i++) {
		bk = book.elem[i];
		book.elem[i] = book.elem[num-i-1];
		book.elem[num-i-1] = bk;
	}

	ofstream outFile("book_inverser.txt");
	outFile << s0 << "\t" << s1 << "\t" << s2 << endl;
	for(i=0;i<num;i++) {
		outFile << book.elem[i].no << "\t" << book.elem[i].name << "\t" << book.elem[i].price << endl; 
	}
	
	cout << "完畢!請輸入數字繼續:" << endl;
}


void Ascende() {   //升序輸出
	int i,j;
	Book bk;

	if(flag==0) {
		cout << "還未讀取圖書!!!" << endl;
	}

	ifstream inFile("book.txt");
	inFile >> s0 >> s1 >> s2;     //ISBN 書名 定價
	for(j=0;!inFile.eof();j++) {
		inFile >> book.elem[j].no >> book.elem[j].name >> book.elem[j].price;
		num = j+1;
	} 
	inFile.close();

	for(i=0;i<num;i++) {
		for(j=0;j<num-i-1;j++) {
			if(book.elem[j].price > book.elem[j+1].price) {
				bk = book.elem[j];
				book.elem[j] = book.elem[j+1];
				book.elem[j+1] = bk;				
			}
		}
	}

	ofstream outFile("book_sort.txt");
	outFile << s0 << "\t" << s1 << "\t" << s2 << endl;
	for(i=0;i<num;i++) {
		outFile << book.elem[i].no << "\t" << book.elem[i].name << "\t" << book.elem[i].price << endl; 
	}
	
	cout << "完畢!請輸入數字繼續:" << endl;
}


int main() {
	int n;
	CreatSqList(book);

	while(1) {
		cout << "************************************************************************" << endl;
		cout << "            |            歡迎進入圖書管理系統~!           |" << endl;
		cout << "            |                                             |" << endl;
		cout << "            |            請輸入數字選擇服務:             |" << endl;
		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 << "            |            9.刪除指定圖書                   |" << endl;
		cout << "            |            10.逆序輸出圖書                  |" << endl;
		cout << "            |            11.升序輸出圖書                  |" << endl;
		cout << "            |            0.退出系統                       |" << endl;
		cout << "***********************************************************************" << endl << endl;

		cin >> n;

		switch(n) {
			case 1:Input();break;
			case 2:Output();break;
			case 3:OutputN();break;
			case 4:OutputM();break;
			case 5:Average();break;
			case 6:SearchN();break;
			case 7:SearchP();break;
			case 8:Insert();break;
			case 9:Delete();break;
			case 10:Reverse();break;
			case 11:Ascende();break;
			case 0:cout << "歡迎再次使用~" << endl;
				   exit(0);	
			default:
					cout << "請選擇正確的操作!!!" << endl;
					break;
		}
	}

	return 0;
}