2021-04-19

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include
using namespace std;
#define max 100000
struct book {
char name[20];
char num[20];
float price;
};
struct node {
book data;
int length;
};
void print0(node p) {
int i;
for (i = p.length-1; i >=0; i–)
printf("%s %s %.2f\n", p.data[i].num,
p.data[i].name, p.data[i].price);
}
void print(node p) {
int i;
for (i = 0; i < p.length; i++)
printf("%s %s %.2f\n", p.data[i].num,
p.data[i].name, p.data[i].price);
}
node creat() {
node p;
p.length = 0;
p.data = (book
)malloc(max * sizeof(book));
while (1) {
scanf("%s%s%f",
p.data[p.length].num,
p.data[p.length].name,
&p.data[p.length].price);
if (strcmp(p.data[p.length].num, “0”) == 0
&& strcmp(p.data[p.length].name, “0”) == 0
&& p.data[p.length].price == 0) break;
p.length++;
}
return p;
}
node creat0() {
node p; int i;
scanf("%d", &i);
p.length = 0;
p.data = (book*)malloc(max * sizeof(book));
while (i > 0) {
i–;
scanf("%s%s%f", p.data[p.length].num,
p.data[p.length].name, &p.data[p.length].price);
p.length++;
}
return p;
}
bool sort_price(book a, book b) {
return a.price > b.price;
}
void change(node& p) {
float ave=0;
int i;
for (i = 0; i < p.length; i++) ave += p.data[i].price;
ave /= p.length;
printf("%.2f\n", ave);
for (i = 0; i < p.length; i++) {
if (p.data[i].price < ave) p.data[i].price *= 1.2;
else p.data[i].price = 1.1; }
}
void flove(node p,char
name) {
int i;;
for (i = 0; i < p.length; i++)
if (strcmp(p.data[i].name, name) == 0)
{ printf("%s %s %.2f\n",
p.data[i].num, p.data[i].name, p.data[i].price); break;
}
if (i == p.length) printf(“抱歉,没有你的最爱!\n”);
}
void add(node &p,int m) {
int i;
for (i = p.length; i >= m; i–)
p.data[i] = p.data[i - 1];
scanf("%s %s %f", p.data[i].num,
p.data[i].name, &p.data[i].price);
p.length++;
}
void del(node& p,int m) {
for(m–;m<p.length;m++) p.data[m] = p.data[m + 1];
p.length–;
}
void dell(node& p) {
int a, b;
for (a = 0; a < p.length; a++)
for (b = a + 1; b < p.length; b++)
{
if (strcmp(p.data[a].num, p.data[b].num) == 0
&& strcmp(p.data[a].name, p.data[b].name) == 0
&& p.data[a].price == p.data[b].price) {
del(p, b + 1); b–;
}
}
}
int main(void) {
printf(“1.创建和输出\n2.排序\n3.修改\n4.逆序存储\n
5.最贵图书的查找\n6.最爱图书的查找\n7.最佳位置图书的查找\n
8.新图书的入库\n9.旧图书的出库\n10.图书去重\n”);
int n;
node p;
while (scanf("%d", &n) != EOF) {
printf(“请输入数据:\n”);
if (n == 1) {
p = creat(); printf("%d\n", p.length); print§;
}
if (n == 2) {
p = creat();
sort(p.data, p.data + p.length, sort_price);
print§;
}
if (n == 3) {
p = creat(); change§; print§;
}
if (n == 4) {
p = creat0(); print0§;
}
if (n == 5) {
int sum=0,i;
p = creat0(); sort(p.data,
p.data + p.length, sort_price);
for (i = 0; i < p.length; i++) {
if (p.data[i].price == p.data[0].price)
sum++;
else break;
}
printf("%d\n", sum);
for(i=0;i<sum;i++) printf("%s %s %.2f\n",
p.data[i].num, p.data[i].name, p.data[i].price);
}
if (n == 6) {
p = creat0(); int i;
scanf("%d", &i);
while (i > 0) {
char name[20];
i–;
scanf("%s", name);
flove(p, name);
}
}
if (n == 7) {
p = creat0(); int i;
scanf("%d", &i);
int m;
while (i > 0) {
i–;
scanf("%d", &m);
if (m<1 || m>p.length)
printf(“抱歉,最佳位置上的图书不存在!\n”);
else printf("%s %s %.2f\n", p.data[m-1].num,
p.data[m-1].name, p.data[m-1].price);
}
}
if (n == 8) {
p = creat0(); int m;
scanf("%d", &m); if (m<1 || m>p.length + 1)
printf(“抱歉,入库位置非法!\n”);
else { add(p, m); print§; }
}
if (n == 9) {
p = creat0();
int m;
scanf("%d", &m);
if (m < 1 || m > p.length)
printf(“抱歉,出库位置非法\n”);
else { del(p, m); print§; }
}
if (n == 10) {
p = creat0(); dell§;
printf("%d\n", p.length); print§;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值