Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures Structure Arrays. Arrays of Structures Dr. Sadık EşmelioğluCENG 1142 • For the student structure type defined previously, an array of students.

Similar presentations


Presentation on theme: "Structures Structure Arrays. Arrays of Structures Dr. Sadık EşmelioğluCENG 1142 • For the student structure type defined previously, an array of students."— Presentation transcript:

1 Structures Structure Arrays

2 Arrays of Structures Dr. Sadık EşmelioğluCENG 1142 • For the student structure type defined previously, an array of students can be defined as follows … { struct studentstds[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); … }

3 Array of Structures in Memory fname 15 chars lname 15 chars no integer GPA double JohnSmith JaneDoe GregoryAdams ………… CatherineFontaine Dr. Sadık EşmelioğluCENG 1143 stds[0] stds[1] stds[2] stds[49] struct student stds[50] stds[1].fnameis“Jane” stds[1].fname[2]is‘n’ stds[49].nois76543 stds[48].GPA = 3.9; scanf(“%d”, &stds[48]. no); gets(stds[48]. lname); strcpy(stds[48]. fname, “Joan”);

4 Array of Structures in Memory fname 15 chars lname 15 chars no integer GPA double JohnSmith JaneDoe GregoryAdams ………… CatherineFontaine Dr. Sadık EşmelioğluCENG 1144 stds[0] stds[1] stds[2] stds[49] stds[2].lname[3] stdsarray of structures stds[2]structure stds[2].lnamearray of characters stds[2].lname[2]a character

5 Operator Precedence SymbolsOperator NamesAssoc.Precedence x[i] fnc(…). Array scripting, Function Calls, direct component selection leftHighest x++ y--Postfix increment and decrementleft ++x -–y ! - + & * Prefix incr. and decr., NOT, unary negation and plus, address of, pointer right (int)xType castingright * / %Multip., Div., Remainderleft + –Addition and Subtractionleft >=Less Than, Greater Than, etc.left == !=Equal to and Not Equal toleft &&ANDleft ||ORleft =AssignmentrightLowest Dr. Sadık EşmelioğluCENG 1145

6 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. Dr. Sadık EşmelioğluCENG 1146

7 Example Dr. Sadık EşmelioğluCENG 1147 #include #defineMAX_CUST 1000 #defineMAX_NAME 15 struct date { intDay;/* 0-31 */ intMonth;/* 1-12 */ intYear;/* 4 digit year */ }; #include #defineMAX_CUST 1000 #defineMAX_NAME 15 struct date { intDay;/* 0-31 */ intMonth;/* 1-12 */ intYear;/* 4 digit year */ }; AyseCantez12/12/ H HasanTopcuoglu31/05/ H CanerHepten22/05/ L CanHepten17/08/ M AyseCantez12/12/ H HasanTopcuoglu31/05/ H CanerHepten22/05/ L CanHepten17/08/ M

8 Example Dr. Sadık EşmelioğluCENG 1148 struct phone { intAcode;/* Area Code 3 digit */ intExch;/* Switch Exchange 3 digit */ intSubs;/* Subscriber no 4 digit */ }; struct customer { charfname[MAX_NAME];/* First Name */ charlname[MAX_NAME];/* Last Name */ structdatebdate;/* Birth Date */ struct phonehphone;/* Home Phone */ charvip;/* VIP Status (H, M, L) */ } ; intReadCustArr(struct customer cust[], FILE *fptr); voidPrntCustArr(struct customer cust[], int NoofCust); voidPrntCustVip(struct customer cust[], int NoofCust, char v); struct phone { intAcode;/* Area Code 3 digit */ intExch;/* Switch Exchange 3 digit */ intSubs;/* Subscriber no 4 digit */ }; struct customer { charfname[MAX_NAME];/* First Name */ charlname[MAX_NAME];/* Last Name */ structdatebdate;/* Birth Date */ struct phonehphone;/* Home Phone */ charvip;/* VIP Status (H, M, L) */ } ; intReadCustArr(struct customer cust[], FILE *fptr); voidPrntCustArr(struct customer cust[], int NoofCust); voidPrntCustVip(struct customer cust[], int NoofCust, char v);

9 Example Dr. Sadık EşmelioğluCENG 1149 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'); } 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'); }

10 Example Dr. Sadık EşmelioğluCENG int ReadCustArr(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); int ReadCustArr(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);

11 Example Dr. Sadık EşmelioğluCENG 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); } 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); }

12 Example Dr. Sadık EşmelioğluCENG void PrntCustArr(struct customer cust[], int n) { int i; 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); } void PrntCustArr(struct customer cust[], int n) { int i; 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); }

13 Example Dr. Sadık EşmelioğluCENG void PrntCustVip(struct customer cust[], int n, char v) { int i; 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); } void PrntCustVip(struct customer cust[], int n, char v) { int i; 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); }


Download ppt "Structures Structure Arrays. Arrays of Structures Dr. Sadık EşmelioğluCENG 1142 • For the student structure type defined previously, an array of students."

Similar presentations


Ads by Google