1、上機作業唄(老師最近越來越沒人性了)
2、代碼
頭文件texteditor.h
#ifndef TEXTEDITOR_H_INCLUDED
#define TEXTEDITOR_H_INCLUDED
#include"string.h"
#include<iostream>
#include<list>
#include<fstream>
#include<algorithm>
using namespace std;
class TextEditor{
private:
int cursor;
int line;
int offset;
int total;
int letters;
int quots;
int nums;
int spaces;
string textName;
list<string> article;
public:
TextEditor();
~TextEditor();
const string & getName() const;
void setName(const string &name);
int getCursor(int *,int *) const;
int moveCursor(int);
int setCursor(int ,int);
int addText(const string &);
int insertText(string);
int findTextInAll(const string &,int *,int *);
int deleteText(const string &);
int deleteText(int ,int,int);
int saveFile();
void wordState(int *,int *,int *,int *,int *) const;
list<string>::iterator getIndex(int x);
friend ostream & operator<<(ostream &,TextEditor &);
friend istream & operator>>(istream &,TextEditor &);
};
TextEditor:: TextEditor(){
textName = "Untitled";
cursor=1;
line=1;
offset=0;
total =0;
spaces =0;
quots =0;
letters = 0;
nums = 0;
}
TextEditor::~TextEditor(){
}
list<string>::iterator TextEditor::getIndex(int x){
if(x<0)x=0;
if(x>line)x=line;
list<string>::iterator it;
int l = 0;
for(it=article.begin();it!=article.end();it++){
l++;
if(l==x) break;
}
return it;
}
const string & TextEditor::getName() const{
return textName;
}
void TextEditor::setName(const string &name){
if(name.length()!=0){
textName=name;
}
}
int TextEditor::getCursor(int * pLine=NULL,int *pOffset=NULL) const{
*pLine = cursor;
*pOffset=offset;
return 1;
}
int TextEditor::moveCursor(int offset){
list<string>::iterator it =getIndex(cursor);
this->offset+=offset;
while(it->length()<this->offset&&it!=article.end()){
this->offset-=it->length();
cursor++;
it++;
}
return 1;
}
int TextEditor::setCursor(int line ,int offset){
if(line>article.size()) line = article.size();
this->cursor=line;
if(offset<0) offset=0;
if(offset>getIndex(line)->length()) offset = getIndex(line)->length();
this->offset=offset;
return 1;
}
int TextEditor::addText(const string & text){
article.push_back(text);
total+=text.length();
for(int i=0;i<text.length();i++){
if(text[i]==' ') spaces++;else
if(text[i]>='0'&&text[i]<='9') nums++;else
if(text[i]>='a'&&text[i]<='z') letters++;else
if(text[i]>='A'&&text[i]<='Z') letters++;else
quots++;
}
line++;
return 1;
}
int min(int x,int y)
{
return x<y?x:y;
}
int TextEditor::insertText(string text){
total+=text.length();
for(int i=0;i<text.length();i++){
if(text[i]==' ') spaces++;else
if(text[i]>='0'&&text[i]<='9') nums++;else
if(text[i]>='a'&&text[i]<='z') letters++;else
if(text[i]>='A'&&text[i]<='Z') letters++;else
quots++;
}
if(article.empty()) {
article.push_back(text);
offset = text.length();
return 1;
}
list<string>::iterator it = getIndex(cursor);
string temp=it->substr(0,offset);
temp+=text;
temp +=it->substr(offset);
*it = temp;
return 1;
}
int TextEditor::findTextInAll(const string & text ,int *line,int *offset){
list<string>::iterator it;
int l=0;
int li = *line;
int pp = *offset;
for(it=article.begin();it!=article.end();it++){
l++;
if(*line>l) continue;
if(*line<l) *offset = -1;
if(it->find(text,(*offset)+1)!=string::npos){
*line = l;
*offset = it->find(text,(*offset)+1);
return 1;
}
}
if(li==*line&&pp==*offset) *offset=-1;
return 1;
}
int TextEditor::deleteText(const string & text){
int line=1,offset=-1;
findTextInAll(text,&line,&offset);
while(1){
if(offset==-1)break;
cout<<"是否刪除第"<<line<<"行位置"<<offset<<"處的數據(Y of N):";
char c[100];
cin>>c;
if(c[0]!='Y'&&c[0]!='y') continue;
deleteText(line,offset,text.length());
findTextInAll(text,&line,&offset);
}
}
int TextEditor::deleteText(int cursor ,int pos,int length){
list<string>::iterator it = getIndex(cursor);
total-=length;
string text = *it;
for(int i=pos;i<min(it->length(),pos+length);i++){
if(text[i]==' ') spaces--;else
if(text[i]>='0'&&text[i]<='9') nums--;else
if(text[i]>='a'&&text[i]<='z') letters--;else
if(text[i]>='A'&&text[i]<='Z') letters--;else
quots--;
}
*it = it->erase(pos,length);
}
void TextEditor::wordState(int *pTotal,int *pLetter,int *pDigit,int *pSpace,int *pQuot) const{
*pTotal = total;
*pLetter =letters;
*pDigit = nums;
*pSpace =spaces;
*pQuot = quots;
}
ostream & operator<<(ostream & out,TextEditor & editor){
int line = 1;
list<string>::iterator it;
out<<"***********"<<editor.textName<<".txt*********"<<endl;
for (it = editor.article.begin();it!=editor.article.end();it++){
out<<line++<<":";
out<<*it<<endl;
}
out<<endl;
return out;
}
istream & operator>>(istream & in,TextEditor &editor){
char s[100];
getchar();
gets(s);
string text(s);
editor.insertText(text);
while(1){
cout<<"繼續輸入(Y 或 N):";
string c;
in>>c;
if(c[0]!='Y'&&c[0]!='y') break;
getchar();
gets(s);
string ss(s);
editor.insertText(ss);
}
return in;
}
int TextEditor::saveFile(){
string name = textName+".txt";
ofstream out(name.c_str(),ios::out);
list<string>::iterator it;
for(it=article.begin();it!=article.end();it++){
out<<*it<<endl;
}
out.close();
}
#endif // TEXTEDITOR_H_INCLUDED
源文件texteditor.cpp
#include"texteditor.h"
void showInstructions(){
cout<<"*************簡易文本編輯器***************\n";
cout<<"選擇以下數字進行文本編輯,請使用英文輸入法"<<endl;
cout<<"0.獲取幫助信息"<<endl;
cout<<"1.獲取文檔名字"<<endl;
cout<<"2.設置文檔名字"<<endl;
cout<<"3.顯示全文"<<endl;
cout<<"4.移動光標位置"<<endl;
cout<<"5.在光標處添加文字"<<endl;
cout<<"6.在文檔最後添加文字"<<endl;
cout<<"7.統計文章字數"<<endl;
cout<<"8.查找文本出現的位置"<<endl;
cout<<"9.刪除文本"<<endl;
cout<<"10.保存文件"<<endl;
cout<<"11.退出系統"<<endl;
cout<<"******************************************"<<endl;
}
void getChoice(){
TextEditor editor;
while(1)
{
cout<<"\n";
cout<<"請選擇(0.顯示幫助):";
int n;
cin>>n;
if(n==11) break;
switch(n)
{
case 0:{
showInstructions();
break;
}
case 1:{
cout<<"文檔名稱爲:"<<editor.getName()<<endl;
break;
}
case 2:{
string s;
cout<<"請輸入 新的文檔名字: ";
cin>>s;
editor.setName(s);
cout<<"設置成功"<<endl;
break;
}
case 3:{
cout<<editor;
int line,pos;
editor.getCursor(&line,&pos);
cout<<"光標目前的行數爲:"<<line<<" 偏移量爲:"<<pos<<endl;
break;
}
case 4:{
int line,pos;
editor.getCursor(&line,&pos);
cout<<"光標目前的行數爲:"<<line<<" 偏移量爲:"<<pos<<endl;
cout<<"光標要移動的行數:";
cin>>line;
cout<<"光標在這一行的位置:";
cin>>pos;
editor.setCursor(line,pos);
break;
}
case 5:{
cout<<editor;
int line,pos;
editor.getCursor(&line,&pos);
cout<<"光標目前的行數爲:"<<line<<" 偏移量爲:"<<pos<<endl;
string text;
cout<<"請輸入你要寫入的文字(英文):"<<endl;
cin>>editor;
break;
}
case 6:{
cout<<"請輸入要添加的文字:"<<endl;
string text;
char s[100];
getchar();
gets(s);
text = s;
editor.addText(text);
while(1){
cout<<"繼續插入(Y 或 N):";
string c;
cin>>c;
if(c[0]!='Y'&&c[0]!='y') break;
getchar();
gets(s);
text = s;
editor.addText(text);
}
break;
}
case 7:{
int total,space,num,quot,letter;
editor.wordState(&total,&letter,&num,&space,");
cout<<"全文總數爲:"<<total<<endl;
cout<<"全文字母數爲:"<<letter<<endl;
cout<<"全文數字數爲:"<<num<<endl;
cout<<"全文標點數爲:"<<quot<<endl;
cout<<"全文空格數爲:"<<space<<endl;
break;
}
case 8:{
cout<<"請輸入要查找的文本:"<<endl;
string text;
char s[100];
getchar();
gets(s);
text = s;
int line=1,offset=-1;
editor.findTextInAll(text,&line,&offset);
while(1){
if(offset==-1) break;
cout<<"文本在第"<<line<<"行的第"<<offset<<"個位置"<<endl;
editor.findTextInAll(text,&line,&offset);
}
break;
}
case 9:{
cout<<"1.刪除一段文本"<<endl;
cout<<"2.刪除特定位置的文本"<<endl;
int as;
cout<<"請輸入選擇:";
cin>>as;
if(as==1){
cout<<"請輸入要刪除的文字:"<<endl;
string text;
char s[100];
getchar();
gets(s);
text = s;
editor.deleteText(text);
cout<<"刪除成功"<<endl;
}else if (as ==2){
cout<<"輸入行號:";
int line,pos,length;
cin>>line;
cout<<"\n輸入位置:";
cin>>pos;
cout<<"\n輸入長度:";
cin>>length;
editor.deleteText(line,pos,length);
cout<<"刪除成功"<<endl;
}
break;
}
case 10:{
editor.saveFile();
cout<<"保存文本成功!"<<endl;
break;
}
default:
cout<<"輸入數字錯誤"<<endl;
continue;
}
}
}
int main()
{
showInstructions();
getChoice();
system("pause");
return 0;
}