#include <stdio.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <string.h>
char Menu(const char *prompt, const char *item); int YesNo(const char *question); void GetStr(char *str);
char Menu(const char *prompt, const char *option) { char choice; int err; do { printf(prompt); scanf(" %c", &choice); choice = toupper(choice); err = !strchr(option, choice); if (err) { puts("Incorrect choice!"); } } while (err); return choice; }
int YesNo(const char *question) { char answer; int err; do { printf(question); scanf(" %c", &answer); answer = toupper(answer); err = !strchr("YN", answer); if (err) { puts("Incorrect answer!"); } } while (err); return answer == 'Y'; }
void GetStr(char *str) { scanf(" %c", str); gets(str + 1); }
typedef struct { short year; char month, day; } DATE;
int IsLeapYear(int year); int MaxDayMonth(int year, int month); int IsValidDate(int year, int month, int day); void DateInput(DATE *date); void DateOutput(const DATE *date); int DateCmp(const DATE *date1, const DATE *date2);
int IsLeapYear(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }
int MaxDayMonth(int year, int month) { const static char mdm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return year > 0 && month >= 1 && month <= 12 ? mdm[month - 1] + (2 == month && IsLeapYear(year)) : 0; }
int IsValidDate(int year, int month, int day) { return year > 0 && month >= 1 && month <= 12 && day >= 1 && day <= MaxDayMonth(year, month); }
void DateInput(DATE *date) { int year = 0, month = 0, day = 0; while (scanf("%d / %d / %d", &year, &month, &day) < 3 || !IsValidDate(year, month, day)) { printf("Incorrect date! Please reenter: "); while (getchar() != '\n'); } date->year = year; date->month = month; date->day = day; }
void DateOutput(const DATE *date) { printf("%04d/%02d/%02d", date->year, date->month, date->day); }
int DateCmp(const DATE *date1, const DATE *date2) { int r = date1->year - date2->year; if (!r) r = date1->month - date2->month; if (!r) r = date1->day - date2->day; return r; }
typedef enum _bookFmt_ {card, table} BOOKFMT;
typedef struct { char isbn[16]; char title[32]; char author[32]; char pub[32]; DATE press; double price; } BOOK;
void SetItem(int i); int GetItem(); void SelectItem(); void SelectAll();
void Card(); void Table(); void Head();
void Input(BOOK *book); void Output(const BOOK *book);
int BookCmp(const BOOK *book1, const BOOK *book2);
#define epsilon 1e-6
static int item;
void SetItem(int i) { if (i >= 1 && i <= 6) { item = i; } }
int GetItem() { return item; }
void SelectItem() { item = Menu("Item: 1)ISBN 2)Title 3)Author 4)Publisher 5)Pub date 6)Price > ", "123456") - '0'; }
void SelectAll() { item = 0; }
static BOOKFMT bookFmt = card;
void Card() { bookFmt = card; }
void Table() { bookFmt = table; }
void Head() { puts("ISBN--------- Title------------------------- Author-------------- " "Publisher--------------------- Pub-date-- Price---"); }
void Input(BOOK *book) { switch (item) { case 0: printf(" ISBN: "); GetStr(book->isbn); printf(" Title: "); GetStr(book->title); printf(" Author: "); GetStr(book->author); printf("Publisher: "); GetStr(book->pub); printf(" Pub date: "); DateInput(&book->press); printf(" Price: "); scanf("%lg", &book->price); break; case 1: printf("ISBN: "); GetStr(book->isbn); break; case 2: printf("Title: "); GetStr(book->title); break; case 3: printf("Author: "); GetStr(book->author); break; case 4: printf("Publisher: "); GetStr(book->pub); break; case 5: printf("Pub date: "); DateInput(&book->press); break; case 6: printf("Price: "); scanf("%lg", &book->price); break; } }
void Output(const BOOK *book) { switch (bookFmt) { case card: switch (item) { case 0: printf(" ISBN: %s\n", book->isbn); printf(" Title: %s\n", book->title); printf(" Author: %s\n", book->author); printf("Publisher: %s\n", book->pub); printf(" Pub date: "); DateOutput(&book->press); putchar('\n'); printf(" Price: %.2f\n", book->price); break; case 1: printf("ISBN: %s\n", book->isbn); break; case 2: printf("Title: %s\n", book->title); break; case 3: printf("Author: %s\n", book->author); break; case 4: printf("Publisher: %s\n", book->press); break; case 5: printf("Pub date: "); DateOutput(&book->press); putchar('\n'); break; case 6: printf("Price: %.2f\n", book->price); break; } break; case table: printf("%-13s %-30s %-20s %-30s ", book->isbn, book->title, book->author, book->pub); DateOutput(&book->press); printf(" %8.2f\n", book->price); break; } }
int BookCmp(const BOOK *book1, const BOOK *book2) { int ok; switch (item) { case 1: ok = strcmp(book1->isbn, book2->isbn); break; case 2: ok = strcmp(book1->title, book2->title); break; case 3: ok = strcmp(book1->author, book2->author); break; case 4: ok = strcmp(book1->pub, book2->pub); break; case 5: ok = DateCmp(&book1->press, &book2->press); break; case 6: if (fabs(book1->price - book2->price) < epsilon) { ok = 0; } else if (book1->price > book2->price) { ok = 1; } else { ok = -1; } break; } return ok; }
#define LibSize 1024 #define LibInc 1024
typedef struct { int size, number; BOOK *book; } LIB;
void Create(LIB *lib); void Destroy(LIB *lib); void Append(LIB *lib); void Find(LIB *lib); void Remove(LIB *lib); void Modify(LIB *lib); void Show(LIB *lib);
static void Resize(LIB *lib, int size); static int Search(const LIB *lib, const BOOK *book, int start); static void Sort(LIB *lib);
void Create(LIB *lib) { lib->size = LibSize; lib->number = 0; lib->book = (BOOK*)malloc(lib->size * sizeof(BOOK)); }
void Destroy(LIB *lib) { lib->size = lib->number = 0; free(lib->book); }
void Append(LIB *lib) { BOOK x; SelectAll(); Input(&x); if (lib->number == lib->size) { Resize(lib, lib->size + LibInc); } lib->book[lib->number] = x; ++lib->number; }
void Find(LIB *lib) { int k, r, i; BOOK book; SetItem(2); Input(&book); r = Search(lib, &book, 0); Table(); if (r < 0) { puts("Not found!"); } else { Head(); } while (r >= 0) { Output(&lib->book[r]); r = Search(lib, &book, r + 1); } }
void Remove(LIB *lib) { int i, r = 0; char answer; BOOK book; SetItem(1); Input(&book); r = Search(lib, &book, r); if (r < 0) { puts("Not found!"); } while (r >= 0) { if (YesNo("Remove(y/n)? ")) { --lib->number; if (r < lib->number) { lib->book[r] = lib->book[lib->number]; } } else { ++r; } SetItem(1); r = Search(lib, &book, r); } }
void Modify(LIB *lib) { int i, r; char answer; BOOK book; SetItem(1); Input(&book); r = Search(lib, &book, 0); if (r < 0) { puts("Not found!"); } while (r >= 0) { if (YesNo("Modify(y/n)? ")) { SelectAll(); Input(&lib->book[r]); r = -1; } else { SetItem(1); r = Search(lib, &book, r + 1); } } }
void Show(LIB *lib) { int k; SetItem(1); Sort(lib); Head(); Table(); SelectAll(); for (k = 0; k < lib->number; ++k) { Output(&lib->book[k]); } }
void Resize(LIB *lib, int size) { BOOK *book; int k; if (size != lib->size && size > lib->number) { lib->size = size; book = (BOOK*)malloc(lib->size * sizeof(BOOK)); for (k = 0; k < lib->number; ++k) { book[k] = lib->book[k]; } free(lib->book); lib->book = book; } }
int Search(const LIB *lib, const BOOK *book, int start) { int k, r = -1; for (k = start; k < lib->number; ++k) { if (!BookCmp(&lib->book[k], book)) { r = k; k = lib->number; } } return r; }
void Sort(LIB *lib) { int i, j; BOOK book; for (i = 1; i < lib->number; ++i) { if (BookCmp(&lib->book[i], &lib->book[i - 1]) < 0) { book = lib->book[i]; for (j = i - 1; j >= 0 && BookCmp(&book, &lib->book[j]) < 0; --j) { lib->book[j + 1] = lib->book[j]; } lib->book[j + 1] = book; } } }
int main() { char choice; LIB lib; Create(&lib); do { choice = Menu("Append Find Remove Modify Show Quit > ", "AFRMSQ"); switch (choice) { case 'A': Append(&lib); break; case 'F': Find(&lib); break; case 'R': Remove(&lib); break; case 'M': Modify(&lib); break; case 'S': Show(&lib); break; case 'Q': break; } } while (choice != 'Q'); Destroy(&lib); return 0; }
|