[英]Alphabetically Ordering an array of words
我正在獨自學習C,為即將來臨的學期做准備,並且想知道到目前為止我的代碼在做什么錯。
如果事情看起來很奇怪,那是因為這是我正在創建的一個更大的排序函數包的一部分,以使您了解如何對數字,字母,數組等進行排序! 我目前在使用C中的字符串操作方面遇到了一些麻煩。
另外,目前我對C的知識非常有限!
我的主要內容包括:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int numbers[10];
int size;
int main(void){
setvbuf(stdout,NULL,_IONBF,0); //This is magical code that allows me to input.
int wordNumber;
int lengthOfWord = 50;
printf("How many words do you want to enter: ");
scanf("%i", &wordNumber);
printf("%i\n",wordNumber);
char words[wordNumber][lengthOfWord];
printf("Enter %i words:",wordNumber);
int i;
for(i=0;i<wordNumber+1;i++){ //+1 is because my words[0] is blank.
fgets(&words[i], 50, stdin);
}
for(i=1;i<wordNumber+1;i++){ // Same as the above comment!
printf("%s", words[i]); //prints my words out!
}
alphabetize(words,wordNumber); //I want to sort these arrays with this function.
}
我正在嘗試構建的排序“方法”如下! 該功能存在嚴重缺陷,但我認為我會盡可能保留所有這些功能,以向您顯示編寫此功能時的想法。
void alphabetize(char a[][],int size){ // This wont fly.
size = size+1;
int wordNumber;
int lengthOfWord;
char sortedWords[wordNumber][lengthOfWord]; //In effort for the for loop
int i;
int j;
for(i=1;i<size;i++){ //My effort to copy over this array for manipulation
for(j=1;j<size;j++){
sortedWords[i][j] = a[i][j];
}
}
//This should be kinda what I want when ordering words alphabetically, right?
for(i=1;i<size;i++){
for(j=2;j<size;j++){
if(strcmp(sortedWords[i],sortedWords[j]) > 0){
char* temp = sortedWords[i];
sortedWords[i] = sortedWords[j];
sortedWords[j] = temp;
}
}
}
for(i=1;i<size;i++){
printf("%s, ",sortedWords[i]);
}
}
我想我也有另一個問題...當我使用fgets()時,我正在做的事情是在數組的第一位得到一個空字。 最近,我遇到了其他問題,試圖以某些方式對scanf()char []進行專門的間隔,以使我輸入的單詞變量“神奇地”擺脫字符前的第一個空空格。 例如,使用scanf()編寫“ Hello”並獲取“ Hello”或“”“ Hello” ...
感謝對此的任何想法,我整個暑假都需要學習,因此不必急於回答! 另外,感謝您對整個堆棧溢出的過去提供的幫助。 這可能是我的第一篇文章,但過去兩年來我一直是常客,它一直是提供有用建議/提示的最佳場所之一。
如您所知,函數聲明無效。 您必須指定除第一個數組之外的每個數組維度的大小,因此您可以編寫:
void alphabetize(char a[][SOMESIZE], int size)
但是,您具有一個非恆定的第二維(因此您正在使用VLA或可變長度數組),這意味着您需要將這兩種尺寸都傳遞給函數,並在傳遞數組之前將它們傳遞給它們:
void alphabetize(int size, int length, char a[size][length])
然后調用它:
alphabetize(wordNumber, lengthOfWords, words);
當然,在嘗試調用該函數之前,還應該聲明該函數。
可能還有其他問題需要解決,但這是最直接的問題。 例如,您需要使用size
和length
來控制函數中的循環。 您可能不需要將數據復制到本地數組中(在這種情況下,您不需要本地數組)。 等等。
您應該考慮使用以下選項進行編譯:
gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition -Wold-style-declaration -Werror …
請注意,請注意,所有GCC版本均支持所有這些選項,但使用的數量與受支持的數量一樣。
你有:
int i;
for (i = 0; i < wordNumber + 1; i++) { //+1 is because my words[0] is blank.
fgets(&words[i], 50, stdin);
}
您正在超出數組的范圍,這可能會對代碼造成嚴重破壞。 第一個條目為空白,因為scanf()
將換行符留在輸入緩沖區中 。 在進入基於行的輸入之前,您應該先讀到行尾:
int c;
while ((c = getchar()) != EOF && c != '\n')
;
您還應該檢查fgets()
返回非空指針; 如果可以,請不要繼續。
您將喜歡上它-它是QSort的派生,適合您的情況。 如果沒有在這里或那里進行潤色,它可能對您不太理想(測試第一!):
void qsort (Strings[], NumberOfItems)
{
char Temp[51]; // Assumes your max string length of 50
int I1 = 0; // Primary index
int I2 = 0; // Index + 1
int NumberOfItems_1 = 0;
bool Swapped = false;
do // Outer loop
{
Swapped = false;
// Decrement our limit
NumberOfItems--;
// Save time not performing this subtraction many times
NumberOfItems_1 = NumberOfItems - 1;
// Repeatedly scan the list
for (I1 = 0; I1 < NumberOfItems_1; I1++)
{
// Save time not performing this addition many times
// I1 points to the current string
// This points to the next string
I2 = I1 + 1;
// If the current string is greater than the next string in the list,
// swap the two strings
if (strcmp(Strings[I1], Strings[I2]) > 0)
{
strcpy (Temp, Strings[I1]);
strcpy (Strings[I1], Strings[I2]);
strcpy (Strings[I2], Temp);
Swapped = true;
}
}
}
while (Swapped); // Break out when we've got nothing left to swap
}
我發現您的代碼有些錯誤。 首先,您將sortedWords聲明為多維數組(因為您已經sortedWords [wordnumber] [lengthofword],但是稍后嘗試將其僅與一個索引一起使用..這不起作用!另外,您傳遞2D數組是無效。請查看這篇文章,了解傳遞2D數組的有效方法: 將2D數組傳遞給C ++函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.