⑴ c++涓璇誨叆涓涓猼xt鏂囨。,鐢ㄩ摼琛ㄥ疄鐜板彲浠ヨ誨彇騫舵洿鏀逛換鎰忚
/*
http://..com/question/329457839.html?seed=0
c++涓璇誨叆涓涓猼xt鏂囨。,鐢ㄩ摼琛ㄥ疄鐜板彲浠ヨ誨彇騫舵洿鏀逛換鎰忚
紼嬪簭緙栬瘧鐜澧冿細c-free 5.0
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
/*閾捐〃鐨勮妭鐐廣備繚瀛樻枃浠朵腑涓琛岀殑鍐呭*/
struct Line
{
int id;
string content;
struct Line* next;
};
class TextEditor
{
public:
// 鏂囦歡涓涓琛屾渶澶氱殑瀛楃︽暟
const static int MAX_SIZE_PER_LINE = 10000;
/*
鏋勯犲嚱鏁
鍙傛暟錛
filePath 鏂囦歡璺寰
*/
TextEditor(string filePath);
/*
鎵撳嵃鏂囦歡鍐呭
*/
void print();
/*
淇濆瓨鏂囦歡
*/
void save();
/*
涓烘枃浠跺炲姞涓琛
鍙傛暟錛
lineIndex 瑕佸炲姞鐨勮屾墍鍦ㄨ屽彿
content 瑕佸炲姞鐨勮岀殑鍐呭
榪斿洖錛
娣誨姞鎴愬姛榪斿洖 true 錛屽惁鍒欒繑鍥 false
*/
bool addLine(int lineIndex,string content);
/*
涓烘枃浠跺垹闄や竴琛
鍙傛暟錛
lineIndex 瑕佸垹闄ょ殑琛屾墍鍦ㄨ屽彿
榪斿洖錛
鍒犻櫎鎴愬姛榪斿洖 true 錛屽惁鍒欒繑鍥 false
*/
bool deleteLine(int lineIndex);
/*
涓烘枃浠朵慨鏀逛竴琛
鍙傛暟錛
lineIndex 瑕佷慨鏀圭殑琛屾墍鍦ㄨ屽彿
content 瑕佷慨鏀圭殑琛岀殑淇鏀瑰悗鐨勫唴瀹
榪斿洖錛
淇鏀規垚鍔熻繑鍥 true 錛屽惁鍒欒繑鍥 false
*/
bool modifyLine(int lineIndex,string content);
/*
鎼滅儲鏂囦歡涓鐨勮
鍙傛暟錛
keyword 瑕佹悳緔㈢殑琛屽惈鏈夌殑鍏抽敭瀛
榪斿洖錛
鎼滅儲緇撴灉錛屾槸涓涓閾捐〃
*/
struct Line * searchLines(string keyword);
/*
鏌ヨ㈡枃浠剁殑鎬昏屾暟
榪斿洖錛
鏂囦歡鐨勬昏屾暟
*/
int getNumOfLines();
private:
// 鏂囦歡璺寰
string filePath;
// 閾捐〃褰㈠紡錛屾寜琛屽瓨鍌ㄦ枃浠跺湪鍐呭瓨涓
struct Line * artical;
};
TextEditor::TextEditor(string filePath)
{
this->filePath = filePath;
this->artical = NULL;
ifstream fin(this->filePath.c_str());
if(fin)
{
int lineId = 0;
struct Line * pr, *p;
char *buffe = new char[MAX_SIZE_PER_LINE+1];
while(!fin.eof())
{
p = new struct Line();
// 鐢熸垚琛屽彿
p -> id = ++ lineId;
// 鏂囦歡璇誨叆琛屽唴瀹
fin.getline(buffe,MAX_SIZE_PER_LINE);
p -> content = string(buffe);
// 鍒濆嬪寲鎸囬拡
p -> next = NULL;
// 娣誨姞鏂扮殑涓琛岋紙鏂扮殑鑺傜偣錛
if(this->artical == NULL)
{
this->artical = p;
pr = p;
}
else
{
pr -> next = p;
pr = p;
}
}
}
fin.close();
}
void TextEditor::print()
{
struct Line *p = this->artical;
// 杈撳嚭鐨勮屽彿鐨勫藉害
int lineIdWidth = (int)log10(this->getNumOfLines()) + 1;
while(p)
{
// 璁劇疆琛屽彿杈撳嚭鐨勫藉害
cout.width(lineIdWidth);
cout << (p -> id) << " : " << (p -> content) << endl;
p = p -> next;
}
}
void TextEditor::save()
{
ofstream fout(this->filePath.c_str());
if(fout)
{
struct Line *p = this->artical;
while(p)
{
fout << p->content;
// 涓嶆槸鏈鍚庝竴琛岋紝杈撳嚭涓涓鎹㈣岀﹀彿
if(p -> next != NULL)
{
fout << endl;
}
p = p->next;
}
}
fout.close();
}
bool TextEditor::addLine(int lineIndex,string content)
{
int numOfLines = this->getNumOfLines();
// 瑕佹彃鍏ユ柊琛岀殑浣嶇疆涓嶅悎娉
if(lineIndex < 1 || lineIndex > (numOfLines+1))
{
return false;
}
else
{
// 鏂拌
struct Line * newLine = new struct Line();
// 鐢熸垚琛屽彿
newLine -> id = lineIndex;
// 琛屽唴瀹
newLine -> content = content;
// 鍒濆嬪寲鎸囬拡
newLine -> next = NULL;
if(this->artical == NULL)
{
this->artical = newLine;
}
else
{
struct Line * pr = this->artical , *p = pr ;
// 鎵懼埌瑕佹彃鍏ヨ岀殑浣嶇疆
while(p && (lineIndex > p->id ))
{
pr = p;
p = p -> next;
}
// 鎻掑叆絎涓琛岀壒鍒澶勭悊
if(lineIndex == 1)
{
p = newLine -> next = this -> artical;
this -> artical = newLine;
}
else
{
p = newLine -> next = pr -> next;
pr -> next = newLine;
}
// 鏇存敼鎻掑叆琛屼箣鍚庢墍鏈夎岀殑琛屽彿
while(p)
{
p -> id ++;
p = p -> next;
}
}
return true;
}
}
bool TextEditor::deleteLine(int lineIndex)
{
int numOfLines = this->getNumOfLines();
// 瑕佸垹闄よ岀殑浣嶇疆涓嶅悎娉
if(lineIndex < 1 || lineIndex > numOfLines)
{
return false;
}
else
{
struct Line * pr = this->artical , *p = pr ;
// 鎵懼埌瑕佸垹闄よ岀殑浣嶇疆
while(p && (lineIndex != p->id ))
{
pr = p;
p = p -> next;
}
// 鍒犻櫎絎涓琛岃佺壒鍒澶勭悊
if(lineIndex == 1)
{
this -> artical = p -> next;
free(p);
p = this -> artical;
}
else
{
pr -> next = p -> next ;
free(p);
p = pr -> next;
}
// 鏇存敼鍒犻櫎琛屼箣鍚庢墍鏈夎岀殑琛屽彿
while(p)
{
p -> id --;
p = p -> next;
}
return true;
}
}
bool TextEditor::modifyLine(int lineIndex,string content)
{
int numOfLines = this->getNumOfLines();
// 瑕佷慨鏀硅岀殑浣嶇疆涓嶅悎娉
if(lineIndex < 1 || lineIndex > numOfLines)
{
return false;
}
else
{
struct Line * pr = this->artical , *p = pr ;
// 鎵懼埌瑕佷慨鏀硅岀殑浣嶇疆
while(p && (lineIndex != p->id ))
{
pr = p;
p = p -> next;
}
// 淇鏀硅岀殑鍐呭
p -> content = content ;
return true;
}
}
struct Line * TextEditor::searchLines(string keyword)
{
struct Line * p = this->artical ,*pr ,*searchResult = NULL ;
while(p)
{
// 褰撳墠琛屽惈鏈夎佹悳緔㈢殑鍏抽敭瀛
if(p -> content.find(keyword) != string::npos)
{
// 鐢熸垚涓涓鍖歸厤欏
struct Line * matchLine = new struct Line();
matchLine -> id = p -> id;
matchLine -> content = p -> content;
matchLine -> next = NULL;
// 娣誨姞鍒扮涓鏉℃悳緔犵粨鏋滆佺壒孌婂勭悊
if(searchResult == NULL)
{
searchResult = matchLine;
pr = matchLine;
}
else
{
pr -> next = matchLine;
pr = matchLine;
}
}
p = p -> next;
}
return searchResult;
}
int TextEditor::getNumOfLines()
{
int numOfLines = 0;
struct Line *p = this->artical;
while(p)
{
numOfLines++;
p = p->next;
}
return numOfLines;
}
int main(int argc, char *argv[])
{
// 鑿滃崟
while(1)
{
/*絎涓綰ц彍鍗*/
cout<<"********銆1銆戞墦寮鏂囦歡********"<<endl;
cout<<"********銆2銆戦銆銆鍑********"<<endl;
int choice_1;
cin >> choice_1;
switch(choice_1)
{
case 1:
{
system("cls");
cout << "杈撳叆瑕佹墦寮鐨勬枃浠惰礬寰勶細";
string filePath;
cin >> filePath;
TextEditor textEditor(filePath);
system("cls");
// 杈撳嚭鏂囦歡鍐呭
textEditor.print();
while(1)
{
/*絎浜岀駭鑿滃崟*/
cout << endl;
cout<<"********銆1銆戞坊鍔犺********"<<endl;
cout<<"********銆2銆戝垹闄よ********"<<endl;
cout<<"********銆3銆戜慨鏀硅********"<<endl;
cout<<"********銆4銆戞悳緔㈣********"<<endl;
cout<<"********銆5銆戞墦銆鍗********"<<endl;
cout<<"********銆6銆戣繑銆鍥********"<<endl;
int choice_2;
cin >> choice_2;
switch(choice_2)
{
// 娣誨姞琛
case 1:
{
cout << "杈撳叆瑕佹坊鍔犺岀殑浣嶇疆[1~" << (textEditor.getNumOfLines()+1) << "] 錛";
int lineIndex;
cin >> lineIndex;
cout << "杈撳叆瑕佹坊鍔犵殑琛岀殑鍐呭癸細" ;
string content;
cin >> content;
if(textEditor.addLine(lineIndex,content))
{
cout << "娣誨姞琛屾垚鍔燂紒"<<endl;
}
else
{
cout << "娣誨姞琛屽け璐ワ紒"<<endl;
}
break;
}
// 鍒犻櫎琛
case 2:
{
cout << "杈撳叆瑕佸垹闄よ岀殑浣嶇疆[1~" << textEditor.getNumOfLines() << "] 錛";
int lineIndex;
cin >> lineIndex;
if(textEditor.deleteLine(lineIndex))
{
cout << "鍒犻櫎琛屾垚鍔燂紒"<<endl;
}
else
{
cout << "鍒犻櫎琛屽け璐ワ紒"<<endl;
}
break;
}
// 淇鏀硅
case 3:
{
cout << "杈撳叆瑕佷慨鏀硅岀殑浣嶇疆[1~" << textEditor.getNumOfLines() << "] 錛";
int lineIndex;
cin >> lineIndex;
cout << "杈撳叆瑕佷慨鏀圭殑琛岀殑鍐呭癸細" ;
string content;
cin >> content;
if(textEditor.modifyLine(lineIndex,content))
{
cout << "淇鏀硅屾垚鍔燂紒"<<endl;
}
else
{
cout << "淇鏀硅屽け璐ワ紒"<<endl;
}
break;
}
// 鎼滅儲琛
case 4:
{
cout << "杈撳叆瑕佹悳緔㈢殑鍏抽敭瀛楋細";
string keyword;
cin >> keyword;
// 鎼滅儲琛
struct Line * searchResult = textEditor.searchLines(keyword) ,*p = searchResult;
// 杈撳嚭鎼滅儲緇撴灉
cout << "鎼滅儲緇撴灉錛"<< endl;
while(p)
{
cout << p->id << " : " << endl << p->content << endl;
p = p -> next;
}
break;
}
// 鎵撳嵃鏂囦歡
case 5:
{
textEditor.print();
break;
}
// 榪斿洖
case 6:
{
textEditor.save();
break;
}
// 閫夋嫨閿欒
default:
{
cout<<"鍛戒護閿欒錛岃烽噸鏂伴夋嫨錛"<<endl;
break;
}
} // end switch
// 浠庝簩綰ц彍鍗曡繑鍥炰竴綰ц彍鍗
if(choice_2 == 6)
{
break;
}
}// end while(1)
break;
}
case 2:
{
exit(0);
break;
}
default:
{
cout<<"鍛戒護閿欒錛岃烽噸鏂伴夋嫨錛"<<endl;
}
}
}
return 0;
}