用结构体输入输出一个学生的信息
定义学生数据类型 struct Student ,输入输出学生的信息
踏月光
浏览 2604回答 1
1回答
-
张奇_1
#include<stdio.h>
#include<malloc.h>
int i;
struct Student
{
char name[100];
//char mingzi[100];
int age;
//int nianling;
float score;
//char fenshu;
};
void shuru(struct Student * p1, int a)
{
for (i = 0; i < a; i++)
{
printf("请输入第 %d 位学生的信息。\n", i+1);
printf("name = ");
scanf("%s", p1[i].name);
printf("age = ");
scanf("%d", &p1[i].age);
printf("score = ");
scanf("%f", &p1[i].score);
}
}
void paixu(struct Student * p2, int n)
{
struct Student t;
for (i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (p2[j] .score < p2[j + 1].score)
// 大于是升序 小于是降序
{
t = p2[j];
p2[j] = p2[j + 1];
p2[j + 1] = t;
}
}
}
}
void shuchu(struct Student * p3, int b)
{
for (i = 0; i < b; i++)
{
printf("第 %d 名学生的信息为: \n", i+1);
printf(" 姓名: = %s\n", p3[i].name);
printf(" 年龄: = %d\n", p3[i].age);
printf(" 分数: = %.2f\n", p3[i].score);
}
}
int main(void)
{
int n;
struct Student * p;
printf("请输入学生的个数: n = ");
scanf("%d", &n);
printf("\n");
p = (struct Student *)malloc(n * sizeof(struct Student));
shuru(p, n);
paixu(p, n);
printf("\n\n");
shuchu(p, n);
return 0;
}
打开App,查看更多内容