130 likes | 247 Views
Structures. Structure Arrays. Arrays of Structures. For the student structure type defined previously, an array of students can be defined as follows … { struct student stds [50]; for( i = 0; i < 50; i ++) FillStd (& stds [ i ]); for( i = 0; i < 50; i ++)
E N D
Structures Structure Arrays
Arrays of Structures • For the student structure type defined previously, an array of students can be defined as follows … { struct student stds[50]; for(i = 0; i < 50; i++) FillStd(&stds[i]); for(i = 0; i < 50; i++) printf(“%d\t%.2f”, stds[i].no, stds[i].GPA); … } CENG 114
Array of Structures in Memory struct student stds[50] stds[0] stds[1] stds[2] stds[49] stds[48].GPA = 3.9; stds[1].fname is “Jane” scanf(“%d”, &stds[48]. no); stds[1].fname[2] is ‘n’ gets(stds[48]. lname); stds[49].no is 76543 strcpy(stds[48]. fname, “Joan”); CENG 114
Array of Structures in Memory stds[0] stds[1] stds[2] stds[49] stds[2].lname[3] stds array of structures stds[2] structure stds[2].lname array of characters stds[2].lname[2] a character CENG 114
Operator Precedence CENG 114
Example • We are to create a file based customer database • Each line of file will keep information about one customer • First Name • Last Name • Birth Date (dd/mm/yyyy) • Home Phone (xxx yyy zzzz) • VIP Status (H or M or L) • Write a function to read this file into an array of structures • Write other functions to query this database • print all customers • print customers according to VIP status • etc. CENG 114
Example AyseCantez 12/12/1975 312-222-1234 H HasanTopcuoglu 31/05/1955 212-343-5555 H Caner Hepten 22/05/1993 316-654-1414 L Can Hepten 17/08/1963 226-323-0567 M #include <stdio.h> #define MAX_CUST 1000 #define MAX_NAME 15 struct date { int Day; /* 0-31 */ int Month; /* 1-12 */ int Year; /* 4 digit year */ }; CENG 114
Example struct phone { intAcode; /* Area Code 3 digit */ intExch; /* Switch Exchange 3 digit */ int Subs; /* Subscriber no 4 digit */ }; struct customer { char fname[MAX_NAME]; /* First Name */ char lname[MAX_NAME]; /* Last Name */ struct date bdate; /* Birth Date */ struct phone hphone; /* Home Phone */ char vip; /* VIP Status (H, M, L) */ } ; intReadCustArr(struct customer cust[], FILE *fptr); void PrntCustArr(struct customer cust[], intNoofCust); void PrntCustVip(struct customer cust[], intNoofCust, char v); CENG 114
Example int main(void) { FILE *CustDB; Cust_tcust[MAX_CUST]; intNoofCust; CustDB = fopen("CUSTDB.txt", "r"); NoofCust = ReadCustArr(cust, CustDB); fclose(CustDB); PrntCustArr(cust, NoofCust); puts(""); PrntCustVip(cust, NoofCust, 'H'); } CENG 114
Example intReadCustArr(struct customer cust[], FILE *fptr) { intReadStat; inti = 0; ReadStat = fscanf(fptr, "%s %s %d/%d/%d %d-%d-%d %c", cust[i].fname, cust[i].lname, &cust[i].bdate.Day, &cust[i].bdate.Month, &cust[i].bdate.Year, &cust[i].hphone.Acode, &cust[i].hphone.Exch, &cust[i].hphone.Subs, &cust[i].vip); CENG 114
Example while(ReadStat != EOF) { i++; ReadStat = fscanf(fptr, "%s %s %d/%d/%d %d-%d-%d %c", cust[i].fname, cust[i].lname, &cust[i].bdate.Day, &cust[i].bdate.Month, &cust[i].bdate.Year, &cust[i].hphone.Acode, &cust[i].hphone.Exch, &cust[i].hphone.Subs, &cust[i].vip); } return(i); } CENG 114
Example void PrntCustArr(struct customer cust[], intn) { inti; for(i = 0; i < n; i++) printf("%-14s %-14s %02d/%02d/%02d %03d-%03d-%04d %c\n", cust[i].fname, cust[i].lname, cust[i].bdate.Day, cust[i].bdate.Month, cust[i].bdate.Year, cust[i].hphone.Acode, cust[i].hphone.Exch, cust[i].hphone.Subs, cust[i].vip); } CENG 114
Example void PrntCustVip(struct customer cust[], intn, char v) { inti; for(i = 0; i < n; i++) if(cust[i].vip == v) printf("%-14s %-14s %02d/%02d/%02d %03d-%03d-%04d %c\n", cust[i].fname, cust[i].lname, cust[i].bdate.Day, cust[i].bdate.Month, cust[i].bdate.Year, cust[i].hphone.Acode, cust[i].hphone.Exch, cust[i].hphone.Subs, cust[i].vip); } CENG 114