簡體   English   中英

為什么我的字符排序程序會產生錯誤的答案?

[英]Why does my character sorting program produce wrong answers?

我正在嘗試在 C 中創建一個程序,其中用戶將隨機字符串插入矩陣。 然后程序使用這個公式來評估每個字符串和代碼的 rest 以顯示具有最高值的字符串。 隨后的每個字母都比前一個字母具有更大的值。

如果有人能指出我正確的方向,我會很高興,目前程序顯示字符串中的一些隨機字母。

公式: Sum=(t*26^(n-1))

t = 字母數,n = 剩余字母數

示例: abc -> 1*26^2+2*26^1+3*26^0

rest的代碼:

#include <stdio.h>
#include <string.h>
void insert(int, char[10][10], int num[]);
int computing(char[10]);
void sort(int, int num[], char[10][10]);
int main(){
    int x;
    char arr[10][10];
    int num[x];
    printf("How many words do you wish to enter: \n");
    scanf(" %d", &x);
    insert(x, arr, num);
    sort(x, num, arr);
return 0;
}
void insert(int x, char arr[10][10], int num[]){
    int i, r;
    for(i=0; i<x; i++){
        printf("Insert %d. word: ", i+1);
        scanf("%s", &arr[i][0]);
        num[i] = computing(arr[i]);
    }
}
int computing(char arr[10]){
    int n, i, t=1, m=0, k;
    n = strlen(arr);
    k = n;
    for(i=0; i<n; i++){ 
        m += (t*26^(k-1));
        t++;
        k = k - 1;
    }
return m;
}
void sort(int x, int num[], char arr[10][10]){
    int i, temp;
    char ch;
        for(i = 0; i < x - 1; i++){
            if(num[i] > num[i+1]){
                temp = num[i];
                num[i] = num[i+1];
                num[i+1] = temp;
        
                ch = arr[i][0];
                arr[i][0] = arr[i+1][0];
                arr[i+1][0] = ch;
            }
        }
    printf("Word with the biggest sum is: %s\n", &arr[x-1][0]);
}

問題:

  1. ^不是電源 function
  2. int num[x]; 高於scanf(" %d",&x); 所以x未初始化。 移動int num[x]; scanf下方
  3. 你的排序壞了。 即使有上述修復,它也會破壞我提供的測試數據條目。
  4. 排序需要對數據執行 N 次傳遞。 目前,它只做一個。

定義一個同時包含單詞 string它的num分數的struct要容易得多。 然后,排序不必只交換兩個arrays 一個。


因此,我重構了代碼以使用這樣的struct並修復了排序。 注釋如下:

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

typedef struct {
    int num;                            // ranking
    char str[10];                       // word string
} word_t;

void insert(int, word_t num[]);
int computing(word_t *);
void sort(int, word_t num[]);

int
main(void)
{
    int x;
// NOTE/BUG: x is _not_ yet initialized
#if 0
    char arr[10][10];
    int num[x];
#endif

    printf("How many words do you wish to enter: \n");
    scanf(" %d", &x);

// NOTE/FIX: x is now initialized
#if 1
    word_t words[x];
#endif

    insert(x, words);

    sort(x, words);

    return 0;
}

void
insert(int x, word_t *words)
{
    int i;

    for (i = 0; i < x; i++) {
        word_t *word = &words[i];

        printf("Insert %d. word: ", i + 1);
        scanf("%s", word->str);

        word->num = computing(word);
        printf("DEBUG: num=%d '%s'\n",word->num,word->str);
    }
}

int
computing(word_t *word)
{
    int n, i,
        t = 1,
        m = 0,
        k;

    n = strlen(word->str);
    k = n;

// NOTE/FIX: compute pow(26,k - 1);
#if 1
    int p26 = 1;
    for (i = 1; i <= (k - 1); ++i)
        p26 *= 26;
#endif

    for (i = 0; i < n; i++) {
#if 0
        m += (t * 26 ^ (k - 1));
#else
        m += (t * p26);
#endif
        t++;

        k = k - 1;
// NOTE/FIX: recalc pow(26,k - 1) now that we've decremented k
#if 1
        p26 /= 26;
#endif
    }

    return m;
}

void
sort(int x, word_t *words)
{
    int i;
    word_t temp;
    int more = 1;

    while (more) {
        more = 0;

        for (i = 0; i < x - 1; i++) {
            word_t *lhs = &words[i];
            word_t *rhs = &words[i + 1];

            if (lhs->num > rhs->num) {
                temp = *lhs;
                *lhs = *rhs;
                *rhs = temp;
                more = 1;
            }
        }
    }

    printf("Word with the biggest sum is: %s\n", words[x - 1].str);
}

這是我使用的輸入數據:

7
abc
defg
hijkl
mnopqr
hello
world
gbye

請注意,在原始代碼中,程序打印出mbye作為答案


這是程序 output:

How many words do you wish to enter:
Insert 1. word: DEBUG: num=731 'abc'
Insert 2. word: DEBUG: num=19010 'defg'
Insert 3. word: DEBUG: num=494265 'hijkl'
Insert 4. word: DEBUG: num=12850896 'mnopqr'
Insert 5. word: DEBUG: num=494265 'hello'
Insert 6. word: DEBUG: num=494265 'world'
Insert 7. word: DEBUG: num=19010 'gbye'
Word with the biggest sum is: mnopqr

暫無
暫無

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

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