我需要字母排序顺序由字符的序数值定义
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
#define MAX_NUM_STUDENTS 500
#define MAX_NAME_SIZE 50
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,
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
student->age = age;
student->next = NULL;
}
return student;
}
Student* readOneStudent(FILE* file) {
char *buffer; // Buffer into which we read a line from stdin
buffer = (char *)malloc(MAX_LINE_LENGTH);
Student* student = NULL;
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;
}
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 {
(insert(student,first))->next= first;
first = insert(student,first);
}
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);
}
}
所以我得到的是
nothing
我的另一个测试是正确的输入是,这个工作正常
input output
Zaphod Beeblebrox,250 Albert Einstein (7)
Albert Einstein,133 Albert Einstein (133)
Albert Einstein,7 Zaphod Beeblebrox (250)
另一个测试是-------------------------并且应该输出
A,1 A (1)
A,2 A (2)
A,33 A (33)
我一无所获 任何建议将不胜感激:)