格式化输出

Description

Anonymous has met a problem.To solve this problem, he has to write 4 classes.
These classes are point, vector, circle and todo.

        point
          /  \
        /      \
       vector  circle
        \      /
         \    /
         todo
We can use two double varibles x and y to describe a point;
We can use four double varibles x, y, dx and dy to describe a vector;
We can use three double varibles x, y and r to describe a circle;
We can use five double varibles x, y, dx, dy and r to describe a todo;

Your should complete a print function in each class.
In point, you should print
point : x y

In vector, you should print
length : length
point : x y

In circle, you should print
area : area
point : x y

In todo, you should print
volume : volume
length : length
point : x y
area : area
point : x y

For example:
volume : 5.97
length : 1.41
point : 1.00 1.00
area : 3.14
point : 1.00 1.00


sample input:

1 1 1 1 1

sample output:

point : 1.00 1.00

length : 1.41
point : 1.00 1.00

area : 3.14
point : 1.00 1.00

volume : 0.00
length : 0.00
point : 0.00 0.00
area : 0.00
point : 0.00 0.00

volume : 5.97
length : 1.41
point : 1.00 1.00
area : 3.14
point : 1.00 1.00

Hint

这里详细解释一下几个类把。

point是点。用一个坐标x, y来描述。

vector是向量。但是我们假设这个向量是从一个点x, y出发的向量,向量的信息为(dx,dy)

也就是从(x,y)到 (x+dx, y+dy)的一个向量

circle是一个在(x,y)点,半径为r的圆

todo是将圆按向量从(x,y)平移到(x+dx, y+dy)经过的所有区域。至于为什么叫todo只是随手取的。volume其实是指这个区域的面积此处用错了

输出行末没有空格,冒号两边有空格, 数字之间用空格隔开,输出保留两位小数。 pi(π) 取acos(-1.0)。。需要#include<cmath> 


这题不使用虚继承也可以实现,但是直接访问会发现从todo访问x,y时会产生错误。

可以在派生类中调用基类的print实现。所以特意把输出格式定为题中这种。

标程用了虚继承,想使用虚继承可自学ppt最后几页。

copy from 王毅峰
class point
{
protected:
    double x,y;
public:
    point(double m=0,double n=0);
    virtual void print();
};

class vector1: public point
{
protected:
    double dx,dy;
public:
    vector1(double m=0,double n=0,double dx=0,double dy=0);

    virtual void print();

};
class circle:public point
{
protected:
    double r;
public:
    circle(double m=0,double n=0,double r=0);
    virtual void print();

};
class todo:virtual public vector1,virtual public circle
{
public:
    todo(double m=0,double n=0,double dx=0,double dy=0,double r=0);
    void print();
};
#include"todo.h"
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
point::point(double m,double n):x(m),y(n){}
void point::print()
{
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"point"<<" : "<<x<<" "<<y<<endl;
}
vector1::vector1(double m,double n,double dx,double dy):point(m,n)
{
    this->dx=dx;
    this->dy=dy;
}
void vector1::print()
{
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"length : "<<sqrt(dx*dx+dy*dy)<<endl;
    point::print();
}
circle::circle(double m,double n,double r):point(m,n)
{
    this->r=r;
}
void circle::print()
{
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"area : "<<acos(-1)*r*r<<endl;
    point::print();
}
todo::todo(double m,double n,double dx,double dy,double r):vector1(m,n,dx,dy),circle(m,n,r){}
void todo::print()
{
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<"volume : "<<acos(-1.0)*r*r+2*r*sqrt(dx*dx+dy*dy)<<endl;
    vector1::print();
    circle::print();
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include "todo.h"
using namespace std;
int main() {
    double x, y, dx, dy, r;
    cin >> x >> y >> dx >> dy >> r;
    point p0;
    point p1(x, y);
    vector1 v0;
    vector1 v1(x, y, dx, dy);
    circle c0;
    circle c1(x, y, r);
    todo t0;
    todo t1(x, y, dx, dy, r);
    p1.print();
    cout << endl;
    v1.print();
    cout << endl;
    c1.print();
    cout << endl;
    t0.print();
    cout << endl;
    t1.print();
    cout << endl;
    return 0;
}


心得:
(1):1、保留有效数字问题(setprecision(3))

#include<iostream>
#include<iomanip>
#include "stdlib.h"
using namespace std;
int main(){
    double PI=3.1415926;
    cout<<setprecision(3)<<PI<<endl;
    system("pause");
    return 0;
}

输出:3.14
(2)保留小数点后几位问题
上例中定义的PI小数点后有数位,可以保留小数点后两位(三位有效数字)。如果double a=100;再按上述方法输出a,则只会输出100,并不是小数,如果不信你可以试一试。
那么该怎么解决这个问题呢?非常简单
只需添加setiosflags(ios::fixed)即可,看代码

#include<iostream>
#include<iomanip>
#include "stdlib.h"
using namespace std;
int main(){
    //double PI=3.1415926;
    double a=100;
    cout<<setiosflags(ios::fixed)<<setprecision(3)<<a<<endl;
    system("pause");
    return 0;
}

(3)
3、格式化输出(01)
当你输出时间格式的时候需要酱紫的输出(01:08:31)作为结果,然而你的输出却是酱紫:1:8:31,What should I do?这时候就需要C++的格式化输出了。

#include "stdlib.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    int a=1;
    cout.setf(ios::right);
    cout.fill('0');
    cout.width(2);
    cout<<a<<endl;;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值