北林oj数据结构211

本文介绍了如何使用顺序存储结构创建一个图书信息表,通过读入现有图书数据并根据给定位置,插入新图书信息,最后输出更新后的所有图书详情。
摘要由CSDN通过智能技术生成

基于顺序存储结构的图书信息表的新图书的入库

描述

定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的待入库的新图书的位置和信息,将新图书插入到图书表中指定的位置上,最后输出新图书入库后所有图书的信息。

输入

总计n+3行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。之后输入第n+2行,内容仅为一个整数,代表待入库的新图书的位置序号。最后输入第n+3行,内容为新图书的信息,书号、书名、价格用空格分隔。

输出

若插入成功: 输出新图书入库后所有图书的信息(书号、书名、价格),总计n+1行,每行是一本图书的信息,书号、书名、价格用空格分隔。其中价格输出保留两位小数。 若插入失败: 只输出以下提示:抱歉,入库位置非法!

输入样例 1 

7
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
2
9787822234110 The-C-Programming-Language 38.00

输出样例 1

9787302257646 Data-Structure 35.00
9787822234110 The-C-Programming-Language 38.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

输入样例 2 

7
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
9
9787822234110 The-C-Programming-Language 38.00

输出样例 2

Sorry,the position to be inserted is invalid!
#include<iostream>
#include<string>
#include<iomanip>
#define Maxsize 100
using namespace std;

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

struct sqlist {
	Book elem[Maxsize];
	int length;
};

void init(sqlist& L) {
	L.length = 0;
}

void input(sqlist& L) {
	int n;
	cin >> n;
	while (n != 0) {
		n--;
		cin >> L.elem[L.length].no >> L.elem[L.length].name >> L.elem[L.length].price;
		L.length++;
	}	
	return ;	
}

void output(sqlist& L) {
	int m = L.length;
	int xuhao;
	cin >> xuhao;
	Book book;
	cin >> book.no >> book.name >> book.price;
	if (xuhao <= 0 || xuhao > L.length + 1) {
		cout << "Sorry,the position to be inserted is invalid!" << endl;
	}
	else {
		L.length++;
		for (int i = L.length - 2; i >= xuhao - 1; i--) {
			L.elem[i + 1].no = L.elem[i].no;
			L.elem[i + 1].name = L.elem[i].name;
			L.elem[i + 1].price = L.elem[i].price;
		}
		L.elem[xuhao - 1] = book;
	}

	if (m < 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;
		}
	}
	
}


int main() {
	sqlist l;
	init(l);
	input(l);
	output(l);
	
	
	return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值