⑴ 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;
}