C语言中的字母排序
我需要按字母顺序排序,排序是由字符的序数值定义的C语言中的字母排序,c,alphabetical-sort,C,Alphabetical Sort,我需要按字母顺序排序,排序是由字符的序数值定义的 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 <s
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);
}
}
我的另一个测试是正确的输入是正确的,这一个很好
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)
我什么都没有
任何建议都将不胜感激:)
qsort(…,strcmp)
?我应该添加到哪里?(插入(第一个,学生))->next=first;第一个=插入(第一个,学生)代码>-->first=插入(学生,第一)代码>@BLUEPIXY先生我有个问题。为什么你只是在评论中写下更正,为什么不作为答案发布?你的大多数评论确实比实际答案更有用。@ameyCU首先,这个网站不是调试服务。
A,1 A (1)
A,2 A (2)
A,33 A (33)