结构程序采用不同格式的输入

时间:2018-09-06 04:38:41

标签: c

我正在参加测试,并有一个问题:修改该程序,使其采用不同格式的输入:每行包含年龄,逗号,空格和名称,例如

23, Angus McGurkinshaw

我了解我需要在readOneStudent函数中进行一些修改。不确定如何通过知道逗号的地址来读取名称。请帮忙。 输入和输出应如下所示:

input = 21, Fred Nurk
        927, Arwen Evensong
output is suppose to be:
        Arwen Evensong (927)
        Fred Nurk (21)

..

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

    #define MAX_LINE_LENGTH 80      // The longest line this program will accept
    #define MAX_NUM_STUDENTS 500    // The maximum number of students this program can handle
    #define MAX_NAME_SIZE 50        // The maximum allowable name length

    typedef struct student_s Student;

    struct student_s {
        char name[MAX_NAME_SIZE];
        int age;
        Student* next;              // Pointer to next student in a list
    };

    // Create a pool of student records to be allocated on demand

    Student studentPool[MAX_NUM_STUDENTS];  // The student pool
    int firstFree = 0;

    // Return a pointer to a new student record from the pool, after
    // filling in the provided name and age fields. Returns NULL if
    // the student pool is exhausted.
    Student* newStudent(const char* name, int age)
    {
        Student* student = NULL;
        if (firstFree < MAX_NUM_STUDENTS) {
            student = &studentPool[firstFree];
            firstFree += 1;
            strncpy(student->name, name, MAX_NAME_SIZE);
            student->name[MAX_NAME_SIZE - 1] = '\0';  // Make sure it's terminated
            student->age = age;
            student->next = NULL;
        }
        return student;
    }

    // Read a single student from a csv input file with student name in first column,
    // and student age in second.
    // Returns: A pointer to a Student record, or NULL if EOF or an invalid
    // student record is read. Blank lines, or lines in which the name is
    // longer than the provided name buffer, or there is no comma in the line
    // are considered invalid.
    Student* readOneStudent(FILE* file)
    {
        char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
        Student* student = NULL;       // Pointer to a student record from the pool

        // Read a line, extract name and age

        char* inputLine = fgets(buffer, MAX_LINE_LENGTH, file);
        if (inputLine != NULL) {        // Proceed only if we read something
            char* commaPos = strchr(buffer, ',');
            if (commaPos != NULL) {
                int age = atoi(commaPos + 1);
                *commaPos = '\0';  // null-terminate the name
                student = newStudent(buffer, age);
            }
        }
        return student;
    }


    Student* readStudents(FILE *file)
    {
        Student* first = NULL;     // Pointer to the first student in the list
        Student* last = NULL;      // Pointer to the last student in the list
        Student* student = readOneStudent(file);
        while (student != NULL) {
            if (first == NULL) {
                first = last = student;   // Empty list case
            } else {
                student -> next = first;
                first = student;
            }
            student = readOneStudent(file);
        }
        return first;
    }

    // printOneStudent: prints a single student, passed by value
    void printOneStudent(Student student)
    {
        printf("%s (%d)\n", student.name, student.age);
    }


    // printStudents: print all students in a list of students, passed
    // by reference
    void printStudents(const Student* student)
    {
        while (student != NULL) {
            printOneStudent(*student);
            student = student->next;
        }
    }



    int main(void)
    {
        FILE* inputFile = stdin;
        if (inputFile == NULL) {
            fprintf(stderr, "File not found\n");
        } else {
            Student* studentList = readStudents(inputFile);
            printStudents(studentList);


        }
    }

1 个答案:

答案 0 :(得分:1)

下面的代码应该可以工作。 请注意

strchr() 
This returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found.

我认为您正在尝试从名称字符串中获取年龄。

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

    #define MAX_LINE_LENGTH 80      // The longest line this program will accept
    #define MAX_NUM_STUDENTS 500    // The maximum number of students this program can handle
    #define MAX_NAME_SIZE 50        // The maximum allowable name length

    typedef struct student_s Student;

    struct student_s {
        char name[MAX_NAME_SIZE];
        int age;
        Student* next;              // Pointer to next student in a list
    };

    // Create a pool of student records to be allocated on demand

    Student studentPool[MAX_NUM_STUDENTS];  // The student pool
    int firstFree = 0;

    // Return a pointer to a new student record from the pool, after
    // filling in the provided name and age fields. Returns NULL if
    // the student pool is exhausted.
    Student* newStudent(const char* name, int age)
    {
        Student* student = NULL;
        if (firstFree < MAX_NUM_STUDENTS) {
            student = &studentPool[firstFree];
            firstFree += 1;
            strncpy(student->name, name, MAX_NAME_SIZE);
            student->name[MAX_NAME_SIZE - 1] = '\0';  // Make sure it's terminated
            student->age = age;
            student->next = NULL;
        }
        return student;
    }

    // Read a single student from a csv input file with student name in first column,
    // and student age in second.
    // Returns: A pointer to a Student record, or NULL if EOF or an invalid
    // student record is read. Blank lines, or lines in which the name is
    // longer than the provided name buffer, or there is no comma in the line
    // are considered invalid.
    Student* readOneStudent(FILE* file)
    {
        char buffer[MAX_LINE_LENGTH];  // Buffer into which we read a line from stdin
        Student* student = NULL;       // Pointer to a student record from the pool

        // Read a line, extract name and age

        char* inputLine = fgets(buffer, MAX_LINE_LENGTH, file);
        if (inputLine != NULL) {        // Proceed only if we read something
            char* commaPos = strchr(buffer, ',');
            if (commaPos != NULL) {
               // int age = atoi(commaPos + 1);
                //printf("age and commaPos is %d,%s \n ",age,commaPos);

                char* name = commaPos+1;
                name[strcspn(name, "\n")] = 0; //remove /n from fgets
                *commaPos = '\0';  // null-terminate the age
                int age = atoi(buffer);
                //printf("age and commaPos is %d,%s \n ",age,name);

                //student = newStudent(buffer, age);
                student = newStudent(name, age);
            }
        }
        return student;
    }


    Student* readStudents(FILE *file)
    {
        Student* first = NULL;     // Pointer to the first student in the list
        Student* last = NULL;      // Pointer to the last student in the list
        Student* student = readOneStudent(file);
        while (student != NULL) {
            if (first == NULL) {
                first = last = student;   // Empty list case
            } else {
                student -> next = first;
                first = student;
            }
            student = readOneStudent(file);
        }
        return first;
    }

    // printOneStudent: prints a single student, passed by value
    void printOneStudent(Student student)
    {
        printf("%s (%d)\n", student.name, student.age);
    }


    // printStudents: print all students in a list of students, passed
    // by reference
    void printStudents(const Student* student)
    {
        while (student != NULL) {
            printOneStudent(*student);
            student = student->next;
        }
    }



    int main(void)
    {
        FILE* inputFile = stdin;
        if (inputFile == NULL) {
            fprintf(stderr, "File not found\n");
        } else {
            Student* studentList = readStudents(inputFile);
            printStudents(studentList);


        }
    }