黑马程序员技术交流社区
标题:
结构体数组的初始化和遍历
[打印本页]
作者:
董立正
时间:
2015-12-5 10:51
标题:
结构体数组的初始化和遍历
#include <stdio.h>
void test(){
struct Student{
char name[20];
int age;
}boy[3]={{"sb",18},{"zbz",38},{"cgx",20}};
struct Student girls[2] = {{"fengjie",18},{"cjk",28}};
struct Student ds[2];
ds[0] = (struct Student){"xzmly",18};
ds[1] = (struct Student){"bdjy",18};
struct Student stu[2];
//stu[0].name = "zhangsan";
//strcpy(stu[0].name,"zhangsan");
scanf("%s",stu[0].name);
stu[0].age = 19;
// char ch[0];
// scanf("%s",ch);
for (int i = 0; i < 3; i++) {
printf("name:%s,age:%d\n",boy[i].name,boy[i].age);
}
}
int main(int argc, const char * argv[]) {
struct stu{
int num;
char *name;
char sex;
float score;
};
struct stu boy[5] = {
{101,"Li ping",'F',45},
{102,"Zhang ping",'M',62.5},
{103,"He fang",'F',92.5},
{104,"Cheng ling",'M',87},
{105,"Wang ming",'M',58}
};
float sum = 0.0f;
int count = 0;
for (int i = 0; i < 5; i++) {
sum += boy[i].score;
if (boy[i].score < 60) {
count++;
}else if (boy[i].score >= 80 && boy[i].score <= 100){
printf("name: %s, score: %.2f\n",boy[i].name,boy[i].score);
}
}
printf("avgscore: %.2f\n",sum/5);
printf("count: %d\n",count);
return 0;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2