∪∩deniable Design

          個人JAVA版GAE(google app engine),struts2+jpa+jQuery開發(fā),互相交流 http://iunbug.appspot.com/
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

           

           

          使用說明

              程序名為 NO3.exe.運行環(huán)境為DOS,執(zhí)行后顯示:

          在"請輸入你的選擇后(1.2.3.4.5.6)"后輸入數字選擇執(zhí)行的功能.

          測試結果:

          1. 選擇1.后輸入:123456789

          2. 選擇2后輸入分別輸入1,3.

            重復1)操作后選擇2,分別輸入10,10.

          3.  
          4. 重復1)操作后選擇3.分別輸入1,abcde

             

            再重復1)操作后選擇3.分別輸入9,abcde

           

           

           

           

           

           

          1.  
          2. 再重復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"的數字

           

          調試過程:

          1. 本調試主要針對置換操作功能進行演示:
            1. 將光標移置String:: Replace(String t1,int pos)函數的第一條語句處Ctrl+F10開始調試
            2. 在DOS窗口中選擇1后輸入"123456789".接著選擇5分別輸入1,abcde.

              這時Debugger停留在String:: Replace(String t1,int pos)的第一條語句處:

            3. 在Watch窗口的名稱欄分別輸入: str, t1.str, q, out, pos, (-pos) +1, pos – size,j,i.進行觀察.

            4. 按F10開始單步調試.
              1. 按F10三次后Debugger停留在最后一個判斷語句處.同時Watch窗口中個名稱的值分別為:

              2. 接著單步調試,for()函數完后,Debugger停留在" delete t1.str;"語句處.

                這時Watch窗口中個各名稱的值分別為:

                接著兩次F10,這時t1.str和out的值已經改變,Debugger停留在String:: Replace(String t1,int pos)的結束處.

              3. 再按一次F10,Debugger停留在main()函數的switch(k)里的case 5的if()語句處:

                F10到調用Display()函數的語句處后F11跟進Display()的內部.

                在Watch窗口的名稱中輸入str,I,len進行觀察.

                單步調試到Display()函數結束,Debugger停留在Display()函結束處.

                在Watch窗口中str,I,len的值分別為:

                同時DOS窗口中顯示如下:

            5. 按Shift+F5退出調試.完成調試操作.

             

             參考源碼:

          2.  

              1Code:
              2//3.h
              3#include <iostream.h>
              4#include <string.h>
              5#include <stdlib.h>
              6//using namespace std;
              7int out;        // 定義一個全局變量
              8class String
              9
             10    public:
             11         String(){}
             12         ~String(){}
             13         String SubString(int pos,int num);        //取子串函數
             14         void Insert(String t,int pos);            //插入子串函數
             15         void Delete(int pos,int num);            //刪除子串函數
             16         void Creat();                            //生成字符串函數
             17         void Display();                        //打印子串函數
             18         Replace(String t1,int pos);            //置換子串函數
             19      private:
             20         char *str;
             21         int size;
             22 }
            ;
             23
             24//3.cpp
             25#include "3.h"
             26 //生成新字符串函數
             27void String:: Creat()
             28 {
             29        char s[100];  
             30        cin>>s;
             31        size=strlen(s);
             32        str=new char[size + 1];
             33        if(str==0
             34            cout<<"沒有申請到空間!"<<endl;
             35        strcpy( str, s);
             36        //strcpy_s(str,sizeof(str)/sizeof(str[0]),s);
             37        
             38 }

             39//輸出
             40void String::Display()
             41
             42    int len = size;
             43    int i;
             44    for( i=0;i < size;i++ )  
             45      cout<<str[i];
             46    cout<<endl;
             47    cout<<"字符串的總長度為:"<<len<<endl;
             48}

             49//求子串
             50//void String String::SubString(int pos,int num)
             51String String::SubString(int pos,int num)
             52
             53    String temp;
             54    int left=size-pos + 1;//從pos位置開始所有余下的字符串長度
             55    char *p,*q;
             56    if( pos > size ) 
             57    {
             58        cout<<"錯誤!"<<endl;
             59        cout<<"指定位置超過字符串長度為:"<< pos - size<<endl;//提示所取子串的錯誤位置
             60        exit (1);//異常退出
             61    }

             62    else if( num > left ) 
             63        num = left;
             64    temp.str=new char[num+1];//重新分配內存空間
             65    if(temp.str==0)   
             66       cout<<"沒有申請到空間!"<<endl;
             67    p=temp.str;
             68    for(int i = pos-1;i < pos+num-1;i++)
             69          {  
             70            q = &str[i];    
             71            *= *q;
             72            p++;
             73          }

             74    *p=0;
             75    temp.size = num;
             76    return temp;
             77}

             78
             79//插入運算:在串對象s的pos位置后插入一個串t
             80void String::Insert(String t,int pos)
             81{
             82    //String temp;
             83    int i_len = t.size;
             84    cout<<"插入字符串的長度為:"<<i_len<<endl;
             85
             86    char *q;
             87    q = new char( size + 1);
             88    strcpy(q,str);
             89
             90    delete str;
             91    str = new char( size + t.size + 1);        //字符串長度要在原長上增加插入串的長度
             92
             93
             94    strcpy(str,q);
             95
             96    //依次后移,空出長度為插入字符串的長度的空間
             97
             98    for(int j = size-1;j > pos-1;j--)        
             99    {
            100            int i = j + t.size;
            101            str[ i-- ] = str[ j ];
            102    }

            103
            104    j = pos;
            105    for(int i = 0;i < t.size;)
            106    {    
            107        str[j++= t.str[i++];
            108    }

            109
            110    size+=t.size;
            111    str[size + 1]='\0';
            112
            113}

            114
            115
            116//刪除 :刪除串中的一個子串
            117void  String:: Delete(int pos,int num)
            118
            119    //用ifelse if語句判斷刪除的位置是否越界
            120    if(pos <= 0)
            121    {
            122        cout<<"無法完成刪除操作!"<<endl<<"刪除位置低于字符串長度為:"<< (-pos) +1<<endl;
            123        exit (1);
            124    }

            125    else if( pos > size )
            126    {
            127        cout<<"無法完成刪除操作!"<<endl<<"刪除位置超過字符串長度為:"<< pos - size <<endl;
            128        exit (1);
            129    }

            130    else if(pos >= 0 && pos <= size )
            131    {
            132            int i = pos - 1;
            133            for(int j = (pos + num -1); j <= size ; j++//從刪除到的位置開始前移
            134            {
            135                str[i] = str[j];
            136                i++;
            137            }

            138    }

            139    size = size-num;    //只取刪除后余下的字符個數
            140}

            141
            142//置換:置換串中的一個子串
            143 String:: Replace(String t1,int pos)
            144
            145    //用ifelse if語句判斷置換的位置是否越界
            146    if(pos <= 0)
            147    {    
            148        cout<<"無法完成轉換操作!"<<endl<<"置換位置低于字符串長度為:"<<(-pos) +1 <<endl;
            149        exit (1);
            150    }

            151    else if(pos > size)
            152    {
            153        cout<<"無法完成置換操作!"<<endl<<"置換位置超過字符串長度為:"<< pos - size <<endl;
            154        exit (1);
            155    }

            156    
            157    //當置換的位置加上置換給的子串長度之和超過原字符串的長度時
            158    //在將不被置換的字符串長度的拷后追加新字符串的長度
            159    else if( (t1.size+pos) >size )
            160    {
            161                
            162        char *q;    //定義指針數組q用來轉存原先將不被置換的字符串
            163        q = new char[ pos +1] ;    //給q分配足夠的空間為將不被置換的字符串的長度
            164        forint i = 0;i < pos-1;i++ )
            165        {
            166            q[i] = str[i];
            167             
            168        }

            169    
            170        q[pos-1= '\0';
            171        delete []str;        //釋放原字符串空間
            172        strcat(q,t1.str);    //通過strcat函數將輸入的子串與原子串的拷貝
            173        cout<<"置換后的字符串為:   "<<q<<endl;
            174        return out = 0;
            175    }

            176
            177    else if( (t1.size+pos) <= size )
            178    {
            179        int j = pos - 1;
            180        for(int i = 0;i < t1.size;i++)
            181            str[j++= t1.str[i];
            182        delete t1.str;
            183        return out = 1;
            184    }

            185
            186    
            187}

            188//主函數
            189int main(int argc, char* argv[])
            190{  
            191    int pos,num,k; 
            192    String s,s1,s2,t,t1;
            193     do{  
            194        cout<<"\n\n    1.生成字符串"  ;
            195        cout<<"\n\n    2.取子串";
            196        cout<<"\n\n    3.插入子串s1";
            197        cout<<"\n\n    4.刪除子串";
            198        cout<<"\n\n    5.置換子串";
            199        cout<<"\n\n    6.結束程序";
            200        cout<<"\n******************************** ";
            201        cout<<"\n    請輸入你的選擇(1,2,3,4,5,6)";  
            202        cin>>k;
            203    switch(k){
            204       case 1:{
            205               cout<<"請輸入一個字符串:";
            206               s.Creat();
            207               cout<<"字符串為:       ";     
            208               s.Display();
            209              }
            break;
            210       case 2:
            211                cout<<"請輸入子串的截取位置pos及子串長度num"<<endl;
            212                cin>>pos>>num;
            213                t = s.SubString(pos,num);
            214                cout<<"你所取的子串為:  "
            215                t.Display();
            216               }
            break;
            217       case 3:
            218                cout<<"請輸入子串插入位置pos"<<endl;
            219                cin>>pos;
            220                cout<<"請輸入要插入的子串:  ";  
            221                s1.Creat();
            222                s.Insert(s1,pos);
            223                cout<<"插入后的字符串為:   ";  
            224                s.Display();
            225              }
            break;
            226       case 4:
            227               cout<<"請輸入要刪除子串的開始位置pos及子串長度num"<<endl;
            228               cin>>pos>>num;
            229               s.Delete(pos,num);
            230               cout<<"刪除后的字符串為:   ";
            231               s.Display();
            232             }
            break;
            233       case 5:
            234               cout<<"請輸入子串置換位置pos"<<endl;
            235               cin>>pos;
            236               cout<<"請輸入要置換的子串:  ";  
            237               s2.Creat();
            238               s.Replace(s2,pos);
            239               ifout != 0 )
            240               {
            241                   cout<<"置換后的字符串為:   ";  
            242                   s.Display();
            243               }

            244              }
            break;
            245     default:break;
            246  }
             //switch
            247     cout<<"\n--------------------------------- ";
            248}
            while(k>=1&&k<6);
            249  cout<<"\n          再見!";
            250  cout<<"\n     按任意鍵,返回。";
            251  return 0;
            252}

            253

             

          主站蜘蛛池模板: 天镇县| 瓦房店市| 彝良县| 亳州市| 井陉县| 靖州| 伊川县| 平安县| 公安县| 兴隆县| 东明县| 枣庄市| 迭部县| 庆阳市| 古蔺县| 疏附县| 北流市| 昌平区| 南岸区| 宝鸡市| 西华县| 安阳市| 太仓市| 沙河市| 庄浪县| 靖安县| 桃园市| 兴宁市| 庄河市| 拉孜县| 闽侯县| 墨玉县| 桓台县| 贵德县| 广西| 长子县| 青田县| 中牟县| 奎屯市| 延川县| 类乌齐县|