我得到了以下主要功能,我必须对要传递的对象进行编码。
这里主要:
#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 数组中的哪个位置具有最高优先级。
我无法编辑主要功能,只能创建一个对象,有人可以帮我解决这个问题吗?