gpt4 book ai didi

c++ - 在 C++ 中使用静态函数,我的教授希望我告诉 main 哪个对象具有最高优先级而不传递任何内容

转载 作者:行者123 更新时间:2023-11-30 03:27:23 26 4
gpt4 key购买 nike

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

主要内容:

#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 个静态 int,如下所示:

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

我仍然不知道如何告诉 main 数组中的哪个位置具有最高优先级。

我无法编辑主函数,只能创建一个对象,谁能帮我解决这个问题?

最佳答案

您需要定义一个 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; }
}

关于c++ - 在 C++ 中使用静态函数,我的教授希望我告诉 main 哪个对象具有最高优先级而不传递任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47437497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com