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

时间:2022-08-31 07:53:47

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&plus;&plus; primer plus 习题答案&lpar;1&rpar;

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

  2. c&plus;&plus; primer plus 习题答案&lpar;8&rpar;

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

  3. c&plus;&plus; primer plus 习题答案&lpar;7&rpar;

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

  4. c&plus;&plus; primer plus 习题答案&lpar;6&rpar;

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

  5. c&plus;&plus; primer plus 习题答案&lpar;4&rpar;

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

  6. c&plus;&plus; primer plus 习题答案&lpar;3&rpar;

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

  7. c&plus;&plus; primer plus 习题答案&lpar;2&rpar;

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

  8. C&plus;&plus;Primer第五版——习题答案目录

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

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

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

随机推荐

  1. Material Design入门(三)

    本文主要包括 CollapsingToolbarLayout实现滚动动画效果 ViewPager+tabLayout实现左右类Tab效果 控件介绍 这次需要用到得新控件比较多,主要有以下几个: Coo ...

  2. SpringMVC&plus;MyBatis(最新)

    目前主流的Web MVC框架,除了Struts这个主力 外,还有Spring MVC,主要是由于Spring MVC配置比较简单,使用起来也十分明了,非常灵活,与Spring 集成较好,对RESTfu ...

  3. 不用配置tnsnames&period;ora&comma;直接通过PL&sol;SQL访问远程数据库

  4. html5中新增的非主体结构的元素

    html5中出了新增了article.section.nav.aside.time主要结构元素外,还增加了一些表示逻辑结构或附加信息的非主体结构元素. 一.header元素 header元素是一种具有 ...

  5. DotNet进阶系列

    一. 回顾历史 回顾个人发展历程,自2012年初次接触开发至今(2018年)已经有六个年头,这期间陆陆续续学习并掌握了不少技术,C#语言.ORM框架.多线程技术.设计模式.前端技术.MVC.MVVM框 ...

  6. luogu 1772 物流运输 ZJOI2006 spfa&plus;dp

    主要路径上存在时间限制(消失) 因为数据较小(点数较小),利用限制条件在规定时间内分别spfa,(也可用floyd) 再通过dp取最优值 #include<bits/stdc++.h> # ...

  7. Wampserver虚拟机配置记录

    原文地址:http://blog.csdn.net/clj9017/article/details/12705725 第一步 在http.conf 文件里面找到 ,开启 Virtual hosts # ...

  8. VB2010新特性

    1.取消了连接符(1)","之后(2)"()"前后(3)"{}"前后(4)XML(5)连接字符"&"后(6)赋值 ...

  9. Python 模块化 from &period;&period; import 语句介绍 (二)

    from语句 例一. from pathlib import Path,PosixPath print(dir()) print(Path) print(PosixPath) 运行结果: ['Path ...

  10. 利用ncurses库开发终端工具箱(1)—— ToDoList小工具开发

    准备工作 腾讯云服务器(Ubuntu),C++编程语言 由于想输出界面中包含中文,所以安装库 libncursesw5,依次输入下面三行命令 sudo apt-get install libncurs ...