(2)撰寫程式:定義一個結構型別, 可以存盤一個學生的平時、實習、測驗、期末成績和總評成績。然后要求用結構體陣列完成以下任務:
a. 編程完成輸入4個學生的平時、實習、測驗和期末成績。封裝成函式。
b. 計算這4個學生的總評成績,封裝成函式。其中平時、實習、測驗和期末分別占10%、20%、20%、50%。
c. 找到總評成績最高的學生,封裝成函式。
d. 輸出總評成績最高的學生的成績資訊(平時、實習、測驗、期末成績和總評成績)。
(3)撰寫程式:修改(2)的程式,使得程式可以支持輸入n個學生的成績。n可以由用戶輸入決定。比如先提示用戶輸入學生人數,然后再輸入成績。
提示:需要定義一個結構體指標,然后用malloc動態分配空間,free回收空間。
(4)撰寫程式:定義一個結構型別, 可以存盤一門課程的名稱、主講教師姓名、選課學生人數和學分數。編程完成輸入4門課程的資訊,計算并輸出最多學分的那門(不一定只有一門)課程的資訊。要求用結構陣列并參考任務(2)封裝函式。
uj5u.com熱心網友回復:
第4問自己參照著寫吧
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
char name[20]; //姓名
float ps; //平時成績
float sx; //實習成績
float cy; //測驗成績
float qm; //期末成績
float zp; //總評成績
}stu;
//輸入成績
void Input(stu *ArrStu, int n)
{
int i;
for (i=0; i<n; i++)
{
printf("輸入第%d個學生的姓名 平時 實習 測驗 期末成績: \n", i+1);
scanf("%s%f%f%f%f", ArrStu[i].name, &ArrStu[i].ps, &ArrStu[i].sx, &ArrStu[i].cy,
&ArrStu[i].qm);
ArrStu[i].zp = (ArrStu[i].ps * 0.1 + ArrStu[i].sx * 0.2 + ArrStu[i].cy * 0.2 +
ArrStu[i].qm * 0.5);
}
}
void MaxZP(stu *StuArr, int n)
{
if (StuArr == NULL)
{
printf("Error!");
exit(1);
}
int i, index;
float max = StuArr[0].zp;
for (i=0; i<n; i++)
{
if (max < StuArr[i].zp)
{
max = StuArr[i].zp;
index = i;
}
}
printf("%d\n", index);
printf("姓名:%s\n平時成績:%.2f\n實習成績:%.2f\n測驗成績:%.2f\n期末成績:%.2f\n總評成績:%.2f\n",
StuArr[index].name, StuArr[index].ps, StuArr[index].sx, StuArr[index].cy, StuArr[index].qm,
StuArr[index].zp);
}
int main()
{
stu *StuArr = NULL;
int n = 0, index = -1;
printf("輸入學生人數:\n");
scanf("%d", &n);
StuArr = (stu *)malloc(sizeof(stu) * n);
Input(StuArr, n);
MaxZP(StuArr, n);
free(StuArr);
StuArr = NULL;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/37762.html
標籤:C語言
