C语言(结构体)
#include <stdio.h>
#include "Coustom.h"int main(int argc, const char * argv[]) {
#pragma mark -- 1、有3个学生,定义一个结构体包含每个学生信息(编号、姓名、性别、分数),借助条件表达式,编程找出分数最高者;
// typedef struct Student {
// int code;
// char *name;
// char sex;
// double score;
// }Student;
// Student student1 = {101,"zhangsan",'f',89.0};
// Student student2 = {102,"lisi",'m',89.0};
//
// Student student3 = {103,"laowang",'f',89.0};
//
// Student maxStudent;//结构体变量
// maxStudent = student1.score > student2.score ? student1 : student2;
// maxStudent = maxStudent.score > student3.score ? maxStudent :student3;
//
// printf("maxStudent's name is %s",maxStudent.name);
#pragma mark -- 2、定义一个结构体,描述日期的年月日;
// 1)提示用户输入日期,计算该日在本年中为第几天?
// 2)求今年任意2天的天数差?
//
// printf("请输入一个日期:\n");
// Date date;
// scanf("%d,%d,%d",&date.year,&date.month,&date.day);
// int count1 = totalDays(date);
#pragma mark -- 3、某班有5个学生,三门课。分别编写实现以下要求:
// (1) 写一个函数,输出一名学生的所有信息
// (2) 求各门课的平均分;
// (3) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
// (4) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名
// 注:结构体元素有:num,name,score[3]
Student students[5] = {{101,"rimi1",{45,89,67}},{101,"rimi1",{45,89,67}},{101,"rimi1",{45,89,67}},{101,"rimi1",{45,89,67}},{101,"rimi1",{45,89,67}}};
int number = 0;
do {
printf("2.各科的平均分");
printf("3.不及格");
printf("请输入您的选择:\n");
scanf("%d",&number);
switch (number) {
case 2:
avergScores(students);
break;
case 3:
failedScores(students);
break;
case 4:
goodScores(students);
break;
default:
break;
}
} while (number != 5);
return 0;
}
创建 C File文件 类名Coustom
.h
typedef struct Date {
int year;
int month;
int day;
}Date;
typedef struct Student {
int number;
char *name;
double score[3];
}Student;
//1。打印学生的信息
void printInformation(Student student);
//2.求各门课的平均成绩
void avergScores(Student *stu);
//3.
void failedScores(Student *stu);
//4.
void goodScores(Student *stu);
int totalDays(Date date);
.c
#include "Coustom.h"
int totalDays(Date date){
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int count = 0;//
//判断闰年
if ((date.year % 4 == 0 && date.year % 100 != 0) || (date.year % 400 == 0)) {
days[1] += 1;
}
for (int i = 0; i < date.month - 1; i++) {
count += days[i];
}
count += date.day;
return count;
}
//1。打印学生的信息
void printInformation(Student student){
printf("姓名: 学号: 语文: 数学: 英语\n%s %d %lf %lf %lf\n",student.name,student.number,student.score[0],student.score[1],student.score[2]);
}
//2.求各门课的平均成绩
void avergScores(Student *stu){
double average[3];//3科的平均数
for (int i = 0; i < 3; i++) {
double sum = 0;
for (int j = 0; j < 5; j++) {
sum += stu[j].score[i];//谋个学生的某个学科的总分
}
average[i] = sum / 5;
}
printf("各科的平均成绩是:\n");
for (int i = 0; i < 3; i++) {
printf("%lf\t",average[i]);
}
}
//3.
void failedScores(Student *stu){
for (int i = 0; i < 5; i++) {
int flag = 0;
for (int j = 0; j< 3; j++) {
if (stu[i].score[j] < 60) {
flag++;
}
}
if (flag>=2) {
printf("不及格学生的学号:%d 成绩如下:\n",stu[i].number);
for (int k = 0; k < 3; k++) {
if (stu[i].score[k] < 60) {
printf("%lf\t",stu[i].score[k]);
}
}
}
}
}
//4.
void goodScores(Student *stu){
double sum[3];
for (int i = 0; i < 5; i++) {
sum[i] = 0;
for (int j = 0; j < 3; j++) {
sum[i] = stu[i].score[j];
}
//
double average = sum[i]/3;
if (average > 85 && average <= 90 ) {
printf("number is %d name is %s\n",stu[i].number,stu[i].name);
}
}
}