C++ 使用“修改文本”;结构";在C++;
我有一首诗:C++ 使用“修改文本”;结构";在C++;,c++,struct,C++,Struct,我有一首诗: 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. 我必须修改这个文本,使所有的行都以相同的位置结束。 使用空格的字符串补码中的位置数不足。这些空间必须平均分配 我知道我的代码非常庞大,但我必须在代码中使用“struct” 如何找到最长的字符串并在其他字符串
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.
我必须修改这个文本,使所有的行都以相同的位置结束。
使用空格的字符串补码中的位置数不足。这些空间必须平均分配 我知道我的代码非常庞大,但我必须在代码中使用“struct” 如何找到最长的字符串并在其他字符串中添加空格以执行任务 谢谢
#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;
}
#包括“stdafx.h”
#包括“iostream”
#包括
使用名称空间std;
结构诗{
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[]
{
结构诗v;
strcpy_s(v.row_one,“五月的蜂群”);
strcpy_s(v.row_two,“值得一负荷嘿;”);
strcpy_s(v.row_三,“六月的一群蜜蜂”);
strcpy_s(v.row_四,“值一银匙;”);
strcpy_s(v.row_five,“七月的一群蜜蜂”);
strcpy_s(v.row_六,“热得值得一飞。”);
cout您应该使用对象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());
//...
只是让我很恼火。我想把它改写为:
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_字符串。如果不是这样,那么使用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;
}
请看这里:
它是这样工作的:
排队阅读
找出每行的长度并计算其中的空格,找出最大长度
对于行,计算所需的空格数,找出每个单词所需的空格数,以及剩余的空格数
将每个字依次放入输出缓冲区
放入所需数量的空格
如果行未完成,则返回到4
null终止输出
如果未处理完整输入,则循环回3
台词都写好了
我是根据您的代码编写的,因此有几件事我会从头开始做:
line
struct而不是VERSE
- 每行
都包含其内容、空格数和长度
行
s存储在数组中-构成韵文
这可能就是这个任务的目的,但是现在你的算法运行起来应该很容易做到(我不会为你做所有的事情)不要使用交叉标记C和C++,语言也有很大的不同。你也应该使用<代码> STD::String < /Cord>代替C字符串。你可能还想使用<代码> STD::vector < /C>而不是固定的“行”。然后,代码变得非常简单。“使用空格的字符串中的位置数不足”我不理解这个。也从代码< >代码>结构> <代码>,C++不需要。你不允许使用<代码> STD::矢量< /代码>和<代码> STD::String < /C> >,如果这是一个练习,你就别无选择,只能手动检查<代码>结构> <代码>的每个成员,以获得它的长度,得到最大值,然后做同样,这一次,在“行”中添加空格的数量更少。@NeilKirk:他希望在单词之间插入新的空格字符,以补充现有字符,从而使每个句子的最终长度相同。
#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.