資源描述:
《c語(yǔ)言程序設(shè)計(jì)教程第10章new》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、195結(jié)構(gòu)體、共用體和枚舉類型10.1結(jié)構(gòu)體類型與變量的定義10.1.1結(jié)構(gòu)體類型的定義structstudent{longintnum;charname[10];floatscore1,score2,score3;};structstudentstudent1,student2;圖10.1structstudent類型量結(jié)構(gòu)圖10.2數(shù)組作為結(jié)構(gòu)體成員10.1.2結(jié)構(gòu)體類型的嵌套定義structdate{intmonth;intday;intyear;};structstudent{longintnum;第10章章結(jié)構(gòu)體、共用體和枚舉類型195chatn
2、ame[10];charsex;structdatebirthday;charaddr[30];}student1,student2;圖10.3結(jié)構(gòu)體的嵌套結(jié)構(gòu)10.1.3結(jié)構(gòu)體變量的初始化structstudent{longintnum;charname[10];charsex;intage;floatscore;}student1={9908001,"LiMing",'M',19,87.5};圖10.4初始化后結(jié)構(gòu)體變量各成員的值圖10.5嵌套結(jié)構(gòu)的結(jié)構(gòu)體變量的初始化10.2結(jié)構(gòu)體類型變量的引用#include#include3、ing.h>#defineSTUstructstudentSTU第10章章結(jié)構(gòu)體、共用體和枚舉類型195{longnum;charname[10];charsex;intage;floatscore;};voidmain(){STUstudent1;printf("num:");scanf("%ld",&student1.num);/*輸入student1的各成員值*/printf("name:");getchar();/*濾掉輸入student1.num后所加的回車符*/gets(student1.name);printf("sex(M/F):");sc
4、anf("%c",&student1.sex);printf("age:");scanf("%d",&student1.age);printf("score:");scanf("%f",&student1.score);/*輸出student1的各成員值*/printf("num:%ld",student1.num);printf("name:%s",student1.name);printf("sex:%c",student1.sex);printf("age:%d",student1.age);printf("score:%f",s
5、tudent1.score);}#includestructdate{intmonth;intday;intyear;};structstudent{longnum;charname[10];charsex;structdatebirthday;charaddr[30];};voidmain(){structstudentstud1={990801,"LiMing",'M',{9,12,1981},"HongKong"};第10章章結(jié)構(gòu)體、共用體和枚舉類型195printf("num:%ld",stud1.num);printf("n
6、ame:%s",stud1.name);printf("sex:%c",stud1.sex);printf("birthday:%d/%d/%d",stud1.birthday.month,stud1.birthday.day,stud1.birthday.year);printf("address:%s",stud1.addr);}10.3結(jié)構(gòu)體數(shù)組structstudent{longintnum;charname[10];floatscore;}stus[3];structstudent{longintnum;charname[10];
7、floatscore;}stus[3]={{990801,"LiMing",92.5},{990802,"LuYun",89.0},{990803,"YangHong",96.5}};#defineMAX30#includestructstuscore{longintnum;charname[10];intscore[3];inttotal;};voidmain(){structstuscorestu[MAX];第10章章結(jié)構(gòu)體、共用體和枚舉類型195inti;for(i=0;i8、f("num:");scanf("%ld",&stu[i].num