自定义类型:结构
学生成绩排序。学生信息包括:学号,姓名,性别,年龄,成绩
1 2 3 4 5 6 7 8 | 100107001 Apple F 20 72 100107002 Darwin M 19 68 100107003 Eric M 19 79 100107004 Peter M 21 46 100107006 Richie M 70 100 100107007 Kern M 69 100 100107008 Jane F 20 74 100107009 Ann F 19 69 |
Brian Kernighan January 1942 (age 69)
Dennis Ritchie September 9, 1941 (age 70)
K said if stranded on an island with only one programming language it would have to be C.
Ritchie is best known as the creator of the C programming language and a key developer of the Unix operating system, and as co-author of the definitive book on C, The C Programming Language, commonly referred to as K&R (in reference to the authors Kernighan and Ritchie).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> typedef struct student { char sno[12]; char name[40]; char sex; int age; int score; } Student; int main(int argc, char *argv[]) { Student list[1000+10]; int i=0; scanf("%s%s%1s%d%d", list[i].sno, list[i].name, &list[i].sex, &list[i].age, &list[i].score); printf("%s %s %c %d %d\n", list[i].sno, list[i].name, list[i].sex, list[i].age, list[i].score); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> typedef struct student { char sno[12]; char name[40]; char sex; int age; int score; } Student; int main(int argc, char *argv[]) { Student list[1000+10]; int i=0; freopen("student.txt", "r", stdin); while(scanf("%s%s%1s%d%d", list[i].sno, list[i].name, &list[i].sex, &list[i].age, &list[i].score)!=EOF) { printf("%s %s %c %d %d\n", list[i].sno, list[i].name, list[i].sex, list[i].age, list[i].score); i++; } printf("%d", i); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <stdio.h> typedef struct student { char sno[12]; char name[40]; char sex; int age; int score; } Student; void bubble_sort(Student a[ ], int n) { int i, j; Student t; for(i=0; i<n-1; i++) for (j=0; j<n-1-i; j++) if (a[j].score<a[j+1].score) { t = a[j]; a[j] = a[j+1]; a[j+1]=t; } } int main(int argc, char *argv[]) { Student list[1000+10]; int i=0, n; freopen("student.txt", "r", stdin); while(scanf("%s%s%1s%d%d", list[i].sno, list[i].name, &list[i].sex, &list[i].age, &list[i].score)!=EOF) { i++; } n = i; bubble_sort(list, n); for (i=0; i<n; i++) printf("%10s %10s %c %4d %4d\n", list[i].sno, list[i].name, list[i].sex, list[i].age, list[i].score); return 0; } |