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 第五章:语句 ...
随机推荐
- python-大话装饰器
装饰器 装饰器是个什么玩意呢?是个会了不难,装逼神器.但是如果不了解理解起来还是很抽象,让我们试试这个装逼神器吧! 1.什么是装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典 ...
- mac pro 设置wifi热点
系统偏好设置-共享-网络共享(Internet共享)共享来源选择你的网络(非wifi), 以下复选框选wifi,wifi设置里能够设置wifi名字password,注意不能有中文. 设置完,inter ...
- 揭密: M2和高房价到底谁推高了谁?
近期,著名经济学家吴敬链向新华网等媒体表示:房价高的根本原本就是货币超发.近期十年来我国M2增长率(广义货币增长率)太快."钱太多了,有些人要保值就要投资买房,又因货币非常多购买力非常强.就 ...
- JavaScript继承的实现
怎样在JavaScript中实现简单的继承? 以下的样例将创建一个雇员类Employee,它从Person继承了原型prototype中的全部属性. function Employee(name, ...
- 权威指南之脚本化jquery
jqury函数 jquery()($())有4种不同的调用方式 第一种是最常用的调用方式是传递css选择器(字符串)给$()方法.当通过这种方式调用时,$()方法会返回当前文档中匹配该选择器的元素集. ...
- ASPxGridView-为每行添加序号
添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...
- Java并发编程:Thread类的使用介绍
在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着介绍Thread类中的方法的具体使用. 以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中 ...
- android之PackageManager简介
PackageManager相关 本类API是对所有基于加载信息的数据结构的封装,包括以下功能: 安装,卸载应用查询permission相关信息 查询Application相关信息(applicati ...
- jQuery获取Select选择的Text(非表单元素)和 Value(表单元素)(转)
jQuery获取Select选择的Text和Value: 语法解释: . $("#select_id").change(function(){//code...}); //为Sel ...
- [转]PB 基本语句 循环语句
PB 基本语句一.赋值语句赋值语句用于给变量.对象属性赋值,这是应用程序中使用最频繁的语句,其语法格式为:variablename = expression_r其中:⑴variablename是变量名 ...