C++PrimerPlus学习——第十七章编程练习
生活随笔
收集整理的這篇文章主要介紹了
C++PrimerPlus学习——第十七章编程练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
17-1
不知道有沒有理解錯題意,參考list17.14
17-2
vs生成解決方案后,在命令行中打開.exe可執行文件,輸入文件路徑,文件內容,文件內容以CTRL+Z結束
17-3
換成用argc輸入
17-4
#include <iostream> #include <fstream> #include <string> #include <cstdlib>int main(int argc, char* argv[]) {using namespace std;string line1, line2;ifstream fin1("C:/test1.txt");ifstream fin2("C:/test2.txt");ofstream fout("C:/test.txt");if (fin1.is_open() && fin2.is_open() && fout.is_open()){while (!fin1.eof() && !fin2.eof() && getline(fin1, line1) && getline(fin2, line2) && line1.size() > 0 && line2.size() > 0)fout << line1 << " " << line2 << endl;while (!fin1.eof() && getline(fin1, line1) && line1.size() > 0)fout << line1 << endl;while (!fin2.eof() && getline(fin2, line2) && line2.size() > 0)fout << line2 << endl;}else{cout << "Couldn't open files.";exit(EXIT_FAILURE);}fin1.close();fin2.close();fout.close();return 0; }17-5
跟之前一樣,逐行讀取,加到一起后,去重,輸出
17-6
enum classkind { Employee, Manager, Fink, Highfink };在test.txt中分別用0,1,2,3表示
增加了writeall和getall函數用于保存文件和輸入,SetAll等其他函數仍然用的是之前寫的沒變
emp.h
emp.cpp
#include <iostream> #include <fstream> #include <string> #include "emp.h" using std::cout; using std::cin; using std::endl;abstr_emp::~abstr_emp() {}void abstr_emp::writeall(std::ofstream& ofs) {ofs << fname << "\n" << lname << "\n" << job << "\n"; }void abstr_emp::getall(std::ifstream& ifs) {getline(ifs, fname);getline(ifs, lname);getline(ifs, job); }abstr_emp::abstr_emp() {fname = "Null";lname = "Null";job = "Null"; }abstr_emp::abstr_emp(const std::string& fn, const std::string& ln, const std::string& j) {fname = fn;lname = ln;job = j; }void abstr_emp::ShowAll() const {cout << "Fullname: " << fname << endl;cout << "Lastname: " << lname << endl;cout << "Job: " << job << endl; }void abstr_emp::SetAll() {cout << "Enter firstname: ";cin >> fname;cout << "Enter lastname: ";cin >> lname;cout << "Ente job: ";cin.get();getline(cin, job); }std::ostream& operator<<(std::ostream& os, const abstr_emp& e) {cout << e.fname << " " << e.lname << " " << e.job << endl;return os;// TODO: 在此處插入 return 語句 }employee::employee() :abstr_emp() { }employee::employee(const std::string& fn, const std::string& ln, const std::string& j) : abstr_emp(fn, ln, j) { }void employee::ShowAll() const {abstr_emp::ShowAll(); }void employee::SetAll() {abstr_emp::SetAll(); }void employee::writeall(std::ofstream& ofs) {ofs << Employee << endl;abstr_emp::writeall(ofs); }void employee::getall(std::ifstream& ifs) {abstr_emp::getall(ifs); }void manager::Data() const {cout << "Inchargeof: " << inchargeof << endl; }void manager::Get() {cout << "Enter Inchargeof: ";cin >> inchargeof; }manager::manager() :abstr_emp() {inchargeof = 0; }manager::manager(const std::string& fn, const std::string& ln, const std::string& j, int ico) : abstr_emp(fn, ln, j) {inchargeof = ico; }manager::manager(const abstr_emp& e, int ico) : abstr_emp(e), inchargeof(0) { }manager::manager(const manager& m) : abstr_emp(m) {inchargeof = m.inchargeof; }void manager::ShowAll() const {abstr_emp::ShowAll();cout << "Inchargeof: " << inchargeof << endl; }void manager::SetAll() {abstr_emp::SetAll();cout << "Enter inchargeof: ";cin >> inchargeof; }void manager::writeall(std::ofstream& ofs) {ofs << Manager << endl;abstr_emp::writeall(ofs);ofs << inchargeof << endl; }void manager::getall(std::ifstream& ifs) {abstr_emp::getall(ifs);ifs >> inchargeof; }void fink::Data() const {cout << "Reportsto: " << reportsto << endl; }void fink::Get() {cout << "Enter resportsto: ";cin.get();getline(cin, reportsto); }fink::fink() :abstr_emp() {reportsto = "Null"; }fink::fink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo) : abstr_emp(fn, ln, j), reportsto(rpo) { }fink::fink(const abstr_emp& e, const std::string& rpo) : abstr_emp(e), reportsto(rpo) { }fink::fink(const fink& e) : abstr_emp(e) {reportsto = e.reportsto; }void fink::ShowAll() const {abstr_emp::ShowAll();cout << "Reports to: " << reportsto << endl; }void fink::SetAll() {abstr_emp::SetAll();cout << "Enter reports to: ";cin.get();getline(cin, reportsto); }void fink::writeall(std::ofstream& ofs) {ofs << Fink << endl;abstr_emp::writeall(ofs);ofs << reportsto << endl; }void fink::getall(std::ifstream& ifs) {abstr_emp::getall(ifs);ifs >> reportsto; }highfink::highfink() :abstr_emp(), manager(), fink() { }highfink::highfink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo, int ico) : abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo) { }highfink::highfink(const abstr_emp& e, const std::string& rpo, int ico) : abstr_emp(e), manager(e, ico), fink(e, rpo) { }highfink::highfink(const fink& f, int ico) : abstr_emp(f), manager(f, ico), fink(f) { }highfink::highfink(const manager& m, const std::string& rpo) : abstr_emp(m), manager(m), fink(m, rpo) { }highfink::highfink(const highfink& h) : abstr_emp(h), manager(h), fink(h) { }void highfink::ShowAll() const {abstr_emp::ShowAll();manager::Data();fink::Data(); }void highfink::SetAll() {abstr_emp::SetAll();manager::Get();fink::Get(); }void highfink::writeall(std::ofstream& ofs) {ofs << Highfink << endl;abstr_emp::writeall(ofs);manager::writeall(ofs);fink::writeReortsto(ofs); }void highfink::getall(std::ifstream& ifs) {abstr_emp::getall(ifs);manager::readInCharge(ifs);fink::readReortsto(ifs); }main.cpp
#include <iostream> #include "emp.h" #include <cstdlib> #include <fstream> const int MAX = 10; const char* file = "c:/test.txt";int main() {using namespace std;char ch;int i;abstr_emp* pc[MAX];ifstream fin;fin.open(file);if (!fin.is_open()){cout << "Couldn't open files!\n"; exit(EXIT_FAILURE);}int classtype;i = 0;while ((fin >> classtype).get(ch)){switch (classtype){case Employee: pc[i] = new employee;break;case Manager: pc[i] = new manager;break;case Fink: pc[i] = new fink;break;case Highfink: pc[i] = new highfink;break;}pc[i]->getall(fin);pc[i]->ShowAll();}fin.close();ofstream fout(file, ios::out | ios::app);if (!fout.is_open()){cerr << "Couldn't open file!";exit(EXIT_FAILURE);}int index = 0;cout << "Please choose the class you want to enter:\n"<< "e for employee, m for manager,\n"<< "f for fink, h for highfink,\n"<< "q to quit\n";while (cin >> ch && index < MAX){if (ch == 'q')break;switch (ch){case 'e': pc[index] = new employee;break;case 'm': pc[index] = new manager;break;case 'f': pc[index] = new fink;break;case 'h': pc[index] = new highfink;break;}pc[index]->SetAll();index++;cout << "Please choose the class you want to enter:\n"<< "e for employee, m for manager,\n"<< "f for fink, h for highfink,\n"<< "q to quit\n";}for (i = 0; i < index; i++)pc[i]->writeall(fout);fout.close();fin.clear();fin.open(file);if (fin.is_open()){cout << "Here are the contents of the " << file << " file:\n";int classtype;i = 0;while ((fin >> classtype).get(ch)){switch (classtype){case Employee: pc[i] = new employee;break;case Manager: pc[i] = new manager;break;case Fink: pc[i] = new fink;break;case Highfink: pc[i] = new highfink;break;}pc[i]->getall(fin);pc[i]->ShowAll();}fin.close();}cout << "Done.\n";return 0; }17.7
不知道為什么從.dat中讀取的信息有亂碼
總結
以上是生活随笔為你收集整理的C++PrimerPlus学习——第十七章编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: solaris php,针对 Solar
- 下一篇: Android 一直往文件写数据_对标苹