qq自由幻想ss技能:关于C++的一道编程题,请高手帮忙阿

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/07 18:09:49
因为没有学过文件,所以不太看得懂题,还请高手指点一下
文本编辑器要求:(1)编辑文本;
(2)保存、打开指定位置的文本文件;
(3)具有输入输出界面。

// Simple Text Editor.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#include <istream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
const long maxLen = 80;
enum Error_code {success, overflow, underflow, range_error, failed};
//////////////////////////////////////////////////////////////////////////
template <class Node_entry>
struct Node
{
//data members
Node_entry entry;
Node<Node_entry> *next;
//constructors
Node();
Node(Node_entry item, Node<Node_entry> *link = NULL);
};
template <class Node_entry>
Node<Node_entry>::Node()
{
next = NULL;
}

template <class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node<Node_entry> *link)
{
entry = item;
next = link;
}

template<class List_entry>
class List
{
public:
~List();
List();
List(const List<List_entry> ©);
void operator=(const List<List_entry> ©);
Error_code insert(long position, const List_entry &x);
Error_code clear();
Error_code remove(long position);
Error_code retrieve(long position, List_entry &item);
Error_code replace(List_entry &x,long position);
bool empty()const;
long size()const;
void write();
protected:
long count;
Node<List_entry> *head;
Node<List_entry> *set_position(long position)const;
private:
};
template<class List_entry>
List<List_entry>::List()
{
count = 0;
head = NULL;
}
template<class List_entry>
List<List_entry>::List(const List<List_entry> ©)
{
Node *new_copy, *copy_node = copy.head;
count = copy.count;
if(copy_node == NULL) head = NULL;
else
{
head = new_copy = new Node<Node_entry>(copy_node->entry);
while(copy_node->next != NULL)
{
copy_node = copy_node->next;
new_copy->next = new Node<Node_entry>(copy_node->entry);
new_copy = new_copy->next;
}
}
}
template<class List_entry>
List<List_entry>::~List()
{
while(!empty())
remove(0);
}
template<class List_entry>
void List<List_entry>::operator =(const List<List_entry> ©)
{
Node *new_head, *new_copy, *copy_node = copy.head;
count = copy.count;
if(copy_node == NULL) new_head = NULL;
else
{
new_copy = new_head = new Node(copy_node->entry);
while(copy_node->next != NULL)
{
copy_node = copy_node->next;
new_copy->next = new Node(copy_node->entry);
new_copy = new_copy->next;
}
}
while(!empty())
remove(0);
head = new_head;
}
template<class List_entry>
Error_code List<List_entry>::insert(long position, const List_entry &x)
{
if(position < 0 || position > count)
return range_error;
Node<List_entry> * new_node, *previous, *following;
if(position > 0)
{
previous = set_position(position - 1);
following = previous->next;
}
else
following = head;
new_node = new Node<List_entry>(x,following);
if(new_node == NULL)
return overflow;
if(position == 0)
head = new_node;
else
previous->next = new_node;
count ++;
return success;
}
template<class List_entry>
Node<List_entry> * List<List_entry>::set_position(long position)const
{
Node<List_entry> *p = head;
for(int i = 0; i < position; i ++)
p = p->next;
return p;
}
template<class List_entry>
Error_code List<List_entry>::clear()
{
while(!empty())
remove(0);
return success;
}
template<class List_entry>
Error_code List<List_entry>::remove(long position)
{
if(position < 0 || position >= count)
return range_error;
Node<List_entry> *p, *current;
if(position == 0)
{
p = head;
head = head->next;
}
else
{
current = set_position(position - 1);
p = current->next;
current->next = p->next;
}
delete(p);
count --;
return success;
}

template<class List_entry>
Error_code List<List_entry>::replace(List_entry &x,long position)
{
Node<List_entry> *current = NULL;
if(position < 0 || position >= count)
return range_error;
current = set_position(position);
current->entry = x;
return success;
}
template<class List_entry>
Error_code List<List_entry>::retrieve(long position,List_entry &item)
{
Node<List_entry> *current = NULL;
if(position < 0 || position >= count)
return range_error;
current = set_position(position);
item = current->entry;
return success;
}
template<class List_entry>
long List<List_entry>::size()const
{
return count;
}
template<class List_entry>
bool List<List_entry>::empty()const
{
return (count == 0);
}
template<class List_entry>
void List<List_entry>::write()
{
Node<List_entry> *current = head;
long n;
for(n = 0; n < count; n ++)
{
cout << current->entry;
current = current->next;
}
}
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
class String
{
public:
String();
~String();
String(const String ©);
String(const char *copy);
String(List<char> ©);
String &operator = (const String ©);
const char *c_str()const;
bool operator == (const String &item);
bool operator > (const String &item);
bool operator < (const String &item);
bool operator >= (const String &item);
bool operator <= (const String &item);
bool operator != (const String &item);
friend ostream & operator<<(ostream &stream, String obj);
protected:
char *entries;
int length;
private:
};
void strcat(String &add_to, const String &add_on);
String read_in(istream &iuput);
String read_in(istream &iuput, int &terminator);
void strcpy(String ©, const String &original);
void strncpy(String ©, const String &original, int n);
long strstr(const String &text, const String &target);
void write(String &s);
//////////////////////////////////////////////////////////////////////////
String::~String()
{
delete []entries;
}
String::String()
{
entries = new char[maxLen + 1];
if(!entries)
{
cerr << "Allocation Error\n";
exit(1);
}
length = 0;
entries[0] = '\0';
}
String::String(const String ©)
{
length = copy.length;
entries = new char [length + 1];
if(!entries)
{
cerr << "Allocation Error\n";
exit(1);
}
strcpy(entries,copy.entries);
}
String::String(const char *copy)
{
length = strlen(copy);
entries = new char [length + 1];
strcpy(entries,copy);
}
String::String(List<char> &in_list)
{
length = in_list.size();
entries = new char [length + 1];
for(int i = 0; i < length; i ++)
in_list.retrieve(i,entries[i]);
entries[length] = '\0';
}

const char *String::c_str()const
{
return (const char *) entries;
}
String & String::operator=(const String ©)
{
if(© != this)
{
length = copy.length;
delete []entries;
entries = new char [length + 1];
if(!entries)
{
cerr << "Allocation Error\n";
exit(1);
}
strcpy(entries, copy.entries);
}
else
cout << "Attempted assignment of String to itself!\n";
return *this;
}
bool String::operator==(const String &item)
{
return strcmp(c_str(),item.c_str()) == 0;
}
bool String::operator< (const String &item)
{
return strcmp(c_str(),item.c_str()) < 0;
}
bool String::operator>(const String &item)
{
return strcmp(c_str(),item.c_str()) > 0;
}
bool String::operator>=(const String &item)
{
return strcmp(c_str(),item.c_str()) >= 0;
}
bool String::operator<=(const String &item)
{
return strcmp(c_str(),item.c_str()) <= 0;
}
bool String::operator!=(const String &item)
{
return strcmp(c_str(),item.c_str()) != 0;
}
ostream &operator<<(ostream &stream, String obj)
{
write(obj);
return stream;
}
void strcat(String &add_to, const String &add_on)
{
const char *cfirst = add_to.c_str();
const char *csecond = add_on.c_str();
char *copy = new char [strlen(cfirst) + strlen(csecond) + 1];
strcpy(copy,cfirst);
strcat(copy,csecond);
add_to = copy;
delete[]copy;
}
String read_in(istream &input)
{
List<char> temp;
int size = 0;
char c;
while((c = input.peek()) != EOF && (c = input.get()) != '\n')
temp.insert(size ++, c);
String answer(temp);
return answer;
}
String read_in(istream &input, int &terminator)
{
List<char> temp;
int size = 0;
char c;
while((c = input.peek()) != EOF && (c = input.get()) != '\n')
temp.insert(size ++, c);
terminator = c;
String answer(temp);
return answer;
}
void strcpy(String ©, const String &original)
{
copy = original;
}
void strncpy(String ©, const String &original, int n)
{
int i;
const char *ori = original.c_str();
char *temp = new char [n+1];
for(i = 0; i < n; i ++)
{
temp[i] = ori[i];
}
temp[n] = '\0';
String noriginal(temp);
copy = noriginal;
delete[]temp;
}
void fail(long f[], const String &target)
{
long i, j;
const char *t = target.c_str();
f[0] = -1;
for(j = 1; j < (long)strlen(t); j ++)
{
i = f[j-1];
while(*(t + j) != *(t + i+1) && i >= 0)
i = f[i];
if(*(t + j) == *(t + i+1))
f[j] = i + 1;
else
f[j] = -1;
}
}
long strstr(const String &text, const String &target)
{
const char *T = text.c_str();
const char *P = target.c_str();
long lenT = strlen(T), lenP = strlen(P), posT = 0, posP = 0;
if(lenP == 0 || lenT == 0)
{
cout << "Empty target or text!" << endl;
return -1;
}
else
{
long *f = new long[lenP];
fail(f,target);
while(posP < lenP && posT < lenT)
{
if(P[posP] == T[posT])
{
posP ++;
posT ++;
}
else if(posP == 0)
posT ++;
else posP = f[posP - 1] + 1;
}
if(posP < lenP)
{
delete []f;
return -1;
}
else
{
delete []f;
return posT - lenP;
}
}
}
void write(String &s)
{
const char *display = s.c_str();
if(display[0] != '\0')
cout << display << endl;
else
cout << "Empty string!" << endl;
}
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

class Editor:public List<String>
{
public:
Editor();
bool get_command();
void run_command();
protected:
private:
char user_command;
Node<String> *current;
long line;
Error_code next_line();
Error_code previous_line();
Error_code goto_line();
Error_code insert_line();
Error_code change_line();
void read_file();
void write_file();
void find_string();
};
Editor::Editor()
{
current = NULL;
line = -1;
}
Error_code Editor::next_line()
{
Error_code outcome = success;
if(line < count - 1)
{
line ++;
current = set_position(line);
}
else
outcome = range_error;
return outcome;
}
Error_code Editor::previous_line()
{
Error_code outcome = success;
if(line > 0)
{
line --;
current = set_position(line);
}
else
outcome = range_error;
return outcome;
}
Error_code Editor::goto_line()
{
Error_code outcome = success;
long offset;
cout << "which line do you want to go?" << flush;
cin >> offset;
if(offset >= 0 && offset < count)
{
line = offset;
current = set_position(line);
}
else
outcome = range_error;
return outcome;
}
void Editor::find_string()
{
long index;
cout << "Enter the string to search for:" << flush;
String search_string = read_in(cin);
while((index = strstr(current->entry,search_string)) == -1)
if(next_line() != success) break;
if(index == -1) cout << "String was not found.";
else
{
cout << (current->entry).c_str() << endl;
for(long i = 0; i < index; i ++)
cout << " ";
for(unsigned long j = 0; j < strlen(search_string.c_str());j ++)
cout << "^";
}
cout << endl;
}
Error_code Editor::change_line()
{
Error_code result = success;
cout << "What text segment do you want to replace?" << flush;
String old_text = read_in(cin);
cout << "What new text segment do you want to add in?" << flush;
String new_text = read_in(cin);

long index = strstr(current->entry,old_text);
if(index == -1) result = failed;
else{
String new_line;
strncpy(new_line, current->entry, index);
strcat(new_line, new_text);
const char *old_line = (current->entry).c_str();
strcat(new_line,(String)(old_line + index + strlen(old_text.c_str())));
current->entry = new_line;
}
return result;
}
Error_code Editor::insert_line()
{
Error_code outcome;
long line_number;
cout << "Insert what line number?" << flush;
cin >> line_number;
while(cin.get() != '\n')
cout << "what is the new line to insert?" << flush;
String to_insert = read_in(cin);
outcome = insert(line_number, to_insert);
line = line_number;
current = set_position(line);
return outcome;
}
bool user_says_yes()
{
char order;
cin >> order;
return (order == 'Y' || order == 'y');
}
bool Editor::get_command()
{
if(current != NULL)
cout << line << ":"
<< current->entry.c_str() << "\n??" << flush;
else
cout << "File is empty.\n??" << flush;
cin >> user_command;
user_command = tolower(user_command);
while(cin.get() != '\n')
;
if(user_command == 'q')
return false;
else
return true;
}
void Editor::run_command()
{
String temp_string;
switch(user_command)
{
case 'b':
if(empty())
cout << "Warning:empty buffer" << endl;
else
while(previous_line() == success)
;
break;
case 'c':
if(empty())
cout << "Warning:Empty buffer" << endl;
else if(change_line() != success)
cout << "Error:Substitution failed" << endl;
break;
case 'd':
if(remove(count - 1) != success)
cout << "Error:Deletion failed" << endl;
break;
case 'e':
if(empty())
cout << "Warning: empty buffer" << endl;
else
while(next_line() == success)
;
break;
case 'f':
if(empty())
cout << "Warning:Empty file" << endl;
else
find_string();
break;
case 'g':
if(goto_line() != success)
cout << "Warning:No such line" << endl;
break;
case '?':
case 'h':
cout << "Valid commands are: b(egin) c(hange) d(el) e(nd) f(ind) g(o) h(elp)" << endl
<< " i(nsert) l(ength) n(ext) p(rior) q(uit) w(rite)" <<endl;
break;
case 'i':
if(insert_line() != success)
cout << "Error:Insertion failed" << endl;
break;
case 'l':
cout << "There are " << size() << " lines in the file." << endl;
if(!empty())
cout << "Current line length is "
<< strlen((set_position(count - 1)->entry).c_str()) << endl;
break;
case 'n':
if(next_line() != success)
cout << "Warning: at end of buffer" << endl;
break;
case 'p':
if(previous_line() != success)
cout << "Warning:at start of buffer" << endl;
break;
case 'w':
if(empty())
cout << "Warning:Empty file" << endl;
else
write();
break;
default:
cout << "Press h or ? for help or enter a valid command:";
}
}

int main()
{
Editor buffer;
cout << "Valid commands are: b(egin) c(hange) d(el) e(nd) f(ind) g(o) h(elp)" << endl
<< " i(nsert) l(ength) n(ext) p(rior) q(uit) w(rite)" <<endl;
while(buffer.get_command())
buffer.run_command();
return 0;
}
我做的数据结构作业,没有文件功能,改一下就可以了的哈

只是解释一下题目吗?
如果是基于控制台编程:
这个题目的意思大概是叫你编一个程序,用户用键盘写一段文字后按回车就可以把刚输入的文字保存至指定位置的文本文件(.txt或者.doc等等)中.它说的"打开指定位置的文本文件"可能是叫你做能够把文件中的内容显示在屏幕上吧!
如果是MFC编程:
运行你做的MFC可执行文件后,在视图区显示指定位置的文件中的内容,此时允许用户在某个地方(弹出的对话框等等)输入文字,之后进行某种操作(用鼠标点确定或者使用快捷键)后把刚输入的文字保存到指定的文件中

不要老是高手高手的,真正的高手才不愿意来帮你这种小问题,问问题可以随便问,至于谁来回答你就不要做这个主了,会的愿意的当然会回答你了,不会的,就算是高手也不会回答你

用纯c++呀,算了吧。100分都不给做。
还记得以前用汇编作编辑器,麻烦。

MFC自动生成一个就可以了