Persi111 2023-04-14 19:36 采纳率: 85.7%
浏览 46
已结题

C++,实在不会了,请教一下

题目1: 将以下代码拷贝到工程中:
//////////////////////////////////////////////////////////////////////

#include <vector>


class CMShape
{
public:
    CMShape();
    virtual void Output();

};

class CMPoint 
{
public:

    virtual void Output() {
        std::cout << "点:" << "坐标:" << x <<" "<< y<<endl;
    }

    double x, y;
};

//多段线
class CMPolyLine
{
public:

    void AddPoint(CMPoint &pt); //增加一个点
    void AddPointArray(CMPoint* lpPt, int numPts);

    vector<CMPoint> m_pts;
};

//多边形,从多段线继承
class CMPolyRegion : public CMPolyLine
{

};

//请根据以下调用代码,补充完善前面的类接口
void main()
{
    vector<CMShape*> shapesData;

    CMPoint pt1(1.0, 0, 0);
    CMPoint pt2(2.0, 0, 0);
    CMPoint pt3(2.0, 0, 0);

    CMPolyLine polyLine;
    polyLine.AddPoint(pt1);
    polyLine.AddPoint(pt2);
    polyLine.AddPoint(pt3);

    CMPolyLine polyLine2; //表示多段线在后面追加一个点
    polyLine2 = polyLine + CMPoint(5.0, 5.0);


    //"拷贝": 将多段线的数据拷贝给多边形对象 
    CMPolyRegion region;  
    region = polyLine;


    //将三个对象的指针加入数组中
    shapesData.push_back(&polyLine);
    shapesData.push_back(&polyLine2);
    shapesData.push_back(®ion);

    //输出数据
    for(int i=0; i<shapesData.size(); i++)
    {
        shapesData[i]->Output();
    }
}


///////////////////////////////////////////////////////////////////////

// 题目2
完成一个多计时器功能的类
功能:一个记时名称,起始时间,结束时间,
输出该记时
用例:
记录 "跑道1", 开始,结束,输出时间, 按时分秒输出

并输出调用例子: 输出用cout 输出

CMyTimer
{
     ...
}

CMyTime time;

time.Start("跑道1");
time.End("跑道1") ;
time.Output("跑道1");

time_t t1 =clock();
time_t t2 =clock();
ti-t2

  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-04-14 19:45
    关注

    基于Monster 组和GPT的调写:
    题目一:

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class CMShape
    {
    public:
        CMShape() {}
        virtual ~CMShape() {}
        virtual void Output() {}
    };
    
    class CMPoint : public CMShape
    {
    public:
        CMPoint(double x = 0, double y = 0) : x(x), y(y) {}
        virtual ~CMPoint() {}
        virtual void Output() {
            std::cout << "点:" << "坐标:" << x << " " << y << endl;
        }
        double x, y;
    };
    
    //多段线
    class CMPolyLine : public CMShape
    {
    public:
        CMPolyLine() {}
        virtual ~CMPolyLine() {}
        void AddPoint(CMPoint &pt) { m_pts.push_back(pt); } //增加一个点
        void AddPointArray(CMPoint* lpPt, int numPts) {}
        CMPolyLine operator+(CMPoint pt) {
            CMPolyLine newLine(*this);
            newLine.AddPoint(pt);
            return newLine;
        }
        virtual void Output() {
            std::cout << "多段线:" << endl;
            for (int i = 0; i < m_pts.size(); i++) {
                m_pts[i].Output();
            }
        }
        vector<CMPoint> m_pts;
    };
    
    //多边形,从多段线继承
    class CMPolyRegion : public CMPolyLine
    {
    public:
        CMPolyRegion() {}
        virtual ~CMPolyRegion() {}
    };
    
    //主函数
    int main()
    {
        vector<CMShape*> shapesData;
    
        CMPoint pt1(1.0, 0.0);
        CMPoint pt2(2.0, 0.0);
        CMPoint pt3(2.0, 0.0);
    
        CMPolyLine polyLine;
        polyLine.AddPoint(pt1);
        polyLine.AddPoint(pt2);
        polyLine.AddPoint(pt3);
    
        CMPolyLine polyLine2; //表示多段线在后面追加一个点
        polyLine2 = polyLine + CMPoint(5.0, 5.0);
    
        //"拷贝": 将多段线的数据拷贝给多边形对象 
        CMPolyRegion region;
        region = polyLine;
    
        //将三个对象的指针加入数组中
        shapesData.push_back(&polyLine);
        shapesData.push_back(&polyLine2);
        shapesData.push_back(&region);
    
        //输出数据
        for (int i = 0; i < shapesData.size(); i++) {
            shapesData[i]->Output();
        }
    
        return 0;
    }
    
    
    

    题目二:

    #include <iostream>
    #include <map>
    #include <chrono>
    #include <iomanip>
    
    class CMyTimer
    {
    public:
        void Start(const std::string& name)
        {
            m_startTimes[name] = std::chrono::high_resolution_clock::now();
        }
    
        void End(const std::string& name)
        {
            m_endTimes[name] = std::chrono::high_resolution_clock::now();
        }
    
        void Output(const std::string& name)
        {
            auto startTime = m_startTimes.find(name);
            auto endTime = m_endTimes.find(name);
    
            if (startTime == m_startTimes.end() || endTime == m_endTimes.end())
            {
                std::cout << "Error: Timer '" << name << "' not found." << std::endl;
                return;
            }
    
            auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime->second - startTime->second);
    
            std::cout << "Timer '" << name << "' duration: ";
            std::cout << std::setw(2) << std::setfill('0') << (duration.count() / (60 * 60 * 1000)) << ":";
            std::cout << std::setw(2) << std::setfill('0') << ((duration.count() / (60 * 1000)) % 60) << ":";
            std::cout << std::setw(2) << std::setfill('0') << ((duration.count() / 1000) % 60) << ".";
            std::cout << std::setw(3) << std::setfill('0') << (duration.count() % 1000) << std::endl;
        }
    
    private:
        std::map<std::string, std::chrono::high_resolution_clock::time_point> m_startTimes;
        std::map<std::string, std::chrono::high_resolution_clock::time_point> m_endTimes;
    };
    
    int main()
    {
        CMyTimer timer;
        timer.Start("跑道1");
        // do something
        timer.End("跑道1");
        timer.Output("跑道1");
    
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 创建了问题 4月14日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效