C++第3版 21章 練習問題解答

目次へ戻る

21.1

#include <fstream>
#include <complex>
#include <iostream>
using namespace std;

int main()
{
    ifstream    fi("test.txt");
    if (!fi.is_open()) return 1;
    double      d,e;
    while (fi >> d >> e) {
        complex<double> c(d,e);
        cout << c << endl;
    }
    return 0;
}

21.2

#include <string>
#include <sstream>
#include <iostream>
using namespace std;

struct Name_and_address {
    string  name;
    string  address;
    Name_and_address(const char* p = "",const char* q = "")
        : name(p),address(q) {}
};
ostream& operator<<(ostream& o,const Name_and_address& n)
{
    return o << "(" << n.name << "," << n.address << ")";
}
istream& operator>>(istream& i,Name_and_address& n)
{
    string  s,t;
    i >> s >> t;
    n = Name_and_address(s.c_str(),t.c_str());
    return i;
}
int main()
{
    stringstream        ss("Taro Tokyo");
    Name_and_address    n;
    ss >> n;
    cout << n << endl;
}
書いたやつ(<<)を読めない(>>)のは、我ながらセンス悪いね。(まぁいいか。)

21.3

パス。

21.4

パス。

21.5

パス。

21.6

パス。

21.7

#include <sstream>
#include <iostream>
using namespace std;

char Rot(char c)
{
    if (isalpha(c)) c += c == 'z' || c == 'Z' ? 'a'-'z' : 1;
    else if (isdigit(c)) c += c == '9' ? -9 : 1;
    return c;
}
int main()
{
    stringstream ss("this is a test 123-9 ABC XYZ.");
    const   n = 6;
    char    buf[n];
    while (!ss.eof()) {
        ss.read(buf,n);
        for (int i=0;i<ss.gcount();++i)
            cout << Rot(buf[i]);
    }
    cout << endl;
}

21.8

パス。

21.9

パス。

21.10

パス。

21.11

パス。

21.12

何か問題がありそう。
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

struct Fstring : fstream {
    class FProxy {
        Fstring&    m_f;
        int         m_i;
    public:
        FProxy(Fstring& f,int i) : m_f(f),m_i(i) {}
        void operator=(char c){m_f.seekp(m_i); m_f.put(c);}
        operator char()const{m_f.seekg(m_i); return m_f.get();}
    };
    Fstring(const char* p) : fstream(p) {}
    FProxy operator[](int i){return FProxy(*this,i);}
    // const FProxy バージョンは省略
};

int main()
{
    Fstring     f("test.txt");
    f[2] = 'x';
    string      s;
    s += f[1];
    s += f[2];
    s += f[3];
    cout << s << endl;
}

21.13

21.12参照のこと。

21.14

パス。

21.15

パス。

21.16

パス。

21.17

パス。

21.18

#include <iostream>
using namespace std;

template <typename T1,typename T2,typename T3>
struct tri {
    T1  first;
    T2  second;
    T3  third;
    tri(T1 t1,T2 t2,T3 t3) : first(t1),second(t2),third(t3) {}
};
template <typename T,typename U>
ostream& operator<<(ostream& o,tri<ostream& (*)(ostream&,T,U),T,U>& t)
    {return t.first(o,t.second,t.third);}

ostream& ShowDigit(ostream& o,int rad,int val)
{
    char buf[80];
    itoa(val,buf,rad);
    return o << buf;
}
typedef ostream& (*FType)(ostream&,int,int);
tri<FType,int,int> based(int i,int j)
    {return tri<FType,int,int>(ShowDigit,i,j);}

int main()
{
    cout << based(2,9) << " " << based(3,19) << endl;
}

21.19

noecho は、当該行でしか有効でない。
#include <iostream>
using namespace std;

class noecho_ostream {
    friend ostream& echo(noecho_ostream o);
    ostream*    m_o;
public:
    noecho_ostream(ostream& o) : m_o(&o) {}
    template <typename T>
        noecho_ostream operator<<(T t)const{return *this;}
};

noecho_ostream noecho(ostream& o){return o;}
noecho_ostream operator<<(ostream& o,
    noecho_ostream (*f)(ostream&)){return f(o);};
ostream& echo(noecho_ostream o){return *o.m_o;}
ostream& operator<<(noecho_ostream o,
    ostream& (*f)(noecho_ostream)){return f(o);};

int main()
{
    cout << "abc" << noecho << "def" << echo << "xyz" << endl;
}

21.20

パス。

21.21

パス。

21.22

間違えました。encrypt ではなく encrypt(k) でした。 ちょっと面倒なので、直しません。
#include <iostream>
using namespace std;

char Rot1(char c)
    {if (isalpha(c)) c += c == 'z' || c == 'Z' ? -25 : 1; return c;}
char Rot2(char c)
    {if (isalpha(c)) c += c == 'a' || c == 'A' ? 25 : -1; return c;}

class crypt_ostream {
    friend ostream& rawocrypt(crypt_ostream o);
    ostream*    m_o;
public:
    crypt_ostream(ostream& o) : m_o(&o) {}
    template <typename T>
        crypt_ostream operator<<(T t){*m_o << t; return *this;}
    crypt_ostream operator<<(char* p)
        {while (*p) *m_o << Rot1(*p++); return *this;}
};

class crypt_istream {
    friend istream& rawicrypt(crypt_istream i);
    istream*    m_i;
public:
    crypt_istream(istream& i) : m_i(&i) {}
    template <typename T>
        crypt_istream operator>>(T t){*m_i >> t; return *this;}
    crypt_istream operator>>(char* p0){char* p = p0;
        *m_i >> p0; while (*p) *p++ = Rot2(*p); return *this;}
};

crypt_ostream encrypt(ostream& o){return o;}
crypt_ostream operator<<(ostream& o,
    crypt_ostream (*f)(ostream&)){return f(o);};
ostream& rawocrypt(crypt_ostream o){return *o.m_o;}
ostream& operator<<(crypt_ostream o,
    ostream& (*f)(crypt_ostream)){return f(o);};
crypt_istream decrypt(istream& i){return i;}
crypt_istream operator>>(istream& i,
    crypt_istream (*f)(istream&)){return f(i);};
istream& rawicrypt(crypt_istream i){return *i.m_i;}
istream& operator>>(crypt_istream i,
    istream& (*f)(crypt_istream)){return f(i);};

int main()
{
    cout << encrypt << "apple\n" << rawocrypt << "apple\n";
    char    buf[80],buf2[80];
    cin >> decrypt >> buf >> rawicrypt >> buf2;
    cout << buf << endl << buf2 << endl;
}

21.23

パス。

21.24

パス。

21.25

パス。

21.26

#include <iostream>
using namespace std;

class ostrstream {
    char*   m_buf;
    int     m_len;
    int     m_pos;
public:
    ostrstream(char* buf,int len) : m_buf(buf),m_len(len) {m_pos = 0;}
    ostrstream& operator<<(char* p);
    ostrstream& operator<<(int i);
};

ostrstream& ostrstream::operator<<(char* p)
{
    int n = m_len - m_pos - 1, m = strlen(p);
    if (n > m) n = m;
    strncpy(m_buf+m_pos,p,n);
    m_buf[m_pos += n] = 0;
    return *this;
}
ostrstream& ostrstream::operator<<(int i)
{
    char    buf[80];
    sprintf(buf,"%d",i);
    return *this << buf;
}

int main()
{
    char        buf[80];
    ostrstream  o(buf,80);
    o << 123; cout << buf << endl;
    o << " abc"; cout << buf << endl;
}

21.27

#include <iomanip>
#include <iostream>
using namespace std;

ostream& general(ostream& o)
{
    return o << noboolalpha << dec << right << fixed << noshowbase
        << noshowpoint << noshowpos << nounitbuf << nouppercase;
}

int main()
{
    cout << true << setw(6) << 10 << " " << 1.23 << endl;
    cout << boolalpha << hex << left << scientific << showbase
         << showpoint << showpos << uppercase;
    cout << true << setw(6) << 10 << " " << 1.23 << endl;
    cout << general;
    cout << true << setw(6) << 10 << " " << 1.23 << endl;
}
noshowpoint が効いていないようだ。
前章目次次章