cs202ch8.docx
- 文档编号:4311116
- 上传时间:2022-11-29
- 格式:DOCX
- 页数:14
- 大小:25.29KB
cs202ch8.docx
《cs202ch8.docx》由会员分享,可在线阅读,更多相关《cs202ch8.docx(14页珍藏版)》请在冰豆网上搜索。
cs202ch8
CLanguage(CS102)
SECTION8.1
STRUCTURES
{charname[10];//afield
intid;
intyear;
doublegrade;
};
{intmonth,day,year;};
*Weusestructurewhenwewanttocombinedataofdifferenttypesintoasingle
variable.(wecan'tusearraybecausearrayelementallsharethesametype)
*structisakeywordwhichmeansstructure
DECLARINGSTRUCTUREVARIABLE
#include
structdate
{intmonth,day,year;};
main()
{
intindex;
structdatestart;
:
return0;
}
*structdatestart;
Itdeclaresastructurevariable
INITIALIZINGandASSCESSINGaSTRUCTUREVARIABLE
DotOperator
#include
#defineMAXNAME20
#defineMAXNO8
structstd
{charname[MAXNAME];
charstd_no[MAXNO];
unsignedintyear;
doubleGPA;
};
main()
{
//initializeastructureatcompiletime
structstdstudent1;={"MayLee","CS98000",2,87.3};
structstdstudent2;
//initalizeastructureatruntime
strcpy(student2.name,"EddieChan");
strcpy(student2.std_no,"CS97000");
student2.year=1;
student2.GPA=80.88;
printf("%s\n",student2.name);
printf("%s\n",student2.std_no);
printf("%u\n",student2.year);
printf("%lf\n",student2.GPA);
return0;
}
*OUTPUTEddieChan
CS97000
1
80.880000
*Toaccessafield,weneed<1>structurename
<2>adot(.)
<3>fieldname
namestd_noyearGPA
*student1EddieChan\0CS97000\0180.880000
ArrowOperatorAndDerefereningAPointer
#include
#defineMAXNAME20
#defineMAXNO8
structstd
{charname[MAXNAME];
charstd_no[MAXNO];
unsignedintyear;
};
main()
{structstdstudent;
structstd*std_ptr;
scanf(“%s”,student.name);
scanf(“%s”,student.std_no);
scanf(“%u”,&student.year);
std_ptr=(structstd*)malloc(sizeof(structstd));
if(std_ptr!
=NULL)
{strcpy(std_ptr->name,"MayLee");
strcpy(std_ptr->std_no,"CS97300");
std_ptr->year=2;
}
printf("%s\n",student.name);
printf("%s\n",student.std_no);
printf("%u\n",student.year);
printf("%s\n",std_ptr->name);
printf("%s\n",std_ptr->std_no);
printf("%u\n",std_ptr->year);
printf("%s\n",(*std_ptr).name);
printf("%s\n",(*std_ptr).std_no);
printf("%u\n",(*std_ptr).year);
return0;
}
*Theexpression(*std_ptr).year
Theparentheses()areneededbecausethestructurememberoperator(.)has
ahigherprecedencethanthepointerdereferencingoperator(*).
Theexpression*std_ptr.yearsyntaxerror!
!
!
!
!
*Donotputspacearoundthe->and.oprerator.
CopytheContentofOneStructuretoAnotherStructure
#include
structdate
{unsignedintday;
unsignedintmonth;
unsignedintyear;
};
main()
{structdateold_date={15,8,1997};
structdatenew_date,
new_date.day=old_date.day;
new_date.month=old_date.month;new_date=old_date;
new_date.year=old_date.year;
return0;
}
*Assignment(=)isnotallowediftwostructuresareofdifferenttypes(error!
!
!
)
#include
structdatestructspecial_date
{unsignedintday;{unsignedintday;
unsignedintmonth;unsignedintmonth;
unsignedintyear;unsignedintyear;
};};
main()
{structdatedate1;
structspecial_datedate2;
date2=date1//ERROR!
!
!
!
!
!
!
!
date2.month=date1.year;//OK
return0;
}
*Therearenooperatorsforcomparingtwostructures.
Language(CS102)
SECTION8.2
STRUCTUREANDFUNCTIONS
#include
structdate
{unsignedintday;
unsignedintmonth;
unsignedintyear;};
typedefstructdateDATE;
DATEget_info();
voidget_date(DATE*);
voidprint1_date(DATE);
voidprint2_date(DATE*);
main()
{
DATEnew_date,old_date;
old_date=get_info();
get_date(&new_date);
print1_date(old_date);//passastructurebyvalue
print2_date(&old_date);//passastructurebyreference
return0;
}
DATEget_info()
{DATEdate;
scanf(“%u%u%u”,&date.day,&date.month,&date.year);
returndate;
}
voidget_date(DATE*date)
{scanf(“%u%u%u”,&(date->day),&(date->month),&(date->year));}
voidprint1_date(DATEdate)
{printf("%u%u%u\n",date.day,date.month,date.year);}
voidprint2_date(DATE*date)
{printf("%u",date->day);printf("%u",(*date).day);
printf("%u",date->month);printf("%u",(*date).month);
printf("%u\n",date->year);printf("%u\n",(*date).year);
}
*Unlikearrayname,structurenameisNOTapointer.
*WecanpassastructureByvalueandByreference.
Weusuallypassapointer(byreference)becauseit'sfastertopassapointerthan
topasstheentirestructure.
*What'sWrongwiththefollowings?
?
?
?
#include
voidprint1_date(DATE);
voidprint2_date(DATE*);
voidprint3_date(DATE*);
structdate
{unsignedintday;
unsignedintmonth;
unsignedintyear;
};
typedefstructdateDATE;
main()
{
:
}
->weCANNOTuseathestructuretype"DATE"inthefunctionprototype
beforewedefinethetypeDATE.
CLanguage(CS102)
SECTION8.3
STRUCTUREOfSTRUCTURE
#include
structDate
{intmonth;
intday;
intyear;
};
typedefstructDateDATE;
structTerm
{DATEstart;
DATEend;
};
typedefstructTermTERM;
main()
{TERMExamVar,*ExamPtr;
ExamVar.start.month=2;//scanf(“%d”,&ExamVar.start.day);
ExamVar.start.day=6;
ExamVar.start.year=1999;
ExamVar.end.month=2;
ExamVar.end.day=13;
ExamVar.end.year=1999;
ExamPtr=(TERM*)malloc(sizeof(TREM));
if(ExamPtr!
=NULL)
{ExamPtr->start.day=2;//scanf(“%d”,&(ExamPtr->start.day));
ExamPtr->start.month=6;
ExamPtr->start.year=1999;
ExamPtr->start.day=2;
ExamPtr->start.month=13;
ExamPtr->start.year)=1999
}
return0;
}
CLanguage(CS102)
SECTION8.4
ARRAYOfSTRUCTURE
#include
#include
#defineSIZE5
structStudent
{charname[20];
charstd_id[10];
intyear;
};
typedefstructStudentSTUDENT;
main()
{STUDENTstudent_list[SIZE];
intcount=0;
charexit='n';
printf("\nEnterq/Qtoexittheprogram\n");
printf("Studentname:
");
scanf("%s",student_list[count].name);
if(strcmp(student_list[count].name,"q")==0||strcmp(student_list[count].name,"Q")==0)
exit='y';
while(count {printf("StdentID: "); scanf("%s",student_list[count].std_id); printf("Year: "); scanf("%d",&student_list[count].year); count++; printf("\nEnterq/Qtoexittheprogram\n"); printf("Studentname: "); scanf("%s",student_list[count].name); if(strcmp(student_list[count].name,"q")==0|| strcmp(student_list[count].name,"Q")==0) exit='y'; } return0; } OUTPUT: Enterq/QtoexittheprogramEnterq/Qtoexittheprogram Studentname: EddieStudentname: Amy StdentID: CS0011StdentID: CS0012 Year: 1Year: 2 Enterq/Qtoexittheprogram Studentname: q ArrayOf(StructureOfStructure) #include #defineSIZE2 structBIRTH {shortday,month,year;}; structStudent {charname[20]; charstd_id[10]; structBIRTHbirthday; }; typedefstructStudentSTUDENT; voidprint_list(STUDENT*); main() {STUDENTstudent_list[SIZE]; intcount; chargarbage[5]; for(count=0;count {printf("Studentname: "); gets(student_list[count].name); printf("StdentID: "); scanf("%s",student_list[count].std_id); printf("Dayofbirth--day/month/year: "); scanf("%hd%hd%hd",&student_list[count].birthday.day, &student_list[count].birthday.month, &student_list[count].birthday.year); gets(garbage); } print_list(student_list); return0; } voidprint_list(STUDENT*student_list) {intindex=0; while(index {printf("\nRECORD%d\n",index); printf("%s\n",student_list[index].name); printf("%s\n",student_list[index].std_id); printf("%hd%hd%hd\n",student_list[index].birthday.day, student_list[index].birthday.month, student_list[index].birthday.year); index++; } } OUTPUT: Addarecordbyentering: Studentname: EddieChan StdentID: CS0011 Year: 1 Department: CS Dayofbirth--day/month/year: 27380 Addarecordbyentering: Studentname: AmyChan StdentID: CS0012 Year: 2 Department: CS Dayofbirth--day/month/year: 12680 RECORD0 EddieChan CS0011 1 27380 RECORD1 AmyChan CS0012 2 12680
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- cs202ch8