⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 substitute.cpp

📁 卡耐基SSD6全部选择题和练习题解决方法。
💻 CPP
字号:
#include <iostream> 
using namespace std; 
using namespace System; 
using namespace System::IO; 
using namespace System::Text; 

//String* insertChar(String *data, int loc, String *replacement, int i) { 
//  return data->Insert(loc, replacement->Substring(i, 1)); 
//} 

String* replace_word(String* data, int loc, int length, String* replacement) { 
    // delete the pattern string from loc: 
    data = data->Remove(loc, length); 
    // insert each character of the replacement string: 
    //for (int i = 0; i < replacement->Length; i++) { 
    //  data = insertChar(data, loc+i, replacement, i); 
    //  // data->Insert(loc + i, replacement->Substring(i, 1)); 
    //} 
    return data->Insert(loc,replacement); 
    //->Insert(loc,replacement) 
} 

String* string_subst(String *data, String *pattern, String *replacement) { 
    try { 
        int loc; 
        int location = 0; 
        int pl = pattern->Length; 
        // find every occurrence of pattern: 
        for (loc = data->IndexOf(pattern, 0); loc >= 0; 
            loc = data->IndexOf(pattern, location)) { 
                // replace word 
                int dis = loc - pl; 
                if (dis >= 0) 
                { 
                    location = dis; 
                }  
                else 
                { 
                    location = 0; 
                } 
                data = replace_word(data, loc, pl, replacement); 
        }    
        return data; 
    } catch (Exception *e) { 
        Console::WriteLine("Error in substitute "); 
        Console::WriteLine(e->ToString()); 
        return data; 
    } 
} 


String* batch_subst(String *data, const char* subs_filename) { 
    try { 
        String *subs_file = new String(subs_filename); 
        StreamReader *subs_reader = new StreamReader(subs_file); 
        String *pattern, *replacement, *separator; 
        while (subs_reader->Peek() >= 0) { 
            pattern = subs_reader->ReadLine(); 
            replacement = subs_reader->ReadLine(); 
            separator = subs_reader->ReadLine(); 
            data = string_subst(data, pattern, replacement); 
        } 
        return data; 
    } catch(Exception* e ) { 
        Console::WriteLine( "Error in do_substitutions "); 
        Console::WriteLine( e->ToString()); 
        return data; 
    } 
} 

void process_file(const char* filename, const char* subs_filename) { 
    StreamReader *reader; 
    StreamWriter *writer; 
    String *file = new String(filename); 
    try { 
        reader = new StreamReader( file ); 
        String *data = reader->ReadToEnd(); 
        data = batch_subst(data, subs_filename); 
        reader->Close(); 
        // write the data 
        writer = new StreamWriter(file, false); 
        writer->Write(data); 
        writer->Close(); 
    }  catch(Exception* e) { 
        Console::WriteLine( "Error while processing file "); 
        Console::WriteLine( e->ToString()); 
    } 
} 

int main(int argc, char *argv[]) {   
    if (argc < 3) { 
        cout << "Not enough input arguments" << endl; 
        cout << "Usage: substitute subs-file src1 src2 ..." << endl; 
    } else { 

        for (int i = 2; i < argc; i++) { 
            process_file(argv[i], argv[1]); 
        } 
    } 
    return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -