Reader Management System

In the reader management subsystem of a small library system, a reader's information includes the reader's id, name (name) and the book borrowed (book), assuming that each reader is only allowed to borrow one book (represented by the book number) , the reader list can be implemented with a sequential list. Please complete the corresponding function as required.

input request

First, enter the number of readers n (0<=n<=10), and enter the information of n readers, including id, name, and borrowed books (represented by book numbers), and the data are separated by spaces.
Then, enter the command code and required information. See code and sample input for details.

output requirements

See sample output. Empty tables are not output.

Test Data

input example

5
1000 aaa 1002
2000 bbb -1
3000 ccc 0
4000 ddd 4001
5000 eee 5001
2 0 6000 fff -1
2 6 6000 fff -1
3 7
3 1
4 1000 1000
4 2000 2000
4 3000 3000
5 6000
5 5000
1
-1 

#include <stdio.h>
#include <stdlib.h>

#define NAMELEN 20 // name array length limit
#define MREADER 10 // Reader limit

typedef struct { // struct representing a reader
	int id; // reader id
	char name[NAMELEN]; // reader name
	int book; // The book borrowed by the reader, represented by the book number
						// Assume each reader can only borrow one book
} Reader;				

typedef struct { // reader order table
	Reader *readers; // Reader record
	int length; // current number of readers
} ReaderList;

// Based on user input, initialize a list of n readers readers
// If a reader did not borrow any book, its number is represented by -1
// This function assumes that the data entered by the user is correct
void init(ReaderList &rlist, int n);

// Display the information of all readers in sequence.
// If a reader's id is 1000, his name is abc, and he has borrowed a book numbered 1001,
// Then the output information of the reader is: 1000|abc|1001 (line break)
// If a reader's id is 1000, his name is abc, and he has not borrowed books,
// Then the output information of the reader is: 1000|abc| (line break)
void displayall(ReaderList &rlist);

// Insert reader r at the ith position in the reader list rlist (starting at 1)
// return 1 on success, 0 on failure
int insert(ReaderList &rlist, int i, Reader r);

// delete the ith reader in the reader list rlist (starting from 1)
// return 1 on success, 0 on failure
int del(ReaderList &rlist, int i);

 // The reader whose id is "id" borrows the book whose book number is "book"
 // return 1 on success, 0 on failure
 int borrow(ReaderList &rlist, int id, int book);
 
// Reader with id "id" returns the book
 // return 1 on success, 0 on failure
 int returnB(ReaderList &rlist, int id);
 
intmain()
{
	// variable declaration
	ReaderList rlist; // Reader list
	
	// Temporary variables
	int n; // initial number of users
	int cmd = 0; // operation command, -1 means exit
	int result = 0; // operation result
	int i = -1; // position (number)
	int book, id;		 
	Reader r;			

	// initialize the reader list
	scanf("%d", &n);
	init(rlist, n);
	
	while (scanf("%d", &cmd) && cmd!=-1) { // select operation
		switch (cmd) {
		case 1: // show all readers
			displayall(rlist);
			break;
		case 2: // insert 	
			//printf("Inserting a reader...");
			scanf("%d", &i); // serial number
			// Reader's id, name, and borrowed books (-1 means no books are borrowed).
			scanf("%d%s%d", &r.id, r.name, &r.book);
			result = insert(rlist, i, r);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		case 3: // delete
			//printf("Deleting a reader ...");
			scanf("%d", &i); // serial number
			result = del(rlist, i);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		case 4: // borrow book
			//printf("Borrow a book ...");
			scanf("%d%d", &id, &book); // reader id and book number borrowed
			result = borrow(rlist, id, book);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		case 5: // return book
			//printf("Return a book ...");
			scanf("%d", &id); // reader id
			result = returnB(rlist, id);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		default:
			printf("Unknow Command.\n");
			break;
		}
	}
	return 0;
}

****************************************************************************************************************************

#include <stdio.h>
#include <stdlib.h>

#define NAMELEN 20 // name array length limit
#define MREADER 10 // Reader limit

typedef struct { // struct representing a reader
	int id; // reader id
	char name[NAMELEN]; // reader name
	int book; // The book borrowed by the reader, represented by the book number
						// Assume each reader can only borrow one book
} Reader;				

typedef struct { // reader order table
	Reader *readers; // Reader record
	int length; // current number of readers
} ReaderList;

// Based on user input, initialize a list of n readers readers
// If a reader did not borrow any book, its number is represented by -1
// This function assumes that the data entered by the user is correct
void init(ReaderList &rlist, int n);

// Display the information of all readers in sequence.
// If a reader's id is 1000, his name is abc, and he has borrowed a book numbered 1001,
// Then the output information of the reader is: 1000|abc|1001 (line break)
// If a reader's id is 1000, his name is abc, and he has not borrowed books,
// Then the output information of the reader is: 1000|abc| (line break)
void displayall(ReaderList &rlist);

// Insert reader r at the ith position in the reader list rlist (starting at 1)
// return 1 on success, 0 on failure
int insert(ReaderList &rlist, int i, Reader r);

// delete the ith reader in the reader list rlist (starting from 1)
// return 1 on success, 0 on failure
int del(ReaderList &rlist, int i);

 // The reader whose id is "id" borrows the book whose book number is "book"
 // return 1 on success, 0 on failure
 int borrow(ReaderList &rlist, int id, int book);
 
// Reader with id "id" returns the book
 // return 1 on success, 0 on failure
 int returnB(ReaderList &rlist, int id);
 
intmain()
{
	// variable declaration
	ReaderList rlist; // Reader list
	
	// Temporary variables
	int n; // initial number of users
	int cmd = 0; // operation command, -1 means exit
	int result = 0; // operation result
	int i = -1; // position (number)
	int book, id;		 
	Reader r;			

	// initialize the reader list
	scanf("%d", &n);
	init(rlist, n);
	
	while (scanf("%d", &cmd) && cmd!=-1) { // select operation
		switch (cmd) {
		case 1: // show all readers
			displayall(rlist);
			break;
		case 2: // insert 	
			//printf("Inserting a reader...");
			scanf("%d", &i); // serial number
			// Reader's id, name, and borrowed books (-1 means no books are borrowed).
			scanf("%d%s%d", &r.id, r.name, &r.book);
			result = insert(rlist, i, r);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		case 3: // delete
			//printf("Deleting a reader ...");
			scanf("%d", &i); // serial number
			result = del(rlist, i);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		case 4: // borrow book
			//printf("Borrow a book ...");
			scanf("%d%d", &id, &book); // reader id and book number borrowed
			result = borrow(rlist, id, book);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		case 5: // return book
			//printf("Return a book ...");
			scanf("%d", &id); // reader id
			result = returnB(rlist, id);
			if (result) printf("Succeed\n");
			else printf("Failed\n");
			break;
		default:
			printf("Unknow Command.\n");
			break;
		}
	}
	return 0;
}

// ¸ù¾ÝÓû§ÊäÈ룬³õʼ»¯n¸ö¶ÁÕßµÄÁбíreaders
// Èç¹ûÒ »¸ö¶ÁÕßû ÓнèÔÄÈκÎͼÊé £ ¬Æä ± àºÅÓÃ-1 ± íʾ
// ¸Ãº¯Êý¼ÙÉèÓà »§ÊäÈëµÄÊý¾ÝÊÇÕýÈ · µÄ
void init(ReaderList &rlist, int n){
	rlist.readers = new Reader[MREADER];
	for(int i=0;i<n;i++){
		scanf("%d %s %d",&rlist.readers[i].id,&rlist.readers[i].name,&rlist.readers[i].book);
	}
	rlist.length = n;
}

// ÒÀ´ÎÏÔʾËùÓжÁÕßµÄÐÅÏ¢¡£
// Èç¹ûij¶ÁÕßµÄidΪ1000 £ ¬ÐÕÃûΪabc £ ¬½èÔÄÁË ± àºÅΪ1001µÄͼÊé £ ¬
// Ôò¸Ã¶ÁÕßµÄÊä³öÐÅÏ ¢ Ϊ º 0001000 | abc | 1001 (· ÖÐÐ ·))
// Èç¹ûij¶ÁÕßµÄidΪ1000 £ ¬ÐÕÃûΪabc £ ¬Ã »ÓнèÔÄͼÊé £ ¬
// Ôò¸Ã¶ÁÕßµÄÊä³öÐÅÏ ¢ Ϊ º 0001000 | abc | (· ÖÐÐ · û)
void displayall(ReaderList &rlist){
	for(int i=0;i<rlist.length;i++){
		if(rlist.readers[i].book == -1){
			printf("%d|%s|\n",rlist.readers[i].id,rlist.readers[i].name);			
		}
		else
			printf("%d|%s|%d\n",rlist.readers[i].id,rlist.readers[i].name,rlist.readers[i].book);	
	}
}

// «¶ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂyÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂrdÂÂÂÂÂÂÂÂ‎ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂH‎‎‎‎‎Â‎ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ‎‎‎‎‎‎‎‎‎‎‎‎‎‎,‎‎‎‎‎‎‎‎‎‎‎‎‎”)”)‎
// ³É¹¦ · µ »Ø1 £ ¬Ê§ ° Ü · µ» Ø0
int insert(ReaderList &rlist, int i, Reader r){
	if(i<1 || i>rlist.length+1)
		return 0;
	if(rlist.length == MREADER)
		return 0;
	for(int j=rlist.length-1 ;j>=i-1;j--){
		rlist.readers[j+1] = rlist.readers[j];
	}
	rlist.readers[i-1] = r;
	++rlist.length;
	return 1;
}

// ɾ³ý¶ÁÕßÁбírlistÖеĵÚi¸ö¶ÁÕߣ¨´Ó1¿ªÊ¼£©
// ³É¹¦ · µ »Ø1 £ ¬Ê§ ° Ü · µ» Ø0
int del(ReaderList &rlist, int i){
	if(i<1 || i > rlist.length)
		return 0;
	for(int j=i;j<=rlist.length-1;j++){
		rlist.readers[j-1] = rlist.readers[j];
	}
	rlist.length--;
	return 1;
}

 // idΪ "id" µÄ¶ÁÕß½èÔÄÊéºÅΪ "book" µÄͼÊé
 // ³É¹¦ · µ »Ø1 £ ¬Ê§ ° Ü · µ» Ø0
 int borrow(ReaderList &rlist, int id, int book){
 	for(int i=0;i<rlist.length;i++){ 		
 		if(rlist.readers[i].id == id ){
 			if(rlist.readers[i].book == -1){
 				rlist.readers[i].book = book;
 				return 1;
			}
 			else{
	 			return 0; 				
			 }
		 } 			
	}
	return 0;	 
 }
 
// idΪ "id" µÄ¶ÁÕ߹黹ͼÊé
 // ³É¹¦ · µ »Ø1 £ ¬Ê§ ° Ü · µ» Ø0
 int returnB(ReaderList &rlist, int id){
 	for(int i = 0;i<rlist.length;i++){
 		if(rlist.readers[i].id == id ){
 			if(rlist.readers[i].book ==-1 )
 				return 0;
 			else{
 				rlist.readers[i].book = -1;
				 return 1;
			}
		}
	}
	return 0;
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324480851&siteId=291194637