/* 第一題(**)定義一個結構體變量(包括年、月、日),計算該日在本年中爲第幾天?(注意考慮閏年問題),要求寫一個函數 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.