为作业的Buble排序代码不按预期工作

发布于 2025-01-27 15:36:10 字数 1565 浏览 2 评论 0原文

我的任务是制作一个可以排序名称的程序。

程序的输出:

How many names will be inputed? 3
Angelia Kho
Angel
Angelina

Sorted Names(Ascending):
1. Angel
2. Angelia Kho
3. Angelina

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    int a;
    char ignore;
    
    printf("How Many names will be inputed? ");
    scanf("%d", &a);
    
    scanf("%c", &ignore);
    
    char names [a][255];
    char temp [255];
    
    int j;
    for(j = 0; j < a; j++){
        scanf("%[^\n]", names[j]);
        scanf("%c", &ignore);
    }
    
    int i;
    
    for(i = 0; i<a; i++)
    {
        for(j = i; j<a; j++)
        {
            if(strcmp(names[i], names[j]> 0))
            {
                strcpy(temp, names[i]);
                strcpy(names[i], names[j]);
                strcpy(names[j],temp);
            }
        }
    }
    
    printf("\nsorted names(Ascending): \n");
    for(i = 0; i < a; i++)
    {
        printf("%d. %[^\n]", i, names[i]);
    }
}

错误是:

[Warning]passing argument 2 of 'strcmp' makes pointer from integer without cast

但是,它仍然有效,直到

目前用户输入输出:

How Many names will be inputed? 3
Angelia Kho
Angel
Angelina

--------------------------------
Process exited after 23.08 seconds with return value 3221225477
Press any key to continue...

I have task to make a program that can sort names.

The output of the program:

How many names will be inputed? 3
Angelia Kho
Angel
Angelina

Sorted Names(Ascending):
1. Angel
2. Angelia Kho
3. Angelina

This is my code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    int a;
    char ignore;
    
    printf("How Many names will be inputed? ");
    scanf("%d", &a);
    
    scanf("%c", &ignore);
    
    char names [a][255];
    char temp [255];
    
    int j;
    for(j = 0; j < a; j++){
        scanf("%[^\n]", names[j]);
        scanf("%c", &ignore);
    }
    
    int i;
    
    for(i = 0; i<a; i++)
    {
        for(j = i; j<a; j++)
        {
            if(strcmp(names[i], names[j]> 0))
            {
                strcpy(temp, names[i]);
                strcpy(names[i], names[j]);
                strcpy(names[j],temp);
            }
        }
    }
    
    printf("\nsorted names(Ascending): \n");
    for(i = 0; i < a; i++)
    {
        printf("%d. %[^\n]", i, names[i]);
    }
}

The error is:

[Warning]passing argument 2 of 'strcmp' makes pointer from integer without cast

But, it still working until user input

Output for now:

How Many names will be inputed? 3
Angelia Kho
Angel
Angelina

--------------------------------
Process exited after 23.08 seconds with return value 3221225477
Press any key to continue...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2025-02-03 15:36:10

对于这样的初学者来说,

scanf("%c", &ignore);

是多余的。您可以将它们删除,并更改SCANF以输入字符串的呼吁,以下面的方式

scanf( " %254[^\n]", names[j] );
       ^^^

注意格式字符串中的领先空间。它允许跳过白空间字符,例如新的行字符'\ n'在先前的调用scanf之后存储在输入缓冲区中。

在if语句中

if(strcmp(names[i], names[j]> 0))

有一个错字。表达式名称[J]&GT; 等于1

if(strcmp(names[i], names[j] ) > 0 )

printf("%d. %[^\n]", i, names[i]);

0

printf("%d. %s\n", i, names[i]);

具有类型int 不是气泡排序方法。它看起来像是选择排序的方法,并具有冗余掉期。

For starters calls like this

scanf("%c", &ignore);

are redundant. You may remove them and change the call of scanf for entering strings the following way

scanf( " %254[^\n]", names[j] );
       ^^^

Pay attention to the leading space in the format string. It allows to skip white space characters as for example the new line character '\n' stored in the input buffer after a preceding call of scanf.

In the if statement

if(strcmp(names[i], names[j]> 0))

there is a typo. The expression names[j] > 0 has the type int and is equal to 1.

You have to write

if(strcmp(names[i], names[j] ) > 0 )

Another typo in this call of printf

printf("%d. %[^\n]", i, names[i]);

Rewrote it like

printf("%d. %s\n", i, names[i]);

Also the used method of sorting is not the bubble sort method. It looks like the selection sort method with redundant swaps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文