(eden)Student Manager

Student Manager

 

Description:

题目内容:

已知main.c文件代码,请完成StudentManager.h文件中的结构体的定义(已给出),以及相关5个函数的实现。要求,最终能够按照一定的顺序输出每个学生的学号(小于等于8位的正整数)及分数(介于1与100之间的整数)(其中学生的人数n, 1 <= n  <= 20),其中按照分数从高到低输出,如果分数相同,则根据学号的大小(假定学号可以重复),学号越大的排在学号小的前面输出。每个记录输出后都有一个换行。

 

两个结构体的定义已给出。

// strcuture's definition here...

// id用于保存学号,score用于保存分数。

typedef struct struct_one {

    int id;

    int score;

} Student;

 

// num用于记录student的个数,data用于保存student这个结构体信息.

typedef struct struct_two {

    int num;

    Student * data;

} StudentManager;

 

请完成下面5个函数的定义:

// init the student manager.

void initStudentManager(StudentManager* studentManager);

这个函数用于初始化内存,为data开辟空间。

 

// deal with the input.

void dealWithInput(StudentManager* studentManager);

这个函数用于对输入的处理,将数据保存到结构体中。mian.c中已给出,不用写

 

// sort the student in some order given.

void sortStudent(StudentManager* studentManager);

这个函数对student进行排序。

 

// output the result after processing.

void dealWithOutput(StudentManager* studentManager);

这个函数用于输出结果。

 

// recycle the memory.

void recyleStudentManager(StudentManager* studentManager);

这个函数用于回收我们开辟的空间。

 

输入样例:

5

13331111 98

13331112 34

13331113 21

13331114 21

13331115 21

输出样例:

13331111: 98

13331112: 34

13331115: 21

13331114: 21

13331113: 21

 

Hint:

结构体+指针+排序

 

my code

 1 //StudentManager.h
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 // strcuture's definition here...
 5 // id用于保存学号,score用于保存分数。
 6 typedef struct struct_one {
 7     int id;
 8     int score;
 9 } Student;
10 // num用于记录student的个数,data用于保存student这个结构体信息.
11 typedef struct struct_two {
12     int num;
13     Student * data;
14 } StudentManager;
15 // init the student manager.
16 void initStudentManager(StudentManager* studentManager) {
17     Student i;
18     Student* j = malloc(sizeof(i) * studentManager->num);
19     studentManager -> data = j;
20 }
21 // 这个函数用于初始化内存,为data开辟空间。
22 // deal with the input.
23 void dealWithInput(StudentManager* studentManager);
24 // 这个函数用于对输入的处理,将数据保存到结构体中。在mian.c中已给出,不用写。
25 // sort the student in some order given.
26 void sortStudent(StudentManager* studentManager) {
27     int i, j, t;
28     for (i = 0; i <= studentManager -> num - 2; i++) {
29         for (j = i + 1; j <= studentManager -> num - 1; j++) {
30             if (studentManager->data[i].score < studentManager->data[j].score) {
31                 t = studentManager->data[i].score;
32                 studentManager->data[i].score = studentManager->data[j].score;
33                 studentManager->data[j].score = t;
34                 t = studentManager->data[i].id;
35                 studentManager->data[i].id = studentManager->data[j].id;
36                 studentManager->data[j].id = t;
37             }
38             if (studentManager->data[i].score ==
39             studentManager->data[j].score) {
40                 if (studentManager->data[i].id < studentManager->data[j].id) {
41                     t = studentManager->data[i].id;
42                     studentManager->data[i].id = studentManager->data[j].id;
43                     studentManager->data[j].id = t;
44                 }
45             }
46         }
47     }
48 }
49 // 这个函数对student进行排序。
50 // output the result after processing.
51 void dealWithOutput(StudentManager* studentManager) {
52     int i;
53     for (i = 0; i < studentManager->num; i++) {
54         printf("%d: %d\n", studentManager->data[i].id,
55         studentManager->data[i].score);
56     }
57 }
58 // 这个函数用于输出结果。
59 // recycle the memory.
60 void recyleStudentManager(StudentManager* studentManager) {
61     free(studentManager->data);
62 }

 

//main.c
1.// from younglee
2.#include "StudentManager.h"
3. 
4.// deal with the input.
5.void dealWithInput(StudentManager* studentManager) {
6.    int i;
7.    for (i = 0; i < studentManager->num; i++) {
8.        scanf("%d%d", &(studentManager->data[i].id), \
9.        &(studentManager->data[i].score));
10.    }
11.}
12. 
13.int main(void) {
14.    // get a studentManager object.
15.    StudentManager studentManager;
16.    // set the student's num.
17.    scanf("%d", &studentManager.num);
18.    // initialize the memory for storing the students' information.
19.    initStudentManager(&studentManager);
20.    // get input.
21.    dealWithInput(&studentManager);
22.    // sort the student in specific order.
23.    sortStudent(&studentManager);
24.    // output the resut after sorting.
25.    dealWithOutput(&studentManager);
26.    // recycle the memory we used before.
27.    recyleStudentManager(&studentManager);
28.    return 0;
29.}

标程

1.#include<stdio.h>
2.#include<stdlib.h>
3. 
4.// strcuture's definition here...
5.typedef struct struct_one {
6.    int id;
7.    int score;
8.} Student;
9. 
10.typedef struct struct_two {
11.    int num;
12.    Student * data;
13.} StudentManager;
14. 
15.// compare function defined here, which was used latter in quick sort.
16./*int cmp(const void *a, const void * b) {
17.    if (((Student *)a)->score < ((Student *)b)->score) return 1;
18.    if (((Student *)a)->score > ((Student *)b)->score) return -1;
19.    return (((Student *)a)->id < ((Student *)b)->id ? 1 : -1);
20.}*/
21. 
22.// init the student manager.
23.void initStudentManager(StudentManager* studentManager) {
24.    studentManager->data = malloc((studentManager->num)*sizeof(Student));
25.}
26. 
27.void swap(Student *a, Student *b) {
28.    Student temp = *a;
29.    *a = *b;
30.    *b = temp;
31.}
32. 
33.// sort the student in some order given.
34.void sortStudent(StudentManager* studentManager) {
35.    // qsort(studentManager->data, studentManager->num, sizeof(Student), cmp);
36.    // the second method is to use buble sort.
37.    int num = studentManager->num, i, j;
38.    for (i = 0; i < num; i++) {
39.        for (j = num - 1; j > i; j--) {
40.            Student * data = studentManager->data;
41.            if (data[j - 1].score < data[j].score) {
42.                swap(&data[j - 1], &data[j]);
43.            }
44.            if (data[j - 1].score == data[j].score) {
45.                if (data[j - 1].id < data[j].id) {
46.                    swap(&data[j - 1], &data[j]);
47.                }
48.            }
49.        }
50.    }
51.}
52. 
53.// output the result after processing.
54.void dealWithOutput(StudentManager* studentManager) {
55.    int i;
56.    for (i = 0; i < studentManager->num; i++) {
57.        printf("%d: %d\n", studentManager->data[i].id, \
58.        studentManager->data[i].score);
59.    }
60.}
61. 
62.// recycle the memory.
63.void recyleStudentManager(StudentManager* studentManager) {
64.    free(studentManager->data);
65.}
66. 

 

posted @ 2015-12-30 10:33  小预备  阅读(385)  评论(0编辑  收藏  举报