gpt4 book ai didi

c - 使用 struct 和 stdin 在 C 中输出的问题

转载 作者:行者123 更新时间:2023-11-30 19:15:46 24 4
gpt4 key购买 nike

我目前正在做一项计算机科学作业,但已经到了我无法解决的地步。我已经获得了代码,我可以编辑这些代码来完成到目前为止我能够做的某些事情,但我似乎无法得到下一步。该代码从标准输入中读取一些学生的姓名和年龄,直到由 EOF(控件 D 等)终止,构建它们的链接列表。从每个输入行读取一名学生。到目前为止我的代码是:

#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

// The declaration of the student record (or struct). Note that
// the struct contains the name as an array of characters, rather than
// containing just a pointer to the name as before.

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

// Reads a list of students from a given file. Input stops when
// a blank line is read, or an EOF occurs, or an illegal input
// line is encountered.
// Returns a pointer to the first student in the list or NULL if no
// valid student records could be read.
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 {
last->next = student;
last = student;
}
student= readOneStudent(file);
}
return first;
}

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


// 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);
}
}

代码需要做的是获取输入列表,例如:

21, Fred Nurk
92, Arwen Evensong

并返回学生列表,如下:

Fred Nurk (21)
Arwen Evensong (92)

但是我的代码返回:

 0 (21)
0 (92)

我不明白为什么。输入已设置且无法更改,对问题的任何编辑都必须专门对代码进行。

最佳答案

在您的函数 Student* readOneStudent(FILE* file); 中,您期望采用以下格式的输入:

Fred Nurk,21
Arwen Evensong,92

如果您希望输入采用以下形式:

21, Fred Nurk
92, Arwen Evensong

那么这可能会有所帮助:

Student* readOneStudent(FILE* file)
{
char *buffer; // Buffer into which we read a line from stdin
buffer = (char *)malloc(MAX_LINE_LENGTH);
Student* student = NULL; // Pointer to a student record from the pool

// Read a line, extract name and age

char* cp = fgets(buffer, MAX_LINE_LENGTH, file);
if (cp != NULL) { // Proceed only if we read something
char* commaPos = strchr(buffer, ',');
if (commaPos != NULL && commaPos > buffer) {
buffer[strlen(buffer)-1] = '\0';
*commaPos = '\0'; // null-terminate the age part
int age = atoi(buffer);
student = newStudent(commaPos+2, age);
}
}
return student;
}

编辑:

void printOneStudent(Student student)
{
printf("%s (%d)\n", student.name, student.age);
}

关于c - 使用 struct 和 stdin 在 C 中输出的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31869916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com