编程采用C plus plus language,VI text editor and GDB.
单文件代码(未在代码安全性方面做过多考虑)
为了锻炼使用Makefile工具,以及加强代码安全性,特意将原程序分成几个源文件进行处理。
文件目录树如下:
源文件代码如下:
单文件代码(未在代码安全性方面做过多考虑)
#include
<
iostream
>
#include < string >
#include < exception >
using namespace std;
int main()
{
const string s="[*]";
const string begstr="[img]";
const string endstr="[/img]";
int start,end;
cin.exceptions(ios_base::failbit);
string str;
string former,last;
int no;
cout<<"Please input the formula string below: ";
cin>>str;
no=str.find(s,0);
while(no==string::npos)
{
cout<<"string error,input again: ";
cin>>str;
no=str.find(s,0);
}
cout<<"Please input the interger number for beginning: ";
point: //问题尚未解决代码
try {
cin>>start;
}
catch (ios_base::failure & bf)
{
cout << bf.what() << endl;
cout << "Please try again: ";
cin.clear();
goto point;
}
cout<<"Please inut the end number: ";
cin>>end;
FILE *pFile;
pFile=fopen("string.txt","w");
former.assign(str,0,no);
former.insert(0,begstr);
last.assign(str,no+3,str.length());
last+=endstr;
for(int n=start;n<=end;n++)
{
fprintf(pFile,"%s%03d%s ",former.c_str(),n,last.c_str());
}
fclose(pFile);
return 0;
}
#include < string >
#include < exception >
using namespace std;
int main()
{
const string s="[*]";
const string begstr="[img]";
const string endstr="[/img]";
int start,end;
cin.exceptions(ios_base::failbit);
string str;
string former,last;
int no;
cout<<"Please input the formula string below: ";
cin>>str;
no=str.find(s,0);
while(no==string::npos)
{
cout<<"string error,input again: ";
cin>>str;
no=str.find(s,0);
}
cout<<"Please input the interger number for beginning: ";
point: //问题尚未解决代码
try {
cin>>start;
}
catch (ios_base::failure & bf)
{
cout << bf.what() << endl;
cout << "Please try again: ";
cin.clear();
goto point;
}
cout<<"Please inut the end number: ";
cin>>end;
FILE *pFile;
pFile=fopen("string.txt","w");
former.assign(str,0,no);
former.insert(0,begstr);
last.assign(str,no+3,str.length());
last+=endstr;
for(int n=start;n<=end;n++)
{
fprintf(pFile,"%s%03d%s ",former.c_str(),n,last.c_str());
}
fclose(pFile);
return 0;
}
为了锻炼使用Makefile工具,以及加强代码安全性,特意将原程序分成几个源文件进行处理。
文件目录树如下:
|--
Makefile
|-- include
| |-- io.h
| ` -- strmani.h
` -- src
|-- io.cpp
|-- main.cpp
` -- strmani.cpp
|-- include
| |-- io.h
| ` -- strmani.h
` -- src
|-- io.cpp
|-- main.cpp
` -- strmani.cpp
源文件代码如下:
/* 源文件 io.h,定义io处理类 io */
#include < iostream >
#include < string >
using namespace std;
/*
* =====================================================================================
* Class: Io
* Description: this class is using for input/output through console or file.
* =====================================================================================
*/
class Io
{
public:
Io (string tem,string pre,string suf):templa(tem),prefix(pre),suffix(suf)
{
}
/* constructor */
void streamin();
string streamout();
void streamoutfile(FILE *pFILE,string fstr,string lstr);
protected:
private:
const string templa;
const string prefix;
const string suffix;
int start;
int end;
int bits;
int index;
string str;
void streaminstr();
void streaminstart();
void streaminend();
void streaminbits();
void configure();
} ; /* ----- end of class Io ----- */
#include < iostream >
#include < string >
using namespace std;
/*
* =====================================================================================
* Class: Io
* Description: this class is using for input/output through console or file.
* =====================================================================================
*/
class Io
{
public:
Io (string tem,string pre,string suf):templa(tem),prefix(pre),suffix(suf)
{
}
/* constructor */
void streamin();
string streamout();
void streamoutfile(FILE *pFILE,string fstr,string lstr);
protected:
private:
const string templa;
const string prefix;
const string suffix;
int start;
int end;
int bits;
int index;
string str;
void streaminstr();
void streaminstart();
void streaminend();
void streaminbits();
void configure();
} ; /* ----- end of class Io ----- */
/* 源文件 strmani.h,定义字符串处理类strmani*/
#include < iostream >
#include < string >
using namespace std;
/*
* =====================================================================================
* Class: Strmani
* Description: This class is set to deal with the string
* =====================================================================================
*/
class Strmani
{
public:
Strmani (); /* constructor */
void stringcut(string str,const string templa);
string formerstr;
string laststr;
protected:
private:
} ; /* ----- end of class Strmani ----- */
#include < iostream >
#include < string >
using namespace std;
/*
* =====================================================================================
* Class: Strmani
* Description: This class is set to deal with the string
* =====================================================================================
*/
class Strmani
{
public:
Strmani (); /* constructor */
void stringcut(string str,const string templa);
string formerstr;
string laststr;
protected:
private:
} ; /* ----- end of class Strmani ----- */
/*文件 io.cpp*/
#include " io.h "
void Io::streamin ( )
{
streaminstr();
streaminstart();
streaminend();
streaminbits();
} /* ----- end of method Io::streamin ----- */
void Io::streaminstr ( )
{
cout<<"Please input the string template below:"<<endl;
cin>>str;
index=str.find(templa,0);
while(index==string::npos)
{
cout<<"template string input error,input again: ";
cin>>str;
index=str.find(templa,0);
}
} /* ----- end of method Io::streamin() ----- */
void Io::streaminstart ( )
{
cout<<"Please input the start number you want to set:"<<endl;
st:
try
{
cin>>start;
}catch(ios_base::failure & bf){
cout<<"Bad start data,the program will exit"<<endl;
}
if(start<0)
{
cout<<"You have set the start number to be a negative,retype again!"<<endl;
goto st;
}
}
void Io::streaminend ( )
{
cout<<"Please input the end number:"<<endl;
en:
try
{
cin>>end;
}catch(ios_base::failure & bf){
cout<<"Bad end data,the program will exit"<<endl;
}
if(end<start)
{
cout<<"The end number is less than the start number,retype again!"<<endl;
goto en;
}
}
void Io::streaminbits ( )
{
cout<<"Please input the bits:"<<endl;
bi:
try
{
cin>>bits;
}catch(ios_base::failure & bf){
cout<<"Bad bits data,the program will exit"<<endl;
}
if(bits>5||bits<0)
{
cout<<"The bits is supposed to be between 0 and 5,retype again! "<<endl;
goto bi;
}
}
void Io::configure ( )
{
str.insert(0,prefix);
str+=suffix;
} /* ----- end of method Io::configure ----- */
string Io::streamout ( )
{
configure();
return str;
} /* ----- end of method Io::streamout ----- */
void Io::streamoutfile (FILE * pFile, string fstr, string lstr )
{
for(int n=start;n<=end;n++)
{
switch(bits)
{
case 1:
fprintf(pFile,"%s%d%s ",fstr.c_str(),n,lstr.c_str());
break;
case 2:
fprintf(pFile,"%s%02d%s ",fstr.c_str(),n,lstr.c_str());
break;
case 3:
fprintf(pFile,"%s%03d%s ",fstr.c_str(),n,lstr.c_str());
break;
case 4:
fprintf(pFile,"%s%04d%s ",fstr.c_str(),n,lstr.c_str());
break;
default:
;
}
}
} /* ----- end of method Io::streamoutfile ----- */
#include " io.h "
void Io::streamin ( )
{
streaminstr();
streaminstart();
streaminend();
streaminbits();
} /* ----- end of method Io::streamin ----- */
void Io::streaminstr ( )
{
cout<<"Please input the string template below:"<<endl;
cin>>str;
index=str.find(templa,0);
while(index==string::npos)
{
cout<<"template string input error,input again: ";
cin>>str;
index=str.find(templa,0);
}
} /* ----- end of method Io::streamin() ----- */
void Io::streaminstart ( )
{
cout<<"Please input the start number you want to set:"<<endl;
st:
try
{
cin>>start;
}catch(ios_base::failure & bf){
cout<<"Bad start data,the program will exit"<<endl;
}
if(start<0)
{
cout<<"You have set the start number to be a negative,retype again!"<<endl;
goto st;
}
}
void Io::streaminend ( )
{
cout<<"Please input the end number:"<<endl;
en:
try
{
cin>>end;
}catch(ios_base::failure & bf){
cout<<"Bad end data,the program will exit"<<endl;
}
if(end<start)
{
cout<<"The end number is less than the start number,retype again!"<<endl;
goto en;
}
}
void Io::streaminbits ( )
{
cout<<"Please input the bits:"<<endl;
bi:
try
{
cin>>bits;
}catch(ios_base::failure & bf){
cout<<"Bad bits data,the program will exit"<<endl;
}
if(bits>5||bits<0)
{
cout<<"The bits is supposed to be between 0 and 5,retype again! "<<endl;
goto bi;
}
}
void Io::configure ( )
{
str.insert(0,prefix);
str+=suffix;
} /* ----- end of method Io::configure ----- */
string Io::streamout ( )
{
configure();
return str;
} /* ----- end of method Io::streamout ----- */
void Io::streamoutfile (FILE * pFile, string fstr, string lstr )
{
for(int n=start;n<=end;n++)
{
switch(bits)
{
case 1:
fprintf(pFile,"%s%d%s ",fstr.c_str(),n,lstr.c_str());
break;
case 2:
fprintf(pFile,"%s%02d%s ",fstr.c_str(),n,lstr.c_str());
break;
case 3:
fprintf(pFile,"%s%03d%s ",fstr.c_str(),n,lstr.c_str());
break;
case 4:
fprintf(pFile,"%s%04d%s ",fstr.c_str(),n,lstr.c_str());
break;
default:
;
}
}
} /* ----- end of method Io::streamoutfile ----- */
/*源文件 strmani.cpp*/
#include " strmani.h "
void Strmani::stringcut( string str, const string templa)
{
int index=str.find(templa);
if(index!=string::npos)
{
cout<<templa<<"founded at:"<<index<<endl;
formerstr.assign(str,0,index);
laststr.assign(str,index+templa.length(),str.length());
}
} /* ----- end of method Strmani::stringcut(cstring str,const cstring templa) ----- */
Strmani::Strmani ()
{
} /* ----- end of method Strmani::Strmani (constructor) ----- */
#include " strmani.h "
void Strmani::stringcut( string str, const string templa)
{
int index=str.find(templa);
if(index!=string::npos)
{
cout<<templa<<"founded at:"<<index<<endl;
formerstr.assign(str,0,index);
laststr.assign(str,index+templa.length(),str.length());
}
} /* ----- end of method Strmani::stringcut(cstring str,const cstring templa) ----- */
Strmani::Strmani ()
{
} /* ----- end of method Strmani::Strmani (constructor) ----- */
/* 主程序 main.cpp*/
#include " strmani.h "
#include " io.h "
int main()
{
const string temp="[*]";
FILE *pFile;
pFile=fopen("string.txt","w");
Io s("[*]","[IMG]","[/IMG]");
s.streamin();
Strmani t;
t.stringcut(s.streamout(),temp);
s.streamoutfile(pFile,t.formerstr,t.laststr);
fclose(pFile);
return 0;
}
#include " strmani.h "
#include " io.h "
int main()
{
const string temp="[*]";
FILE *pFile;
pFile=fopen("string.txt","w");
Io s("[*]","[IMG]","[/IMG]");
s.streamin();
Strmani t;
t.stringcut(s.streamout(),temp);
s.streamoutfile(pFile,t.formerstr,t.laststr);
fclose(pFile);
return 0;
}