请求完善程序(制作阅读器实现翻页)
#include <iostream>#include <fstream>
#include <iomanip>
#include <windows.h>
using namespace std;
//设计和实现一个NewsBrowser类,读取一个txt文件,允许按一定格式(10列*15行,15列*22行)
//来显示允许page down and page up
class txtRead{
private:
fstream newsFile;
char y;
int rows;
int cols;
char news;
public:
void printTxt();
void countNum();
void chooseSize();
void changePage();
txtRead();
};
txtRead::txtRead(){
newsFile.open("I:\\NewBrowserText.txt");
if(!newsFile){
cout<<"you get wrong file's address!"<<endl;
rows=15,cols=10;
}
}
void txtRead::printTxt(){
while(newsFile.get(news)){
if(news!=' '&& news!='\n'){
cout<<news;
}
if(news==' '){
cout<<" ";
}
if(news=='\n'){
cout<<"\n";
}
if(news=='\0')break;
}
newsFile.close();
}
void txtRead::changePage(){
int countRow=1;
int countCol=1;
int i=0;
countNum();
while(printTxt()){
if(countRow<rows){
cout<<news;
//cout<<endl<<setfill(' ')<<setw(40);
countRow++;
countCol=1;
i++;
}
else{
cout<<endl;
cout<<news;
countRow=1;
countCol=1;
countCol++;
i++;
}
//cout<<endl<<setfill(' ')<<setw(40);
if(countCol==cols){
cout<<"the words:"<<i<<endl;
cout<<"the"<<(i/(rows*cols))<<"page"<<endl;
system("pause");
system("cls");
countCol=0;
countRow=0;
changePage();
}
}
}
void txtRead::countNum(){
int countAll=0;
while(newsFile.get(news)){
if(news==' '){
countAll++;
}
else
countAll++;
}
chooseSize();
int page;
if(countAll%(rows*cols)!=0){
page=countAll/(rows*cols)+1;
}
if(countAll%(rows*cols)==0){
page=countAll/(rows*cols);
}
newsFile.close();
//cout<<" "<<"conclude"<<countAll<<"words "<<"and "<<page<<" pages"<<endl;
// system("pause");
}
/*void txtRead::chooseSize(){
cout<<"Please choose your size:"<<endl;
cout<<"a(15rows*10cols) or b(22rows*15cols)"<<endl;
cin>>y;
if(y=='a'){
rows=15,cols=10;
}
if(y=='b'){
rows=22,cols=15;
}
/*if(y!='a' || y!='b'){
cout<<"You choose the wrong number!"<<endl;
}
system("pause");
return y;}*/
int main(){
txtRead readNews;
cout<<" "<<"NewBrowser"<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
//readNews.countNum();
readNews.printTxt();
}