Current location - Training Enrollment Network - Books and materials - Design a book management system with C language (please come in)
Design a book management system with C language (please come in)
What I wrote basically meets your most basic requirements. I don't know if it's appropriate. I don't have a computer. These codes were written in Internet cafes ... and passed the debugging in TC2.0:

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltstring.h & gt

# Definition number 100

Structural book

{

char booknum[8];

char bookname[ 15];

Char author [12];

Char pub [15];

char pub time[ 1 1];

} book infor[NUM];

Char initialization ()/* Interface initialization */

{

char ch

Printf(" * * * * * * * * * * Library Management System * * * * * * \ n1. Show all book information \n2. Add book information \n3. Delete book information \n4. Search for book information \n5. Exit \ nPlease select: ");

do

{

ch = getch();

if(ch & gt; = ' 1 ' & amp; & ampch & lt='5')

{

printf("%c\n ",ch);

getchar();

Return ch;

}

} while( 1);

}

Void showall()/* Show all */

{

int I = 0;

FILE * fp

System ("CLS");

if((fp=fopen("C:\\bookinfo.txt "," r ")= = NULL)

{

Printf ("Error: Cannot open file \ n");

getchar();

Return;

}

printf(" booknum bookname author pub house pub time \ n ");

And (! feof(fp))

{Fred (& ampbookinfor[i], sizeof(struct book), 1, fp);

printf(" %-8s %- 15s %- 12s %- 15s %- 1 1s \ n ",bookinfor[i]。 booknum,bookinfor[i]。 bookname,bookinfor[i]。 Author, bookinfor[i]. PubHouse,bookinfor[i]。 pub time);

i++;

If (! I %20)

{

Printf ("Press any key to continue ...");

getch();

}

}

fclose(FP);

Printf ("press any key to return");

getch();

Return;

}

Voiddaddinfor ()/* Add data */

{

int i=0,b = 0;

char ch

FILE * fp

do

{

System ("CLS");

printf(" booknum:");

Get (bookinfor[i]). booknum);

Printf ("book name:");

Get (bookinfor[i]). Title);

Printf ("Author:");

Get (bookinfor[i]). Author);

printf(" pub house:");

Get (bookinfor[i]). Pub);

printf(" pub time:");

Get (bookinfor[i]). pub time);

Printf ("Save! Continue? (Y/N)");

do

{

ch = getch();

if(ch = = ' Y ' | | ch = = ' Y ' | | ch = = ' N ' | | ch = = ' N ')

{

printf("%c\n ",ch);

getchar();

Break;

}

} while( 1);

i++;

} while(ch = = ' Y ' | | ch = = ' Y ');

if((fp=fopen("C:\\bookinfo.txt "," a+")= = NULL)

{

Printf ("Error: Cannot open file \ n");

Return;

}

for(b = 0; B< me; b++)

if(fwrite(& amp; Bookinfor[b], sizeof, 1, fp)! = 1)

Printf ("file write error \ n");

fclose(FP);

Return;

}

Voidlin for ()/* Delete information */

{

int i=0,b=0,k;

char delnum[8];

FILE * fp

System ("CLS");

Printf ("Enter the deletion ISBN:");

gets(delnum);

if((fp=fopen("C:\\bookinfo.txt "," r ")= = NULL)

{

Printf ("Error: Cannot open file \ n");

Return;

}

And (! feof(fp))

{

Fred (& ampbookinfor[i], sizeof(struct book), 1, FP);

i++;

}

fclose(FP);

for(b = 0; B< me; b++)

If (! strcmp(delnum,bookinfor[b].booknum))

{

for(k = b; k & ltI- 1; k++)

book infor[k]= book infor[k+ 1];

if((fp=fopen("C:\\bookinfo.txt "," w"))==NULL)

{

Printf ("Error: Cannot open file \ n");

Return;

}

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

if(fwrite(& amp; Bookinfor[i], sizeof (structural book), 1, fp)! = 1)

Printf ("file write error \ n");

fclose(FP);

Printf ("Delete! Press any key to return ... ");

getchar();

Return;

}

Printf ("Not found! Press any key to return ... ");

getchar();

Return;

}

Void searchinfor()/* Find information */

{

char ch

char search book[20];

int I = 0;

FILE * fp

System ("CLS");

Printf ("Search mode: \n 1. By book number \n2. Select by title: ");

do

{

ch = getch();

if(ch==' 1'||ch=='2 ')

{

printf("%c\n ",ch);

getchar();

Break;

}

} while( 1);

if((fp=fopen("C:\\bookinfo.txt "," r ")= = NULL)

{

Printf ("Error: Cannot open file \ n");

Return;

}

if(ch==' 1 ')

{

Printf ("Enter ISBN:");

Get (search book);

printf(" booknum bookname author pub house pub time \ n ");

And (! feof(fp))

{

Fred (& ampbookinfor[i], sizeof(struct book), 1, FP);

If (! strcmp(searchbook,bookinfor[i].booknum))

{

printf(" %-8s %-20s %- 15s %-25s %- 10s \ n ",bookinfor[i]。 booknum,bookinfor[i]。 bookname,bookinfor[i]。 Author, bookinfor[i]. PubHouse,bookinfor[i]。 pub time);

Printf ("Press any key to return ...");

getchar();

fclose(FP);

Return;

}

i++;

}

}

if(ch=='2 ')

{

Printf ("Enter book name:");

Get (search book);

printf(" booknum bookname author pub house pub time \ n ");

And (! feof(fp))

{

Fred (& ampbookinfor[i], sizeof(struct book), 1, FP);

If (! Strcmp(searchbook, bookinfor[i]. Book Name))

{

printf(" %-8s %-20s %- 15s %-25s %- 10s \ n ",bookinfor[i]。 booknum,bookinfor[i]。 bookname,bookinfor[i]。 Author, bookinfor[i]. PubHouse,bookinfor[i]。 pub time);

Printf ("Press any key to return ...");

getchar();

fclose(FP);

Return;

}

i++;

}

}

System ("CLS");

Printf ("Not found! Press any key to return ... ");

fclose(FP);

getchar();

Return;

}

void main()

{

char ch

do

{

System ("CLS");

ch = initial ization();

if(ch = = ' 1 ')showall();

if(ch = = ' 2 ')add info();

if(ch = = ' 3 ')delin for();

if(ch = = ' 4 ')search infor();

if(ch = = ' 5 ')break;

} while( 1);

System ("CLS");

Printf ("Thank you for using it!" );

getchar();

}