本次图书系统使用DEV C++软件来进行操作实现。实现语言是c++。
实现图书系统使用到了顺序表的初始化、顺序表的建立、顺序表的插入、顺序表的删除、顺序表的查找等顺序表的基本操作。还使用到了c++的流进行数据的输入。
#include<iostream> #include<fstream> #include <iomanip>//这三个是c++的头文件 #define MAXSIZE 20 using namespace std; typedef int Status; //定义图书类型 typedef struct { string id;//ISBN string name;//书名 double price;//定价 } Book; typedef struct { Book *elem;//存储基地址 int length;//当前长度 }SqList; //顺序表初始化 Status InitList (SqList &L) { L.elem = new Book[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间 if(L.elem == NULL) { cout<< "初始化分配失败!" <<endl; return 0;//初始化分配失败返回0。 } L.length = 0; return 1;//返回一则初始化成功 } //顺序表的取值 Status GetElem (SqList L, int i, Book &e) { if(i < 1 || i > L.length) { cout << "获取值失败,原因:取值不合理!!!" << endl; exit(1);//取值不合理直接退出。 } e = L.elem[i - 1]; return 1;//取值成功返回一。 } //顺序表的查找 Status LocatElem (SqList L, double e) { for(int i = 0; i < L.length; i++) { if(L.elem[i].price == e) { return i + 1;//查找成功则返回序号 i + 1; } } return 0;//查找失败返回0。 } //顺序表的插入 Status ListInsert (SqList &L, int i, Book e) { //首先判断是否可以插入 if(i < 1 || i > L.length + 1) { return 0; } if(L.length == MAXSIZE) { return 0; } for (int j = L.length - 1; j >= i - 1; j--) {//插入位置及其之后的元素向后移动一位。 L.elem[j + 1] = L.elem[j]; } L.elem[i - 1] = e;//将元素e放入到第i个元素。 ++L.length;//顺序表加长。 return 1;//插入成功返回 1。 } //顺序表的删除 Status ListDelete (SqList &L, Status i) { //在顺序表中删除第i个元素,首先判断i的取值范围。 if(i < 1 || i > L.length) { cout << "删除失败!!!"; return 0;//i值不合法,返回0. } for(int j = i; j <= L.length - 1; j++) { L.elem[j - 1] = L.elem[j]; } --L.length; return 1;//删除成功返回1. } //编写主函数调用顺序表的算法 int main() { SqList L; int i = 0, temp = 0, location = 0; int choose; double price = 0; 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 << "8,退出\n"; choose = -1; while (choose != 0) { cout << "请选择:\n"; cin >> choose; switch (choose) { //创建顺序表 case 1: if(InitList(L) == 1) { cout << "创建成功"; } break; //顺序信息输入 case 2: { i = 0; fstream file; file.open("F://数据结构//线性表//book.txt"); if(!file) { cout << "错误! 未找到文件!" << endl; continue; } 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; //顺序表的取值 //调用此函数:GetElem (SqList L, int i, Book &e) case 3: { cout << "请输入一个位置用来取值:\n"; cin >>i; temp = GetElem(L,i,e); if(temp!=0) { cout<< "查找成功\n"; cout<< "第" << i << "本图书的信息是:\n"; cout<< left << setw(15) << e.id << "\t" ///在C++中,setw(int n)用来控制输出间隔。 << left << setw(50) << e.name << "\t" << left << setw(5) << e.price << endl << endl; } else cout<< "查找失败,位置超出范围!\n\n"; } break; //顺序表的查找 //调用此函数:LocatElem (SqList L, double e) case 4: { cout << "请输入要查找的价格:\n"; cin >> price; i = LocatElem(L,price); if (i == 0) { cout << "查找失败!!!"; } else { cout << "此价格商品的序列号为:" << i <<endl; } } break; //顺序表的插入 , //调用此函数:Status ListInsert (SqList &L, int i, Book e) case 5: { cout << "输入插入的位置,书本信息包括:编号 书名 价格"; cin >> location; cin >> e.id >> e.name >> e.price; i = ListInsert(L,location,e); if (i == 1) { cout << "插入成功!!!"; } } break; //顺序表的删除 //调用此函数:Status ListDelete (SqList &L, Status i) case 6: { cout << "请输入所要删除的书籍的位置:"; cin >> location; if(ListDelete(L,location) == 1) { cout << "删除成功!!!"; } } break; //顺序表的输出 case 7: { cout << "当前图书系统的信息(顺序表)读出:\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; case 8: choose = 0; break; default: break; } } return 0; }