C++ primer plus第六版第17章编程练习

1.

#include <iostream>
using namespace std;
int main() {
  char ch;
  int count = 0;
  while (std::cin.get(ch) && ch != '$') count++;
  if (ch == '$')
    cin.putback(ch);
  else
    cout << "End\n";
  cout << count << " characters are Read\n";
  cin.get(ch);
  cout << "The next character is " << ch << "\n";
  return 0;
}

2.键盘输入Ctrl+Z模拟文件尾

  cout << "Enter a file name:" << endl;
  string filename;
  getline(cin, filename);
  ofstream fout(filename);
  char ch;
  cout << "Enter contents:" << endl;
  while (cin && cin.get(ch)) fout << ch;
  fout.close();

3.

  cout << "Enter the copied file name:" << endl;
  string filename;
  getline(cin, filename);
  ifstream fin(filename);
  if (!fin.is_open()) {
    cout << filename << "is not exist!\n";
    return 0;
  }
  cout << "Enter the destinate file name:" << endl;
  getline(cin, filename);
  ofstream fout(filename);
  char ch;
  while (fin && fin.get(ch)) fout << ch;
  fin.close();
  fout.close();

4.rt和d为前两题生成的文件

  ifstream fin1("rt");
  ifstream fin2("d");
  if (!fin1.is_open()) {
    cout << "the file rt cannot open!" << endl;
    return -1;
  }
  if (!fin2.is_open()) {
    cout << "the file d cannot open!" << endl;
    return -1;
  }
  string line1, line2, filename;
  cout << "Enter the destinate file name:" << endl;
  getline(cin, filename);
  ofstream fout(filename);

  while (true) {
    if (fin1) getline(fin1, line1);
    if (fin2) getline(fin2, line2);
    if (!fin1 && !fin2) break;
    if (fin1 && fin2) fout << line1 << " " << line2 << endl;
    if (!fin1 && fin2) fout << line2 << endl;
    if (fin1 && !fin2) fout << line1 << endl;
  }
  fin1.close();
  fin2.close();
  fout.close();

5.

  ifstream fin1("mat.dat");
  ifstream fin2("pat.dat");
  if (!fin1.is_open()) {
    cout << "the file mat.dat cannot open!" << endl;
    return -1;
  }
  if (!fin2.is_open()) {
    cout << "the file pat.dat cannot open!" << endl;
    return -1;
  }
  string line1, line2;
  vector<string> names1, names2, combine;
  ofstream fout("matnpat.dat");

  cout << "Mat's friends are:" << endl;
  while (fin1) {
    getline(fin1, line1);
    names1.push_back(line1);
    cout << line1 << endl;
  }
  fin1.close();

  cout << "Pat's friends are:" << endl;
  while (fin2) {
    getline(fin2, line2);
    names2.push_back(line2);
    cout << line2 << endl;
  }
  fin2.close();

  combine = names1;
  combine.insert(combine.end(), names2.begin(), names2.end());
  std::sort(combine.begin(), combine.end());
  int n = std::unique(combine.begin(), combine.end()) - combine.begin();
  for (int i = 0; i < n; ++i) {
    fout << combine[i] << endl;
  }
  fout.close();

6.第14章定义的employee、manager、fink和highfink见:

C++ primer plus第六版第14章编程练习

添加的WriteAll和GetAll函数为:

void abstr_emp::WriteAll(ofstream& fout) {
  fout << fname << endl;
  fout << lname << endl;
  fout << job << endl;
}
void abstr_emp::GetAll(ifstream& fin) {
  getline(fin, fname);
  getline(fin, lname);
  getline(fin, job);
}
void employee::WriteAll(ofstream& fout) {
  fout << "1" << endl;
  abstr_emp::WriteAll(fout);
}
void employee::GetAll(ifstream& fin) {
  string type;
  getline(fin, type);
  abstr_emp::GetAll(fin);
}
void manager::WriteAll(ofstream& fout) {
  fout << "2" << endl;
  abstr_emp::WriteAll(fout);
  fout << inchargeof << endl;
}
void manager::GetAll(ifstream& fin) {
  string type, in;
  getline(fin, type);
  abstr_emp::GetAll(fin);
  getline(fin, in);
  inchargeof = atoi(in.c_str());
}
void fink::WriteAll(ofstream& fout) {
  fout << "3" << endl;
  abstr_emp::WriteAll(fout);
  fout << reportsto << endl;
}
void fink::GetAll(ifstream& fin) {
  string type;
  getline(fin, type);
  abstr_emp::GetAll(fin);
  getline(fin, reportsto);
}
void highfink::WriteAll(ofstream& fout) {
  fout << "4" << endl;
  abstr_emp::WriteAll(fout);
  fout << fink::ReportsTo() << endl;
  fout << manager::InChargeOf() << endl;
}
void highfink::GetAll(ifstream& fin) {
  string type, in;
  getline(fin, type);
  abstr_emp::GetAll(fin);
  getline(fin, fink::ReportsTo());
  getline(fin, in);
  manager::SetInChargeOf(atoi(in.c_str()));
}

main中:

  std::vector<abstr_emp*> ems;
  ifstream fin;
  ofstream fout;
  fin.open("em.txt");
  if (fin.is_open()) {
    cout << "The contents of the file are:\n";
    string str;
    abstr_emp* pc = NULL;
    while (fin) {
      getline(fin, str);
      if (str == "1") {
        pc = new employee();
      } else if (str == "2") {
        pc = new manager();
      } else if (str == "3") {
        pc = new fink();
      } else if (str == "4") {
        pc = new highfink();
      }
      if (pc) {
        pc->GetAll(fin);
        pc->ShowAll();
      }
    }
  }
  fin.close();
  fout.open("em.txt");
  if (!fout.is_open()) {
    cerr << "Cannot open the file!";
    exit(EXIT_FAILURE);
  }
  int type;
  cout << "Please enter the type of employee(1 for employee,2 for "
          "manager,3 for fink,4 for highfink):";
  cin >> type;
  bool go_on = true;
  while (go_on) {
    abstr_emp* pc = NULL;
    switch (type) {
      case 1:
        pc = new employee();
        break;
      case 2:
        pc = new manager();
        break;
      case 3:
        pc = new fink();
        break;
      case 4:
        pc = new highfink();
        break;
      default:
        break;
    }
    if (ems.size() < MAX && pc) {
      pc->SetAll();
      pc->WriteAll(fout);
      ems.push_back(pc);
      if (ems.size() < MAX) {
        cout << "Please enter the type of employee(1 for employee,2 for "
                "manager,3 for fink,4 for highfink):";
        cin >> type;
      }
    } else
      go_on = false;
  }
  fout.close();

7.main函数:

  vector<string> vostr;
  string temp;
  cout << "Enter strings(empty line to quit):\n";
  while (getline(cin, temp) && temp[0] != '\0') vostr.push_back(temp);
  cout << "Here is your input.\n";
  for_each(vostr.begin(), vostr.end(), ShowStr);
  ofstream fout("strings.dat", ios_base::out | ios_base::binary);
  for_each(vostr.begin(), vostr.end(), Store(fout));
  fout.close();
  vector<string> vistr;
  ifstream fin("strings.dat", ios_base::in | ios_base::binary);
  if (!fin.is_open()) {
    cerr << "Could not open file for input.\n";
    exit(EXIT_FAILURE);
  }
  GetStrs(fin, vistr);
  cout << "\nHere are the strings read from the file:\n";
  for_each(vistr.begin(), vistr.end(), ShowStr);

用到的函数和类:

class Store {
 public:
  Store(ostream& fout) : fout_(fout) {}
  void operator()(const string& str) {
    size_t len = str.size();
    fout_.write((char*)&len, sizeof(std::size_t));
    fout_.write(str.data(), len);
  }

 private:
  ostream& fout_;
};
void ShowStr(const string& str) { cout << str << endl; }
void GetStrs(ifstream& fin, vector<string>& vistr) {
  size_t len;
  while (fin.read((char*)&len, sizeof(std::size_t))) {
    char* str = new char[len];
    fin.read(str, len);
    str[len + 1] = '\0';
    vistr.push_back(str);
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值