Storage of new books in book information table based on sequential storage structure (C + +)

Keywords: C++ data structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, then insert the new book into the specified position in the book table according to the specified position and information of the new book to be warehoused, and finally output the information of all books after the new book is warehoused.

input

Total n+3 lines. First, enter n+1 line. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type. Then enter line n+2, the content is only an integer, representing the position serial number of the new book to be warehoused. Finally, enter line n+3. The content is the information of the new book. The book number, title and price are separated by spaces.

output

If the insertion is successful: output the information (book number, book name and price) of all books after the new book is warehoused, totaling n+1 lines. Each line is the information of a book, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output. If the insertion fails: only the following prompt is output: sorry, the warehousing location is illegal!

Input sample 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

Output sample 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

Input sample 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

Output sample 2

Sorry,the position to be inserted is invalid!

  Idea:

The meaning of the title is that no matter how large the initial array is, it can't be inserted as long as the insertion position is greater than l.length.

The Insert here also starts from 1, which will be simpler. However, note that the Insert function and the Print function have to be changed. Don't forget!!

The input position can be judged in the main function. If it does not meet the requirements, directly output the corresponding prompt and end the program. If it meets the requirements, enter the InStore function.

#include<string>
#include<iostream>
using namespace std;
class Book
{
public:
	string id, name;
	float price;
};
class Sqlist
{
public:
	int length;
	Book* elem;
};
void Init(Sqlist& l)
{
	l.elem = new Book[1000];
	if (!l.elem)
		exit(1);
	l.length = 0;
}
void Insert(Sqlist& l,int n)
{
	for (int i = 1; i <= n; i++)
	{
		l.length++;
		cin >> l.elem[i].id >> l.elem[i].name >> l.elem[i].price;
	}
}
void Print(Sqlist l)
{
	for (int i = 1; i <= l.length; i++)
		printf("%s %s %.2f\n", l.elem[i].id.c_str(), l.elem[i].name.c_str(), l.elem[i].price);
}
void InStore(Sqlist& l, int pos, Book b)
{
	for (int i = l.length; i >= pos; i--)
		l.elem[i + 1] = l.elem[i];
	l.length++;
	l.elem[pos] = b;
	Print(l);
}
int main()
{
	Sqlist l;
	Init(l);
	int t;
	cin >> t;
	Insert(l,t);
	int p;
	cin >> p;
	if (p <= 0 || p > l.length)
		cout << "Sorry,the position to be inserted is invalid!" << endl;
	else
	{
		Book t;
		cin >> t.id >> t.name >> t.price;
		InStore(l, p, t);
	}
	return 0;
}

Posted by andrewgauger on Wed, 13 Oct 2021 12:25:29 -0700