基础c-结构体-1
一、概要描述
1、背景
输入人数,和每个人的姓名和四科成绩,然后进行排序,如果成绩相同,则按姓名的首字母在字典的顺序排;
2、实现
用scanf来进行输入,并且用结构体来进行接收,在排序时用qsort函数
二、困难
scanf同时输入int,string类型时为什么会爆乱码?
(如果用%0来结束scanf接收string的话该这么弄?而且,还要同时输入int的)
//test data
//5
//Alice 99 98 97 96
//Bob 98 97 96 94
//Coy 94 94 95 96
//Dan 93 95 96 97
//Evan 0 94 95 95
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000
typedef struct Stu {
char name[N];
int chin;
int math;
int eng;
int sci;
int total;
}stu;
int cmp1(const void* a, const void* b) {
stu c = *(stu*)a;
stu d = *(stu*)b;
return d.chin - c.chin;
}
int cmp2(const void* a, const void* b) {
stu c = *(stu*)a;
stu d = *(stu*)b;
return d.sci - c.sci;
}
int cmp3(const void* a, const void* b) {
stu c = *(stu*)a;
stu d = *(stu*)b;
return d.eng - c.eng;
}
int cmp4(const void* a, const void* b) {
stu c = *(stu*)a;
stu d = *(stu*)b;
return d.total - c.total;
}
int cmp5(const void* a, const void* b) {
stu c = *(stu*)a;
stu d = *(stu*)b;
return d.math - c.math;
}
int main() {
int n;
stu sz[100];
scanf_s("%d", &n);
for (int i = 0; i <=n; i++)
{
scanf_s("%s %d %d %d %d", &sz[i].name,5, &sz[i].chin, &sz[i].math, &sz[i].eng, &sz[i].sci);
}
for (int i = 0; i <=n; i++)
{
sz[i].total = sz[i].chin + sz[i].math + sz[i].eng + sz[i].sci;
}
printf("n");
printf("%dn", n);
for (int i = 0; i <= n; i++)
{
printf("%s,%d,%d,%d,%d,%dn", sz[i].name, sz[i].chin, sz[i].math, sz[i].eng, sz[i].sci, sz[i].total);
}
printf("n");
//chin
qsort(sz, n, sizeof(sz[0]), cmp1);
for (int i = 0; i < n ;i++) {
printf("%s ", sz[i].name);
}
printf("b");
printf("n");
//math
qsort(sz, n, sizeof(sz[0]), cmp5);
for (int i = 0; i < n; i++) {
printf("%s ", sz[i].name);
}
printf("b");
printf("n");
//eng
qsort(sz, n, sizeof(sz[0]), cmp3);
for (int i = 0; i < n; i++) {
printf("%s ", sz[i].name);
}
printf("b");
printf("n");
//sci
qsort(sz, n, sizeof(sz[0]), cmp2);
for (int i = 0; i < n ; i++) {
printf("%s ", sz[i].name);
}
printf("b");
printf("n");
//total
qsort(sz, n, sizeof(sz[0]), cmp4);
for (int i = 0; i < n; i++) {
printf("%s ", sz[i].name);
}
printf("b");
return 0;
}

大佬帮我看一下哈…
感谢!!!
最后
以上就是热心裙子最近收集整理的关于基础c-结构体-1的全部内容,更多相关基础c-结构体-1内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复