Data structure course design preview Report
Design and implementation of library information management system
(1) Experiment content:
Design and implement a library information management system. According to the experimental requirements, the menu and interactive logic of the system are designed, and the functions of addition, deletion, modification and query are coded. The system shall at least include the following functions:
(1) Input book information one by one according to the specified number of books;
(2) Display the relevant information of all books in the book table one by one;
(3) According to the location and information of the new books to be stored, the new books can be inserted into the specified location in the book table;
(4) Delete the book from the book list according to the specified location of the old book to be delivered;
(5) Be able to count the number of books in the table;
(6) Realize the book de duplication of book information table;
(7) Realize the query of favorite books. Search in half according to the book name. It is required to use non recursive algorithm to successfully return the book number and price of this book;
(8) The book information table is modified in batch according to the specified conditions;
(9) Use quick sort to sort books in descending order of price;
(10) Realize the search of the most expensive books;
(2) Experimental tips:
Definition of book information:
public class Book { String no; //8-digit book number String name; //title double price; //Price public Book(String no, String name, double price) { this.no = no; this.name = name; this.price = price; } }
1. Creation and output of book information table based on sequential storage / chain storage structure
Define a sequence table containing book information (book number, book title, price). Read in the corresponding book data to complete the creation of the book information table, then count the number of books in the book table, and output the information of each book line by line.
input
Enter n+1 line, where the first 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. Finally, line n+1 is the input end flag: 0 (three zeros separated by spaces). The book number and title are of string type, and the price is of floating point type.
output
Total n+1 rows. The first row is the number of books in the created book table, and the last n rows are the information of n books (book number
Title and price), each book information occupies one line, and the book number, title and price are separated by spaces. Of which, two are reserved for price output
One decimal place.
Input sample:
9787302257646 programming basis 25.00
9787302164340 fundamentals of program design (version 2) 20.00
978730221972 single chip microcomputer technology and application 32.00
9787302203513 single chip microcomputer principle and application technology 26.00
9787810827430 industrial computer control technology - principle and application 29.00
9787811234923 assembly language programming tutorial 32.00
0 0 0
2. Modification of book information table based on sequential storage / chain storage structure
Read in the book information table, then calculate the average price of all books, increase the price of all books lower than the average price by 20%, increase the price of all books higher than or equal to the average price by 10%, and finally output the book information with revised price line by line.
input
Enter n+1 line, where the first 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. Finally, line n+1 is the input end flag: 0 (three zeros separated by spaces). The book number and title are of string type, and the price is of floating point type.
output
Total n+1 lines. The first line is the average price of all books before modification, and the last n lines are the information (book number, title and price) of n books after price modification. Each book information occupies one line, and the book number, title and price are separated by spaces. Two decimal places are reserved for price output.
Input sample:
9787302257646 programming basis 25.00
9787302164340 fundamentals of program design (version 2) 20.00
978730221972 data mining and machine learning 32.00
9787302203513 pattern recognition and intelligent computing 26.00
9787810827430 industrial computer control technology - principle and application 29.00
9787811234923 operating system tutorial 32.00
0 0 0
Output example:
9787302257646 program design basis 30.00
9787302164340 fundamentals of program design (version 2) 24.00
97873022219972 data mining and machine learning 35.20
9787302203513 pattern recognition and intelligent computing 28.60
9787810827430 industrial computer control technology -- principle and application 31.90
9787811234923 operating system tutorial 35.20
3. Searching the most expensive books in book information table based on sequential storage / chain storage structure
Read in the corresponding book information table, then find the book with the highest price and output the information of the corresponding book.
output
Total m+1 lines, of which the first line is the number of the most expensive books (there may be multiple books with the highest price), and the last m line is the information of the most expensive books. Each book information occupies one line, and the book number, title and price are separated by spaces. Two decimal places are reserved for price output.
Output example:
2
97873022219972 data mining and machine learning 35.20
9787811234923 operating system tutorial 35.20
4. Search of favorite books in book information table based on sequential storage / chain storage structure
Read in the corresponding book information table, and then output the information of the corresponding book according to the name of the specified favorite book.
input
Enter 1 line for the name of the favorite book to be found each time.
output
If the search is successful, k+1 line will be output. For each search, the first line is the number of favorite books, and there may be multiple books with the same title. The last K line is the information of favorite books (book number, book name and price). Each book information occupies one line, and the book number, book name and price are separated by spaces, in which the price output retains two decimal places. If the search fails: only the following prompt is output: sorry, there is no your favorite!
sample output
2
9787302257646 program design basis 30.00
9787302164340 fundamentals of program design (version 2) 24.00
5. New book warehousing of book information table based on sequential storage / chain storage structure
Read in the location and information of the specified new books to be warehoused, insert the new books into the location specified in the book table, and finally output the information of all books after the new books are warehoused.
input
Total n+1 lines, first enter the first line, the content is only an integer, representing the location serial number of the new book to be warehoused, and then enter n lines, the content is the information of the new book, and the book number, title and price are separated by spaces.
output
If the insertion is successful, the information of all books (book number, book name and price) after the new book is put into storage will be output, with a total of 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 will be output: sorry, the storage location is illegal!
Input sample:
2
9787302265436 introduction to computer experiment guide 18.00
Output example:
9787302257646 program design basis 30.00
9787302265436 introduction to computer experiment guide 18.00
9787302164340 fundamentals of program design (version 2) 24.00
97873022219972 data mining and machine learning 35.20
9787302203513 pattern recognition and intelligent computing 28.60
9787810827430 industrial computer control technology -- principle and application 31.90
9787811234923 operating system tutorial 35.20
6. Old book delivery of book information table based on sequential storage / chain storage structure
Read in the book number of the specified old book to be delivered, delete the book from the book list, and finally output the information of all books after the old book is delivered.
input
Enter the book number of the old book to be delivered;
output
If the deletion is successful, the information (book number, book name and price) of all books after the old books are output. Each line is the information of one book, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output.
If the deletion fails, only the following prompt will be output: issue failed, the book is not found!
7. Book duplication removal of book information table based on sequential storage / chain storage structure
The book number (ISBN) of any book published by the publishing house is unique, that is, books with duplicate book numbers are not allowed in the book list. Read in the corresponding book information table (add the record of duplicate book number in advance), then de duplicate the book, that is, delete the book with duplicate book number (only the first one), and finally output the information of all books after de duplication.
output
The total output is m+1 line (m < = n), in which the first line is the number of books after de duplication, and the last m line is the information of books after de duplication (book number, book name and price). Each book information occupies one line, and the book number, book name and price are separated by spaces, in which the price output retains two decimal places.
//book class public class Book { String no; //8-digit book number String name; //title double price; //Price public Book(String no, String name, double price) { this.no = no; this.name = name; this.price = price; } }
//Booksystem import java.util.ArrayList; import java.util.List; public class Booksystem { List<Book> books=new ArrayList<>(); public Booksystem() { } public void insert(Book book){ books.add(book); } public double getaverage(){ double sum=0; for(int i=0;i<books.size();i++){ sum+=books.get(i).price; } return sum/books.size(); } public void printallbook(List<Book> books){ for(int i=0;i<books.size();i++){ System.out.println(books.get(i).no+" "+books.get(i).name+" "+books.get(i).price); } } public List findmostexpensive(){ List<Book> result=new ArrayList<>(); double max=0; for(int i=0;i<books.size();i++){ if(books.get(i).price>max){ max=books.get(i).price; } } for(int i=0;i<books.size();i++){ if(books.get(i).price==max){ result.add(books.get(i)); } } return result; } public List findfavoritebook(String name){ List<Book> result=new ArrayList<>(); for(int i=0;i<books.size();i++){ if(books.get(i).name.contains(name)){ result.add(books.get(i)); } } return result; } public void insertnewbook(int index,Book book){ books.add(index,book); } public boolean removeoldbook(String no){ boolean falg=true; for(int i=0;i<books.size();i++){ if(books.get(i).no.equals(no)){ books.remove(i); falg=false; } } return falg; } public int removeduplicates(){ List<Book> newList = new ArrayList<>(); for (Book book:books) { if(!newList.contains(book)){ newList.add(book); } } books=newList; return books.size(); } }
import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); boolean flag=true; Booksystem booksystem=new Booksystem(); while(flag){ System.out.println("Select 1 to create a book list, 2 to modify the book price, 3 to output the books with the highest price, 4 to find books, 5 to store new books, 6 to store old books, and 7 to remove duplicate books,Select 8 to exit"); String choice=scanner.nextLine(); if(choice.equals("1")){ while (true){ String temp=scanner.nextLine(); if(temp.equals("0 0 0")){ break; }else { String []temps=temp.split(" "); String no=temps[0]; String name=temps[1]; double price=Double.valueOf(temps[2]); booksystem.insert(new Book(no,name,price)); } } System.out.println("Creation complete"); booksystem.printallbook(booksystem.books); }else if(choice.equals("2")){ System.out.println("Original book information"); booksystem.printallbook(booksystem.books); double average=booksystem.getaverage(); for (int i = 0; i < booksystem.books.size(); i++) { if(booksystem.books.get(i).price<average){ booksystem.books.get(i).price=booksystem.books.get(i).price*1.2; }else{ booksystem.books.get(i).price=booksystem.books.get(i).price*1.1; } } System.out.println("After modification"); booksystem.printallbook(booksystem.books); }else if(choice.equals("3")){ List<Book> result=booksystem.findmostexpensive(); System.out.println("The most expensive book"); booksystem.printallbook(result); }else if(choice.equals("4")){ System.out.println("Please enter the title of your favorite book"); List<Book> result=booksystem.findfavoritebook(scanner.nextLine()); if(result.size()==0){ System.out.println("Sorry, not your favorite!"); }else{ booksystem.printallbook(result); } }else if(choice.equals("5")){ System.out.println("Please enter the location where you want to insert the new book"); int index=Integer.parseInt(scanner.nextLine()); System.out.println("Please enter the information you want to insert into your new book"); String temp=scanner.nextLine(); String []temps=temp.split(" "); String no=temps[0]; String name=temps[1]; double price=Double.valueOf(temps[2]); booksystem.insertnewbook(index,new Book(no,name,price)); booksystem.printallbook(booksystem.books); }else if(choice.equals("6")){ System.out.println("Please enter the book number of the old book you want to stock out"); String no=scanner.nextLine(); if(booksystem.removeoldbook(no)){ System.out.println("Delivery failed, the book was not found!"); }else{ booksystem.printallbook(booksystem.books); } }else if(choice.equals("7")){ System.out.println("duplicate removal"); System.out.println(booksystem.removeduplicates()); booksystem.printallbook(booksystem.books); }else if(choice.equals("8")){ System.out.println("see you next time"); flag=false; }else{ System.out.println("Please enter the number as required"); } } } }
2. The search problem of implicit graph
Write A heuristic search (A * algorithm) solution program for the nine house rearrangement problem.
In 3 х On the nine palace chessboard composed of 3, place 8 pieces with numbers of 1 ~ 8. There is a space in the chessboard. The pieces around the space can be moved into the space, so as to change the layout of the chessboard. According to the given initial layout and target layout, an optimal walking sequence is given by programming. Output checkerboard for each state
Test data: initial state: 123456780 target state: 012345678
[input format]
The input contains three lines, three integers in each line, which are nine numbers from 0 to 8, separated by a space symbol to identify the initial state of the problem. 0 indicates a space, for example:
2 0 3
1 8 4
7 6 5
Experimental tips:
To solve the problem of nine house rearrangement, we should first consider whether there is an answer. Each state can be considered as a 1 × 9, that is, through the transformation of the matrix, it can be transformed into the matrix corresponding to the target state. From the mathematical knowledge, we can calculate the reverse order values of the two ordered sequences. If both are even or odd, they can be reached through transformation. Otherwise, the two states cannot be reached. In this way, we can judge whether the problem is solvable before solving the problem, so as to avoid unnecessary search.
If the initial state can reach the target state, what method should be adopted? The commonly used state space search methods are depth first and breadth first. One big drawback of breadth first search and depth first search is that they are exhaustive in A given state space. This is A very suitable algorithm when the state space is small, but it is not desirable when the state space is very large and unpredictable. His efficiency is too low to even complete. Due to the rearrangement of the nine palaces, the state space has A total of 9! There are 9 states. If the initial state and target state are selected, there are 9/ Two states need to be searched. Considering the limitations of time and space, A algorithm is required here.
Algorithm A is A heuristic search algorithm. Heuristic search is to evaluate each search branch in the state space to get the best branch, and then search from this branch to the target. In this way, A large number of fearless search paths can be omitted, and efficiency is mentioned. In heuristic search, the current information related to the problem is used as heuristic information to guide the search. These information can effectively omit A large number of unnecessary search paths and greatly improve the search efficiency.
The heuristic search algorithm defines an evaluation function f(n). The heuristic information related to the problem is calculated as a certain value of f(n) and introduced into the search process. f(n) = g(n) +h(n), where f(n) is the evaluation function of node n, g(n) is the actual cost from the initial node to node n in the state space, and h(n) is the estimated cost of the best path from node n to the target node. In the nine house rearrangement problem, it is obvious that g(n) is the number of steps moved from the initial state to the current state. The estimation function h(n) estimates the distance from node n to the target node. We can estimate it by Euclidean distance, Manhattan distance or the dislocation number of numbers in the two section state.
Experimental tips:
1. Definition of storage structure
typedef struct node / / eight digit structure
{
int nine[N][N];// Digital status
int f;// Valuation
int direct;// Space movement direction
struct node *parent;// Parent node
}pNode;
2. Description of heuristic search algorithm:
(1) Put the initial node S0 into the Open table, f(S0)=g(S0)+h(S0);
(2) If the Open table is empty, the problem will not be solved, and the system will exit after failure;
(3) Take out the first node of the Open table and put it into the Closed table, and note that the node is n;
(4) Check whether node n is the target node. If yes, find the solution to the problem and exit successfully;
(5) If node n is not extensible, go to step (2);
(6) Expand node n, generate child nodes ni(i=1,2,......), calculate the evaluation value f(ni) (i=1,2,......) of each child node, set a pointer to the parent node for each child node, and then put these child nodes into the Open table;
(7) Reorder all nodes in the Open table from small to large according to the evaluation function value of each node;
(8) Go to step (2).
Working process of heuristic search algorithm: