用顺序表实现对图书信息的修改排序代码

9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0

上面是图书信息,要求编写代码进行以下操作

1、修改图书价格信息

高于图书平均价格的图书价格增长为原来的10%低于或等于平均价格的图书价格变为原来的20%

输出所有图书价格信息

2、排序,将所有图书按照价格从大到小排序

输出图书信息

代码;

#include<iostream>
#include<string>
#include<iomanip>
#define TRUE 1
#define FALSE 0
#define MAXSIZE 200
using namespace std;
typedef struct
{
	string ISBN;//图书ISBN号码
	string NAME;//书名
	float PRICE;//图书价格
}Book;

typedef struct
{
	Book* elem;//Book类型数组指针
	int length;//顺序表长
}List;
int InitList_L(List& L);
int InsertList_L(List& L);
int  PrintList_L(List L);
int SortList_L(List L);
int ModifyList_L(List L);

int main()
{
	List L;
	InitList_L(L);
	InsertList_L(L);
	SortList_L(L);
	PrintList_L(L);
	ModifyList_L(L);
	PrintList_L(L);
	return 0;
}

//链表初始化

int InitList_L(List& L)
{
	L.elem = new Book[MAXSIZE];
	if (!L.elem) exit(0);//分配空间失败,结束程序
	L.length = 0;
	return TRUE;
}

int InsertList_L(List& L)
{

	for (int i = 0; i < MAXSIZE; i++)
	{
		cin >> L.elem[i].ISBN >> L.elem[i].NAME >> L.elem[i].PRICE;
		if (L.elem[i].ISBN == "0" && L.elem[i].NAME == "0" && L.elem[i].PRICE == 0)
		{
			return TRUE;//到达结束标志时结束程序
		}
		L.length++;
	}
}

int  PrintList_L(List L)
{
	int i = 0;
	for ( i= 0; i < L.length; i++)
	{
		cout << L.elem[i].ISBN << " " << L.elem[i].NAME << " " << fixed << setprecision(2) << L.elem[i].PRICE << endl;
	}

	return TRUE;
}

int SortList_L(List L)
{
	Book temp;
	for(int i=0;i<L.length-1;i++)
		for(int j=0;j<L.length-i-1;j++)
			if (L.elem[j].PRICE < L.elem[j+1].PRICE)
			{
				temp = L.elem[j];
				L.elem[j] = L.elem[j + 1];
				L.elem[j + 1] = temp;
			}
	return TRUE;
}

int ModifyList_L(List L)
{
	float Average_Price;
	float Sum_Price = 0;
	for (int i = 0; i < L.length; i++)
	{
		Sum_Price += L.elem[i].PRICE;
	}
	Average_Price = Sum_Price / L.length;
	for (int i = 0; i < L.length; i++) {
		if (L.elem[i].PRICE < Average_Price)
			L.elem[i].PRICE = (1 + 0.2) * L.elem[i].PRICE;
		else
			L.elem[i].PRICE = (1 + 0.1) * L.elem[i].PRICE;
	}
	cout << fixed << setprecision(2) << Average_Price << endl;
	return TRUE;
}


版权声明:本文为galaxyrt原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。