C-按字母顺序和搜索名称对名称列表进行排序

时间:2019-05-25 08:22:40

标签: c

有一个学生姓名列表(用户输入),程序中有2种方法: 1.按姓名(按字母顺序)对学生进行排序。 2.搜索并打印学生-应该搜索它是否出现在学生列表中。必须使用二进制搜索。方法找到学生后,它将打印列表中学生的姓名。

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40

typedef struct {
    char name[LEN];
    int grade;
} Student;

/* Functions prototypes */

Student **createArrayPointersToStudents(unsigned n);
void getStudentsFromUser(Student* p[], unsigned n);
char  **getExcellentStudentsFromUser(unsigned nex);
char **createArrayPointersToNames(Student **name , int nex);
char** binaryList( char **name, char **mark, int h);
void sortNames( Student **name, int nex);
void printArrayStudents(Student **A, unsigned n);
void printExArrayStudents(char **eStudents, unsigned nex);
void printNewStudents(Student **str,char* p[],unsigned n);
void freeAlloc(void *p);
void removeNewLine(char *s);
void freeAllocations(Student **ptrStudents, int n);
void clean();
static int read_user_selection();
//void mostExcellentStudent(Student **p, int *n, Student **toFire);

unsigned nex;

    int main() {
        int numStudent;
        printf("Please enter how many students do you have in the software department: ");
        scanf("%d", &numStudent);
        printf("\nPlease enter how many excellent students do you have from the whole college: ");
        scanf("%d", &nex);

        Student **ptrStudents =(Student **) createArrayPointersToStudents(numStudent);
        char **names = (char **) createArrayPointersToNames(ptrStudents , numStudent);
        char **exStudents =(char **) getExcellentStudentsFromUser(nex);

// **我尝试打印学生的姓名和年级,但仅出现姓名。 **                 printArrayStudents(ptrStudents,numStudent);

            char selection;
        int choice;

                while(1)
                {
            // Displays all options
            printf("1- Sort the students from software dep. by name: \n");
            printf("2 -Search and print excellent students from software dep:\n");
            printf("5 -  Create and print a new array consists of excellent students from software \n");
            printf("E/e - Exit this program\n");
            printf("\n==================\n\n\n");

                    //** When I choose case 1 (sort) the program ends (exit -1).

当我选择案例2(搜索)时,没有出现。**

                    switch (selection = read_user_selection())
                    {
            printf("1 - Sort all the students from by name: \n");
            printf("2 -Search and print excellent students\n");

                    switch (selection = read_user_selection())
                    {
                        case '1':
                            sortNames(ptrStudents , numStudent);
                            printArrayStudents(ptrStudents, numStudent);//sortNames(names, nex);
                            break;

                        case'2':
                            printExArrayStudents(binaryList(names, exStudents, numStudent), nex);
                            break; 

                        case 'e':
                        case 'E':
                            printf("This program will end now. goodbye.");
                            exit(0);
                            break;

                        default:
                            printf("\nWrong input number. try again:");
                            break;
                    }   
                }

        return 0;
    }

    static int read_user_selection()
    {
        int selection = getchar();
        if (getchar() == '\n')
            return selection;
    }
    void printNewStudents(Student **str,char* p[],unsigned n)
    {
        int l = 0, i = 0;

        while (l < n) {
            int mid = (l + n) / 2;
            //char str1[LEN] = (char)str[i]->name;
            if (strcmp(p[mid], str[i]->name) == 0) {
                printf("\n%s\t%d",str[i]->name , str[i]->grade);
                i++; l = 0;

            }
            if (strcmp(p[mid], str[i]->name) < 0) {
                l = mid - 1;
            } else {
                l = mid + 1;
            }
        }
    }


    Student **createArrayPointersToStudents(unsigned n) {
       // printf("createArrayPointersToStudents start");
    char tmp[LEN];
    int grade , i;
    Student **s = (Student**) malloc(n * sizeof(Student*));
    if (s == NULL) {
        printf("\nNot enough memory...");
        exit(1);
    }
    for ( i = 0; i < n; i++) {
        printf("\n Please insert name for student #%d:", i);
        clean();
        fgets(tmp, LEN, stdin);
        removeNewLine(tmp);
        printf("\n Please insert grade for student #%d:", i);
        scanf("%d", &grade);
        s[i] = (Student*) malloc(1 * sizeof(Student));
        if (s[i] == NULL) {
            printf("\nNot enough memory...");
            exit(2);
        }
        strcpy(s[i]->name, tmp);
        s[i]->grade = grade;
    }
     //printf("createArrayPointersToStudents start");
    return s;
    }

    void getStudentsFromUser(Student* p[], unsigned n) {
    // const Student **tmp[n];
    int i ;
    Student **p1 = (Student**) malloc(n * sizeof(Student*));
    for (i = 0; i < n; i++, p1++)
        p1[i] = p[i]; //*p
    for (i = 0; i < n; i++) {
        if (!(p1[i]->grade >= 0 && p1[i]->grade <= 100)) {
            printf("Enter grade of student name = %s", p1[i]->name);
            scanf("%d", &p1[i]->grade);
        }
    }
    }

    char **createArrayPointersToNames(Student **name , int nex) {
        int i;
        printf("createArrayPointersToNames start");
    char **Names = (char **) malloc((nex) * sizeof(char *) * LEN); //if not work delete red
        for (i = 0; i < nex; i++) {
            Names[i] = strdup(name[i]->name);
        }printf("createArrayPointersToNames end");
    return Names; // & //NAMES[i]
    }

    char **getExcellentStudentsFromUser(unsigned nex) 
    {
        int i;
        char recv_buf[LEN];
        char **Names = (char **) malloc((nex) * sizeof(char*));
        for ( i = 0; i < nex; i++) {
            printf("Please insert name of excellent student: # %d", (i + 1));
            scanf("\n %s",recv_buf);
            Names[i] = strdup(recv_buf);
        }

        return Names;
    }

    char** binaryList(char **name, char **mark, int h)
    {
    char **ename;
    int l = 0, i = 0;
    //sortNames(name, nex); // my fix (: add argument nex
    while (l < h) {

        int mid = (l + h) / 2;
        if (strcmp(name[mid], mark[i]) == 0) {
            strcpy(ename[i], name[mid]);
            i++; l = 0;
        }
        if (strcmp(name[mid], mark[i]) < 0) {
            l = mid - 1;
        } else {
            l = mid + 1;
        }
    }
    return ename;
    }

    void sortNames(Student **name, int nex){
    printf("sortNames start");
    Student **Names12 = (Student**) malloc(nex * sizeof(Student*));
    //char Names12[nex][LEN] ;//= (char) malloc((nex) * sizeof(char) * LEN);
    printf("sortNames start1..");
    Student *temp = (Student*) malloc(nex * sizeof(Student));//  = (char *) malloc((nex) * sizeof(char) * LEN);printf("sortNames start2..");
    int i, j;

    for (i = 0; i < nex; i++)
        strcpy(Names12[i]->name, name[i]->name);printf("sortNames 1");

    for(i = 0; i < nex; i++)
        printf("\n%s",Names12[i]->name);

    for (i = 0; i < nex - 1; i++)
        for (j = 0; j < nex - i - 1; j++) {printf("sortNames 2");
            if (strcmp(Names12[i]->name, Names12[j]->name) > 0) {
                strcpy(temp->name, Names12[i]->name);
                strcpy(Names12[i]->name, Names12[j]->name);
                strcpy(Names12[j]->name, temp->name);
            }
        }printf("sortNames end");
    /*printf("Order of Sorted Strings:");
    for (i = 0; i <= nex; i++)
        puts(Names[i]);
    */
    }

    void printArrayStudents(Student **students, unsigned n) {
        int i;
    for ( i = 0; i < n; i++)
        printf("\n %s %d", students[i]->name, students[i]->grade);
    }
    void printExArrayStudents(char **eStudents, unsigned nex){
        int i;
        for(i = 0 ; i < nex ; i++)
            printf("\n%s",eStudents[i]);

    }

    void removeNewLine(char *s) {
    char *p;
    if ((p = strchr(s, '\n')) != NULL)
        *p = 0;
    }

    void freeAlloc(void *p) {
    free(p);
    }

    void freeAllocations(Student **ptrStudents, int numStudent) {
        int i;
    for ( i = 0; i < numStudent; i++)
        freeAlloc(ptrStudents[i]);
    if (numStudent > 0)
        free(ptrStudents);
    }

    void clean() {
    char c;
    do {
        c = getchar();
    } while (c != '\n');
    }

程序应类似于: 请输入学生#1的姓名:Bisli 请输入学生#1的分数:88

请输入学生#2的姓名:Bamba 请输入学生#2的成绩:95

优秀学生 请输入优秀学生#1的名字:猫王

请输入优秀学生#2的姓名:Bisli

==================

排序:

Bamba 95 比斯利88

搜索:

Bisli Kinder Bueno

0 个答案:

没有答案