zoukankan      html  css  js  c++  java
  • 计算不同形状的面积

    #include<iostream.h>
    #include<math.h>
    #define pi 3.14
    class cshape
    {
    	int x,y;
    public:
    	cshape(int x1,int y1)
    	{
    		x=x1;
    		y=y1;
    	}
    	virtual float getarea(){return 0;};
    };
    class trial:public cshape
    {
    	int i,j,k;
    public:
    	trial(int i1,int j1,int k1,int x1,int y1):cshape(x1,y1)
    	{
    		i=i1;
    		j=j1;
    		k=k1;
    	}
    	float p;
    	float getarea()
    	{
           p=(float)(i+j+k)/2;
    	   return (float)sqrt(p*(p-i)*(p-j)*(p-k));                              
    	}
    };
    class squrt:public cshape
    {
    	int a;
    public:
    	squrt(int a1,int x1,int y1):cshape(x1,y1)
    	{                                                                                     		a=a1;
    	}
    	float getarea()
    	{
    		return (float)a*a;
    	}
    };
    class circle:public cshape
    {
    	int r;
    public:
    	circle(int r1,int x1,int y1):cshape(x1,y1)
    	{
    		r=r1;
    	}
    	float getarea()
    	{
    		return (float)pi*r*r;
    	}
    };
    void getarea1(cshape &l)
    {
       cout<<l.getarea()<<endl;
    }
    void main()
    {
    	int i1,j1,k1,a1,r1,m,x1=0,y1=0;
    	cout<<"************(1)三角形****************"<<endl;
    	cout<<"************(2)正方形****************"<<endl;
    	cout<<"************(3)圆********************"<<endl;
    	cout<<"请输入你要计算面积的图形的序号"<<endl;
    	cin>>m;
    	if(m==1)
    	{
    		cout<<"请输入三边的长度"<<endl;
    	    cin>>i1>>j1>>k1;
    		int n;
    		n=(i1+j1+k1)/2;
    		if(n*(n-i1)*(n-j1)*(n-k1)<=0)
    			cout<<"无法组成三角形"<<endl;
    		else
    		{
    			trial tri(i1,j1,k1,x1,y1);
    		    getarea1(tri);
    		}
    	}
    	else if(m==2)
    	{
    		cout<<"请输入正方形的边长"<<endl;
    		cin>>a1;
    		squrt squ(a1,x1,y1);
    		getarea1(squ);
    	}
    	    else
    		{
    			cout<<"请输入圆的半径"<<endl;
    			cin>>r1;
    			circle cir(r1,x1,y1);
    			getarea1(cir);
    		}
    }
    

  • 相关阅读:
    NSUserDefaults写作和阅读对象定义自己
    JavaScript三在弹出的对话框中
    mybatis02 架构
    mybatis01
    oracle17 视图
    oracle16 例外
    oracle15 pl/sql 分页
    oracle14 复杂数据类型
    oracle13 触发器 变量
    oracle12 pl/sql
  • 原文地址:https://www.cnblogs.com/zztong/p/6695325.html
Copyright © 2011-2022 走看看