资源预览内容
第1页 / 共89页
第2页 / 共89页
第3页 / 共89页
第4页 / 共89页
第5页 / 共89页
第6页 / 共89页
第7页 / 共89页
第8页 / 共89页
第9页 / 共89页
第10页 / 共89页
亲,该文档总共89页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
结构体结构的声明n结构的概念u结构是由不同数据类型的数据组成的集 合体。n声明结构类型struct 结构名数据类型 成员名 1;数据类型 成员名 2; :数据类型 成员名 n; ;结构体结构的声明n举例: struct student /学生信息结构体 int num; /学号char name20; /姓名char sex; /性别int age; /年龄float score; /成绩char addr30; /住址 结构体结构变量说明n变量说明形式结构名 结构变量名;n注意:u结构变量说明在结构类型声明之后,二 者也可同时进行。u结构变量占内存大小可用 sizeof 运算求 出:sizeof(运算量)结构体 结构变量的初始化 和使用n初始化说明结构变量的同时可以直接设置 初值。n使用结构体成员的引用形式:结构变量名.成员名例2-13结构体变量的初始化和使用 #include #include struct student /学生信息结构体 int num; /学号char name20; /姓名char sex; /性别int age; /年龄 stu=97001,“Lin Lin“,F,19;void main( ) cout struct animal int weight;int feet; ;int main( ) animal dog1, dog2, chicken;dog1.weight = 15;dog2.weight = 37;chicken.weight = 3;dog1.feet = 4;dog2.feet = 4;chicken.feet = 2;cout Using namespace std; Struct student int num; char name20; float score; ; int main() 0联合体n声明形式: union 联合名数据类型 成员名 1;数据类型 成员名 2; :数据类型 成员名 n; ;n联合体类型变量说明的语法形式 联合名 联合变量名;n引用形式: 联合名.成员名例: union uarea char c_data;short s_data;long l_data; uareac_datal_datas_data联合体无名联合n无名联合没有标记名,只是声明一个成 员项的集合,这些成员项具有相同的内存 地址,可以由成员项的名字直接访问。n例: union int i;float f; 在程序中可以这样使用: i=10; f=2.2;例2-15#include struct aircraft int wingspan;int passengers;union float fuel_load; float bomb_load; int pallets; ; fighter, bomber, transport;int main( ) fighter.wingspan = 40;fighter.passengers = 1;fighter.fuel_load = 12000.0;bomber.wingspan = 90;bomber.passengers = 12;bomber.bomb_load = 14000.0;transport.wingspan = 106;transport.passengers = 4;transport.pallets = 42;transport.fuel_load = 18000.0;fighter.pallets = 4;cout enum game_result WIN, LOSE, TIE, CANCEL; int main( ) game_result result; enum game_result omit = CANCEL; int count;for (count = WIN ; count #include using namespace std; void main() char string80; char str380; coutstring1; cout=、 #include using namespace std; int main() string s1=“DEF”,s2=“123”; char cp1=“ABC”; char cp2=“DEF”; cout #include using namespace std; int main() string string1,string2,string3,temp;coutstring1string2string3;if(string2string3)temp=string2;string2=string3;string3=temp;if(string1 name1, name2, ., namenn函数的返回值u由 return 语句给出,例如: return 0u无返回值的函数(void类型),不必 写return语句。函数的调用n1 、函数调用n调用前先说明函数原型:u在调用函数的说明部分,或程序文件 开头所有函数之前,按如下形式说明:类型标识符 被调用函数名 (含类型说明的 形参表);n调用形式函数名(实参列表)例3-1 编写一个求x的n次方的函数#include doublepower (double x, int n); void main(void) cout double power (double x, int n);void main(void) int i; int value = 0; char ch;cout = 0; i-) cin ch; if (ch = 1) value += int(power(2,i); cout void main(void) int a,b;int fun1(int x,int y);cinab;cout long fac(int n) long f;if (nn;y=fac(n);cout void Swap(int a, int b); int main( ) int x(5), y(10); cout void Swap(int int main( ) int x(5), y(10); cout #include void fiddle(int in1, int int main( ) int count = 7, index = 12;cout n#includenusing namespace std;nvoid getroots(double,double,double,doublenint main()nndouble a,b,c;ndouble root1,root2;ncoutabc;ngetroots(a,b,c,root1,root2);ncout inline double CalArea(double radius) return 3.14*radius*radius; int main( ) double r(3.0); double area; area=CalArea(r); cout #include int get_volume(int length, int width = 2, int height = 3); int main( ) int x = 10, y = 12, z = 15;cout struct complex double real; double imaginary; ;void main(void) int m, n; double x, y; complex c1, c2, c3; int add(int m, int n); double add(double x, double y); complex add(complex c1, complex c2);coutmn; coutxy; coutc1.realc1.imaginary; coutc2.realc2.imaginary; c3=add(c1,c2); cout using namespace std; struct time int hour; int minute; ; const int perhour=60; time sum(time t1,time t2); void show(time t); int main() time one=8,15; time two=6,55; time day=sum(one,two); cout using namespace std; int main() int max_value(int x,int max);int i,j,row=0,colum=0,max;int a34=5,12,23,56,19,28,37,46,-12,-34,6,8;max=a00;for(i=0;imax) return x;else return max; )数组与函数#include using namespace std;int sum_array( const int array, int no_of_elements); main() int values10 = 12,4,5,3,4,0,1,8,2,3;int sum;sum= sum_array( values ,10);cout nusing namespace std ;n int sum_array( const int array2, int no_of_rows ) ; nmain()n nint values52 = 31, 14 , 51, 11 , 7, 10 , 13, 41 , 16, 18 ; nint sum ; nsum = sum_array( values, 5 ) ; ncout # include using namespace std ; void display_student_data( struct student_rec student_data ) ; void get_student_data( struct student_rec struct student_recint number ;float scores5 ; ; main() struct student_rec student ; get_student_data( student ) ; display_student_data( student ) ; void display_student_data( struct student_rec student_data ) cout student_ref.number;coutstudent_ref.scoresi; Number:1234 Five test scores: 75 80 65 45 68 The data in the student structure is: Number is 12345 Scores are:75.0 80.0 65.0 45.0
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号