C++第18章答案

Posted 自动秃头化

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++第18章答案相关的知识,希望对你有一定的参考价值。

1.

#include<iostream>
using namespace std;
class Complex
{
public:
Complex(){real=imag=0.0;}
Complex(double r){real=r;imag=0.0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (const Complex &c)
{
return Complex(real + c.real,imag + c.imag);
}
Complex operator - (const Complex &c)
{
return Complex(real-c.real,imag-c.imag);
}
Complex operator + (double x)
{
return Complex(real+x,imag);
}
friend Complex operator +(double x,const Complex &c)
{
return Complex(c.real + x,c.imag);
}
void print()
{
if(imag<0)
cout<<real<<imag<<"i"<<endl;
else
cout<<real<<"+i"<<imag<<endl;
}
private:
double real;
double imag;
};

2.

class RMB{
public:
unsigned int yuan;
unsigned int jf;
RMB(){
yuan=jf=0;
}
RMB(unsigned int d,unsigned int c)
{
yuan=d+c/100;
jf=c%100;
}
RMB operator + (const RMB &r)
{
unsigned int c=jf+r.jf;
unsigned int d=yuan+r.yuan+c/100;
c=c%100;
return RMB(d,c);
}
RMB &operator ++ ();
friend RMB operator*(const RMB &r,double m)
{
return RMB(r.yuan*m,r.jf*m);
}
friend RMB operator*(double m,const RMB &r)
{
return RMB(r.yuan*m,r.jf*m);
}
void display(){
cout<<(yuan+jf/100.0)<<endl;
}
};
RMB& RMB::operator ++(){
if(++jf==100)
{
jf=0;
yuan++;
}
return *this;}

3.

#include<iostream>
using namespace std;
class RMB{
public:
unsigned int yuan;
unsigned int jf;
double rmb;
double p;
double q;
RMB(){
yuan=jf=0;
}
RMB(double value){
yuan=value;
jf=(value-yuan)*100;
}
RMB(unsigned int d,unsigned int c)
{
yuan=d+c/100;
jf=c%100;
}
RMB operator+=(const RMB &r){
jf=jf+r.jf;
yuan=yuan+r.yuan+jf/100;
jf=jf%100;
return RMB(yuan,jf);
}
RMB operator+=(double x){
unsigned int m=yuan;
yuan=yuan+x;
jf=jf+(m+x-yuan)*100;
return RMB(yuan,jf);
}
RMB operator-=(const RMB &r){
yuan=yuan-r.yuan;
q=jf-r.jf;
jf=q;
return RMB(yuan,jf);
}
RMB operator-=(double x){
int m=x*100;
jf=yuan*100+jf-m;
yuan=yuan-x;
return RMB(yuan,jf);
}
void display(){
p=jf;
rmb=yuan+p/100;
cout<<rmb<<endl;
}
};

4.

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
const double pi=3.14159;
class Shape{
public:
virtual double Area()=0;
};
class Circle:public Shape{
public:
string P;
double x;
double y;
double r;
Circle(string p,double X,double Y,double R)
{
this->P=p;
x=X;
y=Y;
r=R;
}
double Area()
{
return pi*r*r;
}
};
class Rectangle:public Shape{
public:
string P;
double x1;
double y1;
double x2;
double y2;
double length;
double width;
Rectangle(string p,double X1,double Y1,double X2,double Y2)
{
this->P=p;
x1=X1;
y1=Y1;
x2=X2;
y2=Y2;
}
double Area()
{
return (x1-x2)*(y1-y2);
}
};
class Triangle:public Shape{
public:
Triangle(string p,double a,double b,double c,double d,double e,double f)
{
this->P = p;
this->x = a;
this->y = b;
this->x1 = c;
this->y1 = d;
this->x2 = e;
this->y2 = f;
}
double Area()
{
double d1,d2,d3;
d1 = pow((pow(x-x1,2) + pow(y-y1,2)),0.5);
d2 = pow((pow(x-x2,2) + pow(y-y2,2)),0.5);
d3 = pow((pow(x1-x2,2) + pow(y1-x2,2)),0.5);
double p;
p = (d1 + d2 + d3)/2;
double out;
out = pow(p*(p-d1)*(p-d2)*(p-d3),0.5);
return out;
}
private:
string P;
double x;
double y;
double x1;
double x2;
double y1;
double y2;};
int main()
{
string P;
double a,b,c,d,e,f;
int sum;
cin>>sum;
Shape *shape[sum];
for(int i=0;i<sum;i++)
{
cin>>P;
if(P=="R")
{
cin>>a>>b>>c>>d;
shape[i]=new Rectangle(P,a,b,c,d);
}
else if(P=="C")
{
cin>>a>>b>>c;
shape[i]=new Circle(P,a,b,c);
}
else if (P=="T")
{
cin>>a>>b>>c>>d>>e>>f;
shape[i]=new Triangle(P,a,b,c,d,e,f);
}
if(i==sum-1)
{
double TotleArea=0;
for(int j = 0;j<sum; j++)
{
cout<<shape[j]->Area()<<endl;
TotleArea += shape[j]->Area();
delete shape[j];
}
cout<<fixed<<setprecision(5)<<TotleArea<<endl;
}
}
}



以上是关于C++第18章答案的主要内容,如果未能解决你的问题,请参考以下文章

C++ Primer Plus编程练习答案——第13章

C++ Primer Plus编程练习答案——第10章

C++ Primer Plus编程练习答案——第14章

C++ Primer Plus编程练习答案——第8章

更新:C++ 指针片段

c++面向对象程序设计 谭浩强 第一章答案