C语言习题,刚学不久遇到这种练习,对函数掌握不够,求各位带带
先定义一个结构体类型student,包含成员学号num,姓名name,成绩score。在排序部分可使用冒泡排序,最大值部分可用“擂台法”找到最大值,也可用排序后的结果来找最大值,本代码采用第一种方法。
#include<stdio.h>
struct student{
int num;
char name[20];
float score;
};
void input(int n,struct student stu[])
{
int i;
for(i=0;i<n;i++)
{
printf("请输入第%d个同学的学号,姓名,成绩\n",i+1);
scanf("%d %s %f",&stu[i].num,stu[i].name,&stu[i].score);
}
}
void sort(int n,struct student stu[])
{
struct student t;
int i,j;
for(i=0;i<n-1;i++)//n个数的数列总共扫描n-1次
{
for(j=0;j<n-i-1;j++)//每一趟扫描到stu[n-i-2]与stu[n-i-1]比较为止结束
{
if(stu[j].score<stu[j+1].score)//后一位数比前一位数大的话,就交换两个数的位置(升序)
{
t=stu[j+1];
stu[j+1]=stu[j];
stu[j]=t;
}
}
}
printf("成绩由高到低为:");
for(i=0;i<n;i++)
printf("%.2f ",stu[i].score);
printf("\n");
}
void max(int n,struct student stu[])
{
struct student max1;
max1=stu[0];
int i;
for(i=0;i<n;i++)
{
if(stu[i].score>max1.score)
max1=stu[i];
}
printf("最高分学生信息: ");
printf(" 学号:%d 姓名:%s 成绩:%f.2",max1.num,max1.name,max1.score);
}
int main()
{
struct student stu[5];
input(5,stu);
sort(5,stu);
max(5,stu);
return 0;
}
还要手写啊你们,真惨,大概是这样,有问题@我
#include<stdio.h>
#define N 5
typedef struct
{
char ID[32];
char Name[32];
float Score;
}Info;
Info stInfo[N] = {0};
void input()
{
int i;
printf("Input Five Students Info:\n");
for(i = 0; i < N; i++)
{
printf("Input Number[%d] Student ID:\n", i + 1);
scanf("%s", stInfo[i].ID);
printf("Input Number[%d] Student Name:\n", i + 1);
scanf("%s", stInfo[i].Name);
printf("Input Number[%d] Student Score:\n", i + 1);
scanf("%f", &stInfo[i].Score);
}
}
void sort()
{
Info ptf[N], temp = {0};
int i, j;
for(i = 0; i < N; i++)
{
ptf[i] = stInfo[i];
}
for(i = 0; i < N - 1; i++)
{
for(j = i; j < N; j++)
{
if(ptf[i].Score < ptf[j].Score)
{
temp = ptf[i];
ptf[i] = ptf[j];
ptf[j] = temp;
}
}
}
printf("Students Score From H To L:\n");
for(i = 0; i < N; i++)
{
printf("%f ",ptf[i].Score);
}
printf("\n");
}
void max()
{
float maxscore = 0;
int i, stuIndex = 0;
maxscore = stInfo[0].Score;
for(i = 1; i < N; i++)
{
if(stInfo[i].Score > maxscore)
{
stuIndex = i;
maxscore = stInfo[i].Score;
}
}
printf("Highest Score Student Info:\n");
printf("ID:%s\n",stInfo[stuIndex].ID);
printf("Name:%s\n",stInfo[stuIndex].Name);
printf("Score:%f\n",stInfo[stuIndex].Score);
}
int main()
{
input();
sort();
max();
return 0;
}
#include<stdio.h>
typedef struct {
int id;
char name[20];
float score;
}Student;
void input(Student stu[], int n)
{
int i;
printf("请输入%d个学生的学号,姓名,成绩:\n", n);
for(i=0; i<n; i++)
{
scanf("%d %s %f", &stu[i].id, stu[i].name, &stu[i].score);
}
}
void sort(Student stu[], int n)
{
Student temp;
int i,j;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(stu[j].score < stu[j+1].score)
{
temp = stu[j+1];
stu[j+1] = stu[j];
stu[j] = temp;
}
}
}
printf("成绩由高到底排序:\n");
for(i=0; i<n; i++)
{
printf("%d\t%s\t%f\n", stu[i].id, stu[i].name, stu[i].score);
}
}
void max(Student stu[], int n)
{
float max = 0;
int i, m;
for(i=0; i<n; i++)
{
if(stu[i].score > max)
{
max = stu[i].score;
m = i;
}
}
printf("最高分学生信息:\n");
printf("%d\t%s\t%f\n", stu[m].id, stu[m].name, stu[m].score);
}
int main()
{
Student stu[5];
input(stu, 5);
sort(stu, 5);
max(stu, 5);
return 0;
}