下面的Uni项目。 在// ###部分中,我添加了一个检查,确定是否需要一个额外的空格来确保最后一个字符在最后排成一行。 但是,检查是为了找到字符串的结尾,但它似乎在第一个空格处停止..
输出
Enter the width of the column : 40
Enter a line a text : the black cat
* * * *** Num = 4
1234567890123456789012345678901234567890
the black cat
我错过了什么,实际上是在提早结束字符串吗?或者我的支票有缺陷吗?
/*
Write a program that reads in the width of the columns in a newspaper and then a line of text.
Justify the line of text to fit into a column of that width.
When your program is running, the screen should look something like this:
Enter the width of the column: 40
Enter a line of text: Good morning how are you?
12345678901234567890123456789012345678901234567890...
Good morning how are you?
The justification is done by counting the number of gaps in the text. In the above example, there are 4 gaps.
Then each gap must have spaces added to it.
The number of extra spaces must be shared out as evenly as possible.
In the above example, the first three gaps have 5 spaces each and the last gap has 4 spaces.
Notes:
1. If the text is longer than the column then you must report an error – don't try and break it into two lines!
2. Assume that the text will have more than one word in it.
3. Note the header line consisting of 123456789012345678.... this is useful to check your result.
You can make this header line as long as you like – 70 spaces would be a useful length.
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int width, length, difference, n, x, b;
int num, i, spaces, words, requiredSpaces;
char sentence[100];
char temp[100];
int main () {
// reads in column width
printf ("Enter the width of the column : ");
scanf ("%d", &width);
getchar(); // used to remove Enter from the gets below
// reads in the string
while (true) {
printf("Enter a line a text : ");
gets(sentence);
length = strlen(sentence);
if (length > width) {
printf ("Error, please enter a line of text shorter than the column width\n");
} else {
break;
}
}
// calculates the difference between width and length
difference = width - length;
// printf ("length is %d\ndifference is %d\n", length, difference);
// count how many words
spaces = 0;
for (i = 0; i < length; i++) {
if (sentence[i] == ' ') {
spaces++;
}
}
words = spaces + 1;
// printf ("spaces %d\nwords %d\n", spaces, words);
// calculates the required spaces between each word
requiredSpaces = difference / spaces;
// printf ("Required spaces %d\n", requiredSpaces);
i = 0;
n = 0;
for (x = 0; x < words; x++) {
while (sentence[i] != ' ') {
if (sentence[i] == '\0') {
break;
} else {
temp[n] = sentence[i];
i++;
n++;
}
}
i++;
n++;
for (b = 0; b < requiredSpaces; b++) {
temp[n] = ' ';
n++;
}
}
n++;
temp[n] = '\0';
printf ("")
// ###################################################################################################################
// if an odd number of elements in the string, this will add one space to the first space gap
if (length % 2 == 1) {
// counts the number of the elements in the array
i = 0;
while (temp[i] != '\0') {
i++;
printf("* ");
}
i++; // this makes sure that the \0 is also moved up one
num = i;
}
printf (" *** Num = %d\n\n", num);
// ###################################################################################################################
i = 0;
printf ("1234567890123456789012345678901234567890\n");
while (i < width) {
printf ("%c", temp[i]);
i++;
}
return 0;
}
答案 0 :(得分:3)
使用此行
// calculates the difference between width and length
difference = width - length;
您似乎忘记length
已包含多个空格。
使用此行
// calculates the required spaces between each word
requiredSpaces = difference / spaces;
你似乎忘了可能会发生一些舍入。你不能在每个单词后添加相同数量的空格。
您必须准确计算出所需的空间数量。像
这样的东西spaces_needed = width - length + words - 1; // + words - 1 because that is the
// number of spaces already in the string
然后您需要将这些空格分配到words-1
个洞中。每个空格的数量不一定相同。
例如,对于3个单词而length
等于13,您将需要总共29个空格(40 - 13 + 3 - 1)。你只有两个洞,所以你必须把15个放在一个,14个放在另一个。
提示强>
当你有spaces_needed
时
spaces_to_add_after_each_word_except_the_last = spaces_needed/(words-1);
remaining_spaces_to_distribute = spaces_needed % (words-1);
变量remaining_spaces_to_distribute
表示需要添加多少额外空格。在第一个单词后面加1。第二个单词之后的另一个单词,依此类推,直到你添加了所有额外的空格。