Visual C++ 6.0調試功能 圖解教程(2)--實例一
Posted on 2008-08-19 17:12 ∪∩BUG 閱讀(2022) 評論(0) 編輯 收藏 所屬分類: VC++/MFC學習筆記
使用說明
程序名為 NO3.exe.運行環(huán)境為DOS,執(zhí)行后顯示:
在"請輸入你的選擇后(1.2.3.4.5.6)"后輸入數字選擇執(zhí)行的功能.
測試結果:
-
選擇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