将元素添加到结构中的 char 数组

Posted

技术标签:

【中文标题】将元素添加到结构中的 char 数组【英文标题】:Adding elements to the char array in a struct 【发布时间】:2021-02-02 23:19:39 【问题描述】:

我写的代码是一个从commands.txt逐行读取和执行命令的程序。如果程序读取一个名为 ADD 1 SENG101 的命令作为示例,它必须将学生号 1 添加到 seng101 课程中。我的 commands.txt 文件的一部分中有这 3 个命令。

ADD 1 SENG101
ADD 1 SENG202
ADD 2 SENG202

在调试我的程序时,我注意到ADD 1 SENG101工作正常,但是在执行以下命令时,我遇到了这样的问题。

Before SENG202 is added to the courses taken by the student:

After SENG202 is added to the courses taken by the student:

从我的鼠标尖端可以看出,需要添加的课程是“SSENG202”,即使它是“SENG202”。

我的代码中执行“添加”过程的部分:

else if (0 == strcmp("ADD", word1))
            
                course_valid = 0;
                lecturer_valid = 0;
                student_valid = 0;
                lecturer_course_load = 0;
                student_course_load = 0;
                student_taken_credits = 0;
                who_is_taught = 0;
                is_student_member_of_course = 0;

                for (i = 0; i < number_of_courses; i++)
                
                    if (0 == strcmp(course[i].courseCode, word3))
                    
                        course_valid = 1;                                           //It was checked whether there was a course with the specified course name.
                        break;
                                                                                   
                
                for (j = 0; j < number_of_students; j++)
                
                    if (0 == strcmp(student[j].studentNumber, word2))
                    
                        student_valid = 1;                                                 //It was checked whether there was a student with the specified name.  
                        if (student[j].number_of_taken_courses < STUDENT_COURSE_LOAD)
                        
                            student_course_load = 1;                                    //The number of courses the specified student took was checked.                            
                           
                        if (student[j].taken_credits < STUDENT_MAX_CREDITS)
                        
                            student_taken_credits = 1;                              //The total course credits of the specified student were checked.
                        
                        break;
                                
                

                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");

                if (student_taken_credits && student_course_load && student_valid && course_valid)
                
                    strcpy(&student[j].taken_courses[student[j].number_of_taken_courses], word3);                  //The specified course was added to the courses taken by the student.
                    student[j].number_of_taken_courses++;                                                  //The number of courses taken by the student has been increased.
                    
                    strcpy(&course[i].who_is_taked[course[i].number_of_taked_students], word2);                 //The specified student has been added to the specified lesson.
                    course[i].number_of_taked_students++;
                    student[j].taken_credits += course[i].courseCredit;
                
                else if (1 != (student_taken_credits && student_course_load && student_valid && course_valid))
                
                    fprintf(foutput, "Error!\n");
                    break;
                
                
                fprintf(foutput, "\n");
            

如果你需要我所有的代码:

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

#pragma warning (disable:4996)
#define MAXCHAR 100
#define TEACHER_COURSE_LOAD 5
#define STUDENT_COURSE_LOAD 8
#define STUDENT_MAX_CREDITS 22

struct students 
    char studentNumber[50];
    char studentName[50];
    char studentSurname[50];
    char taken_courses[50];
    int number_of_taken_courses;
    int taken_credits;
;
struct courses 
    char courseCode[10];
    char courseName[50];
    int courseCredit;
    char who_is_taked[100];
    char who_is_taught[100];
    int number_of_taked_students;
;
struct lecturers 
    char regNumber[10];
    char regSurname[50];
    char regName[50];
    char courses_taught[50];
    int number_of_courses_taught;
;

/*
        PLEASE SEND ARGUMENTS IN THE FOLLOWING ORDER TO THE PROGRAM
        Ex: C:>DBMS students.txt lecturers.txt courses.txt commands.txt output.txt
*/

int main()

    int number_of_students = -1;
    int number_of_lecturers = -1;
    int number_of_courses = -1;

    char readed[MAXCHAR];
    char word1[100];
    char word2[100];
    char word3[100];

    FILE* fstudent = fopen("C:\\Users\\ayber\\Desktop\\Files\\students.txt", "r"); // "r" for read
    FILE* flecturers = fopen("C:\\Users\\ayber\\Desktop\\Files\\lecturers.txt", "r"); // "r" for read
    FILE* fcourses = fopen("C:\\Users\\ayber\\Desktop\\Files\\courses.txt", "r"); // "r" for read
    FILE* fcommands = fopen("C:\\Users\\ayber\\Desktop\\Files\\commands.txt", "r"); // "r" for read
    FILE* foutput = fopen("C:\\Users\\ayber\\Desktop\\Files\\output.txt", "a+"); // "a+" for append

    while (fgets(readed, MAXCHAR, fstudent) != NULL)
    
        number_of_students++;                                                                   //Number of students has been founded!
    

    struct students* student = malloc(number_of_students * sizeof(struct students));            //Memory allocated for number_of_students students!

    rewind(fstudent);                                                                           //File refreshed for re-read!

    int i = 0;
    int j = 0;
    int k = 0;

    while (fgets(readed, MAXCHAR, fstudent) != NULL)
    
        readed[strcspn(readed, "\n")] = 0;
        if (sscanf(readed, "%s %s %s", word1, word2, word3) == 3)
        
            strcpy(student[i].studentNumber, word1);
            strcpy(student[i].studentName, word2);                                              //All students in the file are assigned to the required variables.
            strcpy(student[i].studentSurname, word3);
            student[i].number_of_taken_courses = 0;
            student[i].taken_credits = 0;
            i++;
        
    

    while (fgets(readed, MAXCHAR, flecturers) != NULL)
    
        number_of_lecturers++;                                                                  //Number of lecturers has been founded!
    

    struct lecturers* lecturer = malloc(number_of_lecturers * sizeof(struct lecturers));        //Memory allocated for number_of_lecturers lecturers!

    rewind(flecturers);           //File refreshed for re-read!

    i = 0;

    while (fgets(readed, MAXCHAR, flecturers) != NULL)
    
        readed[strcspn(readed, "\n")] = 0;
        if (sscanf(readed, "%s %s %s", word1, word2, word3) == 3)
        
            strcpy(lecturer[i].regNumber, word1);
            strcpy(lecturer[i].regSurname, word2);                                              //All lecturers in the file are assigned to the required variables.
            strcpy(lecturer[i].regName, word3);
            lecturer[i].number_of_courses_taught = 0;
            i++;
        
    

    while (fgets(readed, MAXCHAR, fcourses) != NULL)
    
        number_of_courses++;                                                                    //Number of courses number has been founded!
    

    struct courses* course = malloc(number_of_courses * sizeof(struct courses));                //Memory allocated for number_of_courses courses!

    rewind(fcourses);                                                                           //File refreshed for re-read!

    i = 0;

    while (fgets(readed, MAXCHAR, fcourses) != NULL)
    
        readed[strcspn(readed, "\n")] = 0;
        if (sscanf(readed, "%s %s %s", word1, word2, word3) == 3)
        
            strcpy(course[i].courseCode, word1);
            strcpy(course[i].courseName, word2);                                                //All courses in the file are assigned to the required variables.
            course[i].courseCredit = atoi(word3);
            strcpy(course[i].who_is_taught, " ");
            course[i].number_of_taked_students = 0;
            i++;
        
    

    bool course_valid;
    bool lecturer_valid;
    bool student_valid;
    bool lecturer_course_load;
    bool student_course_load;
    bool student_taken_credits;
    bool who_is_taught;
    bool is_student_member_of_course;


    while (fgets(readed, MAXCHAR, fcommands) != NULL)                                           //STARTED TO READ COMMANDS.TXT
    
        readed[strcspn(readed, "\n")] = 0;

        if (sscanf(readed, "%s %s %s", word1, word2, word3) == 3)
        
            if (0 == strcmp("ASSIGN", word1))
            
                course_valid = 0;
                lecturer_valid = 0;
                student_valid = 0;
                lecturer_course_load = 0;
                student_course_load = 0;
                student_taken_credits = 0;
                who_is_taught = 0;
                is_student_member_of_course = 0;

                for (i = 0; i < number_of_courses; i++)
                
                    if (0 == strcmp(course[i].courseCode, word3))
                    
                        course_valid = 1;  
                        if (0 == strcmp(course[i].who_is_taught, " "))              //It was checked whether the name of the course and the person who is 
                                                                                   //currently teaching the course.
                            who_is_taught = 1;
                        
                        break;
                                        
                
                for (j = 0; j < number_of_lecturers; i++)
                
                    if (0 == strcmp(lecturer[j].regNumber, word2))
                    
                        lecturer_valid = 1;   
                        if (lecturer[j].number_of_courses_taught < TEACHER_COURSE_LOAD)      //The teacher's validation and the course load of the teacher was checked.
                        
                            lecturer_course_load = 1;
                        
                        break;
                                        
                

                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");
                
                if (lecturer_course_load && lecturer_valid && course_valid && who_is_taught)
                
                    strcpy(&lecturer[j].courses_taught[lecturer[j].number_of_courses_taught], word3);                  //The specified course was added to the courses given by the teacher
                    lecturer[j].number_of_courses_taught++;                         //The number of lectures given by the teacher has been increased.
                    
                    if (0 == strcmp(course[i].courseCode, word3))
                    
                        strcpy(course[i].who_is_taught, word2);                 //The specified teacher has been appointed to the specified lesson.          
                    
                
                else if (1 != (lecturer_course_load && lecturer_valid && course_valid && who_is_taught))
                
                    fprintf(foutput, "Error!\n");
                
                fprintf(foutput, "\n");
            
            else if (0 == strcmp("ADD", word1))
            
                course_valid = 0;
                lecturer_valid = 0;
                student_valid = 0;
                lecturer_course_load = 0;
                student_course_load = 0;
                student_taken_credits = 0;
                who_is_taught = 0;
                is_student_member_of_course = 0;

                for (i = 0; i < number_of_courses; i++)
                
                    if (0 == strcmp(course[i].courseCode, word3))
                    
                        course_valid = 1;                                           //It was checked whether there was a course with the specified course name.
                        break;
                                                                                   
                
                for (j = 0; j < number_of_students; j++)
                
                    if (0 == strcmp(student[j].studentNumber, word2))
                    
                        student_valid = 1;                                                 //It was checked whether there was a student with the specified name.  
                        if (student[j].number_of_taken_courses < STUDENT_COURSE_LOAD)
                        
                            student_course_load = 1;                                    //The number of courses the specified student took was checked.                            
                           
                        if (student[j].taken_credits < STUDENT_MAX_CREDITS)
                        
                            student_taken_credits = 1;                              //The total course credits of the specified student were checked.
                        
                        break;
                                
                

                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");

                if (student_taken_credits && student_course_load && student_valid && course_valid)
                
                    strcpy(&student[j].taken_courses[student[j].number_of_taken_courses], word3);                  //The specified course was added to the courses taken by the student.
                    student[j].number_of_taken_courses++;                                                  //The number of courses taken by the student has been increased.
                    
                    strcpy(&course[i].who_is_taked[course[i].number_of_taked_students], word2);                 //The specified student has been added to the specified lesson.
                    course[i].number_of_taked_students++;
                    student[j].taken_credits += course[i].courseCredit;
                
                else if (1 != (student_taken_credits && student_course_load && student_valid && course_valid))
                
                    fprintf(foutput, "Error!\n");
                    break;
                
                
                fprintf(foutput, "\n");
            
            else if (0 == strcmp("DROP", word1))
            
                course_valid = 0;
                lecturer_valid = 0;
                student_valid = 0;
                lecturer_course_load = 0;
                student_course_load = 0;
                student_taken_credits = 0;
                who_is_taught = 0;      
                is_student_member_of_course = 0;

                for (i = 0; i < number_of_courses; i++)
                
                    if (0 == strcmp(course[i].courseCode, word3))
                    
                        course_valid = 1;                                           //It was checked whether there was a course with the specified course name.
                    
                
                for (i = 0; i < number_of_students; i++)
                
                    if (0 == strcmp(student[i].studentNumber, word2))
                    
                        student_valid = 1;                                                //It was checked whether there was a student with the specified name.
                    
                    for (j = 0; j < number_of_courses; j++)
                    
                        if (0 == strcmp(&student[i].taken_courses[j], word3))
                        
                            is_student_member_of_course = 1;                                    //It was checked whether the student took the course or not.
                        
                                        
                
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");
          
                if ((student_valid && course_valid) && (is_student_member_of_course !=0))
                
                    for (i = 0; i < number_of_courses; i++)
                    
                        if (0 == strcmp(course[i].courseCode, word3))
                        
                            for (j = 0; j < number_of_students; j++)
                            
                                if (0 == strcmp(student[j].studentNumber, word2))
                                
                                    for (k = 0; k < student[j].number_of_taken_courses; k++)
                                    
                                        if (0 == strcmp(&student[j].taken_courses[k], word3))
                                        
                                            student[j].taken_courses[k] = 0;
                                            student[j].taken_credits -= course[i].courseCredit;
                                            student[j].number_of_taken_courses--;
                                        
                                    
                                    for (k = 0; k < course[i].number_of_taked_students; k++)
                                    
                                        if (0 == strcmp(&course[i].who_is_taked[k], word2))
                                        
                                            course[i].who_is_taked[k] = 0;
                                            course[i].number_of_taked_students--;
                                        
                                    
                                
                            
                        
                    
                
                else if (1 != ((student_valid && course_valid) && (is_student_member_of_course != 0)))
                
                    fprintf(foutput, "Error!\n");
                
                fprintf(foutput, "\n");
            
        
        else if (sscanf(readed, "%s %s", word1, word2) == 2)
        
            if (0 == strcmp("LIST4", word1))
            
                course_valid = 0;
                lecturer_valid = 0;
                student_valid = 0;
                lecturer_course_load = 0;
                student_course_load = 0;
                student_taken_credits = 0;
                who_is_taught = 0;
                is_student_member_of_course = 0;

                for (i = 0; i < number_of_students; i++)
                
                    if (0 == strcmp(student[i].studentNumber, word2))
                    
                        student_valid = 1;                                               //It was checked whether there was a student with the specified name.
                        break;
                    
                

                for (j = 0; j < student[i].number_of_taken_courses; j++)
                         
                    for (k = 0; k < number_of_courses; k++)
                    
                        if (0 == strcmp(&student[i].taken_courses[j], course[k].courseCode))
                        
                            is_student_member_of_course = 1;
                            break;
                        
                    
                

                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");

                if (student_valid && is_student_member_of_course)
                
                    fprintf(foutput,"Student : %s - %s %s\n", student[i].studentNumber, student[i].studentName, student[i].studentSurname);
                    fprintf(foutput, "Participating Courses : %d\n",student[i].number_of_taken_courses);
                    for (k = 0; k < student[i].number_of_taken_courses; k++)
                    
                        for (j = 0; j < number_of_courses; j++)
                        
                            if (0 == strcmp(&student[i].taken_courses[k], course[j].courseCode))
                            
                                fprintf(foutput, "%s %s %d\n", course[j].courseCode, course[j].courseName, course[j].courseCredit);
                            
                                           
                    
                    fprintf(foutput, "Achieved Credits: %d\n", student[i].taken_credits);
                
                else if((student_valid && is_student_member_of_course) != 1)
                
                    fprintf(foutput, "Error!\n");
                               

                fprintf(foutput, "\n");
            
            else if (0 == strcmp("LIST5", word1))
            
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");          //The function of LIST5 will be added...
                fprintf(foutput, "\n");
            
            else if (0 == strcmp("LIST6", word1))
            
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");          //The function of LIST6 will be added...
                fprintf(foutput, "\n");
            
            else if (0 == strcmp("LIST7", word1))
            
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");          //The function of LIST7 will be added...
                fprintf(foutput, "\n");
            
        
        else if (sscanf(readed, "%s", word1) == 1)
        
            if (0 == strcmp("LIST1", word1))
            
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");          //The function of LIST1 (Working Propery)
                for (i = 0; i < number_of_students; i++)
                
                    fprintf(foutput, "%s %s %s", student[i].studentNumber, student[i].studentName, student[i].studentSurname);
                    fprintf(foutput, "\n");
                
                fprintf(foutput, "\n");
            
            else if (0 == strcmp("LIST2", word1))                                           //The function of LIST2 (Working Propery) 
            
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");
                for (i = 0; i < number_of_lecturers; i++)
                
                    fprintf(foutput, "%s %s %s", lecturer[i].regNumber, lecturer[i].regName, lecturer[i].regSurname);
                    fprintf(foutput, "\n");
                
                fprintf(foutput, "\n");
            
            else if (0 == strcmp("LIST3", word1))
            
                fprintf(foutput, "%s %s \n%s", "Command: ", readed, "Result: \n");           //The function of LIST3 (Working Propery)
                for (i = 0; i < number_of_courses; i++)
                
                    fprintf(foutput, "%s %s %d", course[i].courseCode, course[i].courseName, course[i].courseCredit);
                    fprintf(foutput, "\n");
                
                fprintf(foutput, "\n");
            
        
        else
        
            printf("Error\n");
            // ERROR
        
    
    return 0;


请你帮我解决这个问题?

【问题讨论】:

【参考方案1】:

我认为你的问题在这里

struct students 
    char studentNumber[50];
    char studentName[50];
    char studentSurname[50];
    char taken_courses[50];   <---------- Can only save one string/name
    int number_of_taken_courses;
    int taken_credits;
;

这里

strcpy(&student[j].taken_courses[student[j].number_of_taken_courses], word3);
student[j].number_of_taken_courses++;

您似乎尝试在taken_courses 中保存多个名称。

也许你真的想要:

struct students 
    char studentNumber[50];
    char studentName[50];
    char studentSurname[50];
    char taken_courses[MAX_NUM_COURSES][50];
    int number_of_taken_courses;
    int taken_credits;
;

并相应地更新到strcpy

【讨论】:

以上是关于将元素添加到结构中的 char 数组的主要内容,如果未能解决你的问题,请参考以下文章

C语言中的结构体数组

c语言结构体中的一个char数组怎么赋值?

将数组值添加到元素不在数组中的 MongoDB

数据结构

c#如何将结构的引用添加到数组或列表?

如何将一个char数组中元素插入到另一个char数组中指定元素后面,在java语言环境,但不能使用java的内置函数