我有一個任務,基本上是要求在給定行長的情況下證明段落的合理性。例如“我是 C 的學生,這是我的第一個作業。我希望我能按時完成”。給定的 17 行長度應如下所示:
output
I am a student of
C, this is my
first assignment.
I hope I finish
on time.
我無法在單詞之間動態放置間距。我目前有一個函式可以計算段落中的單詞并將它們存盤到二維陣列中,但我不知道如何 a) 計算單詞之間的間距量和 b) 如何動態列印對齊的段落。這是我到目前為止的代碼:
int getAllWordsFrom2DArray(char *paragraph, char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH]) {
int i,j,totalWords = 0;
for(i=0; i < strlen(paragraph); i ) {
int wordLength;
if (paragraph[i] == ' ' || paragraph[i 1] == '\0') {
totalWords ;
wordLength = i;
for(j=0; j < wordLength; j ) {
words[i][j] = paragraph[j];
}
}
}
printf("%s", words);
return totalWords;
}
//Code in progress
int getNumberOfWordsForNextLine(int totalWords, int lineLength, char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH]) {
int wordsForNextLine = 0;
for(int i=0; i < totalWords; i ) {
wordsForNextLine = 0 ;
}
}
//code in progress
void printNextLine(int wordsForNextLine) {
}
//skeleton code provided by instructor
void justifyAndPrintParagraph(char* paragraph, int lineLength) {
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH];
int totalWords = getAllWordsFrom2DArray(paragraph, words);
int processedWords = 0;
while (processedWords < totalWords) {
int wordsForNextLine = getNumberOfWordsForNextLine(totalWords, lineLength, words);
printNextLine(wordsForNextLine);
processedWords = wordsForNextLine;
}
}
澄清一下,我們不允許使用 strlok。從本質上講,我們應該只使用基礎知識來做這件事。我需要使用 void justifyAndPrintParagraph 函式和簽名,但除此之外我可以自由地做任何事情。
編輯:我忘了補充一點,如果空間不能被平均分配,那么額外的空間將被從左到右分配。
任何幫助是極大的贊賞。
uj5u.com熱心網友回復:
考慮您必須分配多少空間。例如,給定輸入:
18
I am the very model of a modern Major-General.
計算適合該行的單詞數:
"I" "am" "the" "very" (4-1 words) --> 13
"I" "am" "the" "very" "model" (5-1 words) --> 19
所以只有前 4 個單詞適合 18 個字符的行。然后很容易計算要分配的空格字符數:
N = max_line_width - sum_of_word_lengths
現在是困難的部分:每個單詞之間有多少空格?你的作業要求你從左到右分配額外的不平衡空格,這意味著每對單詞可能有不同數量的空格字符。
但是,差異始終是單個空格字符。花點時間說服自己這是真的:
I···am···the··very
-2-4-6-8-0-2-4-6-8
在我們的小例子中,我們發現前兩個字間距中有三個空格字符,最后兩個空格字符。
每個字間距的最小空格字符數很容易計算:
nsp = N / (number_of_words_in_line - 1)
謹防!如果你只有一個單詞會發生什么?(你真的需要為這樣的一行分配空間嗎?)
現在,對于很酷的棘手數學部分,您可以計算需要在單詞間距中添加空格的次數:
nplus1 = N - nsp * (number_of_words_in_line - 1)
要不就:
nplus1 = N % (number_of_words_in_line - 1)
請記住,所有單詞間距可能是相同數量的空格字符,甚至可能恰好是一個空格字符。請注意我們的計算在這些情況下是如何作業的。
現在您可以回圈列印該行的nsp單詞,在每個單詞后添加空格字符,并在第一個單詞后添加額外的空格nplus1。
請記住,該行的最后一個單詞沒有任何空格。后面是換行符!
希望這可以幫助您完成這項任務。(我個人認為這是你第一次介紹 C 類的任務有點粗心。)
現在,如果我犯了錯誤,那是因為我非常非常困。如果我有,肯定有人會指出。
uj5u.com熱心網友回復:
因此,使用 Dúthomhas 的建議,我能夠創建以下函式:
void justifyAndPrintLine(char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LENGTH], int processedWords, int amountOfWordsForNextLine, int lineLength) {
int total = 0;
for (int i = processedWords; i < processedWords amountOfWordsForNextLine; i ) {
total = (int) strlen(words[i]);
}
int spaces = lineLength - total;
int spacesBetweenWords = spaces / (amountOfWordsForNextLine - 1);
int spacesRemaining = spaces % (amountOfWordsForNextLine - 1);
int spaceForThisWord;
int leftWords = processedWords amountOfWordsForNextLine;
while (processedWords != leftWords) {
spaceForThisWord = spacesBetweenWords;
if (spacesRemaining > 0) {
spaceForThisWord ;
spacesRemaining--;
}
printLine(words[processedWords], spaceForThisWord);
processedWords ;
}
}
我對數學理解的一個關鍵部分是間距的差異總是指向一個空格字符。借用他的數學,我能夠正確地證明這一段。再次感謝 Dúthomhas!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418871.html
標籤:
上一篇:使用PHP在特定點將字串插入字串
下一篇:如何檢測字串中沒有空格的關鍵字?
