| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1057 人关注过本帖
标题:求不同图形的面积
只看楼主 加入收藏
w4654646
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2016-3-2
结帖率:76.47%
收藏
 问题点数:0 回复次数:1 
求不同图形的面积
#include <iostream>
using namespace std;

class Figure {
protected:
    double x,y;

public:
    Figure(double a,double b) { x = a; y = b; }

    virtual void showArea() {
        cout<<"No area computation defined";
        cout<<"For this class. \n";
    }
};

class Triangle:public Figure {
public:
    Triangle(double a,double b):Figure(a,b) {};

    void showArea() {
        cout<<"Triangle with height "<<x;
        cout<<" and base "<<y;
        cout<<x*y/2<<endl;
    }
};

class Square:public Figure {
public:
    Square(double a,double b):Figure(a,b) {};

    void showArea() {
        cout<<"Square with dimension "<<x;
        cout<<" * "<<y<<" has an area of ";
        cout<<x*y<<endl;
    }
};

class Circle:public Figure {
public:
    Circle(double a):Figure(a,a) {};

    void showArea() {
        cout<<"Circle with radius "<<x;
        cout<<" has an area of ";
        cout<<x*x*3.1416<<endl;
    }
};

class Trapezoid:public Figure {
protected:
    double z;

public:
    Trapezoid(double a,double b,double c):Figure(a,b) { z = c; };

    void showArea() {
        cout<<"Trapezoid area is: "<<endl;
        cout<<(x + y) * z / 2<<endl;
    }
};

int main() {
    int m;
    double base,high;
    double length,wide;
    double radius;
    double b1,b2,h1;

    for(int j = 0; j < 3; j++) {

    cout<<"         _______________________         "<<endl;
    cout<<"         |     请选择操作      |         "<<endl;
    cout<<"         |    1.求三角形面积   |         "<<endl;
    cout<<"         |    2.求矩形面积     |         "<<endl;
    cout<<"         |    3.求圆面积       |         "<<endl;
    cout<<"         |    4.求梯形面积     |         "<<endl;
    cout<<"         |     请输入操作      |         "<<endl;
    cout<<"         |_____________________|         "<<endl;
    cin>>m;

   
    cout<<"请输入三角形底与高"<<endl;
    cin>>base>>high;
    Triangle t(base , high);

    cout<<"请输入矩形长与宽"<<endl;
    cin>>length>>wide;
    Square s(length , wide);

    cout<<"请输入圆的半径";
    cin>>radius;
    Circle c(radius);

    cout<<"请输入梯形上下底与高"<<endl;
    cin>>b1>>b2>>h1;
    Trapezoid p(b1 , b2 , h1);

    switch(m) {
    case 1:
        t.showArea();
        break;

    case 2:
        s.showArea();
        break;

    case 3:
        c.showArea();
        break;

    case 4:
        p.showArea();
        break;
    }
    }
    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册
每次出现操作界面,无论按什么操作都会出现“输入三角形底与高”等,这怎么改?
搜索更多相关主题的帖子: include public double 
2016-12-07 10:33
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
你贴的图跟你说的不一样啊。
2016-12-07 13:19
快速回复:求不同图形的面积
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026651 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved