#include #include #include #include #define OK 1 #define ERROE 0 #define OVERFLOW -2 #define MAXSIZE 100 // 表的最大长度 using namespace std; typedef int status; // 返回值的状态 typedef int ElemType; // 返回值的状态 // 书籍结构体 struct Book { string id; // 编号 string name; // 名字 double price; // 价格 }; // 顺序表的结构体 typedef struct { Book *elem; // 顺序表的地址 int length; // 表长 }Sqlist; // 顺序表的初始化 status InitlistSq(Sqlist &L) { L.elem = new Book[MAXSIZE]; if (!L.elem) { exit(OVERFLOW); } L.length = 0; return OK; } // 顺序表的取值 status GetElems(Sqlist L, int i, Book &e) { if (i<1 || i>L.length) { return ERROE; } e = L.elem[i - 1]; return OK; } // 顺序表的查找 int locateElem_Sq(Sqlist L, double e) { for (int i = 0; i < L.length; i++) { if (L.elem[i].price == e) { return i + 1; } } return 0; // 查找失败 } // 顺序表的插入 status ListInsertSq(Sqlist &L,int i,Book e) { if (i < 1 || (i > L.length + 1)) { return ERROE; } if (L.length == MAXSIZE) { return ERROE; } for (int j = L.length-1; j >=i-1 ; j++) { L.elem[j + 1] = L.elem[j]; } L.elem[i - 1] = e; ++L.length; return OK; } // 顺序表的删除 status ListDeleteSq(Sqlist &L,int i) { if (i < 1 || (i > L.length )) { return ERROE; } for (int j = i; j <= L.length; j++) { L.elem[j - 1] = L.elem[j]; } --L.length; return OK; } int main() { Sqlist L; int i = 0; int temp, a, c, choose = -1; double price; Book e; string hand_1, hand_2, hand_3; cout << "*****************************************************" << endl; cout << "* *" << endl; cout << "* 1 建立 2 录入 3 取值 4 查找 *" << endl; cout << "* *" << endl; cout << "* 5 插入 6 删除 7 输出 0 退出 *" << endl; cout << "* *" << endl; cout << "*****************************************************" << endl; while (choose != 0) { cout << "请选择操作项【0-7】:"; cin >> choose; switch (choose) { case 1: // 创建顺序表 if (InitlistSq(L)) { cout << "顺序表创建成功!" << endl; } else { cout << "顺序表创建失败!" << endl; } break; case 2: // 顺序表信息录入 { int i = 0; L.elem = new Book[MAXSIZE]; if (!L.elem) { exit(OVERFLOW); } L.length = 0; fstream FilePtr; FilePtr.open("book.txt"); if (!FilePtr) { cout << "文件读取失败!" << endl; exit(ERROE); } FilePtr >> hand_1 >> hand_2 >> hand_3; while (!FilePtr.eof()) { FilePtr >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price; i++; } cout << "书籍信息导入成功!" << endl; L.length = i; FilePtr.close(); break; } case 6: // 顺序表的删除 cout << "请输入要删除的图书位置:" << endl; cin >> c; if (ListDeleteSq(L,c)) { cout << "删除成功!" << endl; } else { cout << "删除失败!" << endl; } break; case 7: // 输出顺序表 cout << "当前图书信息管理系统输出数据如下:" << endl; for (int i = 0; i < L.length; i++) { cout << left << setw(15) << L.elem[i].id; cout << left << setw(40) << L.elem[i].name; cout << left << setw(10) << L.elem[i].price << endl; } break; case 0: break; } } return 0; }