gpt4 book ai didi

在结构中为输出调用不同的变量

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:36 25 4
gpt4 key购买 nike

我目前正在从事一个计算机科学项目,几乎已经完成,但还停留在最后一部分。我得到了一个大型结构,我打算用它创建两个额外的类来更改它的输出。

当他们从文件中读取时,学生会按字母顺序插入到列表中,而不是在末尾或开头。如果两个或两个以上的学生有相同的名字,他们必须按年龄递增的顺序插入。保证测试数据中没有两个学生同名同龄。

这两个类是 comesBefore 和 insert,它们用于定义一个学生是否应该按字母顺序排在另一个学生之前,如果是,则以正确的顺序将他们插入到列表中。我已经在提供的代码中创建了这些函数。

我现在不确定的是如何实际调用这些函数,以便输出按规定的方式实际按字母顺序排列。

作为引用,输入应按以下方式放置并带有 EOF 终止:

Jenny Craig,47
Billy Bob,33
Jenny Craig,29
Simon Says,234

所需的输出是:

Billy Bob (33)
Jenny Craig (29)
Jenny Craig (47)
Simon Says (234)

我的代码是(相当大):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <assert.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
};

bool comesBefore(const Student* student1, const Student* student2) {
int name_compare = strcmp(student1->name, student2->name);

if (name_compare < 0) {
return true;
}
else if (name_compare == 0) {
int age1 = student1->age;
int age2 = student2->age;
if (age1 < age2) {
return true;
}
}
return false;
}

Student* insert(Student* student, Student* list) {
Student* curr = NULL;
Student* prev = NULL;
if (list == NULL)
return student;

if (comesBefore(student, list)) {
student->next = list;
return student;
}

for (curr = list, prev = NULL;
curr != NULL && comesBefore(student, curr) != true;
prev = curr, curr = curr->next);

assert(prev != NULL);

student->next = curr;
prev->next = student;
return 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("%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;
}
}

// Main program. Read a linked list of students from a csv file, then display
// the contents of that list.
int main(void)
{
FILE* inputFile = stdin;
if (inputFile == NULL) {
fprintf(stderr, "File not found\n");
}
else {
Student* studentList = readStudents(inputFile);
printStudents(studentList);

// The program could now do various things that make use of
// the linked list, like deleting students and adding new ones,
// but the program is already quite long enough!
}
}

最佳答案

readStudents 的实现是直接构建列表,因此保持文件中的顺序。如果要读取改变顺序,readStudents 必须调用insertStudent。如果您希望 readStudents 保留文件中的顺序,那么您将需要一个额外的排序函数。

关于在结构中为输出调用不同的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330024/

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