Data structure: Comments on programming homework -- how to write more complex programs?

The child previewed the data structure course in the summer vacation and wrote an exercise. Let me comment. Let's take a look at the problems with his code.

subject

The topic is to write a linear table related data operation algorithm. Specifically, write a book management program. The information content of the book is as follows:

code

#include<iostream>
#include<string>
#include<stdlib.h>

using namespace std;
//Book structure
struct Book
{
	string ID;//number
	string Name;//title
	double Price;//Price
};
//Book form structure
struct Book_list
{
	struct Book bookarray[10];//Book array (size 10)
	int Book_number;//Number of existing books
};
void addbook(Book_list* abs) {
	if (abs->Book_number < 10) {
		string id;
		cout << "Please enter the book number of the added book:";
		cin >> id;
		abs->bookarray[abs->Book_number].ID = id;
		string name;
		cout << "Please enter the title of the book to add:";
		cin >> name;
		abs->bookarray[abs->Book_number].Name = name;
		double price;
		cout << "Please enter the price of the added book:";
		cin >> price;
		abs->bookarray[abs->Book_number].Price = price;

		abs->Book_number++;
		cout << "Successfully added,";
	}
	else {
		cout << "The library is full, please delete the book information and try to add it!" << endl;
	}
	system("pause");
	system("cls");
}

void insertbook(Book_list* abs) {
	if (abs->Book_number < 10) {
		int line = 0;
		cout << "Please enter the position (number of lines) where the new book will be filled in:";
	
		cin >> line;
		if (line > abs->Book_number) {
			cout << "Please enter a reasonable location!";
		}
		else {
			for (int i = abs->Book_number - 1; i >= line - 1; i--) {
				abs->bookarray[i + 1].ID = abs->bookarray[i].ID;
				abs->bookarray[i + 1].Name = abs->bookarray[i].Name;
				abs->bookarray[i + 1].Price = abs->bookarray[i].Price;
			}
			string id;
			cout << "Please enter the book number of the inserted book:";
			cin >> id;
			abs->bookarray[line - 1].ID = id;
			string name;
			cout << "Please enter the title of the inserted book:";
			cin >> name;
			abs->bookarray[line - 1].Name = name;
			double price;
			cout << "Please enter the price of the inserted book:";
			cin >> price;
			abs->bookarray[line - 1].Price = price;

			abs->Book_number++;
			cout << "Successfully inserted,";
		}
	}
	else {
		cout << "The library is full, please delete the book information and try again!" << endl;
	}
	system("pause");
	system("cls");
}

void deletebook(Book_list* abs) {
	int i;
	bool is;
	cout << "1.Delete by book number" << endl;
	cout << "2.Delete by book title" << endl;
	cout << "The book information can be deleted according to the book number or book name. Please select the deletion option:";
	cin >> i;
	if (i == 1) {
		string id;
		cout << "Please enter the book number you want to select:";
		cin >> id;
		for (int i = 0; i <= abs->Book_number; i++) {
			if (abs->bookarray[i].ID == id) {
				cout << "The following is the book information you are looking for:" << endl;
				cout << endl;
				cout << "ISBN" << "					" << "title" << "					" << "price" << endl;
				cout << abs->bookarray[i].ID << "					" << abs->bookarray[i].Name << "					" << abs->bookarray[i].Price << endl;
				for (int j = i + 1; j < abs->Book_number; j++) {
					abs->bookarray[j - 1].ID = abs->bookarray[j].ID;
					abs->bookarray[j - 1].Name = abs->bookarray[j].Name;
					abs->bookarray[j - 1].Price = abs->bookarray[j].Price;
				}
				abs->Book_number--;
				cout << "Delete succeeded!" << endl;
				is = true;
			}
		}
		if (is == false)
			cout << "Book number does not exist!" << endl;
	}
	else if (i == 2) {
		string name;
		cout << "Please enter the title of the book you want to select:";
		cin >> name;
		for (int i = 0; i <= abs->Book_number; i++) {
			if (abs->bookarray[i].Name == name) {
				cout << "The following is the book information you are looking for:" << endl;
				cout << endl;
				cout << "ISBN" << "					" << "title" << "					" << "price" << endl;
				cout << abs->bookarray[i].ID << "					" << abs->bookarray[i].Name << "					" << abs->bookarray[i].Price << endl;
				for (int j = i + 1; j < abs->Book_number; j++) {
					abs->bookarray[j - 1].ID = abs->bookarray[j].ID;
					abs->bookarray[j - 1].Name = abs->bookarray[j].Name;
					abs->bookarray[j - 1].Price = abs->bookarray[j].Price;
				}
				abs->Book_number--;
				cout << "Delete succeeded!" << endl;
				is = true;
			}
		}
		if (is == false)
			cout << "The title does not exist!" << endl;
	}
	else {
		cout << "Please enter 1 or 2 to select." << endl;
	}
	
	system("pause");
	system("cls");
}

void getbook(Book_list* abs) {
	int i;
	bool is;
	cout << "1.Search by book number" << endl;
	cout << "2.Find by title" << endl;
	cout << "You can find books by book number or title. Please select the query option:";
	cin >> i;
	if (i == 1) {
		string id;
		cout << "Please enter the book number you want to select:";
		cin >> id;
		for (int i = 0; i <= abs->Book_number; i++) {
			if (abs->bookarray[i].ID == id) {
				cout << "The following is the book information you are looking for:" << endl;
				cout << endl;
				cout << "ISBN" << "					" << "title" << "					" << "price" << endl;
				cout << abs->bookarray[i].ID << "					" << abs->bookarray[i].Name << "					" << abs->bookarray[i].Price << endl;
				is = true;
			}
		}
		if(is == false)
			cout << "Book number does not exist!" << endl;
	}
	else if (i == 2) {
		string name;
		cout << "Please enter the title of the book you want to select:";
		cin >> name;
		for (int i = 0; i <= abs->Book_number; i++) {
			if (abs->bookarray[i].Name == name) {
				cout << "The following is the book information you are looking for:" << endl;
				cout << endl;
				cout << "ISBN" << "					" << "title" << "					" << "price" << endl;
				cout << abs->bookarray[i].ID << "					" << abs->bookarray[i].Name << "					" << abs->bookarray[i].Price << endl;
				is = true;
			}
			
		}
		if (is == false)
			cout << "The title does not exist!" << endl;
	}
	else
	{
		cout << "Please enter 1 or 2 to select." << endl;
	}
	
	system("pause");
	system("cls");
}

void showbook(Book_list* abs) {
	cout << "ISBN" << "					" << "title" << "					" << "price" << endl;
	for (int i = 0; i < abs->Book_number; i++) {
		cout << abs->bookarray[i].ID << "					" << abs->bookarray[i].Name << "					" << abs->bookarray[i].Price << endl;
	}
	system("pause");
	system("cls");
}

void showmenu()
{
	cout << "Welcome to the library management system. The following are the system functions:" << endl;
	cout << "1.Add book information" << endl;
	cout << "2.Insert book information" << endl;
	cout << "3.Delete book information" << endl;
	cout << "4.Query book information" << endl;
	cout << "5.Show all book information" << endl;
	cout << "0.Exit the system" << endl;
	cout << "Please select a function option:";
}
int main()
{
	Book_list abs;
	abs.Book_number = 0;
	int select = 0;
	while (true) {
		showmenu();
		cin >> select;
		cout << select << endl;
		switch (select)
		{
		case 1:
			addbook(&abs);
			break;
		case 2:
			insertbook(&abs);
			break;
		case 3:
			deletebook(&abs);
			break;
		case 4:
			getbook(&abs);
			break;
		case 5:
			showbook(&abs);
			break;
		case 0:
			cout << "Thank you for your use" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;

		}
	}
	return 0;
}

First of all, it's amazing to finish this homework quickly. The next question to comment on is also a problem that will occur in the early growth of every programmer. The growth process of programmers is the process of constantly solving one problem after another and constantly surpassing themselves.

The above program has considered how to decompose various functions into several independent functions. It is really rare for beginners to skillfully do this. Next, we will take a small step towards a more "professional" direction and learn about the classic layered architecture design.

Comment 1: establish system architecture awareness

Some of these codes use IO devices such as keyboard and screen, while others do not. We should make a reasonable separation, put the code related to user interface operation into one module, and write the code of other pure algorithms into one module separately.

In the above code, the function showmenu() is well written and encapsulates the menu display into a function. However, there is a problem with the processing of functions such as addbook. These functions mix the operation code of user interaction and data structure into one function. The correct method should separate the data structure operation and write it into a module, and only retain the user interaction in addbook and other functions.

As a simple improvement, I propose the following improvement scheme. It is suggested that the program consists of the following functions:

1. Data Layer

const CAPACITY = 100;
const OK = 0;
const ERROR = -1;

struct Book {
	string id;      //number
	string name;    //title
	double price;   //Price
};

struct BookList {
	Book items[CAPACITY]; //Book array (size 10)
	int count;               //Number of existing books
};

void init_book_list(BookList* list) {
    list->count = 0;
}

int append_book(BookList* list, Book* book) {
    if (list->count < CAPACITY) {
        list->items[count] = *book;
        ++list->count;
        return OK;
    }
    return ERROR;
}

int insert_book(BookList* list, int pos, Book* book) {
    if (pos > 0 && pos < CAPACITY && list->count < CAPACITY) {
        for (int i = list->count - 1; i >= pos; --i) {
            list-items[i + 1] = list->items[i];
        }
        list->items[i] = *book;
        return OK;
    }
    return ERROR;
}

... ...

2. Presentation Layer

void on_menu_add_book(BookList* list) {
    Book book;
	cout << "Please enter the book number of the added book:";
	cin >> book.id;
	cout << "Please enter the title of the book to add:";
	cin >> book.name;
	cout << "Please enter the price of the added book:";
	cin >> book.price;
        
    if (add_book(list, &book) == OK) {
  		cout << "Successfully added!";
    }
    else {
  		cout << "Failed to add!";
    }
}

... ...

In addition, the "pause" system("pause") that appears many times in the program;  system("cls"); These two lines of code are recommended to be placed in the loop of the main function to reduce the redundancy of the code.

Comment 2: further suggestions

Recommendation 1

The code of the data layer can be written into a single module: booklist H and booklist cpp. In this way, this module may be reused in future programming.

Recommendation II

Now that you have learned C + + and used C + +, you might as well use class to realize the design of BookList.

Recommendation III

Pay attention to the naming style of variables, functions and constants, form good habits and write professional code.

In short, there is still a long way to go before you want to be an excellent programmer. I hope you can continue to learn, make continuous progress and gradually improve your programming ability!

Keywords: C Programming data structure

Added by Trojan on Sun, 26 Dec 2021 11:47:44 +0200