主函数部分:
1. (**)定义一个结构体变量(包括年、月、日),计算该日在本年中为第
几天?(注意考虑闰年问题),要求写一个函数 days,实现上面的计算 由主函数将年月日传递给 days 函数,计算后将日子传递回主函数输出
printf("请输入日期:\n");
Date date;
scanf("%d %d %d", &date.year, &date.month, &date.day);
int daysum = days(date.year, date.month, date.day);
printf("%d", daysum);
2. (***)某班有 5 个学生,三门课。分别编写 3 个函数实现以下要求: (1) 求各门课的平均分;
(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩; (3) 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名
(1):
Student student[5] = {{"dantouer", 98, 97, 89},
{"yangting2", 59, 58, 61},
{"wo", 100, 100, 100},
{"kuaipao", 10, 15, 99},
{"xiaomeng", 0, 14, 98}};
randResult(student, 5);
3. (***)模拟n个人参加选举的过程,并输出选举结果:假设候选人有四
人,分别用 A、B、C、D 表示,当选某候选人时直接输入其编号(编号由计算机
随机产生),若输入的不是 A、B、C、D 则视为无效票,选举结束后按得票数从 高到低输出候选人编号和所得票数
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
Election election[5] = {{"A", count1}, {"B", count2}, {"C", count3}, {"D", count4}, {"E", count5}};
4.(***)创建一个 Point 结构体,包含 x,y 两个变量。 并写以下函数:
函数 1,判断两个点是否在一条水平线上
函数 2,判断两个点是否在一条垂直线上 函数 3,判断两个点是否相等
函数1
Decide a = {3, 5};
Decide b = {4, 6};
pointDecide(a, b);
函数2省略
函数3省略
5.(***)创建一个 Size 结构体,包含 width,height 两个变量。 并写以下函数:
函数 1,判断两个 size 是否等宽
函数 2,判断两个 size 是否在等高 函数 3,判断两个 size 是否相等
函数1
mySize a = {4, 8};
mySize b = {5, 8};
ISSame(a, b);
函数2省略
函数2省略
6.(***)创建一个 Rect 结构体,包含 origin,size 两个变量其中 origin 是
Point 类型的,size 是 Size 类型的 并写以下函数:
函数 1,判断两个 Rect 是否相交
函数 2,判断两个 Rect 是否包含某个 Point。 函数 3,判断一个 Rect 是否在另外一个 Rect 中
rects rects1 = {{3, 4}, {6, 8}};
rects rects2 = {{5, 8}, {7, 6}};
banana(rects1, rects2);
声明部分:
#import <Foundation/Foundation.h>
第一题
定义结构体
struct yearMonthDay{
int year;
int month;
int day;
};
typedef struct yearMonthDay Date;
int days(int year, int month, int day);
第二题
(1)
struct student{
char studentName[10];
int result1;
int result2;
int result3;
};
typedef struct student Student;
void randResult(Student courseResult[], int count);
第三题
struct election{
char name[20];
int num;
};
typedef struct election Election;
第四题
struct point{
int x;
int y;
};
typedef struct point Decide;
void pointDecide(Decide a, Decide b);
第五题
struct size{
int width;
int height;
};
typedef struct size mySize;
void ISSame(mySize a, mySize b);
第六题
struct Rects{
Decide decide;
mySize MYsize;
};
typedef struct Rects rects;
void banana(rects a, rects b);
#import "MYFuntion.h"
第一题
int days(int year, int month, int day)
{
int sum = 0;
int monthNum[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ){
monthNum[1] = 29;
}
for (int i = 0; i < month - 1; i++) {
sum += monthNum[i];
}
return sum + day;
}
第二题
(1)
void randResult(Student courseResult[], int count)
{
float avg1 = 0;
float avg2 = 0;
float avg3 = 0;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
for (int i = 0; i < count; i++) {
sum1 += courseResult[i].result1;
}
for (int i = 0; i < count; i++) {
sum2 += courseResult[i].result2;
}
for (int i = 0; i < count; i++) {
sum3 += courseResult[i].result3;
}
avg1 = sum1 / 5;
avg2 = sum2 / 5;
avg3 = sum3 / 5;
printf("%.2f %.2f %.2f", avg1, avg2, avg3);
}
第三题
第四题
void pointDecide(Decide a, Decide b)
{
if (a.y == b.y) {
printf("两个点在同一水平线上\n");
}else{
printf("两个点不在同一条水平线上\n");
}
}
第五题
void ISSame(mySize a, mySize b)
{
if (a.width == b.width) {
printf("两个等宽\n");
}else{
printf("两个不等宽\n");
}
}
第六题
void banana(rects a, rects b)
{
if ((b.decide.x - a.decide.x) > 0) {
if ((b.decide.y - a.decide.y) > 0) {
if ((b.decide.y - a.decide.y) < b.MYsize.height && (b.decide.x - a.decide.x) < a.MYsize.width) {
printf("香蕉");
}else{
printf("梨");
}
}else{
if ((a.decide.y - b.decide.y) < a.MYsize.height && (b.decide.x - a.decide.x) < a.MYsize.width) {
printf("香蕉");
}else{
printf("梨");
}
}
}else{
if ((b.decide.y - a.decide.y) > 0) {
if ((a.decide.x - b.decide.x) < b.MYsize.width && (b.decide.y - a.decide.y) < a.MYsize.height) {
printf("香蕉");
}else{
printf("梨");
}
}else{
if ((a.decide.y - b.decide.y) < a.MYsize.height && (a.decide.x - b.decide.x) < b.MYsize.width) {
printf("香蕉");
}else{
printf("梨");
}
}
}
}