数据结构——基于顺序表管理图书信息

​

#include <iostream>
#include<iomanip>
#include<string>
using namespace std;
#define MAXSIZE 10000

typedef struct
{
	string no;
	string name;
	double price;
}Book;

typedef struct
{
	Book *elem;
	int num;
}SqList;
//价格处理
void Aver(SqList &L)
{
	double aver = 0, sum = 0; int i = 0;
	for (i = 0; i < L.num; i++)
		sum = sum + L.elem[i].price;
	aver = sum / L.num;
	for (i = 0; i < L.num; i++)
	{
		if (L.elem[i].price < aver)
			L.elem[i].price = 1.2*L.elem[i].price;
		else if (L.elem[i].price >= aver)
			L.elem[i].price = 1.1*L.elem[i].price;
	}
	cout << setiosflags(ios::fixed) << setprecision(2) << aver << endl;
}
//根据价格排序
void Sort(SqList &L)
{
	int i = 0, j = 0;
	//冒泡排序
	for (i = 0; i < L.num - 1; i++)
		for (j = 0; j < L.num - 1 - i; j++)
			if (L.elem[j].price < L.elem[j + 1].price)
			{
				Book temp;
				temp = L.elem[j];
				L.elem[j] = L.elem[j + 1];
				L.elem[j + 1] = temp;
			}
}
//输入图书信息
void Input(SqList &L)
{
	int n, i = 0;
	string a, b;
	double c;
	cin >> n;
	while (i<n)
	{
		cin >> a >> b >> c;
		if (i == n)break;
		L.elem[L.num].no = a;
		L.elem[L.num].name = b;
		L.elem[L.num].price = c;
		L.num++;
		i++;
	}
	return;
}
//输出图书信息
void Output(SqList &L)
{
	//cout << L.num << endl;
	int i;
	for (i = 0; i <= L.num - 1; i++)
		cout << L.elem[i].no << " " << L.elem[i].name << " " << setiosflags(ios::fixed) << setprecision(2) << L.elem[i].price << endl;
	return;
}
//初始化
void InitList(SqList &L)
{
	L.elem = new Book[MAXSIZE];
	if (!L.elem) return;
	L.num = 0;
	return;
}
//逆序存储
void Upsidedown(SqList &L)
{
	//逆序存储
	int i;
	for (i = 0; i < L.num / 2; i++)
	{
		Book temp;
		temp = L.elem[i];
		L.elem[i] = L.elem[L.num - 1 - i];
		L.elem[L.num - 1 - i] = temp;
	}

}
//最大价格
void mostexpen(SqList &L)
{
	Book max = L.elem[0];
	int flag[MAXSIZE];
	int i = 0, k = 0;
	for (i = 0; i < L.num - 1; i++)
	{
		if (L.elem[i].price > max.price)
		{
			max = L.elem[i];
		}
	}

	for (i = 0; i < L.num - 1; i++)
	{
		if (L.elem[i].price == max.price)
		{
			flag[k] = i;
			k++;
		}
	}

	cout << k << endl;
	for (i = 0; i <= k-1; i++)
		cout << L.elem[flag[i]].no << " " << L.elem[flag[i]].name << " " << setiosflags(ios::fixed) << setprecision(2) << L.elem[flag[i]].price << endl;
	return;
}
//最爱书籍
void favourite(SqList &L)
{
	int n; int i; int j; string fav[10];	int flag[MAXSIZE]; int k = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> fav[i];
	}

	for (j = 0; j < n; j++)
	{
		for (i = 0; i < L.num - 1; i++)
		{
			if (fav[j] == L.elem[i].name)
			{
				flag[k] = i;
				k++;
			}
		}
		if (k == j)
			cout << "Sorry,there is no your favourite!" << endl;
		else
		{
			cout << k << endl;
			for (i = 0; i < k; i++)
				cout << L.elem[flag[i]].no << " " << L.elem[flag[i]].name << " " << setiosflags(ios::fixed) << setprecision(2) << L.elem[flag[i]].price << endl;
		}

	}

}
//位置查找
void BestLocate(SqList &L)
{
	int n; int i; int j; int no[10]; int k = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> no[i];
	}

	for (j = 0; j < n; j++)
	{
		for (i = 0; i < L.num - 1; i++)
		{
			if (no[j] == i + 1)
			{
				cout << L.elem[i].no << " " << L.elem[i].name << " " << setiosflags(ios::fixed) << setprecision(2) << L.elem[i].price << endl;
				k++;
			}
		}
		if (k == j)
			cout << "Sorry,the book on the best position doesn't exist!" << endl;
	}
}
//添加书籍
void Add(SqList &L)
{
	int no; Book add; int j;
	cin >> no;
	cin >> add.no >> add.name >> add.price;
	if (no > L.num + 1 || no < 1)
	{
		cout << "Sorry,the position to be inserted is invalid!" << endl;
		return;
	}
	for (j = L.num - 1; j >= no - 1; j--)
	{
		L.elem[j + 1] = L.elem[j];
	}
	L.elem[no - 1] = add;
	L.num++;
	Output(L);
	return;
}
//删除书籍
void Del(SqList &L)
{
	int no; int i;
	cin >> no;
	if (no > L.num || no < 1)
	{
		cout << "Sorry,the position to be deleted is invalid!" << endl;
		return;
	}
	for (i = no; i < L.num; i++)
	{
		L.elem[i - 1] = L.elem[i];
	}
	L.num--;
	Output(L);
	return;
}
//两个参数,删除书籍
void Delete(SqList &L, int no)//no为要删除的位置,即数组下标+1 
{
	int i;
	if (no > L.num || no < 1)
	{
		cout << "Sorry,the position to be deleted is invalid!" << endl;
		return;
	}
	for (i = no; i < L.num; i++)
	{
		L.elem[i - 1] = L.elem[i];
	}
	L.num--;
	//Output(L);
	return;
}
//去重操作
void DeleteSame(SqList &L)
{

	int i, j;
	for (i = 0; i < L.num; i++)//遍历L1
	{
		for (j = 1; j < L.num; j++)
			if (L.elem[i].no == L.elem[j].no && i != j)
			{
				Delete(L, j + 1);
			}

	}
	cout << L.num << endl;
	Output(L);
}


int main()
{
	SqList L;
	InitList(L);
	Input(L);
	//Upsidedown(L);
	//Sort(L);
	//Aver(L);
	//Add(L);
	//Del(L);
	DeleteSame(L);
	//BestLocate(L);
	//favourite(L); 
	//Upsidedown(L);
	//mostexpen(L);
	//Output(L);
	return 0;
}

[点击并拖拽以移动]
​

实现了图书管理系统的相关功能

我会继续写下去,即使没有人看hhhh

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值