Current location - Training Enrollment Network - Books and materials - Seek C++ code! ! God bless! ! ! About the book management system! !
Seek C++ code! ! God bless! ! ! About the book management system! !
// book.cpp: Defines the entry point of the console application.

//

# contains "book.h"

# include & ltiostream & gt

# include & ltwindows.h & gt

# include & ltconio.h & gt

# include & ltfstream & gt

Use namespace std

Void gotoxy(int x, int y) // column x: 0~79 rows y: 0~24.

{

HANDLE hConsole = GetStdHandle(STD _ OUTPUT _ HANDLE);

COORD coordScreen={x,y };

SetConsoleCursorPosition(hConsole,coord screen);

Return;

}

Template & lttypename T>

void show_data(int x,int y,T data)

{

gotoxy(x,y);

Cout & lt& lt data;

}

Template & lttypename T>

Void input_data(int x, int y, char prompt[], T& data)

{

gotoxy(x,y);

Cout & lt& lt tips;

CIN & gt; & gt data;

}

Int show_main_menu() // Display the main menu and select.

{

char ch

while ( 1)

{

System ("CLS");

show_data(4, 1,"┌────────────────────────────┐”);

show_data(4,2,“││”);

Show _ data (4,3, "│ library management │");

show_data(4,4,“││”);

show_data(4,5,"└────────────────────────────┘”);

Show _ data (20 20,8, "1- Add book information");

Show _ data (20, 10, "2- modify book information");

Show _ data (20, 12, "3- delete book information");

Show _ data (20, 14, "4- browse book information");

Show _ data (20, 16, "0- exit");

Show_data(20, 18, "Please select");

ch = getche();

if(ch & gt; = ' 0 ' & amp& ampch & lt='4')

Break;

}

Return to ch-48;

}

int main()

{ int selected = 0;

int n = 0;

BOOK BOOK[MAX _ BOOK];

Load_data_from_file (book, n);

while ( 1)

{

selected = show _ main _ menu();

Switch (selected)

{

Case 1: add_book(book, n); Break;

Case 2: update_book(book, n); Break;

Case 3: delete_book(book, n); Break;

Case 4: view_book(book, n); Break;

Case 0: broken;

};

If (selected ==0)

{

Show_data(20, 20, "Exit the library management, thank you! \ n ");

Break;

}

}

save_data_to_file(book,n);

Returns 0;

}

Void add_book(Book book[], int & ampN) // Add a new book.

{

Booking bk;

while ( 1)

{

System ("CLS");

Input _ data (4,2, "Book number (ending when entering 0):", bk.book _ no);

if (strcmp("0 ",bk.book_no)==0)

Break;

Input _ data (4,4, "book name:", bk.book _ name);

Input_data(4,6," Author:",bk . Author);

Input _ data (4,8, "Publisher:", bk. publisher);

input_data(4, 10," ISBN:",bk。 ISBN);

Input_data(4, 12, "book price:", bk. price);

Input_data(4, 14, "Total pages:", bk.pages);

char yn

Input_data(4, 16, "Do you want to save (y/n? ): ",yn);

if ('y'==yn || 'Y'==yn)

{

book[n]= bk;

n++;

}

}

}

Void update_book(Book book[], int & ampN) // Modify books.

{scheduled BP;

int i,j,k,a;

char p;

while ( 1)

{

System ("CLS");

a = 0;

Input _ data (4,2, "Please enter the ISBN to be modified (end when entering 0):", bp.book _ no);

if (strcmp("0 ",bp.book_no)==0)

Break;

for(I = 0; I & ltn;; i++)

{if(strcmp (book [i]). book_no,bp.book_no)==0)

{ BP = book[I]; a = 1; Break; }

}

If (a==0)

{show _ data (4,4, "Book not found"); System ("suspended"); }

other

{

Show _ data (2 2,4, "1- ISBN:");

Show _ data (2 2,6, "2- book name:");

Show _ data (2 2,8, "3- author:");

Show _ data (2, 10," 4-Press:");

show_data(2, 12," 5-ISBN:");

Show _ data (2, 14, "6 price:");

Show _ data (2, 16, "7- total pages:");

Show _ data (2, 18, "0- modification ends");

show_data( 16,4,BP . book _ no);

show_data( 16,6,BP . book _ name);

show_data( 16,8,BP . author);

show_data( 16, 10,BP . publisher);

show_data( 16, 12,bp。 ISBN);

show_data( 16, 14,BP . price);

show_data( 16, 16,BP . pages);

for(j = 0; j & lt 15; j++)

{show _ data (2,20, "Please enter serial number before modifying");

p = getche();

k = p-48;

if(k & gt; = 1。 & ampk & lt=7)

Switch (k)

{case 1 :input_data(38,4,“”,BP . book _ no); Break;

Case 2: input _ data (38,6, "",bp.book _ name); Break;

Case 3: input _ data (38,8, "",BP. author); Break;

Case 4: input_data(38, 10, "",bp.publisher); Break;

Case 5: input_data(38, 12, "",bp. ISBN); Break;

Case 6: input_data(38, 14, "",bp.price); Break;

Case 7 :input_data(38, 16, "",bp.pages); Break;

}

}

if(k = = 0)break;

}

char yn

Input _ data (4,22, "Do you want to save (y/n? ): ",yn);

if(' Y ' = = yn | | ' Y ' = = yn)book[I]= BP;

}

}

}

Void delete_book(Book book[], int & ampN) // Delete books.

{

Book bq;

int i,j;

while ( 1)

{

System ("CLS");

Input _ data (4,2, "Please enter the book number to be deleted (end when entering 0):", bq.book _ no);

if (strcmp("0 ",bq.book_no)==0)

Break;

for(I = 0; I & ltn;; i++)

{if(strcmp (book [i]). book_no,bq . book _ no)= = 0){ j = I; Break; }}

If (i==n)

{show _ data (4,4, "Book not found"); System ("suspended"); }

other

{

Show_data( 1, 4, "ISBN:");

Show_data( 1, 6, "Book name:");

Show_data( 1, 8, "Author:");

Show_data( 1, 10," Press:");

show_data( 1, 12," ISBN:");

Show_data( 1, 14, "Book price:");

Show_data( 1, 16, "Total pages:");

show_data( 10,4,book[j].book _ no);

show_data( 10,6,book[j].book _ name);

Show _ data (10,8, book[j]. Author);

Show_data( 10, 10, book[j]. Publisher);

show_data( 10, 12,book[j].ISBN);

Show_data( 10, 14, book[j]. price);

Show_data( 10, 16, book[j]. Pages);

char yn

Input_data(4, 18, "Do you want to delete (y/n? ): ",yn);

if ('y'==yn || 'Y'==yn)

{ for(I = 0; I< = n-j; i++)

book[I+j]= book[I+j+ 1];

n-; }

}

}

}

Voidview _ book (bookbook [], int n)// Browse all book information.

{

int i,row

System ("CLS");

If (n==0)

{

Show_data( 10, 10, "No book information, quit!" );

System ("suspended");

Return;

}

row = 3;

for(I = 0; I & ltn;; i++)

{

if (row==3)

{ system(" cls ");

Show_data(0, 1, "book number");

Show_data( 12, 1, "title");

Show _ data(34 1, "author");

Show _ data(56 1, "Publishing House");

show_data(0,2,“-”);

}

show_data(0,row,book[i].book _ no);

show_data( 12,row,book[i]。 book _ name);

Show_data(34, row, book[i]. Author);

Show_data(56, row, book[i]. Publisher);

row++;

if(row & gt; =20 || i==n- 1)

{

Show_data(5, row+ 1, "Press any key to continue");

getche();

row = 3;

}

}

}

Voidload _ data _ from _ file (book [], int & ampN) // Import data from disk "book.dat".

{ int I;

ifstream infile("book.dat ",IOs::in);

infile & gt& gtn;

for(I = 0; I & ltn;; i++)

Infile & gt& gt book [me]. Book _ no>& gt book [me]. Book _ name & gt& gt book [me]. Author & gt& gt book [me]. Publisher & gt& gt book [me]. ISBN & gt& gt book [me]. Price & gt& gt book [me]. Number of pages;

infile . close();

}

Voidsave _ data _ to _ file (bookbook [], int n)// Store the data on the disk "book.dat".

{ int I;

of stream outfile(" book . dat ");

If (! Output file)

{ cerr & lt& lt" open book.dat reeor " & lt& ltendl

Exit (1);

}

outfile & lt& ltn & lt& ltendl

for(I = 0; I & ltn;; i++)

Outfile & lt& lt book [me]. Book _ no<& lt" ""<& lt book [me]. Book _ name & lt& lt" ""<& lt book [me]. Author & lt& lt ""

& lt& lt book [me]. Publisher & lt& lt" ""<& lt book [me]. ISBN & lt& lt" ""<& lt book [me]. Price & lt& lt" ""<& lt book [me]. Page & lt& ltendl

outfile . close();

}

This is my code. Delete the book part, just change the comparison content from the book number to the title.