簡體   English   中英

用冒泡排序算法排序字符串

[英]sorting strings with bubble sort algorithm

此代碼存儲整數變量中的單詞數,然后多維數字符串中的單詞使用函數按字母順序對單詞進行排序。 我遇到的問題是在函數調用中。

#include<stdio.h>
#include<string.h>
void sort(char*[50],int);
int main ()
{
    int i,n=0;
    char s[50][10];

    scanf("%d",&n);//scaning the number of words

    for(i=0;i<=n;i++)//scaning the words
    gets(s[i]);

    sort(s,n);

    for(i=0;i<=n;i++)//printing the words sorted
    printf("%s\n",s[i]);
}
void sort(char*s[50],int n)
{
    int i,j,cmp;
    char tmp[1][10];

    //bubble sorting of words
    for(i=0; i<n; i++)
        for(j=0; j<n-1; j++)
        {
            cmp=strcmp(s[j],s[j+1]);

            if(cmp>0)
            {
                strcpy(tmp[0],s[j+1]);
                strcpy(s[j+1],s[j]);
                strcpy(s[j],tmp[0]);
            }   
        }
}

打開你的警告。 char s[50][50]不能轉換為char*

嘗試這個:

void sort(char(*)[50],int);

這告訴編譯器傳入指向至少一個50個字符的緩沖區的指針。 這是多維數組在傳遞給函數時衰減的內容。

並且為了進一步擺脫“ char[]char*相同的愚蠢概念”,由於某種原因仍然在各地教授,請閱讀: http//c-faq.com/aryptr/aryptr2.html

你很親密 這是您的代碼的工作版本。 請注意, scanf()從... fgets()的數字中留下換行符,因為你不會再使用gets() ,是嗎? ...所以你需要在scanf()之后閱讀並包括換行符。 當然,有一個有趣的問題是“為什么人們認為人類計算比計算機數更好呢?” 如果不打擾計數會更明智。 請注意,修訂后的代碼驗證n以確保它不大於50。

修改后的代碼適用於長度為9或更短的行

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

void sort(char s[50][10], int);

int main(void)
{
    int i;
    int n = 0;
    char s[50][10];
    char line[11];

    if (scanf("%d", &n) != 1)
    {
        fprintf(stderr, "Failed to read a number\n");
        return 1;
    }
    if (n <= 0 || n > 50)
    {
        fprintf(stderr, "%d is out of the range 1..50\n", n);
        return 1;
    }
    // Gobble rest of first line
    while ((i = getchar()) != EOF && i != '\n')
        ;

    for (i = 0; i < n; i++)
    {
        if (fgets(line, sizeof(line), stdin) == 0)
            break;
        // Remove newline from input
        size_t len = strlen(line);
        assert(len > 0 && len <= sizeof(s[i]));
        line[len-1] = '\0';
        strcpy(s[i], line);
    }
    n = i;  // In case the file was shorter than stated!

    printf("Before:\n");
    for (i = 0; i < n; i++)
        printf("%s\n", s[i]);

    sort(s, n);

    printf("After:\n");
    for (i = 0; i < n; i++)
        printf("%s\n", s[i]);

    return 0;
}

void sort(char s[50][10], int n)
{
    int i, j, cmp;
    char tmp[10];

    if (n <= 1)
        return; // Already sorted

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n-1; j++)
        {
            cmp = strcmp(s[j], s[j+1]);

            if (cmp > 0)
            {
                strcpy(tmp, s[j+1]);
                strcpy(s[j+1], s[j]);
                strcpy(s[j], tmp);
            }
        }
    }
}

此代碼將行讀入字符串足夠長的時間,最多可占用9個數據字符,換行符和終止值為null。 它刪除換行符,最多留下9個數據字符,終止為null。

樣品運行:

Before:
Number 34
Number 39
Number 32
Number 30
Number 22
Number 34
Number 57
Number 28
Number 30
Number 47
Number 43
Number 23
Number 22
After:
Number 22
Number 22
Number 23
Number 28
Number 30
Number 30
Number 32
Number 34
Number 34
Number 39
Number 43
Number 47
Number 57

適用於長度為8或更短的行的原始代碼

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

void sort(char s[50][10], int);

int main(void)
{
    int i;
    int n = 0;
    char s[50][10];

    if (scanf("%d", &n) != 1)
    {
        fprintf(stderr, "Failed to read a number\n");
        return 1;
    }
    if (n <= 0 || n > 50)
    {
        fprintf(stderr, "%d is out of the range 1..50\n", n);
        return 1;
    }
    // Gobble rest of first line
    while ((i = getchar()) != EOF && i != '\n')
        ;

    for (i = 0; i < n; i++)
    {
        if (fgets(s[i], sizeof(s[i]), stdin) == 0)
            break;
        // Remove newline from input
        size_t len = strlen(s[i]);
        assert(len > 0);
        s[i][len-1] = '\0';
    }
    n = i;  // In case the file was shorter than stated!

    printf("Before:\n");
    for (i = 0; i < n; i++)
        printf("%s\n", s[i]);

    sort(s, n);

    printf("After:\n");
    for (i = 0; i < n; i++)
        printf("%s\n", s[i]);

    return 0;
}

void sort(char s[50][10], int n)
{
    int i, j, cmp;
    char tmp[10];

    if (n <= 1)
        return; // Already sorted

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n-1; j++)
        {
            cmp = strcmp(s[j], s[j+1]);

            if (cmp > 0)
            {
                strcpy(tmp, s[j+1]);
                strcpy(s[j+1], s[j]);
                strcpy(s[j], tmp);
            }
        }
    }
}

“大”變化是聲明和定義函數數組參數的方式。 您傳遞的是50行的數組,每行10個字符,因此只需在函數中指定。 您可以從函數參數的維度中刪除50,而不會更改程序的行為。

樣本輸入:

8
fed
abc
cba
def
hij
cba
xyz
aaa

示例運行:

$ ./srt < data
Before:
fed
abc
cba
def
hij
cba
xyz
aaa
After:
aaa
abc
cba
cba
def
fed
hij
xyz
$

需要修改的事實表明了測試極限的重要性(並仔細定義了極限)。

修訂后的代碼仍然不是通用代碼。 最多50行輸入的固定限制,作為輸入一部分所需的行數,以及每行最多10個字符的固定行長度都使其成為玩具代碼。 因此,GIGO(垃圾進,垃圾出)不是不合理的反應。 如果數據文件包含超長行,則可以獲得所獲得的內容。 代碼不會崩潰,但輸出可能沒有多大意義。

更改

void sort(char*[50],int);
...
void sort(char*s[50],int n)

void sort(char(*)[10],int);//not char(*)[50]
...
void sort(char(*s)[10],int n)

//remain newline
scanf("%d",&n);

scanf("%d%*c",&n);//read and drop newline

所以改變

for(i=0;i<=n;i++)

for(i=0;i<n;i++)

要么

char *p[50];
for(i=0;i<n;++i)
    p[i]=&s[i][0];
sort(p,n);//OK only exchange of pointer in this case

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM