Current location - Training Enrollment Network - Books and materials - Simulation of book sales scene
Simulation of book sales scene
//Just give a general idea, not too detailed. Just execute the following code. Of course, it can be better and more complicated, which is equivalent to a book management system. The following are mainly about borrowing books, returning books and inquiring whether they are due.

# include & ltstdio.h & gt

# include & lttime.h & gt

# include & ltstring.h & gt

#defineN? five

# Definition? m? five

typedef? Structure {

int? Year;

int? Month;

int? Day;

} date;

typedef? Structure {

Charles? Name [100];

Charles? Author [50];

int? Available; //Can I borrow it (1 Yes, 0 can't)

Dating? Date; ? //? Date of warehousing/lending/return

} book;

typedef? Structure {

int? Number;

Charles? Name [20];

int? Count; ? //Number of books held by students

Books? Book [3]; ? //Each student can only keep 3 books at the same time.

} students;

Invalid? Add_book (book? *); ? //Enter a new book

int? Borrow _ books (books? *, students? *); ? //Borrow books

int? Return_book (book? *, students? *); ?

int? Query (book? *,char *); ? //query

Dating? get _ time(); ? //Get the current time

int? Cal_day (date); ? //Calculate whether it is overdue.

Invalid? Master ()

{

Books? Books;

Student? stu[M];

Charles? Name [100];

int? Me;

//Read in book information and student information from the file.

Documents? * fp

fp=fopen("d:\\book.txt "," r ");

For (me? =? 0; I<N & amp& amp feof(FP); i++)

fscanf(fp," %s%s%d%d%d ",book[i]。 Name, book [me]. The author and. Book [me]. Available & Books [i]. Date, year, and; Book [i]. Date, month, and; book[I]. date . day);

fclose(FP);

fp=fopen("d:\\student.txt "," r ");

for(i=? 0; Me<M & amp& amp feof(FP); i++)

{

Fscanf(fp, "%d%s%d", & Stu [me]. No, Stu [me]. Name & Stu [me]. Count);

for(int? j? =0; j & lt3; j++)

fscanf(fp," %s%s%d%d%d ",stu[i]。 Book [j] Name, Stu [me]. Book [j] The author and. Stu [me]. Book [j] Available & Stu [me]. Book [j]. Date, year, and; Stu [me]. Book [j]. Date, month, and; Stu [me]. book[j]. date . day);

}

fclose(FP);

int? Mode;

Cycle:

Printf(" 1。 Enter a book \n2. Borrow books \n3. Return the book \n4. Query \n*. Exit \ n ");

Scanf("%d ",& mode);

Switch (mode)

{

Case? 1:

Add_book;

Break;

Case? 2:

If (borrow _ book (book, stu)) printf ("borrowing the book successfully \ n");

Or what? Printf ("Failed to borrow books \ n");

Break;

Case? 3:

If (return _ book (book, stu)) printf ("book returned successfully \ n");

Or what? Printf ("Failed to return the book \ n");

Break;

Case? 4:

Printf ("Enter title:");

Scanf("%s ",name);

If (! Query(book, name))printf ("Book information not found \ n");

Break;

Default value:

Return;

}

goto? Circulation;

}

//According to the student number, check whether you can still borrow books, such as updating student information and book information.

int? Borrow _ books (books? *b, students? *s)

{

Printf ("Enter title:");

Charles? Name [100];

Scanf("%s ",name);

Printf ("Enter student number:");

int? num

scanf("%d ",& ampnum);

for(int? Me? =? 0; I & ltm;; i++)

If (s[i]. Number? ==? num? & amp& amp? s[i]。 Count & lt3)//3/More than 3 books cannot be borrowed.

{

int? c? =? s[i]。 Count;

for(int? j? =0; j & ltn; j++)

if(strcmp(b[j].name,name)==0)? //Book borrowing is successful, update the information.

{

b[j]。 Available? =? 0;

b[j]。 Dating? =? get _ time();

s[i]。 Book [c]? =? b[j];

s[i]。 Count? =? c+ 1;

Return? 1;

}

}

Return? 0;

}

//Find the borrowing information according to the student number and title, update the return information, and calculate whether it is overdue.

int? Return_book (book? *b, students? *s)

{

Printf ("Enter title:");

Charles? Name [100];

Scanf("%s ",name);

Printf ("Enter student number:");

int? num

scanf("%d ",& ampnum);

for(int? j=? 0; j & ltm; j++)

if(s[j]。 Number? ==? Number & & amps [j]. count & gt0)

{

for(int? k = 0; k & lts[j]。 Count; k++)

If(strcmp(s[j]. Book [k]. name,name)==0)

{

s[j]。 count-; ? //Update the number of books held by students.

for(int? Me? =0; I & ltn;; i++)

if(strcmp(b[i].name,name)==0)

{

if(query(b,name)& gt; 0)

Printf ("If loans overdue has %d days, please pay a fine \ n");

b[i]。 Available? =? 1;

b[i]。 Dating? =? get _ time();

Return? 1;

}

}

}

Return? 0;

}

Invalid? Add_book (book? * book)

{

//Enter a new book and save it to a file.

}

//Find the book information according to the title (it can also be found according to the students, which is not realized here)

int? Query (book? *b,char? * Name)

{

for(int? Me? =0; I & ltn;; i++)

if(strcmp(b[i].name,name)==0)

{

if(b[i]。 Available? == 1){printf ("This book can be checked out \ n"); Return? - 1; }

Or what?

{

int? How many days? =? Cal_day(b[i]. date);

Printf ("This book has been lent out for %d days \n", days);

If (days & gt 10)

Printf ("This book is %d days overdue \n", days-10);

Return? Day-10;

}

}

Return? 0;

}

Dating? Get time ()

{

Dating? Date;

time_t? curtime

struct? tm? *? timeinfo

Time? (? & Curtis? ); //Get time

timeinfo? =? Local time? (? & Curtis? ); //Convert to local time

Date. Year? =? timeinfo-& gt; TM _ year+ 1900;

Date. Month? =? timeinfo-& gt; TM _ mon+ 1;

Date. What day is it? =? timeinfo-& gt; tm _ mday

Return? Date;

}

//Calculate the total lending days.

int? Cal_day (date? d)

{

Dating? od? =? get _ time();

If (od.year? ==? D years)

{

If (od.month? ==? D. month)? Return? D-Day? -? od . day;

Or what? Return? D-Day? -? od.day? +(od.month? -? d . month)* 30; //30 days per month

}

other

{Return? ((d.month- 1)*30+d.day)? -? ((od.month- 1)*30? +od.day)? +? (d.year? -? od . year)* 365; }? //365 days per year

The following are some running results and the student information text of the book.

Many books are saved by arrays, and time is calculated by library functions.