/* 第一题(**)定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题),要求写一个函数 days,实现上面的计算。 由主函数将年月日传递给 days 函数,计算后将日子传递回主函数输出。*/
{
typedef struct data{
int year;
int month;
int day;
}Data;
Data today;
printf("第一题:\n");
printf("请输入年月日:");
scanf("%d%d%d",&today.year,&today.month,&today.day);
printf("今天是本年中的第%d天\n",days(today));
}
{//子函数
int days(Data today);
int days(Data today){
int monthday[]={31,28,31,30,31,30,31,31,30,31,30,31},sumDay = 0;
for (int i = 0; i <= today.month - 1; i++) {
sumDay += monthday[i];
}
if (today.month > 2) {
if ((today.year % 4 == 0 && today.year % 100 != 0)||(today.year % 400 == 0)) {
sumDay += 1;
}
}
return sumDay;
}
}
/* 第二题(***)某班有 5 个学生,三门课。分别编写 3 个函数实现以下要求:
(1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
(3) 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名*/
// (1) 求各门课的平均分;
//定义五个学生成绩
typedef struct score{
float chinese;
float math;
float english;
}Score;
typedef struct student{
int num;
char name[20];
Score stuScore;
}Student;
typedef struct avergeM{
float avergeC;
float avergeM;
float avergeE;
}AvergeM;
Score score1 = { 96, 98, 95};
Score score2 = { 49, 89, 32};
Score score3 = { 106, 48, 95};
Score score4 = { 85, 85, 85};
Score score5 = { 96, 98, 95};
Student stu1 = {1, "德莱厄斯", score1};
Student stu2 = {2, "杰斯", score2};
Student stu3 = {3, "内瑟斯",score3 };
Student stu4 = {4, "雷克顿", score4};
Student stu5 = {5, "瑞文", score5};
Student stu[] ={stu1, stu2, stu3, stu4, stu5};
float chinese = average(stu).avergeC;
float math = average(stu).avergeM;
float english = average(stu).avergeE;
printf("\n第二题(1):\n");
printf("语文的平均分为:%.2f\n数学的平均分为:%.2f\n英语的平均分为:%.2f\n",chinese,math,english);
// 地址不同
// printf("%p\n",&score1.chinese);
// printf("%p\n",&stu1.stuScore.chinese);
{
AvergeM average(Student stu[]){
int sum3 = 0,sum1 = 0,sum2 = 0;
AvergeM a ;
for (int i = 0; i < 5; i++) {
sum1 += stu[i].stuScore.chinese;
sum2 += stu[i].stuScore.math;
sum3 += stu[i].stuScore.english;
}
a.avergeC = sum1/5;
a.avergeM = sum2/5;
a.avergeE = sum3/5;
return a;
}
}
// (2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
void findStu(Student stu[]){
printf("\n第二题:\n");int flagB = 0;
for (int i = 0; i < 5; i++) {
if(stu[i].stuScore.chinese < 60 && stu[i].stuScore.math < 60 && stu[i].stuScore.english < 60)
{ printf("%d号学生两门以上不及格.\n",stu[i].num);
printf("语文不及格,成绩为:%d\n",stu[i].stuScore.chinese);
printf("数学不及格,成绩为:%d\n",stu[i].stuScore.math);
printf("英语不及格,成绩为:%d\n\n",stu[i].stuScore.chinese);
flagB ++;
}
}
if (flagB == 0) {
printf("没有两门以上不及格的学生\n");
}
}
//(3) 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名
{
void outstanding(Student stu[]){
float averge;int flagB = 0;
printf("\n第二题(3):\n");
for (int i = 0; i < 5; i++) {
averge = (stu[i].stuScore.english + stu[i].stuScore.chinese + stu[i].stuScore.math)/3;
if ( averge >= 85 && averge <= 90) {
printf("学号为%d的%s同学三门课平均成绩在 85-90 分\n",stu[i].num,stu[i].name);
flagB ++;
}
}
if (flagB == 0) {
printf("没有三门课平均成绩在 85-90 分的学生\n\n");
}
else{
printf("\n");
}
}
}
/* 第三题(***)模拟n个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用 A、B、C、D 表示,当选某候选人时直接输入其编号(编号由计算机随机产生),若输入的不是 A、B、C、D 则视为无效票,选举结束后按得票数从高到低输出候选人编号和所得票数*/
typedef struct candidate {
char name;
int ticket;
}Candidate;
Candidate per1 = {'A',0};
Candidate per2 = {'B',0};
Candidate per3 = {'C',0};
Candidate per4 = {'D',0};
Candidate person[] = {per1,per2,per3,per4};
result(person);
{//子函数
void result(Candidate person[]){
int code;
printf("第三题:\n");
for (int i = 0; i < 100; i++) {
code = arc4random() % (10 - 1 + 1) + 1;
switch (code) {
case 1:
person[0].ticket++;
break;
case 2:
person[1].ticket++;
break;
case 3:
person[2].ticket++;
break;
case 4:
person[3].ticket++;
break;
default:
break;
}
}
for (int i = 0; i < 4 - 1; i++) {
for (int j = 0; j < 4 - 1 - i; j++) {
if (person[j].ticket < person[j].ticket) {
int temp = person[j].ticket;
person[j].ticket = person[j + 1].ticket;
person[j + 1].ticket = temp;
char tempN = person[j].name;
person[j].name = person[j + 1].name;
person[j + 1].name = tempN;
}
}
}
for (int k = 0; k < 4; k++) {
printf("%c的票数为:%d\n",person[k].name,person[k].ticket);
}
}
}
// *第四题(***)创建一个 Point 结构体,包含 x,y 两个变量。 并写以下函数:
// 函数 1,判断两个点是否在一条水平线上。
// 函数 2,判断两个点是否在一条垂直线上。
// 函数 3,判断两个点是否相等。
{
BOOL horizontal(PointA po1,PointA po2){
if (po1.x == po2.x) {
return YES;
}
return NO;
}
BOOL vertical(PointA po1,PointA po2){
if (po1.y == po2.y) {
return YES;
}
return NO;
}
BOOL equal(PointA po1, PointA po2){
if (po1.x == po2.x && po1.y == po2.y) {
return YES;
}
return NO;
}
}
typedef struct pointA{
float x;
float y;
}PointA;
PointA po1 = {12, 56};
PointA po2 = {12, 56};
printf("\n第五题:\n");
printf("两个点 \"%s\" 在一条水平线上\n",horizontal( po1, po2) == 1 ? "是":"不");
printf("两个点 \"%s\" 在一条垂直线上\n",vertical( po1, po2) == 1 ? "是":"不");
printf("两个点 \"%s\" 相等\n",equal( po1, po2) == 1 ? "是":"不");
C - 结构体 Homework
最新推荐文章于 2018-12-14 18:45:19 发布