顺序表实践——图书管理(C++和C)

使用顺序表编写的一个图书管理机构,严薇敏《数据结构》一书提供的C++代码,我下面附上C代码。功能包括:

  1. 建立\n";
  2. 输入\n";
  3. 取值\n";
  4. 查找\n";
  5. 插入\n";
  6. 删除\n";
  7. 输出\n";
  8. 退出\n\n";

    \

1.C++代码

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型

#define MAXSIZE 100			//顺序表可能达到的最大长度
struct Book {
	string id;//ISBN
	string name;//书名
	double price;//定价
};
typedef struct {
	Book *elem; //存储空间的基地址
	int length; //当前长度
} SqList;

Status InitList_Sq(SqList &L) { //算法2.1 顺序表的初始化
	//构造一个空的顺序表L
	L.elem = new Book[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
	if (!L.elem)
		exit(OVERFLOW); //存储分配失败退出
	L.length = 0; //空表长度为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]单元存储第i个数据元素
	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;//查找成功,返回序号i+1
	return 0;//查找失败,返回0
}

Status ListInsert_Sq(SqList &L, int i, Book e) { //算法2.4 顺序表的插入
	//在顺序表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; //将新元素e放入第i个位置
	++L.length; //表长增1
	return OK;
}

Status ListDelete_Sq(SqList &L, int i) { //算法2.5 顺序表的删除
	//在顺序表L中删除第i个元素,并用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; //表长减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 << "请选择:";
		cin >> choose;
		switch (choose) {
		case 1://创建顺序表
			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 << "错误!未找到文件!" << 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 << "请输入一个位置用来取值:\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 << "请输入所要查找价格:";
			cin >> price;
			temp = LocateElem_Sq(L, price);
			if (temp != 0) {
				cout << "查找成功\n";
				cout << "该价格对应的书名为:" << L.elem[temp - 1].name << endl << endl;
			} else
				cout << "查找失败!没有这个价格对应的书籍\n\n";
			break;
		case 5: //顺序表的插入
			cout << "请输入插入的位置和书本信息,包括:编号 书名 价格(用空格隔开):";
			cin >> a;
			cin >> e.id >> e.name >> e.price; //输入a和b,a代表插入的位置,b代表插入的数值(书本信息)
			if (ListInsert_Sq(L, a, e))
				cout << "插入成功.\n\n";
			else
				cout << "插入失败.\n\n";
			break;
		case 6: //顺序表的删除
			cout << "请输入所要删除的书籍的位置:";
			cin >> c;
			if (ListDelete_Sq(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;
			cout << endl;
			break;
		}
	}
	return 0;
}


\

2.C语言代码——用指针

//图书管理系统   *顺序表*   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;//Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType;//ElemType 为可定义的数据类型,此设为int类型

#define MAXSIZE 100//顺序表可能达到的最大长度

typedef struct {
    char id[50];//书号 
    char name[50];//书名 
    double price;//定价 
} Book;

typedef struct {
    Book elem[MAXSIZE];//存储结构体的数组 
    int length;
} SqList;

Status InitList_Sq(SqList *L) {//顺序表初始化 
    if (!L)
        return ERROR;
    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];//elem[i-1]单元存储第i个数据元素
    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;//查找成功,返回序号i+1
    return 0;
}

Status ListInsert_Sq(SqList *L, int i, Book e) {// 顺序表的插入
    //在顺序表L中第i个位置之前插入新的元素e
	//i值的合法范围是1<=i<=L.length+1
    if (!L || i < 1 || i > L->length + 1)
        return ERROR;
    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;//将新元素e放入第i个位置
    ++L->length;//表长增1
    return OK;
}

Status ListDelete_Sq(SqList *L, int i) {// 顺序表的删除
    //在顺序表L中删除第i个元素,并用e返回其值
	//i值的合法范围是1<=i<=L.length
    if (!L || i < 1 || i > L->length)
        return ERROR;
    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, temp, a, c, choose;
    double price;
    Book e;
    
    printf("1. 建立\n");
    printf("2. 输入\n");
    printf("3. 取值\n");
    printf("4. 查找\n");
    printf("5. 插入\n");
    printf("6. 删除\n");
    printf("7. 输出\n");
    printf("0. 退出\n\n");

    choose = -1;
    while (choose != 0) {
        printf("请选择:");
        scanf("%d", &choose);
        switch (choose) {
            case 1:
                if (InitList_Sq(&L))
                    printf("成功建立顺序表\n\n");
                else
                    printf("顺序表建立失败\n\n");
                break;
            case 2: {
                i = 0;
                L.length = 0;
                FILE *file;
                file = fopen("book.txt", "r");
                if (!file) {
                    printf("错误!未找到文件!\n");
                    exit(ERROR);
                }
                fscanf(file, "%s", e.id);
                fscanf(file, "%s", e.name);
                fscanf(file, "%lf", &e.price);
                while (!feof(file)) {
                    L.elem[i] = e;
                    i++;
                    fscanf(file, "%s", e.id);
                    fscanf(file, "%s", e.name);
                    fscanf(file, "%lf", &e.price);
                }
                printf("输入 book.txt 信息完毕\n\n");
                L.length = i;
                fclose(file);
            }
                break;
            case 3:
                printf("请输入一个位置用来取值:\n");
                scanf("%d", &i);
                temp = GetElem(L, i, &e);
                if (temp != 0) {
                    printf("查找成功\n");
                    printf("第%d本图书的信息是:\n", i);
                    printf("%-15s\t%-50s\t%-5.2lf\n\n", e.id, e.name, e.price);
                } else
                    printf("查找失败!位置超出范围\n\n");
                break;
            case 4: 
                printf("请输入所要查找价格:");
                scanf("%lf", &price);
                temp = LocateElem_Sq(L, price);
                if (temp != 0) {
                    printf("查找成功\n");
                    printf("该价格对应的书名为:%s\n\n", L.elem[temp - 1].name);
                } else
                    printf("查找失败!没有这个价格对应的书籍\n\n");
                break;
            case 5:
                printf("请输入插入的位置和书本信息,包括:编号 书名 价格(用空格隔开):");
                scanf("%d", &a);
                scanf("%s", e.id);
                scanf("%s", e.name);
                scanf("%lf", &e.price);
                if (ListInsert_Sq(&L, a, e))
                    printf("插入成功.\n\n");
                else
                    printf("插入失败.\n\n");
                break;
            case 6: 
                printf("请输入所要删除的书籍的位置:");
                scanf("%d", &c);
                if (ListDelete_Sq(&L, c))
                    printf("删除成功.\n\n");
                else
                    printf("删除失败.\n\n");
                break;
            case 7: 
                printf("当前图书系统信息(顺序表)读出:\n");
                for (i = 0; i < L.length; i++)
                    printf("%-15s\t%-50s\t%-5.2lf\n", L.elem[i].id, L.elem[i].name, L.elem[i].price);
                printf("\n");
                break;
        }
    }
    return 0;
}
                
                
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九层指针

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值