C examples for Structure:Structure Value
Read book information and prints the book descriptions in the order entered, then alphabetized by title, and then in order of increased value.
#include <stdio.h> #include <string.h> char * s_gets(char * st, int n); #define MAXTITL 40 //ww w. jav a2 s . co m #define MAXAUTL 40 #define MAXBKS 100 /* maximum number of books */ struct book { /* set up book template */ char title[MAXTITL]; char author[MAXAUTL]; float value; }; void sortt(struct book * pb[], int n); void sortv(struct book * pb[], int n); int main(void) { struct book my[MAXBKS]; /* array of book structures */ struct book * pbk[MAXBKS]; /* pointers for sorting */ int count = 0; int index; printf("Please enter the book title. Press [enter] at the start of a line to stop.\n"); while (count < MAXBKS && s_gets(my[count].title, MAXTITL) != NULL && my[count].title[0] != '\0') { printf("author:\n"); s_gets(my[count].author, MAXAUTL); printf("value:\n"); scanf("%f", &my[count].value); pbk[count] = &my[count]; count++; while (getchar() != '\n') continue; /* clear input line */ if (count < MAXBKS) printf("Enter the next title.\n"); } printf("Here is the list of your books:\n"); for (index = 0; index < count; index++) printf("%s by %s: $%.2f\n", my[index].title, my[index].author, my[index].value); printf("Here is the list of your books sorted by title:\n"); sortt(pbk, count); for (index = 0; index < count; index++) printf("%s by %s: $%.2f\n", pbk[index]->title, pbk[index]->author, pbk[index]->value); sortv(pbk, count); printf("Here is the list of your books sorted by value:\n"); for (index = 0; index < count; index++) printf("%s by %s: $%.2f\n", pbk[index]->title, pbk[index]->author, pbk[index]->value); return 0; } void sortt(struct book * pb[], int n) { int top, search; struct book * temp; for (top = 0; top < n -1; top++) for (search = top + 1; search < n; search++) if (strcmp(pb[search]->title, pb[top]->title) < 0) { temp = pb[search]; pb[search] = pb[top]; pb[top] = temp; } } void sortv(struct book * pb[], int n) { int top, search; struct book * temp; for (top = 0; top < n -1; top++) for (search = top + 1; search < n; search++) if (pb[search]->value < pb[top]->value) { temp = pb[search]; pb[search] = pb[top]; pb[top] = temp; } } 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; }