Re: [問題] c++字串取代,助教給的程式碼看不懂

看板C_and_CPP作者 (Austin)時間14年前 (2010/03/21 03:06), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/5 (看更多)
感覺還蠻有趣的題目,看了大家的想法,實作了一下 ch_all = true時aaaa => bbbb,false時aaaa => aaab #include <iostream> #include <fstream> #include <cstring> using namespace std; const int max_length = 1024, a_length = 3; const bool ch_all = false; int main(int argc, char *argv[]) { ifstream Input("input.txt"); ofstream Output("output.txt"); if(!Input || !Output) { cout << "檔案開啟失敗!" << endl; return 0; } char str[max_length]; int chk_length = 0, cont_length = 0; while (Input.getline(str, max_length)) { /* 未幾碼不用看,最後一碼為'\0' */ chk_length = Input.gcount() - a_length; for(int i = 0; i < chk_length; ++i) { /* 找出有幾個連續出現的a */ cont_length = 0; while(ch_all || cont_length < a_length) { if(str[i+cont_length] != 'a') break; ++cont_length; } if(cont_length >= a_length) { /* 將連續的a取代成b */ memset(str + i, 'b', cont_length); /* 換掉不用再看 */ i += cont_length - 1; } else { /* 看過不用再看 */ i += cont_length; } } Output << str << endl; } Input.close(); Output.close(); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.240.19.135
文章代碼(AID): #1BfHog3Y (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1BfHog3Y (C_and_CPP)