我有以下经文:
A swarm of bees in May
Is worth a load hey;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is hot a worth a fly.
我必须修改此文本,以便所有行以相同的位置结束。 使用空格填充字符串中的位置数量不足。这些空间必须均匀分配 我知道我的代码非常笨重,但我必须在代码中使用"结构"
如何找到最长的字符串并在其他字符串中添加空格来执行任务?
谢谢!
#include "stdafx.h"
#include "iostream"
#include <string.h>
using namespace std;
struct VERSE {
char row_one[25];
char row_two[25];
char row_three[25];
char row_four[25];
char row_five[25];
char row_six[25];
};
int _tmain(int argc, _TCHAR* argv[])
{
struct VERSE v;
strcpy_s(v.row_one, "A swarm of bees in May");
strcpy_s(v.row_two, "Is worth a load hey;");
strcpy_s(v.row_three, "A swarm of bees in June");
strcpy_s(v.row_four, "Is worth a silver spoon;");
strcpy_s(v.row_five, "A swarm of bees in July");
strcpy_s(v.row_six, "Is hot a worth a fly.");
cout << v.row_one << endl << v.row_two << endl << v.row_three << endl
<< v.row_four << endl << v.row_five << endl << v.row_six << endl;
cout << strlen(v.row_one) << endl;
cout << strlen(v.row_two) << endl;
cout << strlen(v.row_three) << endl;
cout << strlen(v.row_four) << endl;
cout << strlen(v.row_five) << endl;
cout << strlen(v.row_six) << endl;
//the length of row
/*
int length = 0;
for(int i = 0; v.row_two[i] != '\0'; i++) {
length++;
}
printf("Length of second row is: %d\n", length);
*/
return 0;
}
答案 0 :(得分:1)
A swarm of bees in May
Is worth a load hey;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is hot a worth a fly.
让我烦恼。我想把它重写为:
A swarm of bees in May
Is worth a load of hay;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is not worth a fly.
无论如何,在那之后:
我正在写这个答案,假设这是一个作业并且你被告知要使用c_strings。如果不是这种情况,那么使用std::string
会更容易。
无论如何,我已经提出了这个代码:
#include "iostream"
#include <string.h>
using namespace std;
const int maxrowlength = 25, maxrowcount = 6;
struct VERSE {
char rows[maxrowcount][maxrowlength];
int spaces[maxrowcount];
int line_length[maxrowcount];
};
char* get_row(VERSE &v, int row)
{
return &v.rows[row][0];
}
int main()
{
struct VERSE v;
strcpy(get_row(v,0), "A swarm of bees in May");
strcpy(get_row(v,1), "Is worth a load hey;");
strcpy(get_row(v,2), "A swarm of bees in June");
strcpy(get_row(v,3), "Is worth a silver spoon;");
strcpy(get_row(v,4), "A swarm of bees in July");
strcpy(get_row(v,5), "Is hot a worth a fly.");
//calculate lengths and count spaces
int max_space_count = 0;
for (size_t i = 0; i < maxrowcount; i++)
{
char* line = get_row(v,i);
/*/we could find the length with strlen() and spaces with memchr() but
that will involve traversing the string multiple times (at least twice)
we can do better
/*/
v.line_length[i] = 0;
v.spaces[i] = 0;
while(*line)
{
v.line_length[i]++;
if(*line == ' '){v.spaces[i]++;}
line++;
}
if (v.line_length[i] > max_space_count){max_space_count = v.line_length[i];}
}
for (size_t i = 0; i < maxrowcount; i++)
{
int length_diff = max_space_count - v.line_length[i];
int spaces_to_add = v.spaces[i]?length_diff / v.spaces[i]:0; //number of spaces to add every word
int extra_spaces = v.spaces[i]?length_diff % v.spaces[i]:0; //extra spaces to add to make line fit
char output[maxrowlength];
char* current_output = output;
char* current_word = get_row(v,i);
char* current_word_end = current_word;
while(*current_word)
{
current_word_end++;
if (*current_word_end == ' ' || *current_word_end == '\0')
{
//write word to output
strncpy(current_output, current_word, current_word_end - current_word);
//update pointer to end of new word
current_output += current_word_end - current_word;
//write in the number of new spaces needed
if (*current_word_end == ' ')
{
for (int j = 0; j < spaces_to_add; j++)
{
*current_output = ' ';
current_output++;
}
//if extra spaces are needed, add those too
if (extra_spaces)
{
extra_spaces--;
*current_output = ' ';
current_output++;
}
}
//step current word to look at the next word
current_word = current_word_end;
}
}
//null terminate
*current_output = '\0';
strcpy(get_row(v,i),output);
}
for (size_t i = 0; i < maxrowcount; i++)
{std::cout << get_row(v,i) << std::endl;}
return 0;
}
输出:
A swarm of bees in May
Is worth a load hey;
A swarm of bees in June
Is worth a silver spoon;
A swarm of bees in July
Is hot a worth a fly.
在此处查看: On Ideone
它的工作原理如下:
我是根据你的代码编写的,所以我可以从头开始做一些不同的事情:
line
结构而不是您的VERSE
line
包含其内容,空格数和长度line
要存储在一个数组中 - 形成一节经文这可能就是这个任务的目的,但是现在你的算法很有效(我不会为你做一切);-)
答案 1 :(得分:0)
您应该使用对象std::string
来存储您的行。
然后,使用函数std::max
:
unsigned long int max_size = std::max(str1.length(),str2.length());
max_size = std::max(max_size, str3.length());
max_size = std::max(max_size, str4.length());
//...