zoukankan      html  css  js  c++  java
  • c++ primer plus 习题答案(5)

    p333.7

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 using namespace std;
     5 
     6 class Plorg{
     7 private:
     8     char fullname[19];
     9     int CI;
    10 public:
    11     Plorg(char *ar = "Plorga", int ct = 50);
    12     void index();
    13     void show()const;
    14 };
    15 
    16 Plorg::Plorg(char *ar, int ct){
    17     strcpy(fullname, ar);
    18     CI = ct;
    19 }
    20 
    21 void Plorg::index(){
    22     cout << "enter a number of CI
    ";
    23     cin >> CI;
    24 }
    25 
    26 void Plorg::show()const{
    27     cout << "fullname is " << this->fullname <<
    28         " CI is " << (*this).CI << endl;
    29 }
    30 
    31 int main(){
    32     Plorg test1;
    33     cout << "enter a line
    ";
    34     char ar[19];
    35     int ct;
    36     cin.getline(ar, 19);
    37     cout << "enter a number
    ";
    38     cin >> ct;
    39     cin.get();
    40     Plorg test2(ar, ct);
    41     test1.show();
    42     cout << endl;
    43     test2.show();
    44     test1.index();
    45     cout << endl;
    46     test1.show();
    47 
    48     system("pause");
    49     return 0;
    50 }

    p375.5

      1 //头文件:
      2 #include<iostream>
      3 
      4 #ifndef _STONEWT_H
      5 #define _STONEWT_H
      6 class Stonewt{
      7 private:
      8     static const int lbs_per_stn = 14;
      9     int stone;
     10     double pds_left;
     11     double pounds;
     12     char mode;
     13 public:
     14     Stonewt();
     15     Stonewt(double, char ch='P');
     16     Stonewt(int, double, char ch='P');
     17     ~Stonewt();
     18     void set(Stonewt &);
     19     void mode_invert(char);
     20     Stonewt operator+(const Stonewt & a)const 
     21     {return Stonewt(pounds + a.pounds); }
     22     friend Stonewt operator-(const Stonewt &, const Stonewt &);
     23     friend Stonewt operator*(double n, const Stonewt &);
     24     friend std::ostream & operator<<(std::ostream &, const Stonewt &);
     25 };
     26 
     27 #endif
     28 
     29 //方法:
     30 #include<iostream>
     31 #include"stack.h"
     32 using std::cout;
     33 using std::cin;
     34 using std::endl;
     35 
     36 Stonewt::Stonewt(){
     37     pds_left = pounds = stone = 0;
     38     mode = 'P';
     39 }
     40 
     41 Stonewt::Stonewt(double a, char ch){
     42     pounds = a;
     43     stone = int(a / lbs_per_stn);
     44     pds_left = pounds - stone*lbs_per_stn;
     45     mode = ch;
     46 }
     47 
     48 Stonewt::Stonewt(int a, double b, char ch){
     49     pounds = a*lbs_per_stn + b;
     50     stone = a + int(b / lbs_per_stn);
     51     pds_left = pounds - stone*lbs_per_stn;
     52     mode = ch;
     53 }
     54 
     55 void Stonewt::set(Stonewt &a){
     56     cout << "enter stone
    ";
     57     cin >> a.stone;
     58     cout << "enter pounds
    ";
     59     cin >> a.pds_left;
     60     a.pounds = a.stone*lbs_per_stn + a.pds_left;
     61     cout << "enter a mode that you want to choice
    ";
     62     cin >> a.mode;
     63 }
     64 
     65 
     66 Stonewt::~Stonewt(){
     67 
     68 }
     69 
     70 Stonewt operator-(const Stonewt &a, const Stonewt &b){
     71     Stonewt sum;
     72     sum = a + b;
     73     return sum;
     74 }
     75 
     76 Stonewt operator*(double n, const Stonewt &a){
     77     return Stonewt(n*a.pounds);
     78 }
     79 
     80 std::ostream & operator<<(std::ostream &os, const Stonewt &t){
     81     if (t.mode == 'P')
     82         cout << "pounds is " << t.pounds;
     83     else if (t.mode == 'S')
     84         cout << "stone is " << t.stone << " pds_left is " << t.pds_left << endl;
     85     else cout << "wrong choices
    ";
     86     return os;
     87 }
     88 
     89 //驱动:
     90 #include<iostream>
     91 #include<cstdlib>
     92 #include"stack.h"
     93 using std::cout;
     94 using std::endl;
     95 
     96 int main(){
     97     Stonewt ct(54.6, 'S');
     98     Stonewt bt(23, 56.3, 'P');
     99     Stonewt at;
    100     Stonewt dt = bt + ct;
    101     Stonewt ft = bt - ct;
    102     int n = 4;
    103     Stonewt gt = n*bt;
    104     cout << ct << endl << bt << endl
    105         << dt << endl << ft << endl
    106         << gt << endl<<at;
    107 
    108     system("pause");
    109     return 0;
    110 }

    p375.7

      1 //头文件:
      2 #include<iostream>
      3 
      4 #ifndef _COMPLEX_H
      5 #define _COMPLEX_H
      6 
      7 using std::ostream;
      8 using std::istream;
      9 
     10 namespace COMPLEX{
     11     class Complex{
     12     private:
     13         double real;
     14         double imaginary;
     15     public:
     16         Complex();
     17         Complex(double, double);
     18         Complex operator+(const Complex &);
     19         Complex operator-(const Complex &);
     20         friend Complex operator*(const Complex &, const Complex &);
     21         friend Complex operator*(const Complex &, const double);
     22         friend Complex operator~(const Complex &);
     23         friend ostream & operator<<(ostream &, const Complex &);
     24         friend istream & operator>>(istream &, Complex &);
     25     };
     26 }
     27 
     28 #endif
     29 
     30 //方法:
     31 #include<iostream>
     32 using namespace std;
     33 #include"stack.h"
     34 
     35 namespace COMPLEX{
     36     Complex::Complex(){
     37         real = imaginary = 0;
     38     }
     39 
     40     Complex::Complex(double a, double b){
     41         real = a;
     42         imaginary = b;
     43     }
     44 
     45     Complex Complex::operator+(const Complex &a){
     46         return Complex(real + a.real, imaginary + a.imaginary);
     47     }
     48 
     49     Complex Complex::operator-(const Complex &a){
     50         return Complex(real - a.real, imaginary - a.imaginary);
     51     }
     52 
     53     Complex operator*(const Complex &a, const Complex &b){
     54         return Complex(a.real*b.real - a.imaginary*b.imaginary, a.real*b.imaginary + a.imaginary*b.real);
     55     }
     56 
     57     Complex operator*(const Complex &a, const double b){
     58         return Complex(a.real*b, a.imaginary*b);
     59     }
     60 
     61     Complex operator~(const Complex &a){
     62         return Complex(a.real, -a.imaginary);
     63     }
     64 
     65     ostream & operator<<(ostream &os, const Complex &b){
     66         cout << "(" << b.real << ", " << b.imaginary << "i)";
     67         return os;
     68     }
     69 
     70     istream & operator>>(istream &is, Complex &a){
     71         if (is >> a.real){
     72             is >> a.imaginary;
     73             cout << "real: " << a.real << endl;
     74             cout << "imaginary: " << a.imaginary << endl;
     75         }
     76         return is;
     77     }
     78 }
     79 
     80 //驱动:
     81 #include<iostream>
     82 using namespace std;
     83 #include"stack.h"
     84 using namespace COMPLEX;
     85 
     86 int main(){
     87     Complex a(3.0, 4.0);
     88     Complex c;
     89     cout << "enter a complex number(q to quit)
    ";
     90     while (cin>>c){
     91         cout << "c is " << c << endl;
     92         cout << "complex conjugate is " << ~c << endl;
     93         cout << "a is " << a << endl;
     94         cout << "a+c  is " << a + c << endl;
     95         cout << "a-c is " << a - c << endl;
     96         cout << "a*c is " << a*c << endl;
     97         cout << "2*c is " << c*2 << endl;
     98         cout << "enter a complex number(q to quit)
    ";
     99     }
    100     cout << "Done!
    ";
    101     system("pause");
    102     return 0;
    103 }
  • 相关阅读:
    Confluence 6 PostgreSQL 设置准备
    Confluence 6 MySQL 问题解决
    Confluence 6 MySQL 测试你的数据库连接
    Confluence 6 MySQL 输入你的数据库细节
    Confluence 6 下载和安装 MySQL 驱动
    Confluence 6 MySQL 创建数据库和数据库用户
    Confluence 6 配置 MySQL 服务器
    Confluence 6 MySQL 数据库设置准备
    Confluence 6 SQL Server 问题解决
    Confluence 6 SQL Server 数据库驱动修改
  • 原文地址:https://www.cnblogs.com/coding-time/p/4530874.html
Copyright © 2011-2022 走看看