C++ Primer Plus 第六版 第十七章课后编程练习答案 - Go语言中文社区

C++ Primer Plus 第六版 第十七章课后编程练习答案


1.

play.cpp

#include <iostream>
using namespace std;

int main()
{
    cout << "Enter input: n";
    int count = 0;
    char ch;
    //题目要求将$留在输入流中, 用cin.get()函数来获取字符,这样停止获取的最后一个字符会留在输入流中
    cin.get(ch);
    while (ch != '$' && ch != 'n')
    {
        count ++;
        cin.get(ch);
    }
    if(ch == '$')
        cout << " $ has been deleted.n ";
    else
        cout << "Input finished.n";
    cout << "Total count: " << count << endl;

    return 0;
}

2.

play.cpp

#include <iostream>
using namespace std;
#include <cstdlib>
#include <fstream>
#include <string>

int main(int argc, char * argv[])
{
    ofstream fout;
    for(int i = 1; i < argc; i++)
    {
        fout.open(argv[i]);
        if(! fout.is_open())
        {
            cerr << "Can't open " << argv[i] << endl;
            exit(EXIT_FAILURE);
        }
        cout << "Enter input(enter blank line to quit): n";
        string input;
        while (getline(cin, input) && input.size() > 0)
            fout << input << endl;
        cout << "Input finished.n";
        fout.close();
    }

    return 0;
}

3.

play.cpp

#include <iostream>
using namespace std;
#include <cstdlib>
#include <fstream>
#include <string>

int main(int argc, char * argv[])
{
    ifstream  fin;
    ofstream fout;
    char ch;
    fin.open(argv[1]);
    fout.open(argv[2]);
    if(! fin.is_open())
    {
        cerr << "Can't open " << argv[1] << endl;
        fin.clear();
        exit(EXIT_FAILURE);
    }
    if(! fout.is_open())
    {
        cerr << "Can't open " << argv[2] << endl;
        fout.close();
        exit(EXIT_FAILURE);
    }
    cout << "Open successfully.n";
    cout << "Now begin to copy and paste.n";
    while (fin.get(ch))
        fout << ch;
    fin.clear();
    fin.close();
    fout.close();
    cout << "Copy and paste successfully.n";

    return 0;
}

4.

play.cpp

#include <iostream>
using namespace std;
#include <cstdlib>
#include <fstream>
#include <string>

int main(int argc, char * argv[])
{
    ifstream fin1, fin2;
    ofstream fout;
    fin1.open("input1.txt");
    fin2.open("input2.txt");
    fout.open("output.txt");
    if(! fin1.is_open())
    {
        cerr << "Can't open input1.txt n";
        fin1.clear();
        fin2.clear();
        fout.close();
        exit(EXIT_FAILURE);
    }
    if(! fin2.is_open())
    {
        cerr << "Can't open input2.txt n";
        fin1.clear();
        fin2.clear();
        fout.close();
        exit(EXIT_FAILURE);
    }
    if(! fout.is_open())
    {
        cerr << "Can't open output.txt n";
        fin1.clear();
        fin2.clear();
        fout.close();
        exit(EXIT_FAILURE);
    }
    cout << "Open successfully.n";
    cout << "Begin to read and write.n";
    string input1;
    string input2;
    int count1 = 0, count2 = 0, total_count;
    while (getline(fin1, input1))
        count1++;
    while (getline(fin2, input2))
        count2++;
    total_count = (count1 >= count2 ? count2 : count1);//取小

    //先使用两个输入流打开文件,使用getline()函数读一遍之后获取两个文件的行数,
    // 关闭文件之后又重新使用输入流再打开,进行数据的复制,此时因为第一遍读取的时候已经获取了行数,所以可以直接复制。
    fin1.clear();
    fin2.clear();
    fin1.close();
    fin2.close();
    fin1.open("input1.txt");
    fin2.open("input2.txt");
    if(! fin1.is_open())
    {
        cerr << "Can't open input1.txt n";
        fin1.clear();
        fin2.clear();
        fout.close();
        exit(EXIT_FAILURE);
    }
    if(! fin2.is_open())
    {
        cerr << "Can't open input2.txt n";
        fin1.clear();
        fin2.clear();
        fout.close();
        exit(EXIT_FAILURE);
    }
    for(int i = 0; i < total_count; i++)
    {
        getline(fin1, input1);
        getline(fin2, input2);
        fout << input1 << " " << input2 << endl;
    }
    if(count1 >= count2)
    {
        for(int i = total_count; i < count1; i++)
        {
            getline(fin1, input1);
            fout << input1 << endl;
        }
    }
    else
    {
        for(int i = total_count; i < count2; i++)
        {
            getline(fin2, input2);
            fout << input2 << endl;
        }
    }
    cout << "Finished.n";

    return 0;
}

5.

play.cpp

#include <iostream>
using namespace std;
#include <cstdlib>
#include <fstream>
#include <string>
#include <set>
#include <iterator>

int main()
{
    ifstream fin1, fin2;
    ofstream fout;
    fin1.open("mat.dat");
    fin2.open("pat.dat");
    fout.open("matnpat.dat");
    if(! fin1.is_open())
    {
        cerr << "Can't open mat.dat n";
        exit(EXIT_FAILURE);
    }
    if(! fin2.is_open())
    {
        cerr << "Can't open pat.dat n";
        exit(EXIT_FAILURE);
    }
    if(! fout.is_open())
    {
        cerr << "Can't open matnpat.dat n";
        exit(EXIT_FAILURE);
    }
    string temp;
    set<string> mat, pat, matnpat;
    while (getline(fin1, temp))
        mat.insert(temp);
    ostream_iterator<string, char> output(cout, "n");
    cout << "Mat's friends:n";
    copy(mat.begin(), mat.end(), output);
    while (getline(fin2, temp))
        pat.insert(temp);
    cout << "Pat's friends:n";
    copy(pat.begin(), pat.end(), output);
    ostream_iterator<string, char> out(fout, "n");
    set_union(mat.begin(), mat.end(), pat.begin(), pat.end(),
            insert_iterator<set<string>>(matnpat, matnpat.begin()));
    cout << "Total friends: n";
    copy(matnpat.begin(), matnpat.end(), output);
    copy(matnpat.begin(), matnpat.end(), out);

    return 0;
}

6.

emp.h

#ifndef EMP_H
#define EMP_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
enum classkind{Zero, Employee, Manager, Fink, HighFink};
class abstr_emp
{
    string fname;
    string lname;
    string job;
public:
    abstr_emp();
    abstr_emp(const string &fn, const string &ln, const string &j);
    virtual void ShowAll() const ;
    virtual void SetAll();
    virtual void setall(ifstream &fin);
    virtual void writeall(ofstream &fout);
    friend ostream &operator<<(ostream &os, const abstr_emp &e);
    virtual ~abstr_emp() = 0;
};

class employee : public abstr_emp
{
public:
    employee();
    employee(const string &fn, const string &ln, const string &j);
    virtual void ShowAll() const ;
    virtual void SetAll();
    virtual void setall(ifstream &fin);
    virtual void writeall(ofstream &fout);
};

class manager : virtual public abstr_emp
{
    int inchargeof;
protected:
    int InChargeOf() const { return inchargeof;}
    int & InChargeOf() { return inchargeof;}
public:
    manager();
    manager(const string &fn, const string &ln, const string &j, int ico = 0);
    manager(const abstr_emp &e, int ico);
    manager(const manager &m);
    virtual void ShowAll() const ;
    virtual void SetAll();
    virtual void setall(ifstream &fin);
    virtual void writeall(ofstream &fout);
};

class fink : virtual public abstr_emp
{
    string reportsto;
protected:
    const string ReportsTo() const { return reportsto;}
    string &ReportsTo() { return reportsto;}
public:
    fink();
    fink(const string &fn, const string &ln, const string &j, const string &rpo);
    fink(const abstr_emp &e, const string &rpo);
    fink(const fink &e);
    virtual void ShowAll() const ;
    virtual void SetAll();
    virtual void setall(ifstream &fin);
    virtual void writeall(ofstream &fout);

};

class highfink : public manager, public fink
{
public:
    highfink();
    highfink(const string &fn, const string &ln, const string &j, const string &rpo, int ico);
    highfink(const abstr_emp &e, const string &rpo, int ico);
    highfink(const fink &f, int ico);
    highfink(const manager &m, const string &rpo);
    highfink(const highfink &h);
    virtual void ShowAll() const ;
    virtual void SetAll();
    virtual void setall(ifstream &fin);
    virtual void writeall(ofstream &fout);
};

#endif

emp.cpp

#include "emp.h"
#include <iostream>
using namespace std;
#include <fstream>
#include <string>

abstr_emp::abstr_emp()
{
    fname = "None";
    lname = "None";
    job = "None";
}

abstr_emp::abstr_emp(const string &fn, const string &ln, const string &j)
{
    fname = fn;
    lname = ln;
    job = j;
}

void abstr_emp::ShowAll() const
{
    cout << "First name: " << fname << endl;
    cout << "Last name: " << lname << endl;
    cout << "Job: " << job << endl;
}

void abstr_emp::SetAll()
{
    cout << "Enter firstname: ";
    getline(cin, fname);
    cout << "Enter lastname: ";
    getline(cin, lname);
    cout << "Enter job: ";
    getline(cin, job);
}

void abstr_emp::setall(ifstream & fin)
{
    getline(fin, fname);
    getline(fin, lname);
    getline(fin, job);
}

void abstr_emp::writeall(ofstream & fout)
{
    fout << fname << endl << lname << endl << job << endl;
}

ostream &operator<<(ostream &os, const abstr_emp &e)
{
    os << endl;
    os << "Firstname: " << e.fname << endl;
    os << "Lastname: " << e.lname << endl;
    return os;
}

abstr_emp::~abstr_emp() {}

employee::employee()
:  abstr_emp()
{}

employee::employee(const string &fn, const string &ln, const string &j)
: abstr_emp(fn, ln, j)
{}

void employee::ShowAll() const
{
    cout << "nEmployee: " << endl;
    abstr_emp::ShowAll();
}

void employee::SetAll()
{
    cout << "Employee: " << endl;
    abstr_emp::SetAll();
}

void employee::setall(ifstream & fin)
{
    abstr_emp::setall(fin);
}

void employee::writeall(ofstream & fout)
{
    fout << Employee << endl;
    abstr_emp::writeall(fout);
}

manager::manager()
: abstr_emp()
{}

manager::manager(const string &fn, const string &ln, const string &j, int ico)
: abstr_emp(fn, ln, j), inchargeof(ico)
{}

manager::manager(const abstr_emp &e, int ico)
: abstr_emp(e), inchargeof(ico)
{}

manager::manager(const manager &m)
: abstr_emp(m)
{
    inchargeof = m.inchargeof;
}

void manager::ShowAll() const
{
    cout << "nManager: " << endl;
    abstr_emp::ShowAll();
    cout << "In charge of " << inchargeof << " employees.n";
}

void manager::SetAll()
{
    cout << "Manager: " << endl;
    abstr_emp::SetAll();
    cout << "In charge of: ";
    cin >> inchargeof;
    cin.ignore();
}

void manager::setall(ifstream & fin)
{
    abstr_emp::setall(fin);
    fin >> inchargeof;
    fin.ignore();
}

void manager::writeall(ofstream & fout)
{
    fout << Manager << endl;
    abstr_emp::writeall(fout);
    fout << inchargeof << endl;
}

fink::fink()
: abstr_emp()
{
    reportsto = "None";
}

fink::fink(const string &fn, const string &ln, const string &j, const string &rpo)
: abstr_emp(fn, ln, j), reportsto(rpo)
{}

fink::fink(const abstr_emp &e, const string &rpo)
: abstr_emp(e), reportsto(rpo)
{}

fink::fink(const fink &f)
: abstr_emp(f)
{
    reportsto = f.reportsto;
}

void fink::ShowAll() const
{
    cout << "nFink: " << endl;
    abstr_emp::ShowAll();
    cout << "Reports to: " << reportsto << endl;
}

void fink::SetAll()
{
    cout << "Fink: " << endl;
    abstr_emp::SetAll();
    cout << "Reports to: " ;
    getline(cin, reportsto);
}

void fink::setall(ifstream & fin)
{
    abstr_emp::setall(fin);
    fin >> reportsto;
    fin.ignore();
}

void fink::writeall(ofstream & fout)
{
    fout << Fink << endl;
    abstr_emp::writeall(fout);
    fout << reportsto << endl;
}

highfink::highfink()
: abstr_emp(), manager(), fink()
{}

highfink::highfink(const string &fn, const string &ln, const string &j, const 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 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 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
{
    cout << endl;
    cout << "Highfink: " << endl;
    abstr_emp::ShowAll();
    cout << "In charge of: " << manager::InChargeOf() << endl;
    cout << "Reports to: " << fink::ReportsTo() << endl;
    cout << endl;
}

void highfink::SetAll()
{
    cout << "Highfink: " << endl;
    abstr_emp::SetAll();
    cout << "In charge of: " ;
    cin >> manager::InChargeOf();
    cout << "Reports to: ";
    getline(cin, fink::ReportsTo());
}

void highfink::setall(ifstream & fin)
{
    abstr_emp::setall(fin);
    fin >> manager::InChargeOf();
    fin.ignore();
    fin >> fink::ReportsTo();
    fin.ignore();
}

void highfink::writeall(std::__1::ofstream & fout)
{
    fout << HighFink << endl;
    abstr_emp::writeall(fout);
    fout << manager::InChargeOf() << endl;
    fout << fink::ReportsTo() << endl;
}

play.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include "emp.h"

const int MAX = 10;
const char * file = "staff.txt";

int main()
{
    using namespace std;
    abstr_emp *pc[MAX];

    //打开文件并读取
    ifstream fin;
    int classtype;
    int i = 0;
    char ch;
    fin.open(file);
    if (fin.is_open())
    {
        cout << "Here are the current contents of the " << file << " file:n";
        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;
                default: cerr << "Warning: Type error!n"; break;
            }
            pc[i]->setall(fin);
            pc[i]->ShowAll();
            i++;
        }
        fin.close();
    }

    //添加新内容
    ofstream fout(file, ios::out | ios::app);  //打开写入,只追加
    if (!fout.is_open())
    {
        cerr << "Can't open " << file << " file for output.n";
        system("pause");
        exit(EXIT_FAILURE);
    }
    int index = 0;
    cout << "nPlease enter the class type of your input:n";
    cout << "1)Employeet2)Managert3)Finktt4)Highfinktq)Quitn";
    while (cin >> classtype && index < MAX)
    {
        cin.ignore();
        switch (classtype)
        {
            case 1:
                pc[index] = new employee;
                pc[index]->SetAll();
                break;
            case 2:
                pc[index] = new manager;
                pc[index]->SetAll();
                break;
            case 3:
                pc[index] = new fink;
                pc[index]->SetAll();
                break;
            case 4:
                pc[index] = new highfink;
                pc[index]->SetAll();
                break;
            default:
                cerr << "Warning: Type error!n";
                break;
        }
        index++;
        cout << "nPlease enter the class type of your input:n";
        cout << "1)Employeet2)Managert3)Finktt4)Highfinktq)Quitn";
    }
    cout << "Input over.nThank You!n";
    cout << "Begin to write into the file...n";
    cout << "Writing...n";
    for (i = 0; i < index; i++)
        pc[i]->writeall(fout);
    fout.close();
    cout << "Write over.n";

    //显示所有数据
    fin.clear();
    fin.open(file);
    if (fin.is_open())
    {
        cout << "Here are the current contents of the " << file << " file:n";
        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;
                default: cerr << "Warning: Type error!n"; break;
            }
            pc[i]->setall(fin);
            pc[i]->ShowAll();
            i++;
        }
        fin.close();
    }

    system("pause");
    return 0;
}
 

7.

play.cpp

#include <iostream>
using namespace std;
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include <vector>
#include <string>

void ShowStr(const string &str);
void GetStrs(istream &is, vector<string> &vstr);

class Store
{
public:
    ostream &os;
    Store(ostream &o) : os(o) {}
    void operator() (const string &str);
};

void Store::operator()(const string & str)
{
    size_t len = str.size();
    os.write((char*)&len, sizeof(size_t));
    os.write(str.data(), len);
}

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

    cout << "Enter string(empty line to quit): n";
    while (getline(cin, temp) && temp[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 << "Can't open.n";
        exit(EXIT_FAILURE);
    }
    GetStrs(fin, vistr);
    cout << "Here are the strings read from the file:n";
    for_each(vistr.begin(), vistr.end(), ShowStr);

    return 0;
}

void ShowStr(const string &str)
{
    cout << str << endl;
}

void GetStrs(istream &is, vector<string> &vstr)
{
    string temp;
    size_t len;
    while (is.read((char *)&len, sizeof(size_t)) && len > 0)
    {
        char ch;
        temp = "";
        for(int i = 0; i < len; i++)
        {
            if(is.read(&ch, 1))
                temp += ch;
            else
                break;
        }
        if(is)
            vstr.push_back(temp);
    }
}

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_39938666/article/details/105567457
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 17:15:10
  • 阅读 ( 656 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢