C++ Primer Plus第十七章练习

博主在完成C++ Primer Plus第16章的学习后,尝试了Windows程序设计但感到困惑,于是回归C++,专注于第17章的练习。文章详细介绍了17.8节的多个练习,包括emp.h、emp.cpp和main.cpp的实现,并提到使用之前编写的类可以简化部分工作。此外,还强调了ostream成员只能作为引用,因为其拷贝和赋值操作被禁用,整体难度适中,遵循书中的指导即可。
摘要由CSDN通过智能技术生成

看完16章之后觉得又行了 直接去看了2天windows程序设计 看晕了又回来继续cpp了
17.8.1

#include <iostream>
using namespace std;
int main(void)
{
   
    int count = 0;
    cout << "请输入一串字符串:";
    char temp[30];
    cin.get(temp, 30, '$');
    for(int i = 0; temp[i]; i++)
        count++;
    cout << "一共有" << count << "个字符" << endl;
    return 0;
}

17.8.2

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
   
    //为了简单就自认为打开文件并不会发生错误
    ofstream out(argv[1]);
    cout << "请输入一串字符串:";
    int ch;
    while((ch = cin.get()) != EOF)
        out << (char)ch;
    out.close();
    return 0;
}

17.8.3

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
   
    if(argc != 3)
    {
   
        cerr << "参数不够" << endl;
        return 0;
    }
    char temp[255];
    ifstream in(argv[1]);
    ofstream out(argv[2]);
    while(!in.eof())
    {
   
        in.get(temp, 255).get();
        out << temp;
    }
    in.close();
    out.close();
    return 0;
}

17.8.4

#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
   
    ifstream in1("one"), in2("two");
    ofstream out("put");
    char one[255], two[255];
    bool o, t;
    while(!(o = in1.eof()) || !(t = in2.eof()))
    {
   
        if(!o)
        {
   
            in1.get(one, 255).get();
            out << one << ' ';
        }           
        if(!t)
        {
   
            in2.get(two, 255).get();
            out << two;
        }
        out << endl;
    }
    in1.close();
    in2.close();
    return 0;
}

17.8.5

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void show(string);
int main(void)
{
   
    ifstream Mat("mat.dat"), Pat("pat.dat");
    ofstream MPat("matnpat.dat");
    vector<string> M, P, MP;
    string Mdata, Pdata;
    while(!Mat.eof())
    {
   
        getline(Mat, Mdata);
        M.push_back(Mdata);
    }
    sort(M.begin(), M.end());
    for_each(M.begin(), M.end(), show);
    while(!Pat.eof())
    {
   
        getline(Pat, Pdata);
        P.push_back(Pdata);
    }
    sort(P.begin(), P.end());
    for_each(P.begin(), P.end(), show);
    MP.insert(MP.end(), M.begin(), M.end());
    MP.insert(MP.end(), P.begin(), P.end());
    sort(MP.begin(), MP.end());
    unique(MP.begin(), MP.end());
    for(auto x : MP)
        MPat << x << endl;
    Mat.close();
    Pat.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值