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 第五章:语句 ...
随机推荐
- 2016 Multi-University Training Contest 4 总结
第四场多校队伍的发挥还是相当不错的. 我倒着看题,发觉最后一题树状数组可过,于是跟队友说,便开始写,十分钟AC. 欣君翻译01题给磊哥,发现是KMP裸题,但是发现模板太旧,改改后过了. 11题是一道毒 ...
- 排序方法之标准库中的快排 qsort ()函数
C标准库qsort()函数的用法(快排) 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base, int nelem, int width, i ...
- SVN使用Tips
1. 如果在本地删除了某个文件,在Cornerstone上的本地仓库会出现D的标志,并且文件不存在. 这时,只需要将该文件提交到服务器上,本地仓库就会清除了已删除的文件的标识,同时,服务器上也会自动删 ...
- 编写可维护的JS 03
3.语句和表达式 所有语句都应当使用花括号 if else语句 for 循环 while 循环 do...while try...catch...finally 花括号对齐方式 左括号在第一行语句末尾 ...
- MATLAB一句总结
MATLAB使用过程中的一些小总结: 1.sqrt函数的输入参数应为double类型: 2.im2bw把图像转换为二值图像: 3.double类型的图片必须转换为uint8类型后才能用imshow显示 ...
- Problem B: 最少步数
DescriptionA friend of you is doing research on theTraveling Knight Problem (TKP) where you are to f ...
- [C#参考]事件机制
还是那个项目,为了降低程序的耦合性,我决定小小的重构一下自己原来的代码,把Socket通信和帧的分析这两部分分别封装成一个类,当然线程没有变,只是封装了一下,为的就是模块测试完容易拼接.这也是我打算降 ...
- (4)事件处理——(3)代码执行的顺序(Timing of code execution)
In Chapter 1, Getting Started, we noted that $(document).ready()was jQuery's primary way to perform ...
- springmvc的ModelAndView的简单使用
参考:http://blog.csdn.net/zzjjiandan/article/details/34089313 先上图: MAVTest.java package com.wyl; impor ...
- PHP查询MYSQL表的主键
$sql = "SELECT * from Person"; $result = mysql_query($sql,$con); while ($property = mysql_ ...