adding cs235
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
#include "Factory.h"
|
||||
#include "circle.h"
|
||||
//You may add #include statments here
|
||||
|
||||
/*
|
||||
You will MODIFY THIS DOCUMENT.
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getGame()
|
||||
|
||||
Creates and returns an object whose class extends JosephusInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Circle", you might say, "return new Circle();".
|
||||
*/
|
||||
JosephusInterface * Factory::getGame()
|
||||
{
|
||||
return new circle();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "JosephusInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getGame()
|
||||
|
||||
Creates and returns an object whose class extends JosephusInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Circle", you might say, "return new Circle();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static JosephusInterface * getGame();
|
||||
};
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,54 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class JosephusInterface
|
||||
{
|
||||
public:
|
||||
JosephusInterface(void){}
|
||||
virtual ~JosephusInterface(void){}
|
||||
/*
|
||||
getNames
|
||||
|
||||
Returns a list of names in the order in which the people will be standing for the "game".
|
||||
Although fewer people may be playing, you must return 20 names here. Do not provide
|
||||
duplicate names.
|
||||
|
||||
For the sake of the test driver, this method must return the list of 20 names in the
|
||||
same order every time it is called, and this list of 20 names in this order must be used
|
||||
to play the "game".
|
||||
|
||||
This method will be called repeatedly.
|
||||
*/
|
||||
virtual vector<string> getNames() = 0;
|
||||
/*
|
||||
playGame
|
||||
|
||||
Plays a "game" with the first n people from the list (above) counting forward every m. An
|
||||
explanation for how the "game" works can be found in the exam specs.
|
||||
|
||||
This method should return a list of names in the order in which the lot fell upon them (including
|
||||
the survivor, who should be last). If n is not between 10 and 20 or if m is non-positive,
|
||||
return an empty vector.
|
||||
|
||||
This method will be called repeatedly.
|
||||
*/
|
||||
virtual vector<string> playGame(int n, int m) = 0;
|
||||
/*
|
||||
reportSafeIndex
|
||||
|
||||
Returns the "safe index", the last index/location in the circle that will be chosen when
|
||||
the "game" is played. The point of this method is to permit someone to cheat the system
|
||||
by finding the safe location ahead of time.
|
||||
|
||||
If n is not between 10 and 20 or if m is non-positive, return -1.
|
||||
|
||||
This method may be called repeatedly.
|
||||
*/
|
||||
virtual int reportSafeIndex(int n, int m) = 0;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=main.o Factory.o circle.o
|
||||
EXE=main
|
||||
|
||||
all: $(EXE)
|
||||
|
||||
|
||||
$(EXE): main.o
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o circle.o
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
circle.o: circle.cpp circle.h
|
||||
|
||||
run: $(EXE)
|
||||
@./$(EXE)
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
|
||||
debug: $(EXE)
|
||||
gdb ./$(EXE)
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <vector>
|
||||
#include "circle.h"
|
||||
|
||||
ostream & operator<<(ostream & os, vector<string> strings) {
|
||||
for(unsigned int i = 0; i < strings.size(); i++)
|
||||
os << strings[i] << ", ";
|
||||
os << endl;
|
||||
return os;
|
||||
}
|
||||
|
||||
vector<string> circle::getNames() {
|
||||
vector<string> names;
|
||||
names.push_back("Josephus");
|
||||
names.push_back("A");
|
||||
names.push_back("B");
|
||||
names.push_back("C");
|
||||
names.push_back("D");
|
||||
names.push_back("E");
|
||||
names.push_back("F");
|
||||
names.push_back("G");
|
||||
names.push_back("H");
|
||||
names.push_back("I");
|
||||
names.push_back("J");
|
||||
names.push_back("K");
|
||||
names.push_back("L");
|
||||
names.push_back("M");
|
||||
names.push_back("N");
|
||||
names.push_back("O");
|
||||
names.push_back("P");
|
||||
names.push_back("Q");
|
||||
names.push_back("R");
|
||||
names.push_back("S");
|
||||
return names;
|
||||
}
|
||||
vector<string> circle::playGame(int n, int m) {
|
||||
vector<string> names = getNames();
|
||||
vector<string> temp;
|
||||
if(n < 10 || n > 20){
|
||||
names.clear();
|
||||
return names;
|
||||
}
|
||||
if(m < 1){
|
||||
names.clear();
|
||||
return names;
|
||||
}
|
||||
for(int i = 0; i < 20 - n; i++) {
|
||||
names.pop_back();
|
||||
}
|
||||
unsigned int count = 0;
|
||||
for(int i = 0; i < n; i++) {
|
||||
int hits = 0;
|
||||
while(hits < m) {
|
||||
if(names[count] == ""){
|
||||
}
|
||||
else {
|
||||
hits++;
|
||||
if(hits == m) {
|
||||
temp.push_back(names[count]);
|
||||
names[count] = "";
|
||||
hits = 100;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
if(count+1 > names.size()) {
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
int circle::reportSafeIndex(int n, int m) {
|
||||
if(m < 1) {
|
||||
return -1;
|
||||
}
|
||||
else if (n < 10 || n > 20){
|
||||
return -1;
|
||||
}
|
||||
vector<string> names = getNames();
|
||||
vector<string> kill_list = playGame(n, m);
|
||||
string safe_person_name = kill_list[kill_list.size() - 1];
|
||||
for(unsigned int i = 0; i < names.size(); i++) {
|
||||
if(safe_person_name == names[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef __CIRCLE_H__
|
||||
#define __CIRCLE_H__
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "JosephusInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class circle : public JosephusInterface
|
||||
{
|
||||
public:
|
||||
circle(){}
|
||||
vector<string> getNames();
|
||||
vector<string> playGame(int n, int m);
|
||||
int reportSafeIndex(int n, int m);
|
||||
};
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user