TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
Reveiwing the following code please answer the following questions.pdf
1. Reveiwing the following code please answer the following questions
1. Discuss the data structures used
2. Discuss the algorithm used to search the list for a particular video and the time complexity of
the algorithm
#include
#include
#include
class VideoType
{
private:
std::wstring movieTitle;
std::wstring producer;
std::wstring director;
std::wstring company;
int noOfCopies = 0;
//
public:
VideoType(const std::wstring &movieTitle, const std::wstring &producer, const std::wstring
&director, const std::wstring &company, int noOfCopies);
//
virtual std::wstring getMovieTitle();
virtual void setMovieTitle(const std::wstring &movieTitle);
virtual std::wstring getProducer();
virtual void setProducer(const std::wstring &producer);
virtual std::wstring getDirector();
virtual void setDirector(const std::wstring &director);
virtual std::wstring getCompany();
virtual void setCompany(const std::wstring &company);
virtual int getNoOfCopies();
virtual void setNoOfCopies(int noOfCopies);
//
/*
* Rent video to customer
*/
virtual bool rentVideo(CustomerType *customer);
2. /*
* If customer has video, return it back to stock
*/
virtual bool returnVideo(CustomerType *cusomer);
//
/*
* If video is available in stock
*/
virtual bool isAvailableInStore();
//
virtual std::wstring toString() override;
};
VideoType::VideoType(const std::wstring &movieTitle, const std::wstring &producer, const
std::wstring &director, const std::wstring &company, int noOfCopies)
{ this->movieTitle = movieTitle;
this->producer = producer;
this->director = director;
this->company = company;
this->noOfCopies = noOfCopies;
}
std::wstring VideoType::getMovieTitle()
{
return movieTitle;
}
void VideoType::setMovieTitle(const std::wstring &movieTitle)
{
this->movieTitle = movieTitle;
}
std::wstring VideoType::getProducer()
{
return producer;
}
void VideoType::setProducer(const std::wstring &producer)
{
this->producer = producer;
10. static void createVideoList();
/*
* Read from file customers.txt and create Customer list
*/
static void createCustomerList();
/*
* Prompt user to select a customer
*/
static CustomerType *getCustomer();
/*
* Prompt user to select a video
*/
static VideoType *getVideo();
//
std::vector VideoStore::listOfVideos;
std::vector VideoStore::listOfCustomers;
java::util::Scanner *VideoStore::s = new java::util::Scanner(System::in);
void VideoStore::createVideoList()
{
try
{
BufferedReader *videoFile = nullptr;
FileReader tempVar(L"videos.txt");
videoFile = new BufferedReader(&tempVar);
videoFile->readLine(); // to flush comment line
for (std::wstring line = videoFile->readLine(); (line != L""); line = videoFile-
>readLine())
{
std::vector parts = line.split(L"t");
VideoType tempVar2(parts[0], parts[1], parts[2], parts[3], std::stoi(parts[4]));
listOfVideos.push_back(&tempVar2);
}
delete videoFile;
}
catch (const IOException &e)
11. {
System::err::print(e);
exit(-1);
}
}
void VideoStore::createCustomerList()
{
try
{
BufferedReader *customerFile = nullptr;
FileReader tempVar(L"customers.txt");
customerFile = new BufferedReader(&tempVar);
customerFile->readLine(); // to flush comment line
for (std::wstring line = customerFile->readLine(); (line != L""); line = customerFile-
>readLine())
{
std::vector parts = line.split(L"t");
CustomerType tempVar2(parts[0], parts[1], parts[2]);
listOfCustomers.push_back(&tempVar2);
}
delete customerFile;
}
catch (const IOException &e)
{
System::err::print(e);
exit(-1);
}
}
CustomerType *VideoStore::getCustomer()
{
std::wcout << std::wstring(L"Enter Customer Id") << std::endl;
for (int i = 0; i < listOfCustomers.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L"t") <<
listOfCustomers[i]->getFirstName() << std::endl;
}
12. int custId = s->nextInt();
if (custId > 0 && custId <= listOfCustomers.size())
{
return listOfCustomers[custId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Customer Id") << std::endl;
return nullptr;
}
}
VideoType *VideoStore::getVideo()
{
std::wcout << std::wstring(L"Enter Video Id") << std::endl;
for (int i = 0; i < listOfVideos.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L"t") <<
listOfVideos[i]->getMovieTitle() << std::endl;
}
int videoId = s->nextInt();
if (videoId > 0 && videoId <= listOfVideos.size())
{
return listOfVideos[videoId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Video Id") << std::endl;
return nullptr;
}
}
#include
#include
#include
/*
* Driver function
*/
13. static void main(std::vector &a);
/*
* Show menu and ask for user choice
*/
public:
static int displyMenu();
//
//.cpp file code:
void ::main(std::vector &a)
{
createVideoList();
createCustomerList();
//
int choice = displyMenu();
while (choice != 9)
{
if (choice == 1)
{
std::wcout << std::wstring(L"Please type the exact movie Title") << std::endl;
std::wstring title = s::nextLine();
Iterator iterator = listOfVideos::begin();
bool found = false;
while (iterator->hasNext())
{
VideoType *v = iterator->next();
if (v->getMovieTitle().equals(title))
{
found = true;
std::wcout << std::wstring(L"Yes, we have this video in our store.") <<
std::endl;
break;
}
iterator++;
}
if (!found)
{
14. std::wcout << std::wstring(L"Sorry!! we dont have this video in our store") <<
std::endl;
}
}
else if (choice == 2)
{
CustomerType *cust = getCustomer();
VideoType *video = getVideo();
//
if (cust != nullptr && video != nullptr)
{
if (video->rentVideo(cust))
{
std::wcout << std::wstring(L"Rented successfully") << std::endl;
}
else
{
std::wcout << std::wstring(L"Can not rent. Stock not available") << std::endl;
}
}
}
else if (choice == 3)
{
CustomerType *cust = getCustomer();
VideoType *video = getVideo();
//
if (cust != nullptr && video != nullptr)
{
if (video->returnVideo(cust))
{
std::wcout << std::wstring(L"Returned Successfully") << std::endl;
}
else
{
std::wcout << std::wstring(L"Customer don't have this video") << std::endl;
}
15. }
}
else if (choice == 4)
{
VideoType *video = getVideo();
if (video != nullptr)
{
if (video->isAvailableInStore())
{
std::wcout << std::wstring(L"Stock available") << std::endl;
}
else
{
std::wcout << std::wstring(L"Stock Unavaiable.") << std::endl;
}
}
}
else if (choice == 5)
{
std::wcout << std::wstring(L"Title of all the videos:") << std::endl;
for (int i = 0; i < listOfVideos->size(); i++)
{
std::wcout << listOfVideos->get(i).getMovieTitle() << std::endl;
std::wcout << std::endl;
}
}
else if (choice == 6)
{
std::wcout << std::wstring(L"Title of all the videos:") << std::endl;
for (int i = 0; i < listOfVideos->size(); i++)
{
std::wcout << listOfVideos->get(i) << std::endl;
std::wcout << std::endl;
}
}
choice = displyMenu();
16. }
delete s;
}
int ::displyMenu()
{
std::wcout << std::wstring(L"Select one of following:") << std::endl;
std::wcout << std::wstring(L"1: To check whether store carries a particular video") <<
std::endl;
std::wcout << std::wstring(L"2: To Check out a video") << std::endl;
std::wcout << std::wstring(L"3: To Check in a video") << std::endl;
std::wcout << std::wstring(L"4: To Check whether a particular video is in stock.") <<
std::endl;
std::wcout << std::wstring(L"5: To Print only the titles of all the videos") << std::endl;
std::wcout << std::wstring(L"6: To print a list of all the videos") << std::endl;
std::wcout << std::wstring(L"9: to exit") << std::endl;
int result = s::nextInt();
return result;
}
Solution
Stack :-
void VideoStore::createVideoList()
{
try
{
BufferedReader *videoFile = nullptr;
FileReader tempVar(L"videos.txt");
videoFile = new BufferedReader(&tempVar);
videoFile->readLine(); // to flush comment line
for (std::wstring line = videoFile->readLine(); (line != L""); line = videoFile-
>readLine())
{
std::vector parts = line.split(L"t");
VideoType tempVar2(parts[0], parts[1], parts[2], parts[3], std::stoi(parts[4]));
listOfVideos.push_back(&tempVar2);
17. }
delete videoFile;
}
above code declare a object for BufferReader and points to null
created one temp obj which points to video file and then it is assigned to videotype object of
BufferedReader
it reads the video or we can say it stores the video in parts an then it uses the push function of
stack
Pointer : - everywhere in program it is easily seen pointer and their reference type are used
Searching: - linear search is used to find particular video present in store and same for customer
object oriented programming approach used by defining getter and setter for all declared variable
2. algorithm used to search the list for a particular video and the time complexity of the
algorithm
VideoType *VideoStore::getVideo()
{
std::wcout << std::wstring(L"Enter Video Id") << std::endl;
for (int i = 0; i < listOfVideos.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L"t") <<
listOfVideos[i]->getMovieTitle() << std::endl;
}
int videoId = s->nextInt();
if (videoId > 0 && videoId <= listOfVideos.size())
{
return listOfVideos[videoId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Video Id") << std::endl;
return nullptr;
}
}
it shows the list of videos and ask the id of video
it checks if the id persents in the list or not by comparing it videoId > 0 && videoId <=
listOfVideos.size()
if id presents between 0 to size of list then it will return the list of video
18. Complexity :-
for (int i = 0; i < listOfVideos.size(); i++)
{
std::wcout << std::wstring(L"Id:") << (i << 1) << std::wstring(L"t") <<
listOfVideos[i]->getMovieTitle() << std::endl;
}
takes --- > O(N)
if (videoId > 0 && videoId <= listOfVideos.size())
{
return listOfVideos[videoId - 1];
}
else
{
std::wcout << std::wstring(L"Invalid Video Id") << std::endl;
return nullptr;
}
second step takes O(1) time
total Complexity will be O(N)+O(1) = O(N)
algorithm has O(N) Complexity