• 7-003几何形体面积排序,有错,待改


    #include<stdafx.h>
    #include<iostream>
    #include<stdio.h>
    #include<math.h> 
    using namespace std;
    class CShape{
    public:
    	virtual double Area() = 0;//pure virtual fun
    	virtual void PrintInfo()= 0;
    };
    class CRectangle :public CShape//public mean??
    {
    public:
    	int w, h;
    	virtual double Area();//virtual mean??
    	virtual void PrintInfo();
    	double CRectangle::Area()
    	{
    		return w*h;
    	}
    	double CRectangle::PrintInfo()
    	{
    		cout << "CRectangle:" << Area() << endl;
    	}
    };
    class CCircle :public CShape{
    public:
    	int r;
    	virtual double Area();//virtual mean??
    	virtual void PrintInfo();
    	double CCircle::Area()
    	{
    		return 3.14*r*r;
    	}
    	double CCircle::PrintInfo()
    	{
    		cout << "CCircle:" << Area() << endl;
    	}
    };
    class CTriangle :public CShape{
    public:
    	int a, b, c;
    	virtual double Area();//virtual mean??
    	virtual void PrintInfo();
    	double CTriangle::Area()
    	{
    		double p = (a + b + c) / 2.0;
    		return sqrt(p*(p - a)*(p - b)*(p - c));
    	}
    	double CTriangle::PrintInfo()
    	{
    		cout << "CTriangle:" << Area() << endl;
    	}
    };
    CShape *pShapes[100]; //基类指针,使用多态
    int MyCompare(const void *s1, const void *s2);
    int main()
    {
    	int n;
    	CRectangle *pr; CCircle *pc; CTriangle *pt;
    	cin >> n;
    	for (int i; i<n; i++)
    	{
    		char c;
    		cin >> c;
    		switch (c)
    		{
    		case'R':
    			pr = new CRectangle();
    			cin >> pr->>w >> pr->h;
    			pShapes[i] = pr;
    			break;
    		case'C':
    			pr = new CCircle();
    			cin >> pr->>r;
    			pShapes[i] = pc;
    			break;
    		case'T':
    			pr = new CTriangle();
    			cin >> pr->>a >> pr->b >> pr->c;
    			pShapes[i] = pt;
    			break;
    		}
    	}
    	qsort(pShapes, n, sizeof(CShape*), Mycompare);
    	for (i = 0; i<n; i++)
    	{
    		pShapes[i]->PrintInfo();
    	}
    	return 0;
    }

  • 相关阅读:
    js遍历不要使用index 而是使用id(数据唯一表示)
    eureka
    Mybatis-plus自动填充字段的值(如createTime,UpdateTime)
    计算机网络入门
    操作系统入门
    计算机组成原理入门
    《事实》读书笔记
    推荐算法入门
    源码编译安装apache2.4脚本
    Mycat实现读写分离
  • 原文地址:https://www.cnblogs.com/alfredsun/p/4467243.html
Copyright © 2020-2023  润新知