runxinzhi.com
首页
百度搜索
String类封装
//Node.h typedef int Node_entry; typedef int Node_entry; enum Error_code {success, overflow, underflow, rangeError}; template <class Node_entry> struct Node { Node_entry entry; Node<Node_entry> *next; 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; } //List.h #include "Node.h" template<class List_entry> class List { public: List(); ~List(); bool empty() const; List(const List<List_entry> ©); List<List_entry> operator = (const List<List_entry> ©); Error_code insert(int position, const List_entry &x); Error_code retrieve(int position, List_entry &x); Error_code replace(int position, const List_entry &x); Error_code remove(int position); int size() const; void clear(); protected: int count; Node<List_entry> *head; Node<List_entry> *set_position(int position) const; }; template<class List_entry> Error_code List<List_entry>::remove(int position) { if (count == 0) return underflow; if (position < 0 || position >= count) return rangeError; Node<List_entry> *t, *target = set_position(position - 1); t = target->next; target->next = target->next->next; delete t; return success; } template<class List_entry> Error_code List<List_entry>::replace(int position, const List_entry &x) { if (position < 0 || position > count) return rangeError; Node<List_entry> *target = set_position(position); target->entry = x; return success; } template<class List_entry> int List<List_entry>::size() const { return count; } template<class List_entry> List<List_entry> List<List_entry>::operator = (const List<List_entry> ©) { List<List_entry> new_list(copy); clear(); head = new_list.head; count = new_list.count; new_list.count = 0; new_list.head = NULL; return *this; } template<class List_entry> void List<List_entry>::clear() { Node<List_entry> *t = head; while (!empty()) { head = t->next; delete t; t = head; } } template<class List_entry> bool List<List_entry>::empty() const { return head == NULL; } template<class List_entry> List<List_entry>::~List() { clear(); } template<class List_entry> Error_code List<List_entry>::retrieve(int position, List_entry &x) { if (position < 0 || position > count) return underflow; Node<List_entry> *t = set_position(position); x = t->entry; return success; } template<class List_entry> List<List_entry>::List(const List<List_entry> ©) { Node<List_entry> *new_node, *copy_copy = copy.head; if (copy.head == NULL) head = NULL; else { head = new_node = new Node<List_entry>(copy.head->entry); while (copy_copy->next != NULL) { copy_copy = copy_copy->next; new_node->next = new Node<List_entry>(copy_copy->entry); new_node = new_node->next; } } count = copy.count; } template<class List_entry> List<List_entry>::List() { head = NULL; count = 0; } template<class List_entry> Node<List_entry> *List<List_entry>::set_position(int position) const { Node<List_entry> *q = head; for (int i = 0; i < position; i++) q = q->next; return q; } template<class List_entry> Error_code List<List_entry>::insert(int position, const List_entry &x) { if (position < 0 || position > count) return rangeError; 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 == 0) return overflow; if (position == 0) head = new_node; else previous->next = new_node; count++; return success; } //String.h #include <cstring> #include <iostream> #include "List.h" class String { public: String(); ~String(); String(const String ©); String(const char *copy); String(List<char> ©); String& operator = (const String ©); const char *c_str() const; void test(); protected: char *entries; int length; }; bool operator == (const String &first, const String &second); bool operator > (const String &first, const String &second); bool operator < (const String &first, const String &second); bool operator >= (const String &first, const String &second); bool operator <= (const String &first, const String &second); bool operator != (const String &first, const String &second); bool operator == (const String &first, const String &second) { return strcmp(first.c_str(), second.c_str()) == 0; } bool operator > (const String &first, const String &second) { return strcmp(first.c_str(), second.c_str()) > 0; } bool operator < (const String &first, const String &second) { return strcmp(first.c_str(), second.c_str()) < 0; } bool operator >= (const String &first, const String &second) { return strcmp(first.c_str(), second.c_str()) >= 0; } bool operator <= (const String &first, const String &second) { return strcmp(first.c_str(), second.c_str()) <= 0; } bool operator != (const String &first, const String &second) { return strcmp(first.c_str(), second.c_str()) != 0; } const char* String::c_str() const { return (const char *)entries; } String& String::operator = (const String ©) { delete [] entries; length = copy.length; entries = new char[length + 1]; strcpy(entries, copy.entries); return *this; } void String::test() { std::cout << entries ; } String::String(const char *in_string) { length = strlen(in_string); entries = new char[length + 1]; strcpy(entries, in_string); } 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'; } String::String() { length = 0; entries = new char[1]; strcpy(entries, ""); } String::~String() { delete [] entries; } String::String(const String ©) { length = copy.length; entries = new char[length + 1]; for (int i = 0; i < length; i++) entries[i] = copy.entries[i]; entries[length] = '\0'; } //trial.cpp #include <iostream> #include "String.h" using namespace std; class String; void main() { String a("ttt"); String b; String c; b = c = a; a.test(); cout << endl; b.test(); cout << endl; c.test(); cout << c.c_str() << endl; bool x = (a != b); cout << x; system("pause"); return; }
相关阅读:
如何:使用向导来处理项目模板 【转载】
.NET : 如何在生成XML文档时绑定样式表
如何在生产环境部署K2的流程
SQL Server 数据库文档生成工具开源项目【合作邀请】
新的纪元
SharePoint : 谁说WSS不支持审核
C++ : 类型的别名和对象的别名
C++: 引用和地址运算符
Infopath : 如何通过代码复制格式文本框的值
正确地使用投影仪和移动硬盘
原文地址:https://www.cnblogs.com/seebro/p/2476529.html
最新文章
Jmeter基础之jmeter基础概念
类 StringTokenizer
我的JAVA学习(1)
或许我不出类拔萃,但注定与众不同
用于主题检测的临时日志(96a5db69fe6a4802b17196d03f375c8c 3bfe001a32de4114a6b44005b770f6d7)
Hashtable简介
关于“小幽工作室”
.NET : 读取XML文档的处理指令
.NET : 再谈谈XML中的命名空间问题
SharePoint : 使用SPQuery对象时要注意的事项
热门文章
ASP.NET & JAVASCRIPT: 在Javascript代码中访问服务器控件的ID
SharePoint : 为WSS或者MOSS编写自定义的Web Service
.NET : 如何动态根据一个业务实体类型创建XSD架构文件
.NET : 动态执行类型转换
XML数据库的有关架构和细节说明
如何在K2流程设计中使用XMLField
Writing a SmartObject Service
卸载VS2008时可能遇到的问题
Creating a K2 Deployment Package from code
XML数据库与db4o的简要对比
Copyright © 2020-2023
润新知