1

我得到了以下主要功能,我必须对要传递的对象进行编码。

这里主要:

#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" << "Priority\tTask\n";
    // lists the ToDo objects using the output() member
    for (int i = 0; i < numTask; i++) {
        cout << tasks[i]->output("\t\t") << 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(": ")我不知道如何使用它们来告诉 main 数组中的哪个位置具有最高优先级。

我的跑步理论是使用 3 个 staticint如下:

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

我仍然无法弄清楚如何告诉 main 数组中的哪个位置具有最高优先级。

我无法编辑主要功能,只能创建一个对象,有人可以帮我解决这个问题吗?

4

1 回答 1

2

您需要定义 a 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; }
}
于 2017-11-22T15:08:08.157 回答