一个简单的 C++ 代码可以是:
std::ifstream input_file_stream( "file" );
unsigned counter = 0;
while( ++counter <= 80 && std::char_traits< char >::not_eof( input_file_stream.peek() ) ){
std::cout << char ( input_file_stream.get() );
if( counter == 80 ){
std::cout << '\n';
counter = 0;
}
}
每行输出长度为80个字符:
NOTE
而不是使用std::cout
您可以将其写入输出文件
这里还缺少一些单词,例如the
在第 2 行末尾和第 3 行开头。
因此,另一种解决方案可以是这样的:
input:
Is Lorem Ipsum just dummy text of the printing and typesetting industry? Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when
an unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.
C++代码:
std::ifstream input_file_stream( "file" );
unsigned max_size = 0;
for( std::string line; std::getline( input_file_stream, line ); ){
// find the longest line
if ( max_size < line.size() ) max_size = line.size();
}
input_file_stream.clear();
input_file_stream.seekg( 0 , std::ios_base::beg ); // rewind
for( std::string line; std::getline( input_file_stream, line ); ){
if( line.size() == max_size ){
std::cout << line << '\n';
} else if ( line.size() > 70 ) {
line.insert( line.rfind( ' ' ),std::string( max_size - line.size() ,' ' ) );
std::cout << line << '\n';
} else {
std::cout << line << '\n';
}
}
input_file_stream.close();
output:
Is Lorem Ipsum just dummy text of the printing and typesetting industry? Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when
an unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.
怎么运行的
这太容易了。首先你需要找到最长的线81然后根据这条最长的线,您可以使用以下方法来调整其他线line.rfind
(意味着找到最后一件事)和line.insert
插入你想要的内容。每条线的长度由您决定。这里我用的是81到70之间。
每行都基于此行进行合理调整:
一位不知名的印刷商拿走了一系列的字体并将其打乱以制作字体样本
但这里的输出并不漂亮,因为所有空间都聚集在最后。为此你可以传播所有空间中的其他空间之间else if
像这样的部分:
} else if ( line.size() > 70 ) {
unsigned need_space = max_size - line.size();
unsigned count_space = std::count( line.begin(), line.end(), ' ' );
std::istringstream iss( line );
std::string temp_line;
while( need_space-- && count_space-- ){
std::string temp;
iss >> temp;
temp_line += temp + "__";
}
iss.get(); // get rid of a single space in the iss stream
// extracts the rest of the iss stream:
temp_line.append( std::istreambuf_iterator<char>( iss ), std::istreambuf_iterator<char>() );
std::cout << temp_line << '\n';
} else {
std::cout << line << '\n';
}
输出:
Is__Lorem__Ipsum__just dummy text of the printing and typesetting industry? Lorem
Ipsum__has__been__the__industry's__standard dummy text ever since the 1500s, when
an unknown printer took a galley of type and scrambled it to make a type specimen
book.__It has survived not only five centuries, but also the leap into electronic
typesetting,__remaining__essentially__unchanged.__It was popularised in the 1960s
with__the__release__of__Letraset sheets containing Lorem Ipsum passages, and more
recently__with__desktop__publishing__software__like__Aldus__PageMaker__including
versions of Lorem Ipsum.
正如你在这里看到的,我使用了__
代替2-spaces
显示输出上发生的情况,因此您可以将其更改为 2 个空格。
尽管代码仍然存在一些问题,例如最后一行这并不像其他人那样固定,但我不想使代码变得复杂,我认为这对于大多数情况来说就足够了。