一個班級五個同學,請定義方法錄入他們的姓名及其成績,定義一個方法回傳成績最高分的同學的序號,根據用戶輸入的序號查詢成績。若為最高分就輸出“拔得頭籌”,否則顯示成績
uj5u.com熱心網友回復:
#include <stdio.h>
struct student
{
char name[10];
int id;
float score;
};
void input_stu(struct student stu[], int n)
{
int i;
for(i=0; i<n; i++)
{
scanf("%d%s%f", &stu[i].id, &stu[i].name, &stu[i].score);
}
}
int max_score(struct student stu[], int n)
{
int i, j, temp = 0;
for(i=0; i<n; i++)
{
if (temp < stu[i].score)
{
temp = stu[i].score;
j = i;
}
}
return stu[j].score;
}
void find_stu(struct student stu[], int n, int m)
{
int i, j=0;
for (i=0; i<n; i++)
{
if (m == stu[i].id)
{
j = max_score(stu, n);
if (j == m)
{
printf("拔得頭籌");
}
else
{
printf("分數:%.2f", stu[i].score);
}
}
}
}
int main()
{
int m;
struct student stu[5] = {0};
input_stu(stu, 5);
printf("輸入要查詢的學號:\n");
scanf("%d", &m);
find_stu(stu, 5, m);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/117334.html
標籤:新手樂園
上一篇:????小白求救
下一篇:求教解答一個關于編碼問題,重謝
