p333.7

 #include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std; class Plorg{
private:
char fullname[];
int CI;
public:
Plorg(char *ar = "Plorga", int ct = );
void index();
void show()const;
}; Plorg::Plorg(char *ar, int ct){
strcpy(fullname, ar);
CI = ct;
} void Plorg::index(){
cout << "enter a number of CI\n";
cin >> CI;
} void Plorg::show()const{
cout << "fullname is " << this->fullname <<
" CI is " << (*this).CI << endl;
} int main(){
Plorg test1;
cout << "enter a line\n";
char ar[];
int ct;
cin.getline(ar, );
cout << "enter a number\n";
cin >> ct;
cin.get();
Plorg test2(ar, ct);
test1.show();
cout << endl;
test2.show();
test1.index();
cout << endl;
test1.show(); system("pause");
return ;
}

p375.5

 //头文件:
#include<iostream> #ifndef _STONEWT_H
#define _STONEWT_H
class Stonewt{
private:
static const int lbs_per_stn = ;
int stone;
double pds_left;
double pounds;
char mode;
public:
Stonewt();
Stonewt(double, char ch='P');
Stonewt(int, double, char ch='P');
~Stonewt();
void set(Stonewt &);
void mode_invert(char);
Stonewt operator+(const Stonewt & a)const
{return Stonewt(pounds + a.pounds); }
friend Stonewt operator-(const Stonewt &, const Stonewt &);
friend Stonewt operator*(double n, const Stonewt &);
friend std::ostream & operator<<(std::ostream &, const Stonewt &);
}; #endif //方法:
#include<iostream>
#include"stack.h"
using std::cout;
using std::cin;
using std::endl; Stonewt::Stonewt(){
pds_left = pounds = stone = ;
mode = 'P';
} Stonewt::Stonewt(double a, char ch){
pounds = a;
stone = int(a / lbs_per_stn);
pds_left = pounds - stone*lbs_per_stn;
mode = ch;
} Stonewt::Stonewt(int a, double b, char ch){
pounds = a*lbs_per_stn + b;
stone = a + int(b / lbs_per_stn);
pds_left = pounds - stone*lbs_per_stn;
mode = ch;
} void Stonewt::set(Stonewt &a){
cout << "enter stone\n";
cin >> a.stone;
cout << "enter pounds\n";
cin >> a.pds_left;
a.pounds = a.stone*lbs_per_stn + a.pds_left;
cout << "enter a mode that you want to choice\n";
cin >> a.mode;
} Stonewt::~Stonewt(){ } Stonewt operator-(const Stonewt &a, const Stonewt &b){
Stonewt sum;
sum = a + b;
return sum;
} Stonewt operator*(double n, const Stonewt &a){
return Stonewt(n*a.pounds);
} std::ostream & operator<<(std::ostream &os, const Stonewt &t){
if (t.mode == 'P')
cout << "pounds is " << t.pounds;
else if (t.mode == 'S')
cout << "stone is " << t.stone << " pds_left is " << t.pds_left << endl;
else cout << "wrong choices\n";
return os;
} //驱动:
#include<iostream>
#include<cstdlib>
#include"stack.h"
using std::cout;
using std::endl; int main(){
Stonewt ct(54.6, 'S');
Stonewt bt(, 56.3, 'P');
Stonewt at;
Stonewt dt = bt + ct;
Stonewt ft = bt - ct;
int n = ;
Stonewt gt = n*bt;
cout << ct << endl << bt << endl
<< dt << endl << ft << endl
<< gt << endl<<at; system("pause");
return ;
}

p375.7

 //头文件:
#include<iostream> #ifndef _COMPLEX_H
#define _COMPLEX_H using std::ostream;
using std::istream; namespace COMPLEX{
class Complex{
private:
double real;
double imaginary;
public:
Complex();
Complex(double, double);
Complex operator+(const Complex &);
Complex operator-(const Complex &);
friend Complex operator*(const Complex &, const Complex &);
friend Complex operator*(const Complex &, const double);
friend Complex operator~(const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &);
};
} #endif //方法:
#include<iostream>
using namespace std;
#include"stack.h" namespace COMPLEX{
Complex::Complex(){
real = imaginary = ;
} Complex::Complex(double a, double b){
real = a;
imaginary = b;
} Complex Complex::operator+(const Complex &a){
return Complex(real + a.real, imaginary + a.imaginary);
} Complex Complex::operator-(const Complex &a){
return Complex(real - a.real, imaginary - a.imaginary);
} Complex operator*(const Complex &a, const Complex &b){
return Complex(a.real*b.real - a.imaginary*b.imaginary, a.real*b.imaginary + a.imaginary*b.real);
} Complex operator*(const Complex &a, const double b){
return Complex(a.real*b, a.imaginary*b);
} Complex operator~(const Complex &a){
return Complex(a.real, -a.imaginary);
} ostream & operator<<(ostream &os, const Complex &b){
cout << "(" << b.real << ", " << b.imaginary << "i)";
return os;
} istream & operator>>(istream &is, Complex &a){
if (is >> a.real){
is >> a.imaginary;
cout << "real: " << a.real << endl;
cout << "imaginary: " << a.imaginary << endl;
}
return is;
}
} //驱动:
#include<iostream>
using namespace std;
#include"stack.h"
using namespace COMPLEX; int main(){
Complex a(3.0, 4.0);
Complex c;
cout << "enter a complex number(q to quit)\n";
while (cin>>c){
cout << "c is " << c << endl;
cout << "complex conjugate is " << ~c << endl;
cout << "a is " << a << endl;
cout << "a+c is " << a + c << endl;
cout << "a-c is " << a - c << endl;
cout << "a*c is " << a*c << endl;
cout << "2*c is " << c* << endl;
cout << "enter a complex number(q to quit)\n";
}
cout << "Done!\n";
system("pause");
return ;
}

c++ primer plus 习题答案(5)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  3. c++ primer plus 习题答案(7)

    p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...

  4. c++ primer plus 习题答案(6)

    p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...

  6. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  7. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. CentOS 七 vs CentOS 6的不同

    CentOS 七 vs CentOS 6的不同   CentOS 7 vs CentOS 6的不同(1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME ...

  2. Unix/Linux环境C编程入门教程(6) 安装Fedora C/C++开发环境

    安装Fedora  C/C++开发环境 1 Fedora 是一个开放的.创新的.前瞻性的操作系统和平台,基于 Linux. 2.选择自定义配置 3.设置版本为10.0 4.选择稍后安装 5.选择64位 ...

  3. 字符串比较必须使用strcmp

    char s1[]="this" char *s2 = "this" if(s1=="this"){ printf("s1 is ...

  4. poj2017简单题

    #include <stdio.h> #include <stdlib.h> int main() { int n,i; while(scanf("%d", ...

  5. 【Android进阶学习】shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  6. 让 collabtive-11 支持中文

    collabtive, 不错的项目管理工具, 将在新项目中使用之; 但在默认安装 collabtive-11 之后 发现在里面输入中文后会出错, 网上找不了少资料但对 11这版本的中文支持的修改不起不 ...

  7. Ceph对象存储网关中的索引工作原理<转>

    Ceph 对象存储网关允许你通过 Swift 及 S3 API 访问 Ceph .它将这些 API 请求转化为 librados 请求.Librados 是一个非常出色的对象存储(库)但是它无法高效的 ...

  8. 浅谈HtmlUnit的使用

    一.htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容.项目可以模拟浏览器运行,被誉为java浏览器的开源实现.这个没有界面的浏览器,运行 ...

  9. Putty以及adb网络调试

    1.什么是SSH? SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议. 传 ...

  10. 让你在DOS中任意切换目录

    尽管Windows图形界面早已经取代了无趣的DOS字符界面(废话,Vista都呼之欲出了),不过在日常操作中,还是有很多时候需要用到命令提示符.比如批量重命名文件时.执行字符命令时.在命令行下恢复系统 ...