I need to sort an array of structures, each one having a string that is a name. I want to do a simple sorting using bubble sort and strcmp, but my code doesn't work, it outputs the last name I've entered for the entire loop.
我需要对结构数组进行排序,每个结构都有一个名称为string的字符串。我想使用bubble sort和strcmp进行简单的排序,但是我的代码不起作用,它输出我为整个循环输入的姓。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 16
#define N 5
struct Prova {
char nome[SIZE];
};
void sort(struct Prova *ptr) {
char temp[SIZE];
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (strcmp((ptr + i)->nome, (ptr + j)->nome) < 0) {
strcpy(temp, (ptr + i)->nome);
strcpy((ptr + i)->nome, (ptr + j)->nome);
strcpy(temp, (ptr + j)->nome);
}
}
}
}
int main() {
struct Prova * ptr;
ptr = (struct Prova*) malloc(N * sizeof(struct Prova));
for (int i = 0; i < N; i++) {
scanf(" %s", (ptr + i)->nome);
}
sort(ptr);
for (int i = 0; i < N; i++) {
printf("%s\n", (ptr + i)->nome);
}
}
Basically it needs to sort all the names in the structures and print them in ascending order, using the first letter of the name.
基本上,它需要对结构中的所有名称进行排序,并使用名称的第一个字母按升序打印它们。
UPDATED: Later i noticed that mistake in my code, thank you all for the replies/suggestions. Now is this:
更新:后来我发现我的代码有错误,谢谢大家的回复/建议。现在是这样的:
for(int i = 0; i < N - 1; i++)
{
for(int j = i+1; j < N; j++)
{
// < 0 = Z-A invece > 0 = A-Z
if(strcmp((ptr+i)->nome,(ptr+j)->nome) > 0)
{
strcpy(temp, (ptr+i)->nome);
strcpy((ptr+i)->nome,(ptr+j)->nome);
strcpy((ptr+j)->nome, temp);
}
}
}
2 个解决方案
#1
0
OP mis-coded
OP mis-coded
strcpy(temp, (ptr + i)->nome);
strcpy((ptr + i)->nome, (ptr + j)->nome);
// strcpy(temp, (ptr + j)->nome);
strcpy((ptr + j)->nome, temp);
Many other improvements possible
许多其他可能的改进
- Use
qsort()
@Karsten Koop - 使用qsort()@Karsten Koop
- Validate inputs. Better to use
fgets()
. - 验证输入。最好使用fgets()。
- Simplify, check and free -->
struct Prova *ptr = malloc(sizeof *ptr * N); if (ptr == NULL) Handle_OutOfMemory(); ... // use ptr ... free(ptr);
- 简化、检查、*——>结构Prova *ptr = malloc(sizeof *ptr * N);if (ptr == NULL) Handle_OutOfMemory();…/ /使用ptr……免费(ptr);
#2
0
The problem is the last strcpy statement. You need to reverse the parameters like this:
问题是最后的strcpy语句。你需要像这样反转参数:
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (strcmp((ptr + i)->nome, (ptr + j)->nome) < 0) {
strcpy(temp, (ptr + i)->nome);
strcpy((ptr + i)->nome, (ptr + j)->nome);
strcpy((ptr + j)->nome, temp);
}
}
}
}
#1
0
OP mis-coded
OP mis-coded
strcpy(temp, (ptr + i)->nome);
strcpy((ptr + i)->nome, (ptr + j)->nome);
// strcpy(temp, (ptr + j)->nome);
strcpy((ptr + j)->nome, temp);
Many other improvements possible
许多其他可能的改进
- Use
qsort()
@Karsten Koop - 使用qsort()@Karsten Koop
- Validate inputs. Better to use
fgets()
. - 验证输入。最好使用fgets()。
- Simplify, check and free -->
struct Prova *ptr = malloc(sizeof *ptr * N); if (ptr == NULL) Handle_OutOfMemory(); ... // use ptr ... free(ptr);
- 简化、检查、*——>结构Prova *ptr = malloc(sizeof *ptr * N);if (ptr == NULL) Handle_OutOfMemory();…/ /使用ptr……免费(ptr);
#2
0
The problem is the last strcpy statement. You need to reverse the parameters like this:
问题是最后的strcpy语句。你需要像这样反转参数:
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (strcmp((ptr + i)->nome, (ptr + j)->nome) < 0) {
strcpy(temp, (ptr + i)->nome);
strcpy((ptr + i)->nome, (ptr + j)->nome);
strcpy((ptr + j)->nome, temp);
}
}
}
}