// 图书管理系统.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束
//
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
#define MAXSIZE 100
struct Book {
string id;
string name;
double price;
};
typedef struct {
Book* elem;
int length;
}SqList;
Status InitList2(SqList& L)
{//构造一个空的顺序表L
L.elem = new Book[MAXSIZE];//为顺序表分配存储空间
if (!L.elem) exit(OVERFLOW);//存储分配失败退出
L.length = 0;
return OK;
}
Status GetElem(SqList L, int i, Book& e)
{
if (i< 1 || i>L.length)
return ERROR;
e = L.elem[i - 1];
return OK;
}
void LocateElem(SqList L, double e) {
//在顺序表L中查找值为e的数据元素,返回其序号
for (int i = 0; i < L.length; i++)
if (L.elem[i].price == e)
cout << L.elem[i].name << endl;
}
Status ListInsert(SqList& L, int i, Book e)
{//在顺序表L中第i个位置插入新的元素e,i的值的合法范围是1≤i≤L.length+1
if ((i < 1) || (i > L.length + 1)) return ERROR;//i值不合法
if (L.length == MAXSIZE)return ERROR;//储存已满
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 ListDelete(SqList& L, int i)
{//在顺序表L中删除第i个元素,i值的合法范围是1≤i≤L.length
if ((i < 1) || (i > L.length))return ERROR;//i值不合法;
for (int j = i; j < L.length - 1; j++)
L.elem[j - 1] = L.elem[j];
--L.length;
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 << "请选择:";
cin >> choose;
switch (choose) {
case 1:
{
if (InitList2(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 << "错误!未找到文件!" << 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.text 信息完毕\n";
L.length = i;
file.close();
cout << endl;
break;
}
case 3: {
cout << "请输入一个位置用来取值;\n";
cin >> i;
temp = GetElem(L, i, e);
if (temp != 0) {
cout << "查找成功\n";
cout << "第" << i << "本图书的信息是:\n";
cout << e.id << " " << e.name << " " <<e.price << endl;
}
else
cout << "查找失败!位置超出范围\n\n";
break; }
case 4: {
cout << "请输入所要查找价格: ";
cin >> price;
LocateElem(L, price);
break; }
case 5: {
cout << "请输入插入的位置和书本信息,包括:编号 书名 价格:";
cin >> a;
cin >> e.id >> e.name >> e.price;
if (ListInsert(L, a, e))
cout << "插入成功.\n\n";
else
cout << "插入失败.\n\n";
break; }
case 6: {
cout << "请输入所要删除的图书的位置:";
cin >> c;
if (ListDelete(L, c))
cout << "删除成功,\n\n";
else
cout << "删除失败,\n\n";
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;
break; }
}
}
return 0;
}
09-05
3617
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-05
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交