Skip to content

Commit

Permalink
modify project
Browse files Browse the repository at this point in the history
  • Loading branch information
Huang-Libo committed Mar 16, 2019
1 parent 987753e commit bd03196
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Xcode/ch14/Book/Book.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
71EB79FD223CCE7100DB8C43 /* book.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = book.c; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -57,7 +57,7 @@
71E364F1222A4F08009B856E /* Book */ = {
isa = PBXGroup;
children = (
71E364F9222A4F2F009B856E /* book.c */,
71EB79FD223CCE7100DB8C43 /* book.c */,
);
path = Book;
sourceTree = "<group>";
Expand Down Expand Up @@ -118,7 +118,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
71E364FA222A4F2F009B856E /* book.c in Sources */,
71EB79FE223CCE7100DB8C43 /* book.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
49 changes: 49 additions & 0 deletions Xcode/ch14/Book/Book/book.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//* book.c -- one-book inventory */
#include <stdio.h>
#include <string.h>
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;
}
8 changes: 4 additions & 4 deletions Xcode/ch14/ManyBook/ManyBook.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
71EB79FF223CCEB000DB8C43 /* manybook.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = manybook.c; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -57,7 +57,7 @@
71E36506222A6A33009B856E /* ManyBook */ = {
isa = PBXGroup;
children = (
71E3650E222A6A61009B856E /* manybook.c */,
71EB79FF223CCEB000DB8C43 /* manybook.c */,
);
path = ManyBook;
sourceTree = "<group>";
Expand Down Expand Up @@ -118,7 +118,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
71E3650F222A6A61009B856E /* manybook.c in Sources */,
71EB7A00223CCEB000DB8C43 /* manybook.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
65 changes: 65 additions & 0 deletions Xcode/ch14/ManyBook/ManyBook/manybook.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* manybook.c -- multiple book inventory */
#include <stdio.h>
#include <string.h>
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;
}

0 comments on commit bd03196

Please sign in to comment.