From a8ee03c9768b8eaed11948d37a07c5c80abb4977 Mon Sep 17 00:00:00 2001 From: Steven Schronk Date: Tue, 28 Dec 2010 22:43:24 -0600 Subject: [PATCH] Added structure example --- struct.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ struct.txt | 15 ++++++++ 2 files changed, 143 insertions(+) create mode 100644 struct.c create mode 100644 struct.txt diff --git a/struct.c b/struct.c new file mode 100644 index 0000000..3626a9c --- /dev/null +++ b/struct.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include + +typedef struct { + char name [31]; + int age; + char gender; +} Student; + +int main() +{ + Student pupil[100]; + char aName[31]; + int j; + void getString(FILE *, char[]); + int getData(FILE *, Student[]); + int search(char[], Student[], int); + void sort(Student[], int); + void printStudent(Student); + void getString(FILE *, char[]); + FILE *in = fopen("struct.txt", "r"); + if(in == NULL) + { + printf("input file not found\n"); + exit(1); + } + + int numStudents = getData(in, pupil); + if(numStudents == 0) + { + printf("No data supplied for students\n"); + exit(1); + } + + printf("\n"); + for(j = 0; j < numStudents; j++) + printStudent(pupil[j]); + printf("\n"); + + getString(in, aName); + while(strcmp(aName, "END")!= 0) + { + int ans = search(aName, pupil, numStudents); + if(ans == -1) + printf("%s not found\n", aName); + else + printf("%s found at location %d\n", aName, ans); + getString(in, aName); + } + + sort(pupil, numStudents); + printf("\n"); + for(j = 0; j < numStudents; j++) + printStudent(pupil[j]); + } + + void printStudent(Student t) + { + printf("Name: %s Age: %d Gender:%c\n", t.name, t.age, t.gender); + } + + int getData(FILE *in, Student list[]) + { + int n = 0; + char temp[31]; + void getString(FILE *, char[]); + char readChar(FILE *); + + getString(in, temp); + while(strcmp(temp, "END") != 0 ) + { + strcpy(list[n].name, temp); + fscanf(in, "%d", &list[n].age); + list[n].gender = readChar(in); + n++; + getString(in, temp); + } + return n; +} + + int search(char key[], Student list[], int n) + { + int j; + for(j = 0; j < n; j++) + if(strcmp(key, list[j].name)== 0) + return j; + return -1; + } + + void sort(Student list[], int n) + { + Student temp; + int j, k; + for(j = 1; j < n; j++) + { + temp = list[j]; + k = j - 1; + while(k >= 0 && strcmp(temp.name, list[k].name) < 0) + { + list[k+1] = list[k]; + k = k - 1; + } + list[k+1] = temp; + } + } + + void getString(FILE *in, char str[]) + { + char ch, delim; + int n = 0; + str[0] = '\0'; + while(isspace(ch = getc(in))); /* empty while body */ + if(ch == EOF) { return; } + + delim = ch; + while(((ch = getc(in)) != delim) && (ch != EOF)) + str[n++] = ch; + str[n] = '\0'; + } + + char readChar(FILE *in) + { + char ch; + while(isspace(ch = getc(in))); /* empty while body */ + return ch; + } diff --git a/struct.txt b/struct.txt new file mode 100644 index 0000000..17e8715 --- /dev/null +++ b/struct.txt @@ -0,0 +1,15 @@ +"Jones, John" 24 M +"Mohammed, Lisa" 33 F +"Singh, Sandy" 29 F +"Layne, Dennis" 49 M +"Singh, Cindy" 16 F +"Ali, Irman" 39 M +"Kelly, Trudy" 30 F +"Cox, Kerry" 25 M +"END" + +"Kelly, Trudy" +"Layne, Dennis" +"Layne, Cindy" +"END" + -- 2.11.4.GIT