adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
CPPFLAGS=-Wall -g -std=c++0x
all: magic test00 test01
magic: magic.cc num_set.o converts.o
test00: test00.cc num_set.o converts.o
test01: test01.cc num_set.o converts.o
num_set.o: num_set.cc num_set.h
converts.o: converts.cc converts.h
clean:
@rm -rfv test00 test00.dSYM
@rm -rfv test01 test01.dSYM
@rm -rvf magic magic.dSYM *.o
+17
View File
@@ -0,0 +1,17 @@
#include <iostream>
#include <cmath>
#include "converts.h"
int str2int(string s) {
int r = 0;
int i = 0;
for(auto s_it = s.rbegin(); s_it != s.rend(); s_it++) {
if (*s_it == '1') {
r += pow(2, i);
}
i++;
}
return r;
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef __CONVERTS_H__
#define __CONVERTS_H__
#include <string>
using namespace std;
int str2int(string i);
#endif
+45
View File
@@ -0,0 +1,45 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include "num_set.h"
using namespace std;
const string usage = "usage: magic ";
int main(int argc, char * argv[]) {
string play_again;
srand(time(NULL));
while (true) {
string cur_guess_str;
for(int i=0; i < 5; i++) {
int compliment = rand() % 2;
vector<string> nums = generate_numbers(i, compliment);
cout << nums << endl << endl;
string cur_guess;
cout << "is your number in there: ";
cin >> cur_guess;
if(cur_guess[0] == 'y' or cur_guess[0] == 'Y') {
if(compliment == 0)
cur_guess_str.push_back('0');
else
cur_guess_str.push_back('1');
} else {
if(compliment == 0)
cur_guess_str.push_back('1');
else
cur_guess_str.push_back('0');
}
}
reverse(cur_guess_str.begin(), cur_guess_str.end());
cout << cur_guess_str << ", " << str2int(cur_guess_str) << endl;
cout << "play again? ";
cin >> play_again;
if(!(play_again[0] == 'y' or play_again[0] == 'Y'))
break;
}
cout << "good bye" << endl;
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
#include <iostream>
#include <sstream>
#include <iomanip>
#include "num_set.h"
vector<string> zerone = {"0", "1"};
vector<string> generate_numbers(int constant_index, int compliment) {
vector<string> r;
stringstream out;
out << compliment;
auto compliment_str = out.str();
for(int i=0; i <= 1; i++) {
for(int j=0; j <= 1; j++) {
for(int k=0; k <= 1; k++) {
for(int l=0; l <= 1; l++) {
ostringstream cur_str;
cur_str << i << j << k << l;
string s = cur_str.str();
s.insert(s.size() - constant_index, compliment_str);
r.push_back(s);
}
}
}
}
return r;
}
ostream & operator<<(ostream & os, const vector<string> & nums) {
for (unsigned int i = 0; i < nums.size(); i++) {
os << setw(4) << str2int(nums[i]);
if(i % 4 == 3 and i != nums.size() - 1) {
os << endl;
}
}
return os;
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef __NUM_SET_H__
#define __NUM_SET_H__
#include <vector>
#include <string>
#include <ostream>
#include "converts.h"
using namespace std;
vector<string> generate_numbers(int, int);
ostream & operator<<(ostream &, const vector<string> &);
#endif
+29
View File
@@ -0,0 +1,29 @@
#include <iostream>
#include <sstream>
#include <vector>
#include "num_set.h"
using namespace std;
const string usage = "usage: test <index> <0/1>";
int main(int argc, char * argv[]) {
if (argc < 3) {
cerr << usage << endl;
return 1;
}
int index, compliment;
istringstream iss0(argv[1]);
iss0 >> index;
index = index % 5;
istringstream iss1(argv[2]);
iss1 >> compliment;
compliment = compliment % 2;
vector<string> nums = generate_numbers(index, compliment);
for(string i: nums)
cout << i << endl;
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <vector>
#include "num_set.h"
#include "converts.h"
using namespace std;
const string usage = "usage: test <index> <0/1>";
int main() {
vector<string> nums = generate_numbers(0, 0);
cout << "should print evens:" << endl;
for(string n: nums) {
int i = str2int(n);
cout << ">>> " << n << ", " << i << endl;
}
return 0;
}