c++ primer plus 习题答案(5)
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)的更多相关文章
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- c++ primer plus 习题答案(8)
p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...
- c++ primer plus 习题答案(7)
p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...
- c++ primer plus 习题答案(6)
p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(4)
p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...
- c++ primer plus 习题答案(3)
p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...
- c++ primer plus 习题答案(2)
p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- Cpp again
1,
- nodejs微信开发获取token,ticket-1
/* jshint -W079 */ /* jshint -W020 */ "use strict"; var _ = require("lodash"); v ...
- 借@阿里巴巴 耍了个帅——HTML5 JavaScript实现图片文字识别与提取
写在前面 8月底的时候,@阿里巴巴 推出了一款名为“拯救斯诺克”的闯关游戏,作为前端校园招聘的热身,做的相当不错,让我非常喜欢.后来又传出了一条消息,阿里推出了A-star(阿里星)计划,入职阿里的技 ...
- MSSQL 当前会话设置隔离级别与查询
之前因为MySQL没有with(nolock)这种写法,于是想设置隔离级别,结果被坑. 直觉以为和MSSQL一样只要打set transaction isolation level xxx 就能搞定 ...
- Hibernate中为什么要重写equals方法和hashcode方法
1.*为什么要重写equals方法,首先我们来看一下equals源码: public boolean equals(Object anObject) { if (this == anObject) { ...
- python学习之 dictionary 、list、tuple操作
python 内置类型数据 有dictionary(字典).list(列表)和tuple(元组) 一.Dictionary Dictionary 是 Python 的内置数据类型之一,它定义了键和值之 ...
- Performance tool httperf
httperf: A relatively well-known open source utility developed by HP, for Linux operating systems on ...
- Unity 4.2.0 官方最新破解版(Unity3D 最新破解版,3D游戏开发工具和游戏引擎套件)
Unity是一款跨平台的游戏开发工具,从一开始就被设计成易于使用的产品.作为一个完全集成的专业级应用,Unity还包含了价值数百万美元的功能强大的游戏引擎.Unity作为一个游戏开发工具,它的设计主旨 ...
- Windows 取得至高无上的权限
第一步:gpedit.msc 第二步:计算机配置-->windows 设置 -->安全设置 -->安全选项 -->用户账户控制 -->以管理员批准模式运行所有管理员 -- ...
- 如何从 0 开始学 ruby on rails (漫步版)
如何从 0 开始学 ruby on rails (漫步版) ruby 是一门编程语言,ruby on rails 是 ruby 的一个 web 框架,简称 rails. 有很多人对 rails 感兴 ...