01 | class _string |
02 | { |
03 | friend std::istream& operator>>(std::istream& is, _string& a); |
04 | friend std::ostream& operator<<(std::ostream& os,_string& a); |
05 | |
06 | public : |
07 | _string() //默认构造函数 |
08 | { |
09 | length = 0; |
10 | b= new char [1]; |
11 | b[0]= '\0' ; |
12 | } |
13 | _string( char *a); //构造函数 |
14 | _string( int n, char a); |
15 | ~_string(); //析构函数 |
16 | _string(_string &a); //复制构造函数 |
17 | int size(){ return length;} //获得字符串长度 |
18 | _string operator+( const _string& a); //重载'+'操作符 |
19 | _string& operator+=( const _string& a); //重载'+='操作符 |
20 | _string& operator=( const _string& a); //重载赋值操作符 |
21 | char & operator[]( int n); //重载下标操作符 |
22 | _string substr( int pos, int n); //返回子字符串 |
23 | _string substr( int pos); //返回子字符串 |
24 |
25 |
26 | private : |
27 | char *b; |
28 | int length; |
29 | }; |
2. [文件] _string.cpp ~ 3KB 下载(22)
001 | // _string.cpp : 定义控制台应用程序的入口点。 |
002 | // |
003 |
004 | #include "stdafx.h" |
005 | #include<iostream> |
006 | #include<string.h> |
007 | #include"_string.h" |
008 | #include<stdlib.h> |
009 | using namespace std; |
010 |
011 | _string::_string( char *a) //构造函数 |
012 | { |
013 | length = strlen (a); |
014 | b = new char [length+1]; |
015 | for ( int i= 0;i<length;i++) |
016 | { |
017 | b[i] = a[i]; |
018 | } |
019 | b[length] = '\0' ; |
020 | } |
021 |
022 | _string::_string( int n, char a) |
023 | { |
024 | b= new char [n+1]; |
025 | for ( int i= 0;i<n;i++) |
026 | { |
027 | b[i] =a; |
028 | } |
029 | b[n] = '\0' ; |
030 | } |
031 |
032 | _string::~_string() //析构函数 |
033 | { |
034 | delete []b; |
035 | length=0; |
036 | } |
037 |
038 | _string::_string(_string &a) //复制构造函数 |
039 | { |
040 | length=a.size(); |
041 | b= new char [length+1]; |
042 | for ( int i = 0;i<length;i++) |
043 | { |
044 | b[i] = a.b[i]; |
045 | } |
046 | b[length] = '\0' ; |
047 | } |
048 |
049 | _string _string::operator+( const _string& a) //重载+ |
050 | { |
051 | int newLen = length+a.length; |
052 | char *str; |
053 | str = new char [newLen+1]; |
054 | int count = 0; |
055 | for ( int i = 0;i<length;i++) |
056 | { |
057 | str[i] = this ->b[i]; |
058 | count++; |
059 | } |
060 | |
061 | for ( int i =0;count<newLen;count++,i++) |
062 | { |
063 | str[count] = a.b[i]; |
064 | } |
065 | str[newLen] = '\0' ; |
066 | _string temp(str); |
067 | |
068 | return temp; |
069 | } |
070 |
071 | _string& _string:: operator+=( const _string& a) //重载+= |
072 | { |
073 | int newLen = length+a.length; |
074 | char *str; |
075 | str = new char [newLen+1]; |
076 | int count = 0; |
077 | for ( int i = 0;i<length;i++) |
078 | { |
079 | str[i] = this ->b[i]; |
080 | count++; |
081 | } |
082 | |
083 | for ( int i =0;count<newLen;count++,i++) |
084 | { |
085 | str[count] = a.b[i]; |
086 | } |
087 | str[newLen] = '\0' ; |
088 | _string temp(str); |
089 | * this =temp; |
090 | return * this ; |
091 | } |
092 | _string& _string:: operator=( const _string &a) //重载= |
093 | { |
094 | if ( this ==&a) |
095 | return * this ; |
096 |
097 | delete []b; |
098 | length = a.length; |
099 | b = new char [length+1]; |
100 | for ( int i = 0;i<length;i++) |
101 | { |
102 | b[i] = a.b[i]; |
103 | } |
104 | b[length] = '\0' ; |
105 | return * this ; |
106 |
107 | } |
108 |
109 | char & _string:: operator[]( int n) //重载下标操作符 |
110 | { |
111 | if (n>length) |
112 | return b[length-1]; |
113 | else |
114 | return b[n]; |
115 | } |
116 |
117 | ostream& operator<<(ostream& os, _string& a) //重载输出符 |
118 | { |
119 | os<<a.b; |
120 | return os; |
121 | } |
122 | istream& operator>>(std::istream& is, _string& a) //重载输入符 |
123 | { |
124 | is>>a.b ; |
125 | a.length= strlen (a.b); |
126 | return is; |
127 | } |
128 |
129 | _string _string::substr( int pos, int n) //两个接受不同参数的substr函数,返回子字符串 |
130 | { |
131 | char *p = new char [n+1]; |
132 | for ( int i=0;i<n;i++) |
133 | { |
134 | p[i]=b[pos]; |
135 | pos++; |
136 | } |
137 | p[n]= '\0' ; |
138 | _string k(p); |
139 | k.length=n; |
140 | return k; |
141 | } |
142 | |
143 | _string _string::substr( int pos) |
144 | { |
145 | int len=length; |
146 | char *p= new char [len-pos+1]; |
147 | int t=pos; |
148 | for ( int i=0;t<len;t++,i++) |
149 | { |
150 | p[i]=b[t]; |
151 | } |
152 | p[t]= '\0' ; |
153 | _string k(p); |
154 | k.length=len-pos; |
155 | return k; |
156 | } |
3. [文件] main.cpp ~ 846B 下载(17)
01 | #include "stdafx.h" |
02 | #include<iostream> |
03 | #include"_string.h" |
04 |
05 | using namespace std; |
06 | int main() |
07 | { |
08 | /*_string a("1234"); |
09 | cout<<a.size()<<endl; |
10 | _string b("5678"); |
11 | a+=b; |
12 | cout<<a.size()<<endl; |
13 | _string aa("111"); |
14 | _string bb("222"); |
15 | _string c; |
16 | c=aa+bb; |
17 | cout<<"c size 1 "<<c.size()<<endl; |
18 | c=aa; |
19 | cout<<"c size 2 "<<c.size()<<endl; |
20 | c="1234567"; |
21 | cout<<"c size 3 "<<c.size()<<endl; |
22 | cout<<c<<endl; |
23 | _string xxx(10,'2'); |
24 | cout<<"xxx is "<<xxx<<endl; |
25 | _string str; |
26 | cout<<"请输入str"<<endl; |
27 | cin>>str; |
28 | cout<<"你输入的是:"; |
29 | cout<<str<<endl; |
30 | cout<<"你输入的字符串长度为: "<<str.size()<<endl; |
31 | cout<<"substr"<<endl; |
32 | _string abc("1234567"); |
33 | _string xx=abc.substr(3,3); |
34 | cout<<"xx is "<<xx<<endl; |
35 | _string qq=abc.substr(3); |
36 | cout<<"qq is "<<qq<<endl;*/ |
37 | _string a; |
38 | cin>>a; |
39 | cout<<a; |
40 | return 0; |
41 | } |
4. [文件] stdafx.h ~ 233B 下载(12)
01 | // stdafx.h : 标准系统包含文件的包含文件, |
02 | // 或是经常使用但不常更改的 |
03 | // 特定于项目的包含文件 |
04 | // |
05 |
06 | #pragma once |
07 |
08 | #include "targetver.h" |
09 |
10 | #include <stdio.h> |
11 | #include <tchar.h> |
12 |
13 |
14 |
15 | // TODO: 在此处引用程序需要的其他头文件 |