From bd03196941387bcd2ed96c845a26da1ca609a2d9 Mon Sep 17 00:00:00 2001 From: Huang-Libo Date: Sat, 16 Mar 2019 14:29:26 +0800 Subject: [PATCH] modify project --- .../ch14/Book/Book.xcodeproj/project.pbxproj | 8 +-- Xcode/ch14/Book/Book/book.c | 49 ++++++++++++++ .../ManyBook.xcodeproj/project.pbxproj | 8 +-- Xcode/ch14/ManyBook/ManyBook/manybook.c | 65 +++++++++++++++++++ 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 Xcode/ch14/Book/Book/book.c create mode 100644 Xcode/ch14/ManyBook/ManyBook/manybook.c diff --git a/Xcode/ch14/Book/Book.xcodeproj/project.pbxproj b/Xcode/ch14/Book/Book.xcodeproj/project.pbxproj index 924d519..ca7d073 100644 --- a/Xcode/ch14/Book/Book.xcodeproj/project.pbxproj +++ b/Xcode/ch14/Book/Book.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - 71E364FA222A4F2F009B856E /* book.c in Sources */ = {isa = PBXBuildFile; fileRef = 71E364F9222A4F2F009B856E /* book.c */; }; + 71EB79FE223CCE7100DB8C43 /* book.c in Sources */ = {isa = PBXBuildFile; fileRef = 71EB79FD223CCE7100DB8C43 /* book.c */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -24,7 +24,7 @@ /* Begin PBXFileReference section */ 71E364EF222A4F08009B856E /* Book */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Book; sourceTree = BUILT_PRODUCTS_DIR; }; - 71E364F9222A4F2F009B856E /* book.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = book.c; path = ../../../../raw_source_code/Ch14/book.c; sourceTree = ""; }; + 71EB79FD223CCE7100DB8C43 /* book.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = book.c; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,7 +57,7 @@ 71E364F1222A4F08009B856E /* Book */ = { isa = PBXGroup; children = ( - 71E364F9222A4F2F009B856E /* book.c */, + 71EB79FD223CCE7100DB8C43 /* book.c */, ); path = Book; sourceTree = ""; @@ -118,7 +118,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 71E364FA222A4F2F009B856E /* book.c in Sources */, + 71EB79FE223CCE7100DB8C43 /* book.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Xcode/ch14/Book/Book/book.c b/Xcode/ch14/Book/Book/book.c new file mode 100644 index 0000000..3cb32b9 --- /dev/null +++ b/Xcode/ch14/Book/Book/book.c @@ -0,0 +1,49 @@ +//* book.c -- one-book inventory */ +#include +#include +char * s_gets(char * st, int n); +#define MAXTITL 41 /* maximum length of title + 1 */ +#define MAXAUTL 31 /* maximum length of author's name + 1 */ + +struct book { /* structure template: tag is book */ + char title[MAXTITL]; + char author[MAXAUTL]; + float value; +}; /* end of structure template */ + +int main(void) +{ + struct book library; /* declare library as a book variable */ + + printf("Please enter the book title.\n"); + s_gets(library.title, MAXTITL); /* access to the title portion */ + printf("Now enter the author.\n"); + s_gets(library.author, MAXAUTL); + printf("Now enter the value.\n"); + scanf("%f", &library.value); + printf("%s by %s: $%.2f\n",library.title, + library.author, library.value); + printf("%s: \"%s\" ($%.2f)\n", library.author, + library.title, library.value); + printf("Done.\n"); + + return 0; +} + +char * s_gets(char * st, int n) +{ + char * ret_val; + char * find; + + ret_val = fgets(st, n, stdin); + if (ret_val) + { + find = strchr(st, '\n'); // look for newline + if (find) // if the address is not NULL, + *find = '\0'; // place a null character there + else + while (getchar() != '\n') + continue; // dispose of rest of line + } + return ret_val; +} diff --git a/Xcode/ch14/ManyBook/ManyBook.xcodeproj/project.pbxproj b/Xcode/ch14/ManyBook/ManyBook.xcodeproj/project.pbxproj index ae0c9c8..8241fa5 100644 --- a/Xcode/ch14/ManyBook/ManyBook.xcodeproj/project.pbxproj +++ b/Xcode/ch14/ManyBook/ManyBook.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - 71E3650F222A6A61009B856E /* manybook.c in Sources */ = {isa = PBXBuildFile; fileRef = 71E3650E222A6A61009B856E /* manybook.c */; }; + 71EB7A00223CCEB000DB8C43 /* manybook.c in Sources */ = {isa = PBXBuildFile; fileRef = 71EB79FF223CCEB000DB8C43 /* manybook.c */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -24,7 +24,7 @@ /* Begin PBXFileReference section */ 71E36504222A6A33009B856E /* ManyBook */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ManyBook; sourceTree = BUILT_PRODUCTS_DIR; }; - 71E3650E222A6A61009B856E /* manybook.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = manybook.c; path = ../../../../raw_source_code/Ch14/manybook.c; sourceTree = ""; }; + 71EB79FF223CCEB000DB8C43 /* manybook.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = manybook.c; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,7 +57,7 @@ 71E36506222A6A33009B856E /* ManyBook */ = { isa = PBXGroup; children = ( - 71E3650E222A6A61009B856E /* manybook.c */, + 71EB79FF223CCEB000DB8C43 /* manybook.c */, ); path = ManyBook; sourceTree = ""; @@ -118,7 +118,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 71E3650F222A6A61009B856E /* manybook.c in Sources */, + 71EB7A00223CCEB000DB8C43 /* manybook.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Xcode/ch14/ManyBook/ManyBook/manybook.c b/Xcode/ch14/ManyBook/ManyBook/manybook.c new file mode 100644 index 0000000..8dab9d3 --- /dev/null +++ b/Xcode/ch14/ManyBook/ManyBook/manybook.c @@ -0,0 +1,65 @@ +/* manybook.c -- multiple book inventory */ +#include +#include +char * s_gets(char * st, int n); +#define MAXTITL 40 +#define MAXAUTL 40 +#define MAXBKS 100 /* maximum number of books */ + +struct book { /* set up book template */ + char title[MAXTITL]; + char author[MAXAUTL]; + float value; +}; + +int main(void) +{ + struct book library[MAXBKS]; /* array of book structures */ + int count = 0; + int index; + + printf("Please enter the book title.\n"); + printf("Press [enter] at the start of a line to stop.\n"); + while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL + && library[count].title[0] != '\0') + { + printf("Now enter the author.\n"); + s_gets(library[count].author, MAXAUTL); + printf("Now enter the value.\n"); + scanf("%f", &library[count++].value); + while (getchar() != '\n') + continue; /* clear input line */ + if (count < MAXBKS) + printf("Enter the next title.\n"); + } + + if (count > 0) + { + printf("Here is the list of your books:\n"); + for (index = 0; index < count; index++) + printf("%s by %s: $%.2f\n", library[index].title, + library[index].author, library[index].value); + } + else + printf("No books? Too bad.\n"); + + return 0; +} + +char * s_gets(char * st, int n) +{ + char * ret_val; + char * find; + + ret_val = fgets(st, n, stdin); + if (ret_val) + { + find = strchr(st, '\n'); // look for newline + if (find) // if the address is not NULL, + *find = '\0'; // place a null character there + else + while (getchar() != '\n') + continue; // dispose of rest of line + } + return ret_val; +}