神秘博客

C++ Primer Plus [第六版]第17章编程练习题答案

1

#include <iostream>

int main()
{
  using namespace std;
  char ch;

  cout << "输入字符计数[$停止]: \n";
  int count = 0;
  while (cin.peek() != '$')
  {
    cin.get(ch);
    count++;
  }
  cin.get(ch);
  cout << ch << ": " << count << endl;

  system("pause");
  return 0;
}

2

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

int main(int argc, char *argv[])
{
  using namespace std;

  if (argc == 1 || argc > 2)
  {
    cerr << "使用: " << argv[0] << " 文件名\n";
    exit(EXIT_FAILURE);
  }
  ofstream fout(argv[1]);
  if (!fout.is_open())
  {
    cerr << "无法打开 " << argv[1] << endl;
    exit(EXIT_FAILURE);
  }

  cout << "请输入内容[quit退出]: \n";
  string temp;
  while (getline(cin, temp) && temp != "quit")
    fout << temp << endl;

  cout << "完成\n";
  fout.close();

  return 0;
  
}

3

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

int main(int argc, char *argv[])
{
  using namespace std;
  if (argc == 1 || argc > 3)
  {
    cerr << "使用: " << argv[0] << " 源文件名 拷贝文件名\n";
    exit(EXIT_FAILURE);
  }
  ifstream fin(argv[1], ios::in);
  if (!fin.is_open())
  {
    cerr << "无法打开 " << argv[1] << endl;
    exit(EXIT_FAILURE);
  }
  ofstream fout(argv[2], ios::out | ios::app);
  if (!fout.is_open())
  {
    cerr << "无法打开 " << argv[2] << endl;
    exit(EXIT_FAILURE);
  }
  char ch;

  cout << "复制中...\n";
  while (fin.get(ch))
    fout << ch;
  if (fin.eof())
    cout << "正常复制完成...\n";
  else
    cout << "读取源文件错误...\n";
  cout << argv[1] << " 复制到 " << argv[2] << " 完成...\n";

  fin.close();
  fout.close();

  return 0;
}

4

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main()
{
  using namespace std;
  ifstream fin1;
  fin1.open("1.txt");
  if (!fin1.is_open())
  {
    cerr << "无法打开 1.txt\n";
    exit(EXIT_FAILURE);
  }
  ifstream fin2;
  fin2.open("2.txt");
  if (!fin2.is_open())
  {
    cerr << "无法打开 2.txt\n";
    exit(EXIT_FAILURE);
  }
  ofstream fout;
  fout.open("3.txt", ios::app);
  if (!fout.is_open())
  {
    cerr << "无法打开 3.txt\n";
    exit(EXIT_FAILURE);
  }

  string s1, s2;
  int count1 = 0;
  int count2 = 0;
  while (getline(fin1, s1))
    count1++;
  while (getline(fin2, s2))
    count2++;
  cout << "任务: 1.txt 拼接 2.txt 输出到 3.txt 进行中...\n";
  s1 = s2 = "";
  fin1.clear();
  fin1.close();
  fin2.clear();
  fin2.close();


  fin1.open("1.txt");
  if (!fin1.is_open())
  {
    cerr << "无法打开 1.txt\n";
    exit(EXIT_FAILURE);
  }
  fin2.open("2.txt");
  if (!fin2.is_open())
  {
    cerr << "无法打开 2.txt\n";
    exit(EXIT_FAILURE);
  }


  while (getline(fin1, s1) && getline(fin2, s2))
    fout << s1 << " " << s2 << endl;
  if (count1 > count2)
  {
    fout << s1 << endl;
    while (getline(fin1, s1))
      fout << s1 << endl;
  }
  else
  {
    fout << s2 << endl;
    while (getline(fin2, s2))
      fout << s2 << endl;
  }
  
  cout << "完成\n";
  fin1.close();
  fin2.close();
  fout.close();

  system("pause");
  return 0;
}

5

#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <fstream>
#include <cstdlib>

void Show(const std::string &st);

int main()
{
  using namespace std;
  list<string> MatFriend;  //Mat朋友
  string temp;  //临时数据
  list<string> PatFriend;  //Pat朋友

  ifstream finMat;
  finMat.open("mat.dat");
  if (finMat.is_open())
  {
    while (finMat >> temp)
      MatFriend.push_back(temp);
  }
  else
  {
    ofstream foutMat;
    foutMat.open("mat.dat");
    if (!foutMat.is_open())
    {
      cerr << "无法创建 Mat.dat\n";
      exit(EXIT_FAILURE);
    }
    cout << "请输入 Mat 的朋友[\"quit\"退出]: \n";
    while (getline(cin, temp) && temp != "quit")
    {
      foutMat << temp << " ";
      MatFriend.push_back(temp);
    }
  }

  ifstream finPat;
  finPat.open("pat.dat");
  if (finPat.is_open())
  {
    while (finPat >> temp)
      PatFriend.push_back(temp);
  }
  else
  {
    ofstream foutPat;
    foutPat.open("pat.dat");
    if (!foutPat.is_open())
    {
      cerr << "无法创建 Pat.dat\n";
      exit(EXIT_FAILURE);
    }
    cout << "请输入 Pat 的朋友[\"quit\"退出]: \n";
    while (getline(cin, temp) && temp != "quit")
    {
      foutPat << temp << " ";
      PatFriend.push_back(temp);
    }
  }

  cout << "Mat 的朋友: \n";
  for_each(MatFriend.begin(), MatFriend.end(), Show);
  cout.put('\n');

  cout << "Pat 的朋友: \n";
  for_each(PatFriend.begin(), PatFriend.end(), Show);
  cout.put('\n');

  list<string> Friend;
  list<string>::iterator p;  //创建容器指针

  Friend = MatFriend;
  for (p = PatFriend.begin(); p != PatFriend.end(); p++)
    Friend.push_back(*p);
  Friend.sort(); //排序
  Friend.unique(); //去除相邻重复值

  cout << "Mat and Pat 共同朋友: \n";
  for_each(Friend.begin(), Friend.end(), Show);
  cout.put('\n');

  ofstream MPat;
  MPat.open("matnpat.dat");
  if (!MPat.is_open())
  {
    cerr << "无法创建 matnpat.dat\n";
    exit(EXIT_FAILURE);
  }
  for (p = Friend.begin(); p != Friend.end(); p++)
    MPat << *p << " ";



  system("pause");
  return 0;


}
void Show(const std::string &st)
{
  std::cout << st << '\n';
}

6

#ifndef EMP_H_
#define EMP_H_
#include <iostream>
#include <string>
#include <fstream>

class abstr_emp
{
private:
  std::string fname;  //名字
  std::string lname;  //姓氏
  std::string job;    //职业
public:
  abstr_emp();
  abstr_emp(const std::string &fn, const std::string &ln, const std::string &j) :fname(fn), lname(ln), job(j) {}
  virtual void ShowAll() const;
  virtual void SetAll();
  virtual void Writeall(std::ofstream &f);
  virtual void GetAll(std::ifstream &f);
  friend std::ostream &operator<<(std::ostream &os, const abstr_emp &e);
  virtual ~abstr_emp() = 0;
};
class employee :public abstr_emp
{
public:
  employee() :abstr_emp() {}
  employee(const std::string &fn, const std::string &ln, const std::string &j) :abstr_emp(fn, ln, j) {}
  virtual void ShowAll() const { abstr_emp::ShowAll(); }
  virtual void SetAll() { abstr_emp::SetAll(); }
  void Writeall(std::ofstream &f);
  void GetAll(std::ifstream &f) { abstr_emp::GetAll(f); }
};
class manager :virtual public abstr_emp
{
private:
  int inchargeof;  //职责
protected:
  int InChargOf() const { return inchargeof; }
  int &InChargeOf() { return inchargeof; }
public:
  manager() :abstr_emp() { inchargeof = 0; }
  manager(const std::string &fn, const std::string &ln, const std::string &j, int ico = 0) :abstr_emp(fn, ln, j), inchargeof(ico) {}
  manager(const abstr_emp &e, int ico) :abstr_emp(e), inchargeof(ico) {}
  manager(const manager &m);
  virtual void ShowAll() const;
  virtual void SetAll();
  void Writemanager(std::ofstream &f) { abstr_emp::Writeall(f); f << inchargeof << '\n'; }
  void Writeall(std::ofstream &f);
  void GetAll(std::ifstream &f);
};
class fink :virtual public abstr_emp
{
private:
  std::string reportsto;   //报告
protected:
  const std::string ReportsTo() const { return reportsto; }
  std::string &ReportsTo() { return reportsto; }
public:
  fink() :abstr_emp() { reportsto = "no"; }
  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(const abstr_emp &e, const std::string &rpo) :abstr_emp(e), reportsto(rpo) {}
  fink(const fink &e) :abstr_emp(e) { reportsto = e.reportsto; }
  virtual void ShowAll() const;
  virtual void SetAll();
  void Writefink(std::ofstream &f) { abstr_emp::Writeall(f); f << reportsto << '\n'; }
  void Writeall(std::ofstream &f);
  void GetAll(std::ifstream &f);
};
class highfink :public manager, public fink
{
public:
  highfink() :abstr_emp(), manager(), fink() {}
  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(const abstr_emp &e, const std::string &rpo, int ico) :abstr_emp(e), manager(e, ico), fink(e, rpo) {}
  highfink(const fink &f, int ico) :abstr_emp(f), manager(f, ico), fink(f) {}
  highfink(const manager &m, const std::string &rpo) :abstr_emp(m), manager(m), fink(m, rpo) {}
  highfink(const highfink &h) :abstr_emp(h), manager(h), fink(h) {}
  virtual void ShowAll() const;
  virtual void SetAll();
  void Writeall(std::ofstream &f);
  void GetAll(std::ifstream &f);
};
#endif
#include "emp.h"
abstr_emp::abstr_emp()
{
  fname = lname = job = "no";
}
void abstr_emp::ShowAll() const
{
  std::cout << "姓名: " << lname << " " << fname << std::endl;
  std::cout << "职业: " << job << std::endl;
}
void abstr_emp::SetAll()
{
  std::cout << "请输入姓氏: ";
  getline(std::cin, lname);
  std::cout << "请输入名字: ";
  getline(std::cin, fname);
  std::cout << "请输入职业: ";
  getline(std::cin, job);
}

void abstr_emp::Writeall(std::ofstream &f)
{
  f << lname << '\n';
  f << fname << '\n';
  f << job << '\n';
}
void abstr_emp::GetAll(std::ifstream &f)
{
  getline(f, lname);
  getline(f, fname);
  getline(f, job);
}

std::ostream &operator<<(std::ostream &os, const abstr_emp &e)
{
  os << "姓名: " << e.lname << " " << e.fname << std::endl;
  os << "职业: " << e.job << std::endl;
  return os;
}
abstr_emp::~abstr_emp()
{
}

void employee::Writeall(std::ofstream &f)
{
  f << "1: \n";
  abstr_emp::Writeall(f);
}



manager::manager(const manager &m) :abstr_emp(m)
{
  inchargeof = m.inchargeof;
}
void manager::ShowAll() const
{
  abstr_emp::ShowAll();
  std::cout << "职责: " << inchargeof << std::endl;
}
void manager::SetAll()
{
  abstr_emp::SetAll();
  std::cout << "请输入职责: ";
  std::cin >> inchargeof;
}
void manager::Writeall(std::ofstream &f)
{
  f << "2: \n";
  Writemanager(f);
}
void manager::GetAll(std::ifstream &f)
{
  abstr_emp::GetAll(f);
  f >> inchargeof;
  f.get();
}


void fink::ShowAll() const
{
  abstr_emp::ShowAll();
  std::cout << "报告: " << reportsto << std::endl;
}
void fink::SetAll()
{
  abstr_emp::SetAll();
  std::cout << "请输入报告: ";
  getline(std::cin, reportsto);
}
void fink::Writeall(std::ofstream &f)
{
  f << "3: \n";
  Writefink(f);
}
void fink::GetAll(std::ifstream &f)
{
  abstr_emp::GetAll(f);
  getline(f, reportsto);
}



void highfink::ShowAll() const
{
  manager::ShowAll();
  std::cout << "报告: " << fink::ReportsTo() << std::endl;
}
void highfink::SetAll()
{
  manager::SetAll();
  std::cin.get();
  std::cout << "请输入报告: ";
  getline(std::cin, fink::ReportsTo());
}
void highfink::Writeall(std::ofstream &f)
{
  f << "4: \n";
  manager::Writemanager(f);
  f << fink::ReportsTo() << '\n';
}
void highfink::GetAll(std::ifstream &f)
{
  manager::GetAll(f);
  getline(f, fink::ReportsTo());
}
#include <iostream>
#include "emp.h"


inline void discard() 
{
  while (std::cin.get() != '\n')
    continue;
}

int main()
{
  using namespace std;

  abstr_emp *p[10];
  ifstream fin;
  fin.open("data.txt");
  int i = 0;
  if (fin.is_open())
  {
    string temp;
    while (getline(fin, temp) && i < 10)
    {
      switch (temp[0])
      {
      case '1':
        p[i] = new employee;
        p[i]->GetAll(fin);
        i++;
        break;
      case '2':
        p[i] = new manager;
        p[i]->GetAll(fin);
        i++;
        break;
      case '3':
        p[i] = new fink;
        p[i]->GetAll(fin);
        i++;
        break;
      case '4':
        p[i] = new highfink;
        p[i]->GetAll(fin);
        i++;
        break;
      default:
        break;
      }
    }
  }
  fin.close();
  if (i != 0)
  {
    cout << "********************文件内容********************\n";
    cout << "当前文件内容: \n";
    for (int j = 0; j < i; j++)
    {
      p[j]->ShowAll();
      cout.put('\n');
    }
    cout << "************************************************\n";
  }
  else
  {
    cout << "********************文件内容********************\n";
    cout << "文件没有内容...\n";
    cout << "************************************************\n";
  }
  system("pause");
  system("cls");  //清屏

  cout << "********************操作菜单********************\n";
  cout << "1. 添加内容 \t2.退出\n";
  char ch;
  if (i < 10)
  {
    ofstream fout;
    fout.open("data.txt", ios::out | ios::app);
    if (!fout.is_open())
    {
      cerr << "无法打开 data.txt\n";
      exit(1);
    }
    while (cin >> ch && ch != '2')
    {
      discard();
      if (ch != '1')
      {
        cerr << "请输入1 or 2: ";
        continue;
      }
      system("cls");  //清屏
      cout << "********************操作菜单********************\n";
      cout << "1. 添加 employee \t2. 添加 manager\n";
      cout << "3. 添加 fink \t\t4. 添加 highfink\n";
    bak:cin >> ch;
      discard();
      string select = "1234";
      if (select.find(ch) == string::npos)
      {
        cerr << "请输入1 2 3 or 4: ";
        goto bak;
      }
      switch (ch)
      {
      case '1':
        p[i] = new employee;
        break;
      case '2':
        p[i] = new manager;
        break;
      case '3':
        p[i] = new fink;
        break;
      case '4':
        p[i] = new highfink;
        break;
      default:
        break;
      }
      p[i]->SetAll();
      p[i]->Writeall(fout);
      i++;
      if (i == 10)
      {
        cerr << "内容已满...\n";
        break;
      }
      system("cls");  //清屏
      cout << "********************操作菜单********************\n";
      cout << "1. 添加内容 \t2.退出\n";
    }
    fout.close();

  }
  else
    cerr << "内容已满...\n";

  //释放内存
  for (int j = 0; j < i; j++)
    delete p[j];
  
  fin.open("data.txt");
  i = 0;
  if (!fin.is_open())
  {
    cerr << "无法打开 data.txt\n";
    exit(1);
  }
  string temp;
  while (getline(fin, temp) && i < 10)
  {
    switch (temp[0])
    {
    case '1':
      p[i] = new employee;
      p[i]->GetAll(fin);
      i++;
      break;
    case '2':
      p[i] = new manager;
      p[i]->GetAll(fin);
      i++;
      break;
    case '3':
      p[i] = new fink;
      p[i]->GetAll(fin);
      i++;
      break;
    case '4':
      p[i] = new highfink;
      p[i]->GetAll(fin);
      i++;
      break;
    default:
      break;
    }
  }
    
  cout << "********************文件内容********************\n";
  cout << "当前文件内容: \n";
  for (int j = 0; j < i; j++)
  {
    p[j]->ShowAll();
    cout.put('\n');
  }
  cout << "************************************************\n";
  //释放内存
  for (int j = 0; j < i; j++)
    delete p[j];
  fin.close();


  system("pause");
  return 0;
}

7

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

void ShowStr(const std::string &s);

class Store
{
private:
  std::ofstream &fout;
public:
  Store(std::ofstream &f) :fout(f) {}
  void operator()(const std::string &s)
  {
    int len = s.size();
    fout.write((char *)&len, sizeof(int));
    fout.write(s.data(), len);
  }
};

void GetStrs(std::ifstream &f, std::vector<std::string> &s);

int main()
{
  using namespace std;
  vector<string> vostr;
  string temp;

  cout << "输入字符串[空行退出]: \n";
  while (getline(cin, temp) && temp[0] != '\0')
    vostr.push_back(temp);
  cout << "这是你的输入...\n";
  for_each(vostr.begin(), vostr.end(), ShowStr);

  ofstream fout("strings.dat", ios::out | ios::binary);
  for_each(vostr.begin(), vostr.end(), Store(fout));
  fout.close();

  vector<string> vistr;
  ifstream fin("strings.dat", ios::in | ios::binary);
  if (!fin.is_open())
  {
    cerr << "无法打开文件...\n";
    exit(1);
  }
  GetStrs(fin, vistr);
  cout << "\n当前文件内容: \n";
  for_each(vistr.begin(), vistr.end(), ShowStr);
  fin.close();

  system("pause");
  return 0;
}

void ShowStr(const std::string &s)
{
  std::cout << s << std::endl;
}

void GetStrs(std::ifstream &f, std::vector<std::string> &s)
{
  int len;
  while (f.read((char *)&len, sizeof(int)))
  {
    char *str = new char[len];
    f.read(str, len);
    str[len + 1] = '\0';
    s.push_back(str);
  }
}

 

版权说明:
点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏