다라다라V
article thumbnail
728x90
반응형
본 글은 "황기태"님의 [명품 C++ Programming]의 연습 문제 답을 공유하고자 작성되었으며, 필자가 직접 문제를 풀며 작성한 것이기에 오류가 있을 수 있습니다. 댓글로 알려주시면 반영하도록 하겠습니다.

시리즈 보기
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 3장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 5장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 6장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 7장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 13장 연습 문제 풀이 (이론 문제)

 

1. 💡 

<cpp />
#include <iostream> #include <string> #include <fstream> using namespace std; int main() { ifstream fin("words.txt"); if (!fin) { cout << "words.txt 열 수 없습니다."; return 0; } int ch; while ((ch = fin.get()) != EOF) cout.put(ch); fin.close(); }

 

2. 💡 

<cpp />
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream fin("c:\\temp\\system.ini"); if (!fin) { cout << "파일 열기 실패"; return 0; } string line; int num =1; while (getline(fin, line)) { cout << num << " : " << line << endl; num++; } fin.close(); }

 

3. 💡 

<cpp />
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fin("c:\\temp\\system.ini"); if (!fin) { cout << "파일 열기 실패"; return 0; } int ch; while ((ch = fin.get()) != EOF) { cout << (char)toupper(ch); } fin.close(); }

 

4. 💡 

<cpp />
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fin("c:\\temp\\system.ini"); if (!fin) { cout << "파일 열기 실패"; return 0; } ofstream fout("c:\\temp\\system.txt"); if (!fout) { cout << "파일 열기 실패"; return 0; } int ch; while ((ch = fin.get()) != EOF) { fout << (char)toupper(ch); } fin.close(); fout.close(); }

 

5. 💡 6번

<cpp />
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream fin("c:\\temp\\system.ini", ios::in | ios::binary); ofstream fout("c:\\temp\\system.txt", ios::out | ios::binary); if (!fin | !fout) { cout << "파일 열기 실패"; return 0; } fin.seekg(0, ios::end); int size = fin.tellg(); int part = size / 10; fin.seekg(0, ios::beg); int ch = 0, cnt = 0, i = 0; cout << "복사 시작..." << endl; while ((ch = fin.get()) != EOF) { fout << (char)ch; cnt++; if (cnt % part == 0) { cout << "." << part << "B " << (i + 1) * 10 << "%" << endl; i++; } } cout << size << "B 복사 완료" << endl; fin.close(); fout.close(); }

 

6. 💡 9번

<cpp />
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; int main() { fstream fin("c:\\temp\\system.ini"); if (!fin) { cout << "파일 열기 실패"; return 0; } cout << "c:\\temp\\system.ini 파일 읽기 완료" << endl; cout << "라인 번호를 입력하세요/ 1보다 작은 값을 입력하면 종료" << endl; vector<string> file; string line; while (getline(fin, line)) file.push_back(line); int lineNum; do { cout << ": "; cin >> lineNum; if (lineNum > file.size()) cout << "숫자가 너무 큽니다."; cout << file[lineNum] << endl; } while (lineNum >= 0); cout << "종료합니다." << endl; fin.close(); }

 

7. 💡 13번

<cpp />
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; int main() { fstream fin("words.txt"); if (!fin) { cout << "파일 열기 실패"; return 0; } cout << "... word.txt 파일 로딩 완료" << endl; vector<string> word; string line; while (getline(fin, line)) word.push_back(line); cout << "검색을 시작합니다. 단어를 입력해주세요" << endl; string quiz = ""; while(true) { cout << "단어>> "; cin >> quiz; if (quiz == "exit") return 0; vector<string> find; for (int i = 0; i < word.size(); i++) { // substr은 문자열의 일부를 리턴 if (quiz == word[i].substr(0, quiz.length())) find.push_back(word[i]); } for (int i = 0; i < find.size(); i++) { cout << find[i] << endl; } if (find.size() == 0) cout << "발견할 수 없음" << endl; } }

 

 

반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!