#include <stdio.h>
void test1(){
//AAAAAAAAAAAA
//11111111
/* 1)定义结构体的同时,定义数组
struct Student
{
int age;
char *name;
int s_no;
float score;
}stu[5];
2)先定义结构体,后定义数组
struct Student boy[5];
*/
/*
#include<stdio.h>
int main()
{
struct Student
{
int age;
char *name;
int s_no;
float score;
}stu[5];
struct Student boy[5];
return 0;
}
*/
//2222222222
//结构体数组的初始化方法
//1、定义结构体数组的时候,进行初始化
struct Student
{
char name[20];
int age;
}boys[3]={
{"sb",18},
{"zbz",38},
{"cgx",28}};
//2、定义的同时进行初始化
struct Student girls[2]={
{"fengjie",18},
{"cjk",28}};
/*
这个也对
//3、先定义后初始化
struct Student ds[2];
ds[0]=(struct Student){"xzmly",18};
ds[1]=(struct Student){"bdjy",18};
*/
//4、先定义结构体数组,后初始化
struct Student stu[2];
//stu[0].name = "zhangsan"
//strcpy(stu[0].name="zhangsan");
scanf("%s",stu[0].name);
stu[0].age = 19;
//BBBBBBBBBBBBBBB
//结构数组的遍历
for(int i=0;i<3;i++){
printf("name:%s,age:%d\n",boys[0].name,boys[i].age);
}
}
int main(int argc, const char * argv[]) {
//结构体
struct stu{
int num;
char *name;
char sex;
float score;
};
//1、定义结构体数组
struct stu boy[5]={
{101,"Li ping",'F',45},
{102,"Zhang ping",'M',62.5},
{102,"He Fang",'F',92.5},
{102,"Cheng ling",'M',87},
{102,"Wang ming",'M',58}
};
//2、有一个循环
float sum = 0.0f;
int count=0;
for (int i=0; i<5; i++){
// 计算总成绩
sum+=boy[i].score;
// 判断成绩 <60 计算器+1
if(boy[i].score<60) {
count++;
}else if (boy[i].score<=100&&boy[i].score>=80){
// 判断成绩 80--100
printf("姓名:%s,成绩:%.2f\n",boy[i].name,boy[i].score);
// 输出姓名和成绩
}
}
printf("平均值:%.2f\n",sum/5);
printf("不及格的人数:%d\n",count);
return 0;
|
|