C++ Object-Oriented Technology and Programming Course Design——Library Information Management System

Table of contents

foreword

1. Basic requirements

2. Flowchart

 3. Code

1. Class creation 

2. Enter book information

3. Delete book information

4. Insert book information

5. Find book information

6. Overall code

Summarize


foreword

Come to a rendering first

 

1. Basic requirements

The book information management system can realize the following functions: the system works in menu mode, book information input function (book information is saved in files); book information includes: accession number, book title, author name, classification number, publishing unit, publishing time, price ; Book information browsing function (output all book information); query and sorting functions: query by book title (display all books with the same name), query by author name (all books by this author); delete, insert and modify book information. Entering any other character (incorrect input) will display the output result of its incorrect input.

2. Flowchart

The interface displays the menu. Through the user's input, the switch judges and calls the corresponding method, and displays the corresponding information. After finishing this function, the interface displays the question: "Continue?" If the user inputs, if it is canceled, it exits the system; if it is confirmed, Then refresh the page to display the menu.

 3. Code

1. Class creation 

A total of two classes are created. The book class stores various information of the books, the library class stores the objects of each book, the single linked list pointer (ie the address of the library class object) and various functions.

class book
{
public:
    char name[100];
    char num[100];
    char tel[100];
    char name1[100];
    char time[100]; 
    char cla[100];
    char adress[100];
};

class library
{
	public:
	    book data;//对象
	    library *next;//单链表指针
	    void londecreat(library L, int n);//录入信息
	    void display();//显示信息
	    void sort();//排序信息
	    void findname(char* arr);//查找作者名
	    void findbook(char* arr);//查找图书名
	    void delete1(library &L, char* arr);//删除信息
	    void modify(library &L, char* arr, char* arr1, char* arr2, 
		char* arr3, char* arr4, char* arr5, char* arr6, char* arr7);//修改信息
	    void insert(library &L, char* arr, char* arr2, char* arr3, 
		char* arr4, char* arr5, char* arr6,char* arr7,char* arr8);//插入信息
};

2. Enter book information

The actual parameter is the object L of the library class and input n books, create n object pointers of the library class, allocate memory for the object pointers, store the corresponding book information, complete the creation of the singly linked list, and write to the file.

void library::londecreat(library L, int n)
{
    L.next = NULL;
    library *p, *q;
    q = &L; //地址相同 
    int i = 0;
    cout << "请您依次输入登录号,图书名,作者名,分类号,出版单位,出版时间,价格" << endl;
    for (i = 0; i < n; i++)
    {
    	if(i>0)
		{
    		cout<<"请输入第"<<i+1<<"本书"<<endl;
		} 
        p=new library();
        cin >> p->data.name;
        cin >> p->data.num;
        cin >> p->data.tel;
        cin >> p->data.cla;
        cin >> p->data.name1;
        cin >> p->data.time;
        cin >> p->data.adress;
        p->next = NULL;
        q->next = p; //等于用这条语句:q->next = p;
        q = p;
    }
    ofstream ofs;
    ofs.open("text.txt", ios::out);
    library *S;
    S = L.next;
    while (S)
    {
        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
        S = S->next;
    }
    ofs.close();
}

Create a singly linked list diagram as follows:

3. Delete book information

The logic of deleting the book information is also basically the same. Reassign the address, complete the singly linked list, and then release the memory of the object to be deleted. The free function is used here, and then write to the file. Note: The actual parameter L here must be an address, otherwise the modified address is only valid in the method.

void library::delete1(library &L, char* arr)
{
	library *S;
	S = L.next;
    while(S)
    {
    	S = S->next;
	}
    char a[100], b[100];
    int i = 0, j = 0, count = 0;
    strcpy(a, arr);
    while (*arr != '\0')
    {
        count++;
        arr++;
    }
    library *p, *q, *r;
    q = &L;
    p = L.next;
    while (p != NULL)
    {
        strcpy(b, p->data.name);
        i = 0, j = 0;
        while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0')
        {
            i++;
            j++;
        }
        if (i == count)
        {
            r = p;
            while (q)
            {
                while (q->next == r)
                {
                    q->next = q->next->next;
                    free(r);
                    cout << "删除图书信息成功!" << endl;
                    ofstream ofs;
                    ofs.open("text.txt", ios::out);
                    library *S;
                    S = L.next;
                    while (S)
                    {
                        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " "<< S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
                        S = S->next;
                    }
                    ofs.close();
                    return;
                }
                q = q->next;
            }
        }
        p = p->next;
    }
    cout<<"查无此书,删除失败"<<endl;
 
}

The single linked list diagram is as follows:

4. Insert book information

The logic of inserting book information is also the same, add a new object pointer s, assign it to memory, reallocate the singly linked list, and then write it into the file.

void library::insert(library &L, char* arr, char* arr2, char* arr3, char* arr4, char* arr5, char* arr6,char* arr7,char* arr8)
{
    library *p, *q, *s;
    char a[100], b[100];
    int count = 0, i = 0, j = 0;
    s = new library();
    strcpy(s->data.name, arr2);
    strcpy(s->data.num, arr3);
    strcpy(s->data.tel, arr4);
    strcpy(s->data.cla, arr5);
    strcpy(s->data.name1, arr6);
    strcpy(s->data.time, arr7);
    strcpy(s->data.adress, arr8);
    strcpy(a, arr);
    while (*arr != '\0')
    {
    	
        count++;
        arr++;
    }
    q = &L;
    p = L.next;
    while (p != NULL)
    {
        strcpy(b, p->data.name);
        i = 0, j = 0;
        while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0')
        {
            i++;
            j++;
        }
        if (i == count)
        {
            while (q)
            {
                while (q->next == p)
                {
                    q->next = s;
                    s->next = p;
                    cout << "插入成功" << endl;
                    ofstream ofs;
                    ofs.open("text.txt", ios::out);
                    library *S;
                    S = L.next;
                    while (S)
                    {
                        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
                        S = S->next;
                    }
                    ofs.close();
                    S = L.next;
                    return;
                }
                q = q->next;
            }
        }
        p = p->next;
    }
    cout<<"查无此书,插入失败"<<endl;
}

The single linked list diagram is as follows:

5. Find book information

Output the information in the file into an array, compare it with the information input by the user, and use the strcmp function (comparing the size of two character arrays). Once the matching is successful, the book information will be output, because the book information is output sequentially, and the book information of the title of the book to be searched (there are seven information for each book) is in the second, then an array must be created to save the first A book information (book number).

void library::findbook(char* arr)
{
    char a[100];
    strcpy(a, arr);
    ifstream ifs;
    ifs.open("text.txt", ios::in);
    char buf[10000] = { 0 };
    string buf1;
    int i = 0,find=0;
    while (ifs >> buf)
    {
        if (i == 5)
        {
            cout << buf << " "<<endl;
            i=0;
            find=1;
        }
        if (i >= 1&&i<5)
        {
            cout << buf << " ";
            i++;
        }
        int n = strcmp(a, buf); //比较两个字符串的大小 
        if(n!=0)
        {
        	buf1=buf;
        	
		}
        if (n == 0)
        {
        	cout << buf1 << " ";
            cout << buf << " ";
            i++;
        }
    }
    ifs.close();
    if(find==0)
	{
    	cout<<"查无此书"<<endl;
	}
}

6. Overall code

There is no singly linked list in the memory at the beginning of operation, so you need to add a code to create a singly linked list in the main function, which is the same as the method of entering book information, that is, change keyboard input to file input.

#include<iostream>
#include<stdio.h>
#include<string>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include <windows.h>
using namespace std;

class book
{
public:
    char name[100];
    char num[100];
    char tel[100];
    char name1[100];
    char time[100]; 
    char cla[100];
    char adress[100];
};

class library
{
	public:
	    book data;//对象
	    library *next;//单链表指针
	    void londecreat(library L, int n);//录入信息
	    void display();//显示信息
	    void sort();//排序信息
	    void findname(char* arr);//查找作者名
	    void findbook(char* arr);//查找图书名
	    void delete1(library &L, char* arr);//删除信息
	    void modify(library &L, char* arr, char* arr1, char* arr2, 
		char* arr3, char* arr4, char* arr5, char* arr6, char* arr7);//修改信息
	    void insert(library &L, char* arr, char* arr2, char* arr3, 
		char* arr4, char* arr5, char* arr6,char* arr7,char* arr8);//插入信息
};

void color(short x)//改变颜色
{
    if(x>=0 && x<=15)
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
    else
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

void library::londecreat(library L, int n)
{
    L.next = NULL;
    library *p, *q;
    q = &L; //地址相同 
    int i = 0;
    cout << "请您依次输入登录号,图书名,作者名,分类号,出版单位,出版时间,价格" << endl;
    for (i = 0; i < n; i++)
    {
    	if(i>0)
		{
    		cout<<"请输入第"<<i+1<<"本书"<<endl;
		} 
        p=new library();
        cin >> p->data.name;
        cin >> p->data.num;
        cin >> p->data.tel;
        cin >> p->data.cla;
        cin >> p->data.name1;
        cin >> p->data.time;
        cin >> p->data.adress;
        p->next = NULL;
        q->next = p; //等于用这条语句:q->next = p;
        q = p;
    }
    ofstream ofs;
    ofs.open("text.txt", ios::out);
    library *S;
    S = L.next;
    while (S)
    {
        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
        S = S->next;
    }
    ofs.close();
}
void library::display()
{
    ifstream ifs;
    ifs.open("text.txt", ios::in);
    char buf[10000] = { 0 };
    int i = 0;
    cout << "下面是您录入的信息" << endl;
    while (ifs >> buf)
    {
        if (i % 7 == 0)
        {
            cout << endl;
        }
        cout << buf << " ";
        i++;
    }
    ifs.close();
}
void library::sort()
{
    fstream ifs;
    ifs.open("text.txt", ios::in);
    char buf[10000] = { 0 };
    double clanum[100];  //储存分类号 
    int i = 0,cla=0,a=1;
    while (ifs >> buf)
    {
        if (i % 7 == 3)
        {
        	int x=0;
        	while(buf[x]!='\0')
			{        	   		
				x++;
			}
			x--;
			while(x>=0)
			{
				clanum[cla]=clanum[cla]+(buf[x]-'0')*a;
				x--;
				a=a*10;
				if(x==-1)
				{
					cla++;
				}
			}
        }
        i++;
        a=1;
    }
    ifs.close();
    
    int f=1;
    while (f==1)
    {
    	// 判断分类号的大小,找到最大的号记下位置并删除 
    	int big=0,k=1,t=1;
    	while(t==1)
    	{
    		if(clanum[big]>=clanum[k])
			{
    			k++;
			}
			else
			{
				big=k;
			}
			if(clanum[k]==0)
			{
				t=0;
				clanum[big]=-1;
				cla--;
			}
		}
		
		
    	ifs.open("text.txt", ios::in);
    	for(i=0;i<(big*7+7);i++) 
    	{
    		ifs>>buf;
    		if(i>=big*7)
    		{
    			cout << buf << " ";
			}
		}
		ifs.close();
		
		cout<<endl;
		if(cla==0)
		{
			f=0;
		}
		
		
	}
}

void library::findname(char* arr)
{
    char a[100];
    strcpy(a, arr);
    ifstream ifs;
    ifs.open("text.txt", ios::in);
    char buf[10000] = { 0 };
    string buf1;
    string buf2;
    int i = 0,find=0;
    while (ifs >> buf)
    {
        if (i == 5)
        {
            cout << buf << " "<<endl;
            i=0;
            find=1;
        }
        if (i >= 1&&i<5)
        {
            cout << buf << " ";
            i++;
        }
        int n = strcmp(a, buf); //比较两个字符串的大小 
        if(n!=0)
        {
        	buf2=buf1;
        	buf1=buf;
		}
        if (n == 0)
        {
        	cout << buf2 << " ";
        	cout << buf1 << " ";
            cout << buf << " ";
            i++;
        }
    }
    ifs.close();
    if(find==0)
	{
    	cout<<"查无此书"<<endl;
	}
}
void library::findbook(char* arr)
{
    char a[100];
    strcpy(a, arr);
    ifstream ifs;
    ifs.open("text.txt", ios::in);
    char buf[10000] = { 0 };
    string buf1;
    int i = 0,find=0;
    while (ifs >> buf)
    {
        if (i == 5)
        {
            cout << buf << " "<<endl;
            i=0;
            find=1;
        }
        if (i >= 1&&i<5)
        {
            cout << buf << " ";
            i++;
        }
        int n = strcmp(a, buf); //比较两个字符串的大小 
        if(n!=0)
        {
        	buf1=buf;
        	
		}
        if (n == 0)
        {
        	cout << buf1 << " ";
            cout << buf << " ";
            i++;
        }
    }
    ifs.close();
    if(find==0)
	{
    	cout<<"查无此书"<<endl;
	}
}
void library::delete1(library &L, char* arr)
{
	library *S;
	S = L.next;
    while(S)
    {
    	S = S->next;
	}
    char a[100], b[100];
    int i = 0, j = 0, count = 0;
    strcpy(a, arr);
    while (*arr != '\0')
    {
        count++;
        arr++;
    }
    library *p, *q, *r;
    q = &L;
    p = L.next;
    while (p != NULL)
    {
        strcpy(b, p->data.name);
        i = 0, j = 0;
        while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0')
        {
            i++;
            j++;
        }
        if (i == count)
        {
            r = p;
            while (q)
            {
                while (q->next == r)
                {
                    q->next = q->next->next;
                    free(r);
                    cout << "删除图书信息成功!" << endl;
                    ofstream ofs;
                    ofs.open("text.txt", ios::out);
                    library *S;
                    S = L.next;
                    while (S)
                    {
                        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " "<< S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
                        S = S->next;
                    }
                    ofs.close();
                    return;
                }
                q = q->next;
            }
        }
        p = p->next;
    }
    cout<<"查无此书,删除失败"<<endl;
 
}
void library::insert(library &L, char* arr, char* arr2, char* arr3, char* arr4, char* arr5, char* arr6,char* arr7,char* arr8)
{
    library *p, *q, *s;
    char a[100], b[100];
    int count = 0, i = 0, j = 0;
    s = new library();
    strcpy(s->data.name, arr2);
    strcpy(s->data.num, arr3);
    strcpy(s->data.tel, arr4);
    strcpy(s->data.cla, arr5);
    strcpy(s->data.name1, arr6);
    strcpy(s->data.time, arr7);
    strcpy(s->data.adress, arr8);
    strcpy(a, arr);
    while (*arr != '\0')
    {
    	
        count++;
        arr++;
    }
    q = &L;
    p = L.next;
    while (p != NULL)
    {
        strcpy(b, p->data.name);
        i = 0, j = 0;
        while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0')
        {
            i++;
            j++;
        }
        if (i == count)
        {
            while (q)
            {
                while (q->next == p)
                {
                    q->next = s;
                    s->next = p;
                    cout << "插入成功" << endl;
                    ofstream ofs;
                    ofs.open("text.txt", ios::out);
                    library *S;
                    S = L.next;
                    while (S)
                    {
                        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
                        S = S->next;
                    }
                    ofs.close();
                    S = L.next;
                    return;
                }
                q = q->next;
            }
        }
        p = p->next;
    }
    cout<<"查无此书,插入失败"<<endl;
}

void library::modify(library &L, char* arr, char* arr1, char* arr2, char* arr3, char* arr4, char* arr5, char* arr6, char* arr7)
{
    char a[100], b[100];
    strcpy(a, arr);
    library *p;
    int count = 0, i = 0, j = 0;
    while (*arr != '\0')
    {
        count++;
        arr++;
    }
    p = L.next;
    while (p)
    {
        strcpy(b, p->data.name);
        i = 0, j = 0;
        while (a[i] == b[j] && a[i] != '\0' && b[j] != '\0')
        {
            i++;
            j++;
        }
        if (i == count)
        {
            strcpy(p->data.name, arr1);
            strcpy(p->data.num, arr2);
            strcpy(p->data.tel, arr3);
            strcpy(p->data.cla, arr4);
            strcpy(p->data.name1, arr5);
            strcpy(p->data.time, arr6);
            strcpy(p->data.adress, arr7);
            cout << "修改成功" << endl;
            ofstream ofs;
            ofs.open("text.txt", ios::out);
            library *S;
            S = L.next;
            while (S)
            {
                ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
                S = S->next;
            }
            ofs.close();
            return;
        }
        p = p->next;
    }
    cout<<"查无此书,修改失败"<<endl;
}

void menu()//输出菜单
{
    color(10);
	cout << "\t\t\t****************************************************************" << endl;
    cout << "\t\t\t**********************欢迎光临图书管理系统**********************" << endl;
    cout << "\t\t\t******************                            ******************" << endl;
    cout << "\t\t\t******************    输入0   退出程序        ******************" << endl;
    cout << "\t\t\t******************    输入1   录入信息        ******************" << endl;
    cout << "\t\t\t******************    输入2   陈列信息        ******************" << endl;
    cout << "\t\t\t******************    输入3   按作者名查找    ******************" << endl;
    cout << "\t\t\t******************    输入4   按书名查找      ******************" << endl;
    cout << "\t\t\t******************    输入5   删除信息        ******************" << endl;
    cout << "\t\t\t******************    输入6   插入信息        ******************" << endl;
    cout << "\t\t\t******************    输入7   修改信息        ******************" << endl;
    cout << "\t\t\t******************    输入8   按分类号排序    ******************" << endl;
    cout << "\t\t\t******************                            ******************" << endl;
    cout << "\t\t\t****************************************************************" << endl;
}

int main()
{
	library L;
    char arr[100], arr2[100], arr4[100], arr5[100], arr6[100], arr7[100], arr8[100], arr9[100], 
	arr10[100], arr11[100], arr12[100], arr13[100], arr14[100], arr15[100], arr16[100], arr17[100],
	arr18[100],arr19[100],arr20[100];
    
    //完成单链表的创建
    L.next = NULL;
    library *p, *q;
    q = &L; 
    char buf[100];
    ifstream ifs;
    ifs.open("text.txt",ios::in);
    while (ifs>>buf)
    {
    	p=new library();
        strcpy(p->data.name,buf);
        ifs>>buf;
        strcpy(p->data.num,buf);
        ifs>>buf;
        strcpy(p->data.tel,buf);
        ifs>>buf;
        strcpy(p->data.cla,buf);
        ifs>>buf;
        strcpy(p->data.name1,buf);
        ifs>>buf;
        strcpy(p->data.time,buf);
        ifs>>buf;
        strcpy(p->data.adress,buf);
        p->next = NULL;
        q->next = p; //等于用这条语句:q->next = p;
        q = p;
    }
    ofstream ofs;
    ofs.open("text.txt", ios::out);
    library *S;
    S = L.next;
    while (S)
    {
        ofs << S->data.name << " " << S->data.num << " " << S->data.tel << " " << S->data.cla << " " << S->data.name1 << " " << S->data.time << " " << S->data.adress << " " << endl;
        S = S->next;
    }
    ofs.close();

	int x=0,n=0;
    char y;
    
    while (1)
    {
    	system("cls");
    	menu();
    	cout<<"请输入:";
    	cin>>x;
        if (x <= 0)
        {
            cout <<endl<< "成功退出系统!" << endl;
			break;
        }
        int check=1;
        switch (x)
        {
        case 1:
        	cout<<"此操作将覆盖原先所有的信息,确定吗?"<<endl; 
        	cout<<"输入1  确定"<<endl;
        	cout<<"输入0  取消"<<endl;
        	cin>>check;
        	if(check==1)
        	{
        		cout << "现在开始录入信息,请输入您要录入几本书的信息" << endl;
	            cin >> n;
	            cout<<""<<endl;
	            L.londecreat(L, n);
	            cout << "录入完成" << endl;
			}
            else if(check==0)
            {
    
			}
			else
			{
				cout<<"输入有误";
			}
			break;
        case 2:
            L.display();
            break;
        case 3:
            cout << "请输入您要进行查找图书的作者名" << endl;
            cin >> arr;
            L.findname(arr);
            break;
        case 4:
            cout << "请输入您要进行查找图书的图书名" << endl;
            cin >> arr2;
            L.findbook(arr2);
            break;
        case 5:
            cout << "请输入您要删除图书的登录号" << endl;
            cin >> arr4;
            L.delete1(L, arr4);
            break;
        case 6:
        	cout << "请输入您要进行插入地方的图书的登录号以及插入的图书的登录号,图书名,作者名,分类号,出版单位,出版时间,价格" << endl;
            cin >> arr5 >> arr6 >> arr7 >> arr8 >> arr9 >> arr10>> arr11>> arr20;
            L.insert(L, arr5, arr6, arr7, arr8, arr9, arr10,arr11,arr20);
            break;
        case 7:
        	cout << "请输入您要进行修改的图书的登录号以及您需要修改的图书的登录号,图书名,作者名,分类号,出版单位,出版时间,价格" << endl;
            cin >> arr12 >> arr13 >> arr14 >> arr15 >> arr16 >> arr17>> arr18>> arr19;
            L.modify(L, arr12, arr13, arr14, arr15, arr16, arr17, arr18,arr19);
            break;
        case 8:
    		cout << "已按分类号从大到小排序" << endl<< endl;
    		L.sort();
            break;
        default:
            cout << "您的输入有误,请重新输入" << endl;
        }
        
        cout<<endl<<endl<<"继续吗?"<<endl;
        cout<<"输入1  继续"<<endl; 
        cout<<"输入0  退出"<<endl; 
		cin>>y;
		if(y=='0')
		{
			cout<<"\n成功退出系统!"<<endl;
			break;
		}
		else if(y=='1')
		{
		}
		else
		{
			cout<<"输入有误,自动退出系统";
			break;
		}
    }
    return 0;
}

Summarize

For the C language, if you want to learn it well, the most important thing is practice, which is to do it yourself, and you must learn it better through continuous computer operation.

What impressed me the most was the single-linked list. It is easy to know that the previous grasp was not strong enough. Now that I have finished writing, it seems that this piece of knowledge is much clearer. Not only that, but the mastery of files and streams has been deepened, and pointers have been understood more.

The idea of ​​writing code is very important, otherwise it will be very confusing and impossible to start. Almost all codes are mainly composed of three parts, input-logic-output. The input part is to detect the numbers input from the keyboard, the logic part makes judgments and decisions, processes the input, and calculates the desired thing, while the output part displays information on the window.

When writing print statements, it should be written clearly and concisely, which can make users use the program more conveniently, so the writing of menu functions is extremely important. In actual operation, I realized that a clear understanding of the input and output of each function can help me build code better and faster, and the code written in this way has fewer bugs, and I can find the problem faster when debugging. Which function has a problem, or the logic of the main function is wrong. In multiple cycles, writing indentation can better clarify the logic of the code, make the program more readable, and make it easier to solve bugs, which is beneficial to yourself and others , don't feel troublesome.

Guess you like

Origin blog.csdn.net/Night_Journey/article/details/125484147