gpt4 book ai didi

c++ - 基本 C++ 文本对齐

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:53 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序从文件中获取输入行并使其恰好为 80 个字符(假设输入行始终小于 80),然后打印该行。这是通过在以下标点符号后添加最多两个空格来完成的:.,!;?如果一行少于 41 个字符,则不加修改地打印。

如果该行仍然不是 80 个字符,则可以在整行中添加随机空格,对此统一性无关紧要。

输入文件是:Lorem Ipsum 只是打印和排版行业的虚拟文本吗?定理自 1500 年代以来,Ipsum 一直是行业的标准虚拟文本,当时不知名的打印机拿了一个活字样,然后把它打乱做成活字样书。它不仅存活了五个世纪,而且还跃入了电子领域排版,基本保持不变。它在1960年代流行随着包含 Lorem Ipsum 段落等的 Letraset 表的发布最近使用桌面出版软件,如 Aldus PageMaker,包括Lorem Ipsum 的版本。

输出文件应如下所示:

这个。注意每一行是如何对齐的

enter image description here

这是我的代码:

const int maxLine = 80;

ifstream fin("unjustified.txt");
ofstream fout("justified.txt");

void randomSpace(string &input);
void spaceAdder(string &input);

int main() {

string inputLine;

while (getline(fin, inputLine)) {

if (inputLine.size() > 40)
{
spaceAdder(inputLine);
}

else {
fout << inputLine << endl;

}
}

system("pause");
fin.close();
fout.close();
return 0;
}

void spaceAdder(string &input) {



int perPos = input.find('.');
while (perPos != string::npos && input.size() < maxLine) {
input.insert(perPos + 1, " ");
perPos = input.find('.', perPos + 1);
}


int commaPos = input.find(',');
while (commaPos != string::npos && input.size() < maxLine) {
input.insert(commaPos + 1, " ");
commaPos = input.find(',', commaPos + 1);
}


int questPos = input.find('?');
while (questPos != string::npos && input.size() < maxLine) {
input.insert(questPos + 1, " ");
questPos = input.find('?', questPos + 1);
}


int semiPos = input.find(';');
while (semiPos != string::npos && input.size() < maxLine) {
input.insert(semiPos + 1, " ");
semiPos = input.find(';', semiPos + 1);
}


int exclamPos = input.find('!');
while (exclamPos != string::npos && input.size() < maxLine) {
input.insert(exclamPos + 1, " ");
exclamPos = input.find('!', exclamPos + 1);
}


if (input.size() < maxLine) {
randomSpace(input);


}
else
fout << input << endl;


}



void randomSpace(string &input) {

srand(time(0));

while (input.size() < maxLine) {


int spacePos = input.find(" ");
bool i = (rand() % 2 == 1) ? true : false;

if (i = true && spacePos != string::npos)
{
input.insert(spacePos, " ");
spacePos = input.find(" ", spacePos + 1);

}

}

fout << input << endl;
}

但是我的代码创建的输出是:

这个。请注意第 2 行和第 4 行如何与文本的其余部分不对齐

enter image description here

我这辈子都弄不明白我的代码有什么问题。请指出正确的方向!谢谢

最佳答案

一个简单的 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个字符:

enter image description here


注意

除了使用 std::cout,您还可以将其写入输出文件
此外,这里还缺少一些单词,例如第 2 行末尾和第 3 行开头的 the
因此,另一种解决方案可能是这样的:

输入:

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();

输出:

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(意味着最后找到thing) 和 line.insert 来插入你想要的东西。每行的长度由您决定。这里我使用了 81 到 70 之间的值。

每一行都基于这一行对齐:

an unknown printer took a galley of type and scrambled it to make a type specimen

但是这里的输出并不漂亮,因为所有空间都聚集在最后。为此,您可以在 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 个空格。虽然代码仍然有一些问题,例如 the line before last 没有像其他的那样固定,但我不想让代码变得复杂,我认为这对大多数情况来说已经足够了。

关于c++ - 基本 C++ 文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com