diff options
author | RossTheRoss <msattr@gmail.com> | 2020-02-09 08:46:52 -0600 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2020-02-09 08:46:52 -0600 |
commit | bd1d350c376b45e334697851e29b2f79fee0c956 (patch) | |
tree | eb242032a64442caef7a35434400eb0818911e9a /OLD/ee1301/wk6/hw6_directory | |
parent | Do micro prelab (diff) | |
parent | Rename file (diff) | |
download | homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.gz homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.bz2 homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.lz homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.xz homework-bd1d350c376b45e334697851e29b2f79fee0c956.tar.zst homework-bd1d350c376b45e334697851e29b2f79fee0c956.zip |
Merge branch 'master' of github.com:RosstheRoss/TestingFun
Diffstat (limited to 'OLD/ee1301/wk6/hw6_directory')
-rw-r--r-- | OLD/ee1301/wk6/hw6_directory/strap012_HW6A.cpp | 215 | ||||
-rw-r--r-- | OLD/ee1301/wk6/hw6_directory/strap012_HW6B.cpp | 35 |
2 files changed, 250 insertions, 0 deletions
diff --git a/OLD/ee1301/wk6/hw6_directory/strap012_HW6A.cpp b/OLD/ee1301/wk6/hw6_directory/strap012_HW6A.cpp new file mode 100644 index 0000000..ac2b7d0 --- /dev/null +++ b/OLD/ee1301/wk6/hw6_directory/strap012_HW6A.cpp @@ -0,0 +1,215 @@ +//Matthew Strapp
+//EE1301
+//17 April 2019
+//HW 6A: Dice Class
+#include <iostream>
+#include <cstdlib>
+using namespace std;
+
+const int maxNumDie=50;
+
+int* userInputParser(string s);
+
+class Dice {
+private:
+ int min;
+ int max;
+public:
+ int roll() {
+ return rand() % (max-min+1) + min;
+ };
+ Dice() { //Default constructor for debugging purposes
+ min=1;
+ max=1;
+ }
+ Dice(int gotMin, int gotMax){
+ min = gotMin;
+ max = gotMax;
+ };
+};
+
+int main() {
+ int rounds;
+ Dice die;
+ int roll[maxNumDie];
+ int max=0, min=999999, sum=0, sample=0;
+ double avg;
+ srand(time(NULL)); // DO NOT WRITE THIS LINE AGAIN OR ANYWHERE ELSE
+ cout << "What do you want to roll? ";
+ string s;
+ getline(cin, s);
+ cout << "How many rounds do you want to roll? ";
+ cin >> rounds;
+ int* pairs = userInputParser(s);
+
+ // This will display the array of die information retrieved from user.
+ // Replace the following code when you submit your solution.
+
+ // pairs is an array with the following format:
+ // {num_dice,
+ // first_die_start,
+ // first_die_end,
+ // second_die_start,
+ // ... }
+ double numRolls=0;
+ for (int j=0; j<rounds; j++) {
+ int curRoll=0;
+ numRolls+=1;
+ for(int i=1; i < pairs[0]; i+=2) {
+ die = Dice(pairs[i], pairs[i + 1]);
+ roll[i-1] = die.roll();
+ curRoll += roll[i-1];
+ }
+
+ if (curRoll > max) {
+ max = curRoll;
+ }
+ if (curRoll < min) {
+ min = curRoll;
+ }
+ sum+=curRoll;
+ sample=curRoll;
+ }
+ avg = sum / numRolls;
+ cout << "Sample roll: " << sample << endl
+ << "Minimum roll: " << min << endl
+ << "Maximum roll: " << max << endl
+ << "Average roll: " << avg << endl;
+
+}
+
+
+int* userInputParser(string s) {
+ static int dice[2*maxNumDie+1] = {0}; // array format:
+ // {num_dice,
+ // first_die_start,
+ // first_die_end,
+ // second_die_start,
+ // ... }
+ // max of maxNumDie dice supported
+
+ string data[4*maxNumDie]; // Intermediate storage for parsing input string
+
+ // count how many '+'s or 'd's the roll[i-1] = die[i-1].roll();re are...
+ int parts = 0;
+ for(unsigned int i=0; i < s.length(); i++)
+ {
+ if(s[i] == 'd' || s[i] == '+')
+ {
+ parts++;
+ }
+ }
+ // ... so we know the number of times to decode values
+
+ int index=0;
+ unsigned d = s.find('d');
+ unsigned p = s.find('+');
+ while(d != static_cast<unsigned>(-1) || p != static_cast<unsigned>(-1))
+ {
+ bool dFirst = d < p;
+ if(dFirst)
+ {
+ string before = s.substr(0,d); // part before the 'd' (should be just one number)
+ // figure out what number is after 'd'
+ int count = 0;
+ bool foundDigit=false;
+ for(int i=0; i< static_cast<signed>(s.length()-d-1); i++)
+ {
+ if(isdigit(s[count+d+1]))
+ {
+ foundDigit=true;
+ }
+ if(!isdigit(s[count+d+1]) && foundDigit)
+ {
+ break;
+ }
+ count++;
+ }
+ string after = s.substr(d+1,count); //should be just the number after 'd'
+
+ // store these two parts
+ data[index] = before;
+ data[index+1] = after;
+ index+=2;
+
+
+ // remove this part from the string s
+ s = s.substr(d+count+1); // discard these two parts
+ }
+ else // same idea for the '+'
+ {
+ // figure out what number is after '+'
+ int count = 0;
+ bool foundDigit=false;
+ for(int i=0; i< static_cast<signed>(s.length()-p-1); i++)
+ {
+ if(isdigit(s[count+p+1]))
+ {
+ foundDigit=true;
+ }
+ if(!isdigit(s[count+p+1]) && foundDigit)
+ {
+ break;
+ }
+ count++;
+ }
+ string after = s.substr(p+1,count); //should be just the number after '+'
+
+ // store this part
+ data[index] = "+";
+ data[index+1] = after;
+ index+=2;
+
+
+ // remove this part from the string s
+ s = s.substr(p+count+1); // discard these two parts
+ }
+
+ // update d and p for next loop interation
+ d = s.find('d');
+ p = s.find('+');
+
+ }
+
+ // now we need to figure out how many dice there are (as 2d4 is 2 dice)
+ // we will treat "+2" as a die that rolls [2,2]
+ int diceCount = 0;
+ for(int i=0; i < parts*2; i+=2)
+ {
+ if(data[i][0] == '+')
+ {
+ diceCount++;
+ }
+ else
+ {
+ diceCount+=atoi(data[i].c_str());
+ }
+ }
+
+ dice[0] = diceCount*2+1; // put size in first index
+
+ int ind=1; // index for the "dice" array (as not same as data array)
+ for(int i=0; i < parts*2; i+=2)
+ {
+ // if we have a +, add a "Dice" that has a range of 0
+ if(data[i][0] == '+')
+ {
+ dice[ind] = atoi(data[i+1].c_str());
+ dice[ind+1] = atoi(data[i+1].c_str());
+
+ ind+=2;
+ }
+ else // otherwise add however many of the dice requested
+ {
+ for(int j=0; j < atoi(data[i].c_str()); j++)
+ {
+ dice[ind] = 1;
+ dice[ind+1] = atoi(data[i+1].c_str());
+
+ ind += 2;
+ }
+ }
+ }
+
+ return dice;
+}
diff --git a/OLD/ee1301/wk6/hw6_directory/strap012_HW6B.cpp b/OLD/ee1301/wk6/hw6_directory/strap012_HW6B.cpp new file mode 100644 index 0000000..17a37fd --- /dev/null +++ b/OLD/ee1301/wk6/hw6_directory/strap012_HW6B.cpp @@ -0,0 +1,35 @@ +//Matthew Strapp
+//EE1301
+//17 April 2019
+//HW 6B: Recursion and ff(x)
+#include <iostream>
+
+int ff(int x);
+int main() {
+ int x, y;
+ do {
+ std::cout << "Please enter a value of x: ";
+ std::cin >> x;
+ } while (x<0);
+ std::cout << "Beginning calculation of ff(x)...\n";
+ y = ff(x);
+ std::cout << "Calcuation complete, ff(x) = " << y << std::endl;
+}
+
+// This function either returns 1 when x is one of two recrusive conditions depending on if x is even or odd.
+int ff(int x) {
+ if (x > 1) {
+ if (x%2 == 0) {
+ // x is even
+ std::cout << "Calling ff(" << x/2 << ")\n";
+ return x*ff(x/2);
+ } else {
+ // x is odd but not 1
+ std::cout << "Calling ff(" << x-2 << ")\n";
+ return x*ff(x-2);
+ }
+ } else {
+ // x is 1 (or 0)
+ return 1;
+ }
+}
|