顺序表的定义、构造、输入、输出、
修改价格、查询最高价格的图书、新图书的入库和输出、旧图书的出库和输出
int Initlist_Sq(SqList& L)
int Input_Sq(SqList& L)
int Output_Sq(SqList L)
int RevisePrice_Sq(SqList& L)
int HighestPrice_Sq(SqList L)
int Insert_Sq(SqList& L)
int Delete_Sq(SqList& L)
#include<iostream>
#include<string.h>
using namespace std;
#define MAXSIZE 100
#define OK 1;
#define OVERFLOW -2;
//顺序存储结构类型的定义
typedef struct {
char no[20];
char name[20];
float price;
}BOOK;
//顺序表的存储结构
typedef struct {
BOOK* elem; //存储空间的基地址且elem类型为BOOK类指针 存储BOOK类数据
int length; //顺序表的当前表长
}Sqlist; //顺序表的结构类型
int Initlist_Sq(Sqlist& L) {
//构造一个空的顺序表
L.elem = new BOOK[MAXSIZE];
if (!L.elem) return OVERFLOW;
L.length = 0;
return OK;
}
int Input_Sq(Sqlist& L) {
//顺序表的输入
for (int i = 0; i < MAXSIZE; i++) {
cin>> L.elem[i].no >> L.elem[i].name >> L.elem[i].price;
if (strcmp(L.elem[i].no, "0") == 0) {
return OK;
}
else {
L.length++;
}
}
return OK;
}
int Output_Sq(Sqlist L) {
//顺序表的输出
if (L.length == 0) {
return NULL;
}
cout << L.length << endl;
for (int i = 0; i < L.length; i++) {
cout << L.elem[i].no <<" "<< L.elem[i].name << " " << L.elem[i].price;
}
return OK;
}
int RevisePrice_Sq(SqList& L) {
//修改价格,先求平均价格
float a;
for (int i = 0; i < L.length; i++) {
a += L.elem[i].price;
}
a = a / L.length;
for (int j = 0; j < L.length; j++) {
if (L.elem[j].price > a) {
L.elem[j].price *= 1.1;
}
else {
L.elem[j].price *= 1.2;
}
}
cout << fixed << setprecision(2) << a << endl;//setprecision 需要#include<iomanip> 用于输出精度控制 (2)表示保留两位有效输出
//fixed << setprecision(2)表示保留两位小数输出
return OK;
}
int HighestPrice_Sq(SqList L)
{//查找价格最高的图书并输出相应图书的信息
int max;
int num=0;
//一、寻找最高价格图书的价格price
for (int i = 1; i < L.length; i++) {
if (L.elem[i].price > L.elem[i - 1].price) {
max = i;
}
}
//二、统计最高价格相同图书的数量num
for (int i = 0; i < L.length; i++) {
if (L.elem[max].price == L.elem[i ].price)
num++;
}
cout << num<< endl;
//三、遍历数组输出L.elem[max].price同价的图书
for (int i = 0; i < L.length; i++) {
if (L.elem[max].price == L.elem[i].price) {
cout << L.elem[i].no << " " << L.elem[i].name << " " << fixed << setprecision(2) << L.elem[i].price << endl;
}
}
return OK;
}
int Insert_Sq(SqList& L)
{//新图书的入库和输出
int i, n;
char no[20];
char name[50];
float price;
cin >> n >> no >> name >> price;
if (n<1 || n>L.length) {
cout << "Sorry,the position to be inserted is invalid!";
}
else {
for (i = L.length; i > n - 1; i--) {
//判断i大于多少取决于入库位置,输入为n,则入库位置为n-1,从L.length到n往后移
//注意elem[L.length]开始为NULL
L.elem[i] = L.elem[i - 1];
}
strcpy(L.elem[n - 1].no, no);
strcpy(L.elem[n - 1].name, name);
L.elem[n - 1].price = price;
}
L.length++;
for (i = 0; i < L.length; i++) {
cout << L.elem[i].no << " " << L.elem[i].name << fixed << setprecision(2) << L.elem[i].price<< endl;
}
return OK;
}
int Delete_Sq(SqList& L)
{//旧图书的出库和输出
int i, n;
cin >> n;
if (n<0 || n>L.length) {
cout << "Sorry,the position to be deleted is invalid!";
}
else {
for (i = n - 1; i < L.length; i++) {
L.elem[i] = L.elem[i + 1];
}
L.length--;
for (int i = 0; i < L.length; i++)
cout << L.elem[i].no << " " << L.elem[i].name << " " << fixed << setprecision(2) << L.elem[i].price << endl;
}
return OK;
}
欢迎指正和交流讨论030