目錄
實驗名稱
實驗目的
實驗內容
實驗具體內容:
實驗需求分析:
預期結果:
實驗方法
1. 單鏈表的存盤結構和操作介面
2. 單鏈表操作的實作
3. 除錯運行
實驗結論
實驗最終成果截圖
實驗體會和識訓
附:程式代碼
附加實驗一:基于線性表的圖書管理系統
(一)實驗名稱
(二)實驗目的
(三)實驗內容
(四)程式源代碼
-
實驗名稱
實驗一 單鏈表的基本操作
-
實驗目的
1.理解線性表的邏輯結構;
2.理解單鏈表的存盤結構特點,掌握單鏈表的存盤分配要點;
3.掌握單鏈表的基本操作及實作,并能正確分析其時間復雜度,
-
實驗內容
實驗具體內容:
1. 定義單鏈表的存盤結構;
2. 單鏈表的基本操作
(1)初始化單鏈表(無參和有參);
(2)求單鏈表長度;
(3)按位置查找;
(4)按值查找;
(5)在位置i插入一個資料元素;
(6)洗掉位置i的資料元素;
(7)遍歷單鏈表;
(8)銷毀單鏈表,
實驗需求分析:
在理解線性表邏輯結構,和單鏈表的儲存特點的基礎上,設計、撰寫一段完整代碼,該代碼能夠詳細定義單鏈表的儲存結構,同時完成實驗具體內容中所要求的單鏈表基本操作,在此基礎上,從演算法思想及時間、空間復雜度的角度對代碼進行優化,并獲得關于“單鏈表的基本操作”的最優化代碼,
預期結果:
完成實驗指導書中所要求的具體實驗內容,并對其進行優化,
-
實驗方法
說明:該部分是以直接初始化單鏈表為例,對相關實驗方法及相關操作的實作從理論和演算法的角度進行說明,和最終實作的該實驗題目的源代碼相比較有一定的差異,
1. 單鏈表的存盤結構和操作介面
//LinkList.h 宣告類LinkList
#ifndef LinkList_H
#define LinkList_H
template <class T>
struct Node
{
T data;
Node<T> *next; //此處<T>也可以省略
};
template <class T>
class LinkList
{
public:
LinkList( ); //建立只有頭結點的空鏈表
LinkList(T a[ ], int n); //建立有n個元素的單鏈表
~LinkList(); //解構式
int Length(); //求單鏈表的長度
T Get(int i); //取單鏈表中第i個結點的元素值
int Locate(T x); //求單鏈表中值為x的元素序號
void Insert(int i, T x); //在單鏈表中第i個位置插入元素值為x的結點
T Delete(int i); //在單鏈表中洗掉第i個結點
void PrintList( ); //遍歷單鏈表,按序號依次輸出各元素
private:
Node<T> *first; //單鏈表的頭指標
};
#endif
2. 單鏈表操作的實作
//LinkList.cpp
#include "LinkList.h"
/*
*前置條件:單鏈表不存在
*輸 入:無
*功 能:構建一個單鏈表
*輸 出:無
*后置條件:構建一個單鏈表
*/
template <class T>
LinkList<T>:: LinkList( )
{
first=new Node<T>; first->next=NULL;
}
/*
*前置條件:單鏈表不存在
*輸 入:順序表資訊的陣列形式a[],單鏈表長度n
*功 能:將陣列a[]中元素建為長度為n的單鏈表
*輸 出:無
*后置條件:構建一個單鏈表
*/
template <class T>
LinkList<T>:: LinkList(T a[ ], int n)
{
first=new Node<T>; //生成頭結點
Node<T> *r,*s;
r=first; //尾指標初始化
for (int i=0; i<n; i++)
{
s=new Node<T>; s->data=a[i]; //為每個陣列元素建立一個結點
r->next=s; r=s; //插入到終端結點之后
}
r->next=NULL; //單鏈表建立完畢,將終端結點的指標域置空
}
/*
*前置條件:無
*輸 入:無
*功 能:無
*輸 出:無
*后置條件:無
*/
template <class T>
LinkList<T>:: ~LinkList()
{
}
/*
*前置條件:單鏈表存在
*輸 入:查詢元素位置i
*功 能:按位查找位置為i的元素并輸出值
*輸 出:查詢元素的值
*后置條件:單鏈表不變
*/
template <class T>
T LinkList<T>::Get(int i)
{
Node<T> *p; int j;
p=first->next; j=1; //或p=first; j=0;
while (p && j<i)
{
p=p->next; //作業指標p后移
j++;
}
if (!p) throw "位置";
else return p->data;
}
/*
*前置條件:單鏈表存在
*輸 入:查詢元素值x
*功 能:按值查找值的元素并輸出位置
*輸 出:查詢元素的位置
*后置條件:單鏈表不變
*/
template <class T>
int LinkList<T>::Locate(T x)
{
Node<T> *p; int j;
p=first->next; j=1;
if(p&&p->next){
while(p->data!=x)
{
p=p->next;
j++;
}
return j;
}
else throw "位置";
}
/*
*前置條件:單鏈表存在
*輸 入:插入元素x,插入位置i
*功 能:將元素x插入到單鏈表中位置i處
*輸 出:無
*后置條件:單鏈表插入新元素
*/
template <class T>
void LinkList<T>::Insert(int i, T x)
{
Node<T> *p; int j;
p=first ; j=0; //作業指標p初始化
while (p && j<i-1)
{
p=p->next; //作業指標p后移
j++;
}
if (!p) throw "位置";
else {
Node<T> *s;
s=new Node<T>;
s->data=x; //向記憶體申請一個結點s,其資料域為x
s->next=p->next; //將結點s插入到結點p之后
p->next=s;
}
}
/*
*前置條件:單鏈表存在
*輸 入:無
*功 能:輸出單鏈表長度
*輸 出:單鏈表長度
*后置條件:單鏈表不變
*/
template <class T>
int LinkList<T>::Length( )
{
Node <T> *p = first->next;
int i = 0;
while(p)
{
p = p->next;
i++;
}
return i;
}
/*
*前置條件:單鏈表存在
*輸 入:要洗掉元素位置i
*功 能:洗掉單鏈表中位置為i的元素
*輸 出:無
*后置條件:單鏈表洗掉元素
*/
template <class T>
T LinkList<T>::Delete(int i)
{
Node<T> *p; int j;
p=first ; j=0; //作業指標p初始化
while (p && j<i-1) //查找第i-1個結點
{
p=p->next;
j++;
}
if (!p || !p->next) throw "位置";
//結點p不存在或結點p的后繼結點不存在
else {
Node<T> *q; int x;
q=p->next; x=q->data; //暫存被刪結點
p->next=q->next; //摘鏈
delete q;
return x;
}
}
/*
*前置條件:單鏈表存在
*輸 入:無
*功 能:單鏈表遍歷
*輸 出:輸出所有元素
*后置條件:單鏈表不變
*/
template <class T>
void LinkList<T>::PrintList( )
{
Node<T> *p;
p=first->next;
while (p)
{
cout<<p->data<<endl;
p=p->next;
}
}
3. 除錯運行
//LinkListMain.cpp
#include <iostream.h> //參考輸入輸出流庫函式的頭檔案
#include "LinkList.cpp" //參考單鏈表的類
void main( )
{
LinkList <int> a;
cout<<"執行插入操作:"<<endl;
try
{
a.Insert(1,4);
a.Insert(2,5);
a.Insert(3,6);
}
catch(char* wrong)
{
cout << wrong; //如失敗提示失敗資訊
}
cout<<"已經插入“4,5,6”"<<endl;
cout<<"單鏈表a的長度為:"<<endl;
cout<<a.Length()<<endl; //回傳單鏈表長度
cout<<endl;
cout<<"單鏈表a的元素為:"<<endl;
a.PrintList(); //顯示鏈表中所有元素
cout<<endl;
cout<<"按位查找第二個元素:"<<endl;
cout<<"第二個元素為:";
cout<<a.Get(2)<<endl; //查找鏈表中第二個元素
cout<<endl;
cout<<"按值查找5"<<endl;
cout<<"值為5的元素位置為:";
cout<<a.Locate(5)<<endl; //查找元素5,并回傳在單鏈表中位置
cout<<endl;
cout<<"執行洗掉4的操作"<<endl;
a.Delete(1); //洗掉元素4
cout<<"已洗掉成功,單鏈表a的長度為";
cout<<a.Length()<<endl;
cout<<endl;
cout<<"鏈表a中的元素為:"<<endl;
a.PrintList();
int r[ ]={1,2,3,4,5};
LinkList <int> b(r,5); //根據陣列創建單鏈表
cout<<"執行插入操作前單鏈表b為:"<<endl;
b.PrintList(); //輸出單鏈表所有元素
cout<<"插入前單鏈表b的長度為:";
cout<<b.Length()<<endl;
try
{
b.Insert(3,8);
}
catch(char* wrong)
{
cout << wrong; //如失敗提示失敗資訊
}
cout<<"執行插入操作后單鏈表b為:"<<endl;
b.PrintList(); //輸出單鏈表b所有元素
cout<<"插入后單鏈表b的長度為:";
cout<<b.Length()<<endl;
cout<<endl;
try
{
if(a.Length()){
cout<<"執行洗掉第一個元素操作:"<<endl;
cout<<endl;
b.Delete(1); //洗掉b中第一個元素
cout<<"已洗掉成功,單鏈表b的長度為:";
cout<<b.Length()<<endl;
}
else{
cout<<"順序表b長度為0"<<endl;
}
}
catch(char* wrong)
{
cout << wrong; //如失敗提示失敗資訊
}
cout<<"執行插入操作后單鏈表b為:"<<endl;
b.PrintList(); //輸出單鏈表所有元素
}
-
實驗結論
實驗最終成果截圖

-
實驗體會和識訓
通過這次實驗,我掌握了單鏈表的結構特點和基本操作,鞏固了C++相關的程式設計方法與技術,并且意識到,熟練掌握課本知識是實驗的基礎,不把課本上的相關知識學深學透, 實驗便無從著手, 另外,在做實驗的程序鍛煉思考問題并動手解決的能力很重要,
-
附:程式代碼
#include<iostream>
using namespace std;
typedef int DataType;
#define Node ElemType
#define ERROR NULL
//構建一個節點類
class Node
{
public:
int data; //資料域
Node* next; //指標域
};
//構建一個單鏈表類
class LinkList
{
public:
LinkList(); //構建一個單鏈表;
~LinkList(); //銷毀一個單鏈表;
void CreateLinkList(int n); //創建一個單鏈表
void TravalLinkList(); //遍歷線性表
int GetLength(); //獲取線性表長度
bool IsEmpty(); //判斷單鏈表是否為空
ElemType* Find(DataType data); //查找節點
void InsertElemAtEnd(DataType data); //在尾部插入指定的元素
void InsertElemAtIndex(DataType data, int n); //在指定位置插入指定元素
void InsertElemAtHead(DataType data); //在頭部插入指定元素
void DeleteElemAtEnd(); //在尾部洗掉元素
void DeleteAll(); //洗掉所有資料
void DeleteElemAtPoint(DataType data); //洗掉指定的資料
void DeleteElemAtHead(); //在頭部洗掉節點
private:
ElemType* head; //頭結點
};
//初始化單鏈表
LinkList::LinkList()
{
head = new ElemType;
head->data = 0; //將頭結點的資料域定義為0
head->next = NULL; //頭結點的下一個定義為NULL
}
//銷毀單鏈表
LinkList::~LinkList()
{
delete head; //洗掉頭結點
}
//創建一個單鏈表
void LinkList::CreateLinkList(int n)
{
ElemType* pnew, * ptemp;
ptemp = head;
if (n < 0) { //當輸入的值有誤時,處理例外
cout << "輸入的節點個數有誤" << endl;
exit(EXIT_FAILURE);
}
for (int i = 0; i < n; i++) { //將值一個一個插入單鏈表中
pnew = new ElemType;
cout << "請輸入第" << i + 1 << "個值: ";
cin >> pnew->data;
pnew->next = NULL; //新節點的下一個地址為NULL
ptemp->next = pnew; //當前結點的下一個地址設為新節點
ptemp = pnew; //將當前結點設為新節點
}
}
//遍歷單鏈表
void LinkList::TravalLinkList()
{
if (head == NULL || head->next == NULL) {
cout << "鏈表為空表" << endl;
}
ElemType* p = head; //另指標指向頭結點
while (p->next != NULL) //當指標的下一個地址不為空時,回圈輸出p的資料域
{
p = p->next; //p指向p的下一個地址
cout << p->data << " ";
}
}
//獲取單鏈表的長度
int LinkList::GetLength()
{
int count = 0; //定義count計數
ElemType* p = head->next; //定義p指向頭結點
while (p != NULL) //當指標的下一個地址不為空時,count+1
{
count++;
p = p->next; //p指向p的下一個地址
}
return count; //回傳count的資料
}
//判斷單鏈表是否為空
bool LinkList::IsEmpty()
{
if (head->next == NULL) {
return true;
}
return false;
}
//查找節點
ElemType* LinkList::Find(DataType data)
{
ElemType* p = head;
if (p == NULL) { //當為空表時,報例外
cout << "此鏈表為空鏈表" << endl;
return ERROR;
}
else
{
while (p->next != NULL) //回圈每一個節點
{
if (p->data == data) {
return p; //回傳指標域
}
p = p->next;
}
if (p->data == data)
{
return p;
}
return NULL; //未查詢到結果
}
}
//在尾部插入指定的元素
void LinkList::InsertElemAtEnd(DataType data)
{
ElemType* newNode = new ElemType; //定義一個Node結點指標newNode
newNode->next = NULL; //定義newNode的資料域和指標域
newNode->data = data;
ElemType* p = head; //定義指標p指向頭結點
if (head == NULL) { //當頭結點為空時,設定newNode為頭結點
head = newNode;
}
else //回圈知道最后一個節點,將newNode放置在最后
{
while (p->next != NULL)
{
p = p->next;
}
p->next = newNode;
}
}
//在指定位置插入指定元素
void LinkList::InsertElemAtIndex(DataType data, int n)
{
if (n<1 || n>GetLength()) //輸入有誤報例外
cout << "輸入的值錯誤" << endl;
else
{
ElemType* ptemp = new ElemType; //創建一個新的節點
ptemp->data = data; //定義資料域
ElemType* p = head; //創建一個指標指向頭結點
int i = 1;
while (n > i) //遍歷到指定的位置
{
p = p->next;
i++;
}
ptemp->next = p->next; //將新節點插入到指定位置
p->next = ptemp;
}
}
//在頭部插入指定元素
void LinkList::InsertElemAtHead(DataType data)
{
ElemType* newNode = new ElemType; //定義一個Node結點指標newNode
newNode->data = data;
ElemType* p = head; //定義指標p指向頭結點
if (head == NULL) { //當頭結點為空時,設定newNode為頭結點
head = newNode;
}
newNode->next = p->next; //將新節點插入到指定位置
p->next = newNode;
}
//在尾部洗掉元素
void LinkList::DeleteElemAtEnd()
{
ElemType* p = head; //創建一個指標指向頭結點
ElemType* ptemp = NULL; //創建一個占位節點
if (p->next == NULL) { //判斷鏈表是否為空
cout << "單鏈表空" << endl;
}
else
{
while (p->next != NULL) //回圈到尾部的前一個
{
ptemp = p; //將ptemp指向尾部的前一個節點
p = p->next; //p指向最后一個節點
}
delete p; //洗掉尾部節點
p = NULL;
ptemp->next = NULL;
}
}
//洗掉所有資料
void LinkList::DeleteAll()
{
ElemType* p = head->next;
ElemType* ptemp = new ElemType;
while (p != NULL) //在頭結點的下一個節點逐個洗掉節點
{
ptemp = p;
p = p->next;
head->next = p;
ptemp->next = NULL;
delete ptemp;
}
head->next = NULL; //頭結點的下一個節點指向NULL
}
//洗掉指定的資料
void LinkList::DeleteElemAtPoint(DataType data)
{
ElemType* ptemp = Find(data); //查找到指定資料的節點位置
if (ptemp == head->next) { //判斷是不是頭結點的下一個節點,如果是就從頭部刪了它
DeleteElemAtHead();
}
else
{
ElemType* p = head; //p指向頭結點
while (p->next != ptemp) //p回圈到指定位置的前一個節點
{
p = p->next;
}
p->next = ptemp->next; //洗掉指定位置的節點
delete ptemp;
ptemp = NULL;
}
}
//在頭部洗掉節點
void LinkList::DeleteElemAtHead()
{
ElemType* p = head;
if (p == NULL || p->next == NULL) { //判斷是否為空表,報例外
cout << "該鏈表為空表" << endl;
}
else
{
ElemType* ptemp = NULL; //創建一個占位節點
p = p->next;
ptemp = p->next; //將頭結點的下下個節點指向占位節點
delete p; //洗掉頭結點的下一個節點
p = NULL;
head->next = ptemp; //頭結點的指標更換
}
}
//測驗函式
int main()
{
LinkList l;
int i;
cout << "1.創建單鏈表 2.遍歷單鏈表 3.獲取單鏈表的長度 4.判斷單鏈表是否為空 5.獲取節點\n";
cout << "6.在尾部插入指定元素 7.在指定位置插入指定元素 8.在頭部插入指定元素\n";
cout << "9.在尾部洗掉元素 10.洗掉所有元素 11.洗掉指定元素 12.在頭部洗掉元素 0.退出" << endl;
do
{
cout << "請輸入要執行的操作: ";
cin >> i;
switch (i)
{
case 1:
int n;
cout << "請輸入單鏈表的長度: ";
cin >> n;
l.CreateLinkList(n);
break;
case 2:
l.TravalLinkList();
break;
case 3:
cout << "該單鏈表的長度為" << l.GetLength() << endl;
break;
case 4:
if (l.IsEmpty() == 1)
cout << "該單鏈表是空表" << endl;
else
{
cout << "該單鏈表不是空表" << endl;
}
break;
case 5:
DataType data;
cout << "請輸入要獲取節點的值: ";
cin >> data;
cout << "該節點的值為" << l.Find(data)->data << endl;
break;
case 6:
DataType endData;
cout << "請輸入要在尾部插入的值: ";
cin >> endData;
l.InsertElemAtEnd(endData);
break;
case 7:
DataType pointData;
int index;
cout << "請輸入要插入的資料: ";
cin >> pointData;
cout << "請輸入要插入資料的位置: ";
cin >> index;
l.InsertElemAtIndex(pointData, index);
break;
case 8:
DataType headData;
cout << "請輸入要在頭部插入的值: ";
cin >> headData;
l.InsertElemAtHead(headData);
break;
case 9:
l.DeleteElemAtEnd();
break;
case 10:
l.DeleteAll();
break;
case 11:
DataType pointDeleteData;
cout << "請輸入要洗掉的資料: ";
cin >> pointDeleteData;
l.DeleteElemAtPoint(pointDeleteData);
break;
case 12:
l.DeleteElemAtHead();
break;
default:
break;
}
} while (i != 0);
system("pause");
return 0;
}
-
附加實驗一:基于線性表的圖書管理系統
(一)實驗名稱
基于線性表的圖書管理系統
(二)實驗目的
1.掌握線性表的順序存盤表示和鏈式存盤表示;
2.掌握順序表和鏈表的基本操作,包括建、查找、插入和洗掉等演算法,
(三)實驗內容
實驗具體內容:
圖書資訊表包括以下10項基本操作:圖書資訊表的創建和輸出、排序、修改、逆序存盤、最貴圖書查找、最愛圖書查找、最佳位置圖書查找、新圖書入庫、舊圖書出庫、圖書去重,實驗要求用順序表實作以上的10項基本操作以實作一個圖書館管理系統的基本功能,
實驗需求分析:
在理解線性表邏輯結構,和順序表的儲存特點的基礎上,設計、撰寫一段完整代碼,該代碼能夠實作圖書管理系統包括以下的10項基本操作:圖書資訊表的創建和輸出、排序、修改、逆序存盤、最貴圖書查找、最愛圖書查找、最佳位置圖書查找、新圖書入庫、舊圖書出庫、圖書去重,在此基礎上,從演算法思想及時間、空間復雜度的角度對代碼進行優化,并獲得關于“基于順序表的圖書管理系統”的最優化代碼,
預期結果:
設計一個“基于順序表的圖書管理系統”,實作該系統中所需要的全部功能,并從其他各角度優化該系統,
(四)程式源代碼
#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 << " | 歡迎進入Liocricの圖書管理系統~ |" << 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;
}
如有錯誤歡迎大家批評指正,同時也會根據大家的意見及時對文章進行改正!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357114.html
標籤:其他
