Re: [問題] c++字串取代,助教給的程式碼看不懂
※ 引述《kimgtob (K.L)》之銘言:
: 抱歉今天發了很多次文章
: 也問過同學了@_@,不過有些地方還是不懂
: 題目大意 將一文字檔內的連續3個a 換成 bbb
: 例 aa aa
: aaabbb bbbbbb
: bababa bababa
: cad cad
:
ifstream input;
ofstream output;
int count = 0;
char ch;
char check = 'a';
int matchtime = 3;
// open file and check ...
while(input.get(ch)) {
if(ch == check) {
++count;
if(count == matchtime) {
output << "bbb";
count = 0;
}
} else {
while(count) {
output << check;
--count;
}
output << ch;
}
}
for(int i = 0 ; i < count ; ++i) {
output << check;
}
// close file and return
: 卡在下面的for處 以及n的部分 還有while 中間如何取代的地方
: 他的for 放在while 外面 那跟n 與a的修改有關嗎?
: #include<iostream>
: #include<fstream>
: using namespace std;
: using std::cout;
: int main ()
: {
: ifstream Input;
: ofstream Output;
: int n=0;
: char ch;
: Output.open("output.txt");
: Input.open("inpit.txt");
: if(!Input)
: {
: Output<<"檔案開啟失敗";
: return 0;
: }
: while (Input.get(ch))
: {
: 這邊我想請問一下
: 要如何做到 邊讀邊修改這個動作?
: 它不是一個字一個字讀進去嗎?
: 我用一個counter去數
: 數到第三個時 要修改的話
: 有辦法往後退三個字元再換成b嗎?
: }
: // for (int i=0; i<n; i++)
: // Output<<'a';
: Input.close();
: Output.close();
: return 0;
: }
: 謝謝各位 ! 如有違反版歸自刪
--
希望消失到自由的風中,
渴望解除掉束縛的羈絆。
期望悄悄的消失在風中,
感受那沒有羈絆的自由。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.74.9.2
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 5 之 5 篇):