c语言数组教案.pptVIP

  • 70
  • 0
  • 约1.89万字
  • 约 77页
  • 2017-03-26 发布于贵州
  • 举报
c语言数组教案c语言数组教案

4 3 3 31 29 31 29 34 32 37 35 * 34 32 构造一个结构体类的数据类型的一般形式: struct 结构类型名 { 类型标识符 成员名; 类型标识符 成员名; : : 类型标识符 成员名; }; 结构体类型的定义 例如: struct student { char number[10]; char name[20]; char gender; int age; float score[20]; char addr[30]; }; 定义 先构造结构体类的数据类型,后定义具有这种构造的变量。 struct 结构体类型名 { 类型标识符 成员名; : 类型标识符 成员名; }变量名1,变量名2..; 结构体变量的定义和引用 例如: struct student { char number[10]; char name[20]; char gender; int age; float score[20]; char addr[30]; }stud1,stud2; 引用 对结构体变量的使用是通过对其成员的引用来实现的。引用结构体成员的一般形式如下: 结构体变量名.成员名 例如 : stud1.age 其中的圆点符号称为成员运算符,它的运算级别最高。 结构体变量的定义和引用 struct stud { long num; char name[20]; char gender; char addr[30]; }stud1={9708,”Liwei,F,144BeijingRoad”}; 结构体变量的初始化 #include stdio.h struct student {char name[20]; float s[3]; float ave; }stu[3]={“li”, 70,80,90,0, “wang”,83,94,75,0, “zhang”,92,86,89,0}; void main ( ) {int i; for ( i=0; i3; i++) {stu[i].ave=(stu[i].s[0]+ stu[i].s[1]+ stu[i].s[2])/3; printf(“%s,%5.2f\n”, stu[i].name, stu[i].ave);} } 输出结果是 。 li,80.00 wang,83.00 zhang,89.00 例5.7-1 输入3个学生的信息并输出。 #include stdlib.h #include stdio.h struct stud { long num; char name[20]; char gender; int age; float score; }; 应用举例 #include stdio.h #include stdlib.h void main() { struct stud student[3]; int i; char ch; char numstr[20]; for( i = 0 ; i 3 ; i++) /*输入*/ { printf(\n输入第[%d]个学生的数据:\n,i); gets(numstr);student[i].num = atol(numstr); gets(student[i].name); student[i].gender = getchar(); ch = getchar(); gets(numstr); student[i].age = atoi(numstr); gets(numstr); student[i].score = atof(numstr); } printf(\nnum\tname gender age score\n); for( i = 0 ; i 3 ; i++) printf(%ld%-15s%3c%6d %6.2f\n,student[i].num, student[i].name,student[i].gender,student[i].age, student[i].score); } 小技巧 数 值 转 换 函 数 atoi int ato

文档评论(0)

1亿VIP精品文档

相关文档