Задача Лекции
Здравейте! Моля за съдействие по задача 5 от домашното за тема 5. Смятам, че успях да я реша правилно, но отговорът в Judge е малко странен:
The process executing your submission for this test may not have received the output successfully. Please try to submit again the same solution. If the result does not change, then search the error in the submission itself.
Съответно пробвах да кача файловете неколкократно. Само на някои тестове дава грешка и резултатът е 40 %, но колкото и да го мъча и да го тествам с измислени от мен данни, винаги дава коректен резултат. Каква може да е причината? Ето и кода:
Lecture.h
#pragma once
#include "Resource.h"
#include "ResourceType.h"
namespace SoftUni
{
class Lecture
{
private:
std::vector<Resource> _resources;
public:
std::vector<Resource>* getResources() { return &_resources; }
std::vector<Resource>::iterator begin() { return _resources.begin(); }
std::vector<Resource>::iterator end() { return _resources.end(); }
int operator[] (ResourceType resourceType);
};
// Main - line 27
Lecture& operator<< (Lecture& lecture, Resource& resource)
{
bool ok = 1;
for (int i = 0; i < (*lecture.getResources()).size(); i++)
{
if ((*lecture.getResources())[i].getId() == resource.getId())
{
(*lecture.getResources())[i] = resource;
ok = 0;
}
}
if (ok == 1)
(*lecture.getResources()).push_back(resource);
//sort po metoda na mehur4eto
Lecture tempLecture;
(*tempLecture.getResources()).push_back(resource);
for (int i = 0; i < (*lecture.getResources()).size() - 1; i++)
{
for (int j = 0; j < (*lecture.getResources()).size() - i - 1; j++)
{
if ((*lecture.getResources())[j].getId() > (*lecture.getResources())[j + 1].getId())
{
(*tempLecture.getResources())[0] = (*lecture.getResources())[j];
(*lecture.getResources())[j] = (*lecture.getResources())[j + 1];
(*lecture.getResources())[j + 1] = (*tempLecture.getResources())[0];
}
}
}
return lecture;
}
// Main - line 39
std::vector<ResourceType>* operator<< (std::vector<ResourceType>& resourceType, Lecture& lecture)
{
bool ok = 0;
ResourceType resType[3] = { PRESENTATION, DEMO, VIDEO };
for (int i = 0; i < 3; i++)
{
ok = 0;
for (int j = 1; j < (*lecture.getResources()).size(); j++)
{
if ((*lecture.getResources())[j].getType() == resType[i])
{
ok = 1;
}
}
if (ok == 1)
resourceType.push_back(resType[i]);
}
return &resourceType;
}
// Main - line 42
int Lecture::operator[] (ResourceType resourceType)
{
int count = 0;
for (int i = 0; i < (*getResources()).size(); i++)
if ((*getResources())[i].getType() == resourceType)
count++;
return count;
}
}
Resource.h
#pragma once
#include "ResourceType.h"
namespace SoftUni
{
class Resource
{
private:
int _id;
ResourceType _type;
std::string _link;
public:
int getId() { return _id; }
ResourceType getType() const { return _type; }
friend std::istream& operator>>(std::istream& in, Resource& resource);
friend std::ostream& operator<<(std::ostream& out, const Resource& resource);
bool operator<(const Resource& resource) const { return _id > resource._id ? false : true; }
};
std::istream& operator>>(std::istream& in, Resource& resource)
{
std::string type;
in >> resource._id >> type >> resource._link;
if (type == "Presentation")
resource._type = PRESENTATION;
else if (type == "Demo")
resource._type = DEMO;
else if (type == "Video")
resource._type = VIDEO;
else if (type == "[unknown]")
return in;
}
std::ostream& operator<<(std::ostream& out, const Resource& resource)
{
out << resource._id << ' ' << resource._type << ' ' << resource._link;
return out;
}
}