數(shù)據(jù)結(jié)構(gòu)


線性表綜合練習(xí)-圖書數(shù)據(jù)為例
利用book.txt導(dǎo)入數(shù)據(jù)到C語(yǔ)言程序中,輸入所有圖書數(shù)據(jù),對(duì)圖書數(shù)據(jù)進(jìn)行查詢、修改、刪除等算法操作;
參考代碼:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函數(shù)返回值類型,其值是函數(shù)結(jié)果狀態(tài)代碼。
typedef int ElemType; //ElemType 為可定義的數(shù)據(jù)類型,此設(shè)為int類型
#define MAXSIZE 100 //順序表可能達(dá)到的最大長(zhǎng)度
struct Book {
string id;//ISBN
string name;//書名
double price;//定價(jià)
};
typedef struct {
Book *elem; //存儲(chǔ)空間的基地址
int length; //當(dāng)前長(zhǎng)度
} SqList;
Status InitList_Sq(SqList &L) { //算法2.1 順序表的初始化
//構(gòu)造一個(gè)空的順序表L
L.elem = new Book[MAXSIZE]; //為順序表分配一個(gè)大小為MAXSIZE的數(shù)組空間
if (!L.elem)
exit(OVERFLOW); //存儲(chǔ)分配失敗退出
L.length = 0; //空表長(zhǎng)度為0
return OK;
}
Status GetElem(SqList L, int i, Book &e) {//算法2.2 順序表的取值
if (i < 1 || i > L.length)
return ERROR; //判斷i值是否合理,若不合理,返回ERROR
e = L.elem[i - 1]; //elem[i-1]單元存儲(chǔ)第i個(gè)數(shù)據(jù)元素
return OK;
}
int LocateElem_Sq(SqList L, double e) { //算法2.3 順序表的查找
//順序表的查找
for (int i = 0; i < L.length; i++)
if (L.elem[i].price == e)
return i + 1;//查找成功,返回序號(hào)i+1
return 0;//查找失敗,返回0
}
Status ListInsert_Sq(SqList &L, int i, Book e) { //算法2.4 順序表的插入
//在順序表L中第i個(gè)位置之前插入新的元素e
//i值的合法范圍是1<=i<=L.length+1
if ((i < 1) || (i > L.length + 1))
return ERROR; //i值不合法
if (L.length == MAXSIZE)
return ERROR; //當(dāng)前存儲(chǔ)空間已滿
for (int j = L.length - 1; j >= i - 1; j--)
L.elem[j + 1] = L.elem[j]; //插入位置及之后的元素后移
L.elem[i - 1] = e; //將新元素e放入第i個(gè)位置
++L.length; //表長(zhǎng)增1
return OK;
}
Status ListDelete_Sq(SqList &L, int i) { //算法2.5 順序表的刪除
//在順序表L中刪除第i個(gè)元素,并用e返回其值
//i值的合法范圍是1<=i<=L.length
if ((i < 1) || (i > L.length))
return ERROR; //i值不合法
for (int j = i; j <= L.length; j++)
L.elem[j - 1] = L.elem[j]; //被刪除元素之后的元素前移
--L.length; //表長(zhǎng)減1
return OK;
}
int main() {
SqList L;
int i = 0, temp, a, c, choose;
double price;
Book e;
string head_1, head_2, head_3;
cout << "1. 建立\n";
cout << "2. 輸入\n";
cout << "3. 取值\n";
cout << "4. 查找\n";
cout << "5. 插入\n";
cout << "6. 刪除\n";
cout << "7. 輸出\n";
cout << "0. 退出\n\n";
choose = -1;
while (choose != 0) {
cout << "請(qǐng)選擇:";
cin >> choose;
switch (choose) {
case 1://創(chuàng)建順序表
if (InitList_Sq(L))
cout << "成功建立順序表\n\n";
else
cout << "順序表建立失敗\n\n";
break;
case 2: {//順序表信息輸入
i = 0;
L.elem = new Book[MAXSIZE];
if (!L.elem)
exit(OVERFLOW);
L.length = 0;
fstream file;
file.open("book.txt");
if (!file) {
cout << "錯(cuò)誤!未找到文件!" << endl;
exit(ERROR);
}
file >> head_1 >> head_2 >> head_3;
while (!file.eof()) {
file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
i++;
}
cout << "輸入 book.txt 信息完畢\n\n";
L.length = i;
file.close();
}
break;
case 3://順序表的取值
cout << "請(qǐng)輸入一個(gè)位置用來(lái)取值:\n";
cin >> i;
temp = GetElem(L, i, e);
if (temp != 0) {
cout << "查找成功\n";
cout << "第" << i << "本圖書的信息是:\n";
cout << left << setw(15) << e.id << "\t" << left << setw(50)
<< e.name << "\t" << left << setw(5) << e.price << endl
<< endl;
} else
cout << "查找失敗!位置超出范圍\n\n";
break;
case 4: //順序表的查找
cout << "請(qǐng)輸入所要查找價(jià)格:";
cin >> price;
temp = LocateElem_Sq(L, price);
if (temp != 0) {
cout << "查找成功\n";
cout << "該價(jià)格對(duì)應(yīng)的書名為:" << L.elem[temp - 1].name << endl << endl;
} else
cout << "查找失?。](méi)有這個(gè)價(jià)格對(duì)應(yīng)的書籍\n\n";
break;
case 5: //順序表的插入
cout << "請(qǐng)輸入插入的位置和書本信息,包括:編號(hào) 書名 價(jià)格(用空格隔開):";
cin >> a;
cin >> e.id >> e.name >> e.price; //輸入a和b,a代表插入的位置,b代表插入的數(shù)值(書本信息)
if (ListInsert_Sq(L, a, e))
cout << "插入成功.\n\n";
else
cout << "插入失敗.\n\n";
break;
case 6: //順序表的刪除
cout << "請(qǐng)輸入所要?jiǎng)h除的書籍的位置:";
cin >> c;
if (ListDelete_Sq(L, c))
cout << "刪除成功.\n\n";
else
cout << "刪除失敗.\n\n";
break;
case 7: //順序表的輸出
cout << "當(dāng)前圖書系統(tǒng)信息(順序表)讀出:\n";
for (i = 0; i < L.length; i++)
cout << left << setw(15) << L.elem[i].id << "\t" << left
<< setw(50) << L.elem[i].name << "\t" << left
<< setw(5) << L.elem[i].price << endl;
cout << endl;
break;
}
}
return 0;
}
圖書數(shù)據(jù):
ISBN ? ? ? ? 書名 ? ? ? ? ? ? ? 定價(jià)
9787302257646? 程序設(shè)計(jì)基礎(chǔ) 25
9787302219972? 單片機(jī)技術(shù)及應(yīng)用 32
9787302203513? 編譯原理 46
9787811234923? 匯編語(yǔ)言程序設(shè)計(jì)教程 21
9787512100831? 計(jì)算機(jī)操作系統(tǒng) 17
9787302265436? 計(jì)算機(jī)導(dǎo)論實(shí)驗(yàn)指導(dǎo) 18
9787302180630? 實(shí)用數(shù)據(jù)結(jié)構(gòu) 29
9787302225065? 數(shù)據(jù)結(jié)構(gòu)(C語(yǔ)言版) 38
9787302171676? C#面向?qū)ο蟪绦蛟O(shè)計(jì) 39
9787302250692? C語(yǔ)言程序設(shè)計(jì) 42
9787302150664? 數(shù)據(jù)庫(kù)原理 35
9787302260806? Java編程與實(shí)踐 56
9787302252887? Java程序設(shè)計(jì)與應(yīng)用教程 39
9787302198505? 嵌入式操作系統(tǒng)及編程 25
9787302169666? 軟件測(cè)試 24
9787811231557? Eclipse基礎(chǔ)與應(yīng)用 35

