在C 中使用静态功能,我的教授希望我告诉Main哪个对象具有最高优先级,而无需传递任何内容



i获得了以下主函数,我必须编码对象以通过。

这是主要的:

#include <iostream>
#include <string>
#include <ctime>
#include <stdexcept>
#include "ToDo.h"
using namespace std;
int getRand(int min, int range) {
    return (rand() % range) + min;     
}

const unsigned int NUM_VERB = 4;
const unsigned int NUM_ACTIVITY = 7;
const string VERB[] = { "Play", "Work on", "Practice","Eat", };
const string TARGET[] = { "homework", "dishes", "games", "guitar","vacuuming","aardvarking","coding" };
int main()
{
    srand(static_cast<unsigned int>(time(NULL))); // seed random number generator
    int numTask = getRand(3, 3); // number of tasks is 3->3+3
    ToDo** tasks = new ToDo*[numTask]; // create array of ToDo pointers, sized with numTask
    // creates new ToDo objects and keeps the pointers in tasks array
    for (int i = 0; i < numTask; i++) {
        tasks[i] = new ToDo(getRand(1, 9), VERB[rand() % NUM_VERB] + " " + TARGET[rand() % 7]);
    }
    cout << "The tasks are:n" << "PrioritytTaskn";
    // lists the ToDo objects using the output() member
    for (int i = 0; i < numTask; i++) {
        cout << tasks[i]->output("tt") << endl;
    }
    cout << "nYou should work on:n";
    cout << ":==> " << ToDo::highestPriority()->getTask() << endl << endl;
    unsigned int increaseBy = rand() % 7 + 1;
    cout << "But if i increase the priority of: " << tasks[numTask -1]->getTask() << " by " << increaseBy << endl;
    tasks[numTask - 1]->increasePriority(increaseBy);
    cout << "nYou should work on:n";
    cout << ":==> " << ToDo::highestPriority()->output(": ") << endl;
    // make sure all priorities are greator than 0
    for (int i = 0; i < numTask; i++) {
        if (tasks[i]->getPriority() < 1) {
            throw invalid_argument("Invalid Priority Found!");
        }
    }
    // de-allocate memory, pointer null-ing not important as end of progran
    for (int i = 0; i < numTask; i++) {
        delete tasks[i];
    }
    delete tasks;

    getchar();
    return 0;
}

我对ToDo::highestPriority()->getTask()ToDo::highestPriority()->output(": ")感到困惑,我不知道我将如何使用这些位置来告诉阵列中的哪个位置的优先级最高。

我的运行理论是使用3个静态int s,如下:

  • 跟踪对象的数量(计数器)
  • 跟踪哪个对象具有最高优先级(通过使其在最高优先级相等),
  • 跟踪最高优先级的数字。

我仍然无法弄清楚如何判断数组中的哪个位置的优先级最高。

我不能编辑主函数,只能创建一个对象,任何人都可以帮助我解决这个问题吗?

您需要定义 class Todo,并且存在现有用法。

让我们从最小的实现开始:

class Todo {
public:
    static ToDo * highestPriority() { return nullptr; }
    Todo (int, std::string) {}
    int getPriority() { return {}; }
    std::string getTask() { return {}; }
    std::string output(std::string) { return {}; }
    void increasePriority(int) {}
}

现在我们需要对每个

做明智的事情
class ToDo;
bool todo_less(ToDo const * lhs, ToDo const * rhs) { return lhs->getPriority() < rhs->getPriority(); }
class ToDo {
    int priority;
    std::string task;
    static std::vector<ToDo*> instances;
public:
    static ToDo * highestPriority() { return *std::max_element(instances.begin(), instances.end(), todo_less); }
    ToDo (int _priority, std::string _task) : priority(_priority), task(_task) { instances.push_back(this); }
    ~ToDo() { instances.erase(std::find(instances.begin(), instances.end(), this)); }
    int getPriority() const { return priority; }
    std::string getTask() const { return task; }
    std::string output(std::string joiner) const { return to_string(priority) + joiner + task; }
    void increasePriority(int inc) { priority += inc; }
}

相关内容

  • 没有找到相关文章

最新更新