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

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

#include 


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

};

class CMPoint 
{
public:

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

//多段线
class CMPolyLine
{
public:

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

    vector m_pts;
};

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

};

//请根据以下调用代码,补充完善前面的类接口
void main()
{
    vector 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; isize(); 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

基于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;
}


题目一不太懂
第二题:

#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;
}
 
 

引用chatGPT作答,关于题目一,这个程序中需要你定义几个类和实现它们的方法,以下是这些类和方法的说明:

1.CMShape:基础图形类,拥有一个默认的构造函数和一个 Output() 虚函数。

2.CMPoint:点类,继承自 CMShape,拥有 x 和 y 两个成员变量以表示它在平面直角坐标系中的坐标。

3.CMPolyLine:多段线类,拥有一个 m_pts 成员变量表示它由哪些点组成,并且拥有两个 AddPoint() 函数,可以将一个 CMPoint 对象或一个 CMPoint 数组加入到 m_pts 中。

4.CMPolyRegion:多边形类,继承自 CMPolyLine,没有新增的成员变量或方法。

在你开始编写代码之前,需要先引入一些头文件:

#include <iostream>
#include <vector>

现在,你可以开始编写代码了。以下是你需要补充完善的类接口:

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

class CMPoint : public CMShape
{
public:
    CMPoint(double _x, double _y) : x(_x), y(_y) {}
    void Output() override {
        std::cout << "点:" << "坐标:" << x <<" "<< y<<std::endl;
    }
    double x, y;
};

class CMPolyLine : public CMShape
{
public:
    void AddPoint(CMPoint &pt) { m_pts.push_back(pt); }
    void AddPointArray(CMPoint* lpPt, int numPts) {
        for (int i = 0; i < numPts; ++i)
            m_pts.push_back(lpPt[i]);
    }
    CMPolyLine operator+(const CMPoint& pt) const {
        CMPolyLine result = *this;
        result.AddPoint(const_cast<CMPoint&>(pt));
        return result;
    }
    void Output() override {
        std::cout << "多段线:" << std::endl;
        for (const auto& pt : m_pts)
            pt.Output();
    }
    std::vector<CMPoint> m_pts;
};

class CMPolyRegion : public CMPolyLine
{
};

关于题目二,首先,我们需要一个计时器的结构体 TimerData,它包含计时器名称、起始时间、结束时间等信息:

struct TimerData {
    std::string name;   // 计时器名称
    std::chrono::system_clock::time_point start;  // 起始时间
    std::chrono::system_clock::time_point end;    // 结束时间
};

接下来,我们可以定义 CMyTimer 类,它包含计时器的容器 timers_,并提供以下函数:

1.Start 函数:开始计时,记录计时器的名称和起始时间。
2.End 函数:结束计时,记录计时器的名称和结束时间。
3.Output 函数:输出指定名称的计时器的时间差,以时、分、秒的形式输出。
代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <chrono>

class CMyTimer {
public:
    // 开始计时
    void Start(const std::string& name) {
        timers_.push_back({name, std::chrono::system_clock::now(), {}});
    }

    // 结束计时
    void End(const std::string& name) {
        for (auto& timer : timers_) {
            if (timer.name == name && timer.end.time_since_epoch().count() == 0) {
                timer.end = std::chrono::system_clock::now();
                break;
            }
        }
    }

    // 输出指定名称的计时器的时间差
    void Output(const std::string& name) {
        for (const auto& timer : timers_) {
            if (timer.name == name && timer.end.time_since_epoch().count() > 0) {
                std::chrono::duration<double> diff = timer.end - timer.start;
                std::cout << "计时器名称:" << timer.name << std::endl;
                std::cout << "时间差为:" << diff.count() << " 秒" << std::endl;
                int hours = std::chrono::duration_cast<std::chrono::hours>(diff).count();
                diff -= std::chrono::hours(hours);
                int minutes = std::chrono::duration_cast<std::chrono::minutes>(diff).count();
                diff -= std::chrono::minutes(minutes);
                int seconds = static_cast<int>(diff.count());
                std::cout << "时间差为:" << hours << " 小时 " << minutes << " 分钟 " << seconds << " 秒" << std::endl;
                break;
            }
        }
    }

private:
    std::vector<TimerData> timers_;
};

最后,我们可以按照题目中的用例来测试代码:

int main() {
    CMyTimer timer;

    timer.Start("跑道1");
    std::cout << "正在计时..." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    timer.End("跑道1");
    timer.Output("跑道1");

    return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^