44. Library Fine

I'm a slow walker, but I never walk backwards.

Task Description

Write a program to compute library fine. You will be given two .h files. The first is a book.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef BOOK_H
#define BOOK_H
 
enum Type {NOVEL, COMICS, MANUAL, TEXTBOOK};
 
struct Book {
    char title[128];
    enum Type type;
    char ISBN[128];
    char authors[128];
    unsigned int importance;
};
 
#endif

You will also be given a date.h.

1
2
3
4
5
6
7
8
9
10
#ifndef DATE_H
#define DATE_H
 
struct Date {
    unsigned int year;
    unsigned int month;
    unsigned int day;
};
 
#endif

Your function has the following prototype.

1
2
3
4
#include "book.h"
#include "date.h"
 
unsigned int library_fine(struct Book book, struct Date date_borrowed, struct Date date_returned);

Now you need to write a function that when given a book, a borrowed_date, and a returned_date, determines whether the book is overdue. If it is, you need to compute the fine. First you need to determine if the book is overdue. For this you need the "type" information. For NOVEL the maximum allowed period for borrowing a book is 90 days. For COMICS, MANUAL, TEXTBOOK, it is 10, 100, and 5 days respectively.

Note that if you borrowed a book on 2007/11/14, and returned it on 2007/12/3, then you'd borrowed it for 20 days. Even if you borrowed and return a book at the same day, it is counted as one day. If you borrowed a NOVEL for 100 days, then it is 10 days overdue. It is guaranteed that the returned date will not be earlier than the borrowed date, and the year is between 1000 and 3000 inclusively.

After you have determined whether a book is overdue, next you need to determine the fine. The fine is determined by two factors -- how many days are overdue and the importance of the book. Unfortunately the importance of a book is encoded in an unsigned integer in a very strange way. The importance value contains exactly two '1'-bits in its binary representation, and the importance factor is the distance between the two '1'-bits, i.e. the number of '0'-bits between them. The fine is equal to the product of the importance factor and the day of overdue. For example, if the importance is 17 (whose binary representation is 00000000000000000000000000010001) and the book is overdue for 10 days, then the library fine is 30.

每一本書都有其預定的出租時間,若在規定時間內歸還,則不算任何費用。如小說 NOVEL 在 90 天內 (含) 不計算任何費用,同理漫畫 COMIC 10 天、操作說明書 MANUAL 100 天、教科書 TEXTBOOK 5 天。如果超出規定時間,則需要繳交額外的罰款,每晚一天就必須多付 $X$ 元,$X$ 取決於 importance 轉成二進制的兩個 1-bit 位置距離。

給定書籍資訊、借出日期 borrowed_date 和歸還日期 returned_date,請撰寫一個函數計算出租費用。

Notes

You only need to submit your library_fine function. The judge system would use its own main function to test your program. You may use the following program to test your program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include "book.h"
#include "date.h"
 
unsigned int library_fine(struct Book book, struct Date date_borrowed, struct Date date_returned);
 
int main(){
    struct Book book = {
        "Neon Genesis Evangelion",
        COMICS,
        "978-159-116-400-5",
        "Yoshiyuki Sadamoto",
        17
    };
    struct Date date_borrowed, date_returned;
    date_borrowed.year  = 2007;
    date_borrowed.month = 11;
    date_borrowed.day   = 14;
    date_returned.year  = 2007;
    date_returned.month = 12;
    date_returned.day   = 3;
    printf("The fine is $%d.\n", library_fine(book, date_borrowed, date_returned));
    return 0;
}

Discussion