#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
using namespace std;
class Book
{
public:
Book(string format)
{
this->book_format = format;
setPageStyle(book_format);
this->FirstLineChar = '=';
this->cur_line = 0;
this->isNewPage=this->isNewLine = true;
};
~Book(){};
void Print_book(ifstream &input,ofstream &output);
private:
string book_format;
int line_max;
int width_max;
int cur_line;
char FirstLineChar;
bool isNewPage;
bool isNewLine;
//output page first line
void print_Page1stLine(ofstream &output);
//output empty line
void print_EmptyLine(ofstream &output);
// set page style A4 A3
void setPageStyle(string book_format );
void readLine(ofstream &output,string line);
void print_Line(ofstream &output,string s);
string RemovePreAndLastSpace(string str);
void string_replace(string&s1,const string&s2,const string&s3);
int Book::find_split(string str);
};
以上 book.h 文件
#include "Book.h"
using namespace std;
void Book::Print_book(ifstream &input,ofstream &output)
{
string line,word,tmp;
while( getline(input,tmp) )
{
// first line
//cout <<line<<endl;
if(isNewPage)
print_Page1stLine(output);
// new line
string_replace(tmp,"\t"," ");
line = RemovePreAndLastSpace(tmp);
//string_replace(tmp,"\t"," ");
if(line == "")
{
if(isNewLine&&!isNewPage)
print_EmptyLine(output);
isNewLine = false;
}
else
{
readLine(output,line);
isNewLine = true;
}
}
}
void Book::print_EmptyLine(ofstream &output)
{
output<<endl;
cur_line++;
isNewLine = false;
if(cur_line == line_max)
{
isNewPage = true;
cur_line = 0;
}
}
void Book::print_Page1stLine(ofstream &output)
{
for(int i = 0;i<width_max;i++)
output<<FirstLineChar;
output<<endl;
isNewPage = false;
isNewLine = false;
cur_line++;
}
void Book::print_Line(ofstream &output,string s)
{
if(isNewPage)
{
print_Page1stLine(output);
}
if(s=="")
return;
output<<s<<endl;
cur_line++;
if(cur_line == line_max)
{
isNewPage = true;
isNewLine = false;
cur_line = 0;
}
}
void Book::setPageStyle(string book_format )
{
if(!book_format .compare("A4") )
{
this->line_max = 210/2;
this->width_max = 297/3;
}
else if(!book_format .compare("A3") )
{
this->line_max = 297/2;
this->width_max = 420/3;
}
else if(!book_format .compare("TEST") )
{
this->line_max = 100;
this->width_max = 105;
}
else //default A4
{
this->line_max = 210/2;
this->width_max = 297/3;
}
}
string Book::RemovePreAndLastSpace(string str)
{
int length = str.size();
int i = 0, j = length -1;
while(i < length && isspace(str[i] ) ){
i++;
}
while(j >= 0 && isspace(str[j])){
j--;
}
if(j<i)
return "";
return str.substr(i,j-i+1);
}
int Book::find_split(string str)
{
int length = str.size();
int j = length -1;
while(j >= 0&&!isspace(str[j])){
j--;
}
return j;
}
void Book::string_replace(string&s1,const string&s2,const string&s3)
{
string::size_type pos=0;
string::size_type a=s2.size();
string::size_type b=s3.size();
while((pos=s1.find(s2,pos))!=string::npos)
{
s1.replace(pos,a,s3);
pos+=b;
}
}
void Book::readLine(ofstream &output,string line)
{
string str = RemovePreAndLastSpace(line);
string newline1,newline2;
if(str == "")
return ;
if(str.length()<=width_max)
print_Line(output,str);
else
{
if( isspace(str[width_max]) ) // new is space no worry
{
newline1 = str.substr(0,width_max);
readLine(output,newline1);
newline2 = str.substr(width_max);
readLine(output,newline2);
}
else
{
string tmp = str.substr(0,width_max);
int pos = find_split(tmp);
newline1 = str.substr(0,pos);
readLine(output,newline1);
newline2 = str.substr(pos);
readLine(output,newline2);
}
}
}
以上book.c 文件
#include "Book.h"
using namespace std;
int main()
{
string infileName = "9.in";
string outfileName = "9.out";
ifstream infile;
ofstream outfile;
infile.open(infileName.c_str());
if(!infile)
{
cerr<<"can't open file"<<infileName<<endl;
return -1;
}
outfile.open(outfileName.c_str());
if(!outfile)
{
cerr<<"can't open file"<<outfile<<endl;
return -1;
}
Book book1 = Book("TEST");
book1.Print_book(infile,outfile);
infile.close();
outfile.close();
return 0;
}
以上测试文件
\t 在 string 占一个字符
但是输出是4个
所以在处理超长的行的时候,是吧\t 换成4 个 space了,然后分割成前后两端,递归了