在结构数组中搜索匹配项

时间:2016-03-31 01:22:22

标签: c arrays structure

我已经在这个混乱中度过了一段时间,我仍然没有弄明白我在哪里出错了,如果它像指针一样荒谬,那就完全自欺欺人了。

显示的任务:尝试使用学生ID,姓名,姓氏,出生日期和成绩填充结构数组。然后使用提供给用户的匹配ID进行搜索。

我非常感谢与此主题相关的任何帮助,我已经认真地坚持了一段时间。我也提前为法语部分道歉

// Part 1
struct Date{
    int day;
    int month;
    int year;
};

// Part 2
struct Student{
    int ID;
    char name[20];
    char lastname[20];
    struct Date DOB;
    int notes[J];
};

// Part 3
void FillStudentList(struct Student E){
    int i;
    printf("\nInsert ID: ");
    scanf("%d", &E.ID);
    printf("Insert name: ");
    scanf("%s", &E.name);
    printf("Insert last name: ");
    scanf("%s", &E.lastname);
    printf("Insert date of birth: ");
    scanf("%d %d %d", &E.DOB.day, &E.DOB.month, &E.DOB.year);
    printf("Insert notes: ");
    for(i=0; i<J; i++)
        scanf("%d", &E.Notes[i]);
}

// Part 4
void ShowByNb(int Nb, struct Student E[], int NbStudents){
    int j, i;
    for(i=0; i<NbStudents; i++){
        if (E[i].ID== Nb){
            printf("\nID: %d", E[i].ID);
            printf("\nName: %s", E[i].name);
            printf("\nLast Name: %s", E[i].lastname);
            printf("\nDate Of Birth: %s-%s-%s", E[i].DOB.day, E[i].DOB.month, E[i].DOB.year);
            printf("\nNotes: ");
            for(j=0; j<J; j++){
                printf("%d", E[i].Notes[j]);
            }
        }
        else
            printf("\nInvalid Student!\n");
    }
}

// Part 5
void main(){
    int i, x;
    struct Student E[N];
    for(i=0; i<N; i++){
        printf("\n\nStudent #%d", i+1);
        FillStudentList(E[i]);
    }

    printf("\n\nSearch student by NB: ");
    scanf("%d", &x);
    ShowByNb(x, E, N);
    } 

2 个答案:

答案 0 :(得分:0)

我相信下面编辑的代码可以实现您的目标。主要问题(除了使用'%s'读取/打印'int'是你将结构传递给函数的方式。有必要通过引用传递结构,以便可以在FillStudentList函数之外看到它的值;请参阅此link

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define N 2
#define J 2

// Part 1
struct Dat{
    int jour;
    int mois;
    int annee;
};

// Part 2
struct Etudiant{
    int numero;
    char nom[20];
    char prenom[20];
    struct Dat DDN;
    int Notes[J];
};

// Part 3
/* Modified this so that a pointer to the struct is passed instead of a copy of the struct */
void FillStudentList(struct Etudiant *E){
    int i;
    printf("\nInsert ID: ");
    scanf("%d", &E->numero);
    printf("Insert name: ");
    scanf("%s", E->nom);
    printf("Insert last name: ");
    scanf("%s", E->prenom);
    printf("Insert date of birth: ");
    /* These are integers. Do not read with %s */
    scanf("%d %d %d", &E->DDN.jour, &E->DDN.mois, &E->DDN.annee);
    printf("Insert notes: ");
    for(i=0; i<J; i++)
        scanf("%d", &E->Notes[i]);
}

// Part 4
void ShowByNb(int Nb, struct Etudiant E[]){
    /* Don't redefine N == NbEtudiants making it seem that N is variable */
    int j, i;
    for(i=0; i<N; i++){
        if (E[i].numero == Nb){
            printf("\nID: %d", E[i].numero);
            printf("\nName: %s", E[i].nom);
            printf("\nLast Name: %s", E[i].prenom);
            /* Again, can't print integers with %s */
            printf("\nDate Of Birth: %d-%d-%d", E[i].DDN.jour, E[i].DDN.mois, E[i].DDN.annee);
            printf("\nLes notes: ");
            for(j=0; j<J; j++){
                printf("%d ", E[i].Notes[j]);
            }
            return;
        }
        /* Your previous else would print invalid student every time you ran through the loop even 
         * if the student number was valid for a later student.
         */
    }
    /* Only print this if student was not found in any of the N Student structures */
    printf("\nInvalid Student!\n");
}

// Part 5
void main(){

    setbuf(stdout, NULL);

    int i, x;
    struct Etudiant E[N];

    for(i=0; i<N; i++){
        printf("\n\nStudent #%d", i+1);
        FillStudentList(&E[i]);
    }

    printf("\n\nSearch student by NB: ");
    scanf("%d", &x);
    ShowByNb(x, E);
}

输入

1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2

输出

Student #1
Insert ID: 1
Insert name: 1
Insert last name: 1
Insert date of birth: 1
1
1
Insert notes: 1
1


Student #2
Insert ID: 2
Insert name: 2
Insert last name: 2
Insert date of birth: 2
2
2
Insert notes: 2
2


Search student by NB: 2

ID: 2
Name: 2
Last Name: 2
Date Of Birth: 2-2-2
Les notes: 2 2

答案 1 :(得分:0)

经典错误:按值而不是按引用传递参数:

void FillStudentList(struct Student E){
.....
}

这里发生的是在堆栈上创建结构的本地副本,填充输入的任何内容并在函数退出时销毁。

通常在C中,即使您不想修改结构,也可以通过指针传递struct参数;如果按值传递它们,结构的每个成员都会被复制到堆栈中......这样会浪费时间。

因此,更改函数原型(以及使用新签名的代码)应解决问题:

void FillStudentList(struct Student *E){
....
}