adding cs142 code
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "Circle.h"
|
||||
|
||||
Circle::Circle(){}
|
||||
|
||||
vector<string> Circle::getNames() { //used to populate a vector with names
|
||||
string special = "Josephus";
|
||||
vector<string> names = {{
|
||||
"Jacob",
|
||||
"Isabella",
|
||||
"Ethan",
|
||||
"Sophia",
|
||||
"Michael",
|
||||
"Emma",
|
||||
"Jayden",
|
||||
"Olivia",
|
||||
"William",
|
||||
"Ava",
|
||||
"Alexander",
|
||||
"Emily",
|
||||
"Noah",
|
||||
"Abigail",
|
||||
"Daniel",
|
||||
"Madison",
|
||||
"Aiden" ,
|
||||
"Chloe",
|
||||
"Anthony",
|
||||
}};
|
||||
return names;
|
||||
}
|
||||
|
||||
vector<string> Circle::playGame(int n, int m) {
|
||||
vector<string> names = getNames();
|
||||
vector<string> temp;
|
||||
if(n < 10 or n > 20){
|
||||
names = {};
|
||||
return names;
|
||||
}
|
||||
if(m < 0){
|
||||
names = {};
|
||||
return names;
|
||||
}
|
||||
for(int i = 0; i < 20 - n; i++) {
|
||||
names.pop_back();
|
||||
}
|
||||
names.push_back("Josephus"); //insures that Josephus will be in the game
|
||||
int count = 0;
|
||||
int threshold = 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;
|
||||
threshold++;
|
||||
}
|
||||
if(threshold > 100) { //threshold against infinite loop
|
||||
hits = 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
int Circle::reportSafeIndex(int n, int m) { //uses similar function as playGame() to determine index
|
||||
vector<string> names = getNames();
|
||||
vector<string> temp;
|
||||
int count = 0;
|
||||
int threshold = 0;
|
||||
if(n < 10 or n > 20){
|
||||
return -1;
|
||||
}
|
||||
if(m < 0){
|
||||
return -1;
|
||||
}
|
||||
for(int i = 0; i < 20 - n; i++) {
|
||||
names.pop_back();
|
||||
}
|
||||
names.push_back("Josephus");
|
||||
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;
|
||||
threshold++;
|
||||
}
|
||||
if(threshold > 100) { //incase of infinite loop due to improper input
|
||||
hits = 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
string index_holder = temp[n-1];
|
||||
int index = 0;
|
||||
names = getNames();
|
||||
for(unsigned int i = 0; i < names.size(); i++){
|
||||
if(names[i] == index_holder) {
|
||||
index = i+1;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#ifndef CIRCLE //Do not modify this line
|
||||
#define CIRCLE //Do not modify this line
|
||||
#include <vector>
|
||||
//You may add "#include" statements here
|
||||
|
||||
using namespace std; //Do not modify this line
|
||||
|
||||
class Circle //Do not modify this line
|
||||
{ //Do not modify this line
|
||||
//You may insert visibility modifiers anywhere within this document
|
||||
//You may declare member variables anywhere within this document
|
||||
//-----------------------------------------------------------------
|
||||
public:
|
||||
Circle(); //Do not modify this line
|
||||
~Circle(){} //Do not modify this line
|
||||
vector<string> names;
|
||||
//-----------------------------------------------------------------
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
vector<string> getNames(); //Do not modify this line
|
||||
//You may not implement this method here; you must do so in a .cpp document
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
vector<string> playGame(int n, int m); //Do not modify this line
|
||||
//You may not implement this method here; you must do so in a .cpp document
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
int reportSafeIndex(int n, int m); //Do not modify this line
|
||||
//You may not implement this method here; you must do so in a .cpp document
|
||||
//-----------------------------------------------------------------
|
||||
}; //Do not modify this line
|
||||
#endif //Do not modify this line
|
||||
@@ -0,0 +1,16 @@
|
||||
CXX=g++
|
||||
CPPFLAGS=-Wall -g -std=c++0x
|
||||
|
||||
all: Circle
|
||||
|
||||
Circle: main.o Circle.o
|
||||
g++ $(CPPFLAGS) Circle.o main.o -o Circle
|
||||
|
||||
main.o: main.cpp Circle.o Circle.h
|
||||
Circle.o: Circle.cpp Circle.h
|
||||
|
||||
clean:
|
||||
rm -fv Circle *.o
|
||||
|
||||
debug: Circle
|
||||
gdb Circle
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "Circle.h"
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
Circle c;
|
||||
c.playGame(10,2);
|
||||
c.playGame(9,2);
|
||||
c.playGame(21,2);
|
||||
c.playGame(10,-2);
|
||||
c.reportSafeIndex(10,2);
|
||||
c.reportSafeIndex(9,11);
|
||||
c.reportSafeIndex(21,6);
|
||||
c.reportSafeIndex(20,6);
|
||||
c.reportSafeIndex(20,-1);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
vector<string> names;
|
||||
names.push_back("derek");
|
||||
names.push_back("colleen");
|
||||
names.push_back("stpehen");
|
||||
names.pop_back();
|
||||
for(int i = 0; i < names.size(); i++) {
|
||||
cout << names[i] << endl;
|
||||
}
|
||||
cout << names.size() << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user