forked from Huang-Libo/C-Primer-Plus-6e
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
987753e
commit bd03196
Showing
4 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |