diff options
Diffstat (limited to 'C++/Midterm')
-rw-r--r-- | C++/Midterm/Instructions.txt | 45 | ||||
-rw-r--r-- | C++/Midterm/Midterm.cpp | 280 | ||||
-rw-r--r-- | C++/Midterm/input.txt | 93 |
3 files changed, 418 insertions, 0 deletions
diff --git a/C++/Midterm/Instructions.txt b/C++/Midterm/Instructions.txt new file mode 100644 index 0000000..6939ed5 --- /dev/null +++ b/C++/Midterm/Instructions.txt @@ -0,0 +1,45 @@ + For your midterm project, please write a program to do the following: + + Use a struct to: + Create an array or vector variable to store a person’s favorite movie + Create an array or vector variable to store when the year that movie was released + Create an array or vector variable to store the box office gross (total amount of money) it made + Create an input file to store all of the above information. Please use at least 3 sets of data to make the program interesting + Check that the input file was found + Use a loop of your choice to read in an unknown number of records from your input file + When the program reads in a movie name, check to see if it has any <HTML> tags inside of it + Once the movie name is free of tags, store it and the other variables in your arrays or vectors + Use a simple menu where the user can sort the data on the movie name, year released or box office gross + Output the sorted movie list onto the screen + Close your input file + +Assignment Notes: + + You are free to use arrays or vectors. Or, you can mix/match. It is up to you + I expect to functions for steps: 3 - 7 + I expect to see generic functions for step 6 (sort function) and 7 (output function) + I will be closely watching for cheating (copying from friends, online help, etc.) and I will give 0/300 for cheating + There is no output file in this assignment, but if you want to use one, you are free to do so + You are free to make all variables strings for simplicity in your sort function + You are free to place any/all of your functions inside of your struct. This is not required though. You may do this or not. Your choice + For the box office gross, you can abbrev. the number to make it more manageable. Example: 100, 110, 250, etc. + +General Notes: + + Be sure to use comments in your program: Name, Program Description, Date and anywhere else in the program you deem necessary. + Although this is a midterm, if you are stuck, I will still help you! But, not as many answers this time. Nothing here is new, so you can review the keys from your assignments to see how everything works. + +Grading Rubric: + + If you do not include comments at the top of the program (name, program description, date), you will lose 15 points + If you do not use a struct, you will lose 25 points + If you do not use functions, you will use 20 points per missing function (80 points) + If you do not use generic functions for steps 6 and 7, you will lose 50 points (25 points each) + If your program does not compile (run), then I will give a grade of 0/300. But will give you the change to repair for points back (some points are better than none) + If you use global variables, you will lose 10 points for each global variable used + If your program is not formatted nicely (code all over the place, ugly), you will lose up to 50 points depending on the extent + If your program stops working when I run it, you will lose points. The exact amount depends on the severity of the error + If your program still has your friend's name on it, I will send you a message asking you to try harder while giving you a 0/100 + If your program looks like a a professional programmer wrote it, I will write to you to ask if you want a job. Well, maybe not. But, I will ask about the code + This is just a list of typical issues I run into when grading to give you some idea of where your points go. Points can be taken off for other reasons. + diff --git a/C++/Midterm/Midterm.cpp b/C++/Midterm/Midterm.cpp new file mode 100644 index 0000000..e79f58e --- /dev/null +++ b/C++/Midterm/Midterm.cpp @@ -0,0 +1,280 @@ +// Name: msglm +// Date: Oct 3rd, 2022 +// Program Name: Midterm +// Description: Read in a file, sanitize said file of html tags, output the contents in a format sorted that the user chooses. + +//90% of this is self-plagiarism, or as I like to call it, recycled +//and think, some people get really angry about reuse! +//I never understood that mentality, it's just absurd to me: +//just never transfer your copyrights and also put your code +//under a free software license and this won't be a problem. + +#include <iostream> +#include <fstream> +#include <string> +#include <vector> +#include <array> +#include <iomanip> +using namespace std; + +//Structures +// Use a struct to: +// Create an array or vector variable to store a person’s favorite movie +// Create an array or vector variable to store when the year that movie was released +// Create an array or vector variable to store the box office gross (total amount of money) it made +// +struct Person { + vector<string> favMov; + vector<string> yearReleased; + vector<string> gross; +}; + +struct Movie { + string favMov; + string yearReleased; + string gross; +}; + + +//initalizes the file + +//with the amount of times I used this function, I have to wonder +//why I don't end up making my own standard library +//or code processorsor to catconate some standard stuff +void initalize(string location, ifstream & fileVar) { + fileVar.open(location); + + if (!fileVar) { + cout << "ERROR! " << location << " failed to open!"; + exit(1); + } +} + + +//borrowed code from another assignment +// +//it's great that I made this not have any side effects +//I can just slap this down anywhere I please +string sanitize(string userInput) { + bool insideATag = false; + bool metTheTagTerminator = false; + int beginBracketPos; + int endBracketPos; + int tagLength; + + for (int charPos=0; charPos<=userInput.length();charPos++) { + if (insideATag && metTheTagTerminator) { + + //Tag length only exists because std::String.erase() doesn't accept + //a range, but rather a start and the chars to go. it's accursed. + tagLength = (endBracketPos-beginBracketPos)+1; + userInput.erase(beginBracketPos, tagLength); + charPos = 0; + insideATag = false; + metTheTagTerminator = false; + } + if (userInput[charPos] == '<') { + insideATag = true; + beginBracketPos = charPos; + } + + if (userInput[charPos] == '>' && insideATag) { + metTheTagTerminator = true; + endBracketPos = charPos; + } + + + } + return userInput; +} + + +//takes a file as input and, for every 3 lines, puts it into the movieDataContainer's vectors +//Will only run if the next line after a set of 3 is not EOF +//there is no error checking +Person orderAndAssociateIntoStruct(ifstream & fileVar) { + Person movieDataContainer; + string temp; + int oscillate = 0; + int epoch = 0; + + while(getline(fileVar, temp)) { + switch (oscillate) { + case 0: + movieDataContainer.favMov.push_back(sanitize(temp)); + oscillate++; + break; + case 1: + movieDataContainer.yearReleased.push_back(sanitize(temp)); + oscillate++; + break; + case 2: + movieDataContainer.gross.push_back(sanitize(temp)); + epoch++; + oscillate = 0; + break; + if (fileVar.peek() != EOF) { + break; + } + } + } + return movieDataContainer; +} + + +//Prints out all the data in a tabular format +void output(vector<Movie> movieDataContainer) { + + cout << left << setw(16) << "Movie Name" << " "; + cout << left << setw(16) << "Year Published" << " "; + cout << left << setw(16) << "Gross Box Office" << " "; + + for (Movie movie : movieDataContainer) { + cout << endl; + cout << left << setw(16) << movie.favMov; + cout << left << setw(16) << movie.yearReleased; + cout << left << setw(16) << movie.gross; + } + cout << endl; + } + + +//Bubble sort implementations +// +//depending on the menu option selected, this will sort by different values +//The movieList is read for all the movie structs in it, those struct's values +//are what are read when sorting happens. Depending on if the sorting deemes the values +//greater or lesser, the swapping of the movie struct itself will happen +// +//It took many hours in gdb to discover that this function had a bug due to +//movieList.size() returning 5 instead of 4 if 4 movie structs are in movieList +// +//The experience of debugging C++ seg faults is only comparable to one thing: passing a kidney stone; +//It takes 3 hours of your life away, it's painful, and by the end of it, you'll be bleeding out of places +//you never thought you could bleed out of. +// +//The only upside to debugging C++ code is that it doesn't bill your insurance... +//well, it does end up billing it later on when the blood pressure problems start setting in, but right now that's not my problem. +vector<Movie> sort(vector<Movie> movieList, int menuOption) { + Movie temp; + switch(menuOption) { + case 1: + for (int pos = 0; pos < movieList.size() - 1; pos++) { + for (int comparePos = 0; comparePos < ((movieList.size() - 1) - pos); comparePos++) { + if(movieList[comparePos].favMov > movieList[comparePos + 1].favMov) { + temp = movieList[comparePos]; + movieList[comparePos] = movieList[comparePos + 1]; + movieList[comparePos + 1] = temp; + } + } + } + break; + case 2: + for (int pos = 0; pos < movieList.size() - 1; pos++) { + for (int comparePos = 0; comparePos < ((movieList.size() - 1) - pos); comparePos++) { + if(stof(movieList[comparePos].yearReleased) > stof(movieList[comparePos + 1].yearReleased)) { + temp = movieList[comparePos]; + movieList[comparePos] = movieList[comparePos + 1]; + movieList[comparePos + 1] = temp; + } + } + } + break; + case 3: + for (int pos = 0; pos < movieList.size() - 1; pos++) { + for (int comparePos = 0; comparePos < ((movieList.size() - 1) - pos); comparePos++) { + if(stof(movieList[comparePos].gross) > stof(movieList[comparePos + 1].gross)) { + temp = movieList[comparePos]; + movieList[comparePos] = movieList[comparePos + 1]; + movieList[comparePos + 1] = temp; + } + } + } + break; + } + return movieList; +} + +//just a simple menu. returns a number because I don't feel like dealing with flags +int menuOption() { + int choice; + //No input validation here since the rubric doesn't call for it. + cout << "1. Sort by Movie Name\n"; + cout << "2. Sort by Year Released\n"; + cout << "3. Sort by Box office gross\n"; + cout << "Enter Selection: "; + cin >> choice; + return choice; +} + +//effectively inverts the person struct +//instead of being a struct with vectors in it, it's a vector with structs in it +//and those structs hold data about an individual movie +// +//This is achieved using a similar algorithm to reading in the file +vector<Movie> person2MovieList(Person person) { + + vector<Movie> personsMovieList; + int oscillate = 0; + string tempfavMov; + string tempyearReleased; + string tempGross; + + int moviePos = 0; + //Since gross and year release are associated with movie, we can assume + //that for every movie, there should be a gross and year release. + // + //If this was in production, some type of check would be ran to ensure this + while(moviePos < person.favMov.size()) { + switch (oscillate){ + case 0: + tempfavMov = person.favMov[moviePos]; + oscillate++; + break; + case 1: + tempyearReleased = person.yearReleased[moviePos]; + oscillate++; + break; + case 2: + tempGross = person.gross[moviePos]; + oscillate = 0; + personsMovieList.push_back( Movie() ); + personsMovieList[moviePos].favMov = tempfavMov; + personsMovieList[moviePos].yearReleased = tempyearReleased; + personsMovieList[moviePos].gross = tempGross; + moviePos++; + break; + } + } + return personsMovieList; + +} + +int main() { + //Variable declaration + ifstream data; + + initalize("input.txt", data); + Person organizedMovieData = orderAndAssociateIntoStruct(data); + //This current data structure makes the sorting section a nightmare. + //this is due to the lack of a hard associations between the movie's name, year of release, and gross profit + // + //I somehow must associate those three variables despite them being in an array + //Instead of using an accursed flag system to accomplish this, I will instead opt to + //just convert the data in the Person struct into a vector of Movie Structs + //this way, the order of the struct may be managed much more simply + + vector<Movie> personsMovieList = person2MovieList(organizedMovieData); + output(sort(personsMovieList, menuOption())); + + data.close(); + +} + +/* + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. + */ diff --git a/C++/Midterm/input.txt b/C++/Midterm/input.txt new file mode 100644 index 0000000..3d5fe35 --- /dev/null +++ b/C++/Midterm/input.txt @@ -0,0 +1,93 @@ +A Bad Movie +2009 +1000 +A Mediocre Movie +2013 +2000 +A Good Movie +1963 +3000 +<html><p>A Great Movie</p></html> +<html><p>1933</p></html> +<html><p>4000</p></html> +<html><p>The Best Movie</p></html> +<html><p>2019</p></html> +<html><p>5000</p></html> +<html><p>8daf42144e1478242395</p></html> +<html><p>5277</p></html> +<html><p>17323</p></html> +<html><p>992db78074b6626d32d4</p></html> +<html><p>28144</p></html> +<html><p>18078</p></html> +<html><p>eff5f2d89c76c7db845d</p></html> +<html><p>2097</p></html> +<html><p>4657</p></html> +<html><p>5b2ab0ef7c7679608ad4</p></html> +<html><p>21667</p></html> +<html><p>22384</p></html> +<html><p>af130ea34c5fd18e5d90</p></html> +12932 +16067 +53781f8b44d59f0a785b +27052 +15023 +4fb5031d3f412c8eaa00 +5108 +19262 +e5ef1bffca4fc724bc1a +22484 +28116 +f176f40d7d95a68c505b +32107 +11837 +026b0f5819e4f3b1f896 +19896 +8728 +a0cb012372c799465445 +15028 +11357 +de61b87cde83b8a34bb0 +<p>32308</p> +22373 +5c0bf606be264bdf0c0f +22526 +30093 +0ba47d1c54af6d4f0cb6 +19016 +30532 +f7210a737c1e1ffc7c6b +25482 +22580 +8d2bdb0dc90b60deff84 +10442 +30759 +e99b69172334e6898221 +7135 +5818 +facdeb2cacd8ba502e6d +16070 +9232 +4568ec6386779c896ae2 +10475 +4969 +d4ca7742c45d8600775d +31616 +23407 +a604d311130b7bdb5d44 +21036 +25900 +b3c333742475448abd99 +5663 +26144 +ce41c8b616c312ba742c +12394 +28147 +916bcad86d3071937283 +21493 +11734 +6fafd30cb753057940bd +7217 +8621 +7c13bda784439ee8e995 +20462 +22245 |