使用说明
程序名为 NO3.exe.运行环境为DOS,执行后显示:
在"请输入你的选择后(1.2.3.4.5.6)"后输入数字选择执行的功能.
测试结果:
-
选择1.后输入:123456789
-
选择2后输入分别输入1,3.
重复1)操作后选择2,分别输入10,10.
-
-
重复1)操作后选择3.分别输入1,abcde
再重复1)操作后选择3.分别输入9,abcde
-
-
再重复1)操作后选择4,分别输入1,3
再重复1)操作后选择4,分别输入0,3
再重复1)操作后选择4,分别输入10,3
5) 再重复1)操作后选择5,分别输入1,abcde
再重复1)操作后选择5,分别输入9,abcde
再重复1)操作后选择5,分别输入0,abcde
再重复1)操作后选择5,分别输入10,abcde
6)运行No3.exe后选择6或输入非"1,2,3,4,5"的数字
调试过程:
-
本调试主要针对置换操作功能进行演示:
-
将光标移置String:: Replace(String t1,int pos)函数的第一条语句处Ctrl+F10开始调试
-
在DOS窗口中选择1后输入"123456789".接着选择5分别输入1,abcde.
这时Debugger停留在String:: Replace(String t1,int pos)的第一条语句处:
-
在Watch窗口的名称栏分别输入: str, t1.str, q, out, pos, (-pos) +1, pos – size,j,i.进行观察.
-
按F10开始单步调试.
-
按F10三次后Debugger停留在最后一个判断语句处.同时Watch窗口中个名称的值分别为:
-
接着单步调试,for()函数完后,Debugger停留在" delete t1.str;"语句处.
这时Watch窗口中个各名称的值分别为:
接着两次F10,这时t1.str和out的值已经改变,Debugger停留在String:: Replace(String t1,int pos)的结束处.
-
再按一次F10,Debugger停留在main()函数的switch(k)里的case 5的if()语句处:
F10到调用Display()函数的语句处后F11跟进Display()的内部.
在Watch窗口的名称中输入str,I,len进行观察.
单步调试到Display()函数结束,Debugger停留在Display()函结束处.
在Watch窗口中str,I,len的值分别为:
同时DOS窗口中显示如下:
-
-
按Shift+F5退出调试.完成调试操作.
参考源码:
-
- 1
Code:
2// 3.h
3#include < iostream.h >
4#include < string .h >
5#include < stdlib.h >
6// using namespace std;
7int out ; // 定义一个全局变量
8class String
9{
10public:
11String(){}
12~String(){}
13String SubString(int pos,int num); //取子串函数
14void Insert(String t,int pos); //插入子串函数
15void Delete(int pos,int num); //删除子串函数
16void Creat(); //生成字符串函数
17void Display(); //打印子串函数
18Replace(String t1,int pos); //置换子串函数
19private:
20char *str;
21int size;
22} ;
23
24// 3.cpp
25#include " 3.h "
26// 生成新字符串函数
27void String:: Creat()
28{
29char s[100];
30cin>>s;
31size=strlen(s);
32str=new char[size + 1];
33if(str==0)
34cout<<"没有申请到空间!"<<endl;
35strcpy( str, s);
36//strcpy_s(str,sizeof(str)/sizeof(str[0]),s);
37
38}
39// 输出
40void String::Display()
41{
42int len = size;
43int i;
44for( i=0;i < size;i++ )
45cout<<str[i];
46cout<<endl;
47cout<<"字符串的总长度为:"<<len<<endl;
48}
49// 求子串
50// void String String::SubString(int pos,int num)
51String String::SubString( int pos, int num)
52{
53String temp;
54int left=size-pos + 1;//从pos位置开始所有余下的字符串长度
55char *p,*q;
56if( pos > size )
57{
58cout<<"错误!"<<endl;
59cout<<"指定位置超过字符串长度为:"<< pos - size<<endl;//提示所取子串的错误位置
60exit (1);//异常退出
61}
62else if( num > left )
63num = left;
64temp.str=new char[num+1];//重新分配内存空间
65if(temp.str==0)
66cout<<"没有申请到空间!"<<endl;
67p=temp.str;
68for(int i = pos-1;i < pos+num-1;i++)
69{
70q = &str[i];
71*p = *q;
72p++;
73}
74*p=0;
75temp.size = num;
76return temp;
77}
78
79// 插入运算:在串对象s的pos位置后插入一个串t
80void String::Insert(String t, int pos)
81{
82//String temp;
83int i_len = t.size;
84cout<<"插入字符串的长度为:"<<i_len<<endl;
85
86char *q;
87q = new char( size + 1);
88strcpy(q,str);
89
90delete str;
91str = new char( size + t.size + 1); //字符串长度要在原长上增加插入串的长度
92
93
94strcpy(str,q);
95
96//依次后移,空出长度为插入字符串的长度的空间
97
98for(int j = size-1;j > pos-1;j--)
99{
100int i = j + t.size;
101str[ i-- ] = str[ j ];
102}
103
104j = pos;
105for(int i = 0;i < t.size;)
106{
107str[j++] = t.str[i++];
108}
109
110size+=t.size;
111str[size + 1]='\0';
112
113}
114
115
116// 删除 :删除串中的一个子串
117void String:: Delete( int pos, int num)
118{
119//用if
else if语句判断删除的位置是否越界
120if(pos <= 0)
121{
122cout<<"无法完成删除操作!"<<endl<<"删除位置低于字符串长度为:"<< (-pos) +1<<endl;
123exit (1);
124}
125else if( pos > size )
126{
127cout<<"无法完成删除操作!"<<endl<<"删除位置超过字符串长度为:"<< pos - size <<endl;
128exit (1);
129}
130else if(pos >= 0 && pos <= size )
131{
132int i = pos - 1;
133for(int j = (pos + num -1); j <= size ; j++) //从删除到的位置开始前移
134{
135str[i] = str[j];
136i++;
137}
138}
139size = size-num; //只取删除后余下的字符个数
140}
141
142// 置换:置换串中的一个子串
143String:: Replace(String t1, int pos)
144{
145//用if
else if语句判断置换的位置是否越界
146if(pos <= 0)
147{
148cout<<"无法完成转换操作!"<<endl<<"置换位置低于字符串长度为:"<<(-pos) +1 <<endl;
149exit (1);
150}
151else if(pos > size)
152{
153cout<<"无法完成置换操作!"<<endl<<"置换位置超过字符串长度为:"<< pos - size <<endl;
154exit (1);
155}
156
157//当置换的位置加上置换给的子串长度之和超过原字符串的长度时
158//在将不被置换的字符串长度的拷后追加新字符串的长度
159else if( (t1.size+pos) >size )
160{
161
162char *q; //定义指针数组q用来转存原先将不被置换的字符串
163q = new char[ pos +1] ; //给q分配足够的空间为将不被置换的字符串的长度
164for( int i = 0;i < pos-1;i++ )
165{
166q[i] = str[i];
167
168}
169
170q[pos-1] = '\0';
171delete []str; //释放原字符串空间
172strcat(q,t1.str); //通过strcat函数将输入的子串与原子串的拷贝
173cout<<"置换后的字符串为: "<<q<<endl;
174return out = 0;
175}
176
177else if( (t1.size+pos) <= size )
178{
179int j = pos - 1;
180for(int i = 0;i < t1.size;i++)
181str[j++] = t1.str[i];
182delete t1.str;
183return out = 1;
184}
185
186
187}
188// 主函数
189int main( int argc, char * argv[])
190{
191int pos,num,k;
192String s,s1,s2,t,t1;
193do{
194cout<<"\n\n 1.生成字符串" ;
195cout<<"\n\n 2.取子串";
196cout<<"\n\n 3.插入子串s1";
197cout<<"\n\n 4.删除子串";
198cout<<"\n\n 5.置换子串";
199cout<<"\n\n 6.结束程序";
200cout<<"\n******************************** ";
201cout<<"\n 请输入你的选择(1,2,3,4,5,6)";
202cin>>k;
203switch(k){
204case 1:{
205cout<<"请输入一个字符串:";
206s.Creat();
207cout<<"字符串为: ";
208s.Display();
209}break;
210case 2:{
211cout<<"请输入子串的截取位置pos及子串长度num"<<endl;
212cin>>pos>>num;
213t = s.SubString(pos,num);
214cout<<"你所取的子串为: ";
215t.Display();
216}break;
217case 3:{
218cout<<"请输入子串插入位置pos"<<endl;
219cin>>pos;
220cout<<"请输入要插入的子串: ";
221s1.Creat();
222s.Insert(s1,pos);
223cout<<"插入后的字符串为: ";
224s.Display();
225}break;
226case 4:{
227cout<<"请输入要删除子串的开始位置pos及子串长度num"<<endl;
228cin>>pos>>num;
229s.Delete(pos,num);
230cout<<"删除后的字符串为: ";
231s.Display();
232}break;
233case 5:{
234cout<<"请输入子串置换位置pos"<<endl;
235cin>>pos;
236cout<<"请输入要置换的子串: ";
237s2.Creat();
238s.Replace(s2,pos);
239if( out != 0 )
240{
241cout<<"置换后的字符串为: ";
242s.Display();
243}
244}break;
245default:break;
246} //switch
247cout<<"\n--------------------------------- ";
248}while(k>=1&&k<6);
249cout<<"\n 再见!";
250cout<<"\n 按任意键,返回。";
251return 0;
252}
253