Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <vector>
  8. #include <string>
  9. #include <algorithm>
  10. using namespace std;
  11.  
  12.    struct Data {
  13.     string number;
  14.     string theme;
  15.     string name;
  16.     string minifigs;
  17.     string pieces;
  18.     string price;
  19.  
  20.     Data(string number_, string theme_, string name_, string mini_, string pieces_, string price_) {
  21.  
  22.         number_ = number;
  23.         theme_ = theme;
  24.         name_ = name;
  25.         mini_ = minifigs;
  26.         pieces_ = pieces;
  27.         price_ = price;
  28.     }
  29.    };
  30.  
  31.    
  32.    ifstream legoFolder;
  33.    vector<Data> fileData;
  34.    int option;
  35.  
  36.    
  37.    
  38.  
  39.  
  40. string readDataFileChoice(int option) {
  41.  
  42.     switch (option) {
  43.        case 1:
  44.           return "lego1.csv";
  45.           break;
  46.        case 2:
  47.           return "lego2.csv";
  48.           break;
  49.        case 3:
  50.           return "lego3.csv";
  51.           break;
  52.        default:
  53.           cout << "Invalid Response" << endl;
  54.     }
  55.     return "";
  56. }
  57. string chosenFile = readDataFileChoice(option);
  58.  
  59. void readFileData(int option, string chosenFile, vector<Data>& fileData) {
  60.     if (option < 4) {
  61.         legoFolder.open(chosenFile);
  62.         string lineFromFile;
  63.         istringstream stream(lineFromFile);
  64.          string number;
  65.         string theme;
  66.         string name;
  67.         string minifigs;
  68.         string pieces;
  69.         string price;
  70.     int numOfSets = 0;
  71.     while (getline(legoFolder, lineFromFile)) {
  72.          numOfSets++;
  73.         getline(stream, number, ',');
  74.         getline(stream, theme, ',');
  75.         getline(stream, name, ',');
  76.         getline(stream, minifigs, ',');
  77.         getline(stream, pieces, ',');
  78.         getline(stream, price);
  79.  
  80.         Data data(number, theme, name, minifigs, pieces, price);
  81.         fileData.push_back(data);
  82.     }
  83.    
  84.     cout << "Total number of sets: " << numOfSets << endl;
  85.     }
  86. }
  87.  
  88.  
  89.  
  90. void mostExp(vector<Data>& fileData) {
  91.  
  92.     vector<int> mostExpPrice;
  93.     for (unsigned int i = 0; i < fileData.size(); i++) {
  94.         cout << i << ": " << fileData[i].price << endl;
  95.         double convertToD = stod(fileData[i].price);
  96.         mostExpPrice.push_back(convertToD);
  97.     }
  98.     double max = mostExpPrice[0];
  99.     for (unsigned int i = 0; i < mostExpPrice.size(); i++) {
  100.         if (max < mostExpPrice[i]) {
  101.             max = mostExpPrice[i];
  102.         }
  103.    
  104.     }
  105.     for (unsigned int i = 0; i < mostExpPrice.size(); i++) {
  106.         if (max == mostExpPrice[i]){
  107.         cout << "The most expensive set is:" << endl;
  108.         cout << "Name: " << fileData[i].name << endl;
  109.         cout << "Number: " << fileData[i].number << endl;            
  110.         cout << "Theme: " << fileData[i].theme << endl;
  111.         cout << "Price: $" << fileData[i].price << endl;
  112.         cout << "Minifigures: " << fileData[i].minifigs << endl;
  113.         cout << "Piece count: " << fileData[i].pieces << endl;
  114.  
  115.         }
  116.  
  117.     }
  118.    }
  119.  
  120.  
  121. void pieceCount(string chosenFile, vector<Data>& fileData) {
  122.    if (chosenFile != "All Files") {
  123.     vector<int> largestPieceSet;
  124.     for (unsigned int i = 1; i < fileData.size(); i++) {
  125.         int convertToInt = stoi(fileData[i].pieces);
  126.         largestPieceSet.push_back(convertToInt);
  127.     }
  128.     int highest = largestPieceSet[1];
  129.     for (unsigned int i = 1; i < largestPieceSet.size(); i++) {
  130.         if (highest < largestPieceSet[i]) {
  131.             highest = largestPieceSet[i];
  132.         }
  133.    
  134.     }
  135.     for (unsigned int i = 1; i < largestPieceSet.size(); i++) {
  136.         if (highest == largestPieceSet[i]){
  137.         cout << "The set with the highest parts count:" << endl;
  138.         cout << "Name: " << fileData[i].name << endl;
  139.         cout << "Number: " << fileData[i].number << endl;            
  140.         cout << "Theme: " << fileData[i].theme << endl;
  141.         cout << "Price: $" << fileData[i].price << endl;
  142.         cout << "Minifigures: " << fileData[i].minifigs << endl;
  143.         cout << "Piece count: " << fileData[i].pieces << endl;
  144.  
  145.         }
  146.  
  147.     }
  148.    }
  149. }
  150.  
  151. void searchName(string chosenFile, vector<Data>& fileData) {
  152.     if (chosenFile != "All files") {
  153.     vector<string> matchingList;
  154.     cout << "Enter a name to find:" << endl;
  155.     string nameChoice;
  156.     cin >> nameChoice;
  157.     for (unsigned int i = 0; i < fileData.size(); i++) {
  158.         if (fileData[i].name.find(nameChoice)) {
  159.             matchingList.push_back(fileData[i].number);
  160.             matchingList.push_back(fileData[i].name);
  161.             matchingList.push_back(fileData[i].price);
  162.         }
  163.     }
  164.     for (unsigned int i = 0; i < matchingList.size(); i++) {
  165.         cout << matchingList[i] << " ";
  166.         if(i%3 == 0) {
  167.            cout << endl;
  168.         }
  169.  
  170.     }
  171.     if (matchingList.empty()) {
  172.         cout << "No sets found matching that search term" << endl;
  173.     }
  174.    }
  175.  
  176. }
  177.  
  178. void searchTheme(string chosenFile, vector<Data>& fileData) {
  179.     if (chosenFile != "All Files") {
  180.     vector<string> matchingList;
  181.     cout << "Enter a theme to find:" << endl;
  182.     string themeChoice;
  183.     cin >> themeChoice;
  184.     for (unsigned int i = 0; i < fileData.size(); i++) {
  185.         if (fileData[i].theme.find(themeChoice)) {
  186.             matchingList.push_back(fileData[i].number);
  187.             matchingList.push_back(fileData[i].name);
  188.             matchingList.push_back(fileData[i].price);
  189.         }
  190.     }
  191.     for (unsigned int i = 0; i < matchingList.size(); i++) {
  192.         cout << matchingList[i] << " ";
  193.         if (i%3 == 0) {
  194.            cout << endl;
  195.  
  196.     }
  197.     if (matchingList.empty()) {
  198.         cout << "No sets found matching that search term" << endl;
  199.     }
  200.     }
  201.    
  202. }
  203. }
  204.  
  205. void avgCount(string chosenFile, vector<Data>& fileData) {
  206.     if (chosenFile != "All Files") {
  207.     double sum = 0;
  208.     vector<int> countedPieces;
  209.     for (unsigned int i = 0; i < fileData.size(); i++) {
  210.         int oneSet = stoi(fileData[i].pieces);
  211.         countedPieces.push_back(oneSet);
  212.     }
  213.     for (int c:countedPieces) {
  214.         sum = sum + c;
  215.     }
  216.     double average = (sum/countedPieces.size());
  217.     cout << "Average part count for " << countedPieces.size() << " sets: " << average << endl;
  218.     }
  219.  
  220. }
  221.  
  222. void avgPrice(string chosenFile, vector<Data>& fileData) {
  223.    if (chosenFile != "All Files") {
  224.     double sum = 0;
  225.     vector<int> countedPrices;
  226.     for (unsigned int i = 0; i < fileData.size(); i++) {
  227.         int oneSet = stoi(fileData[i].price);
  228.         countedPrices.push_back(oneSet);
  229.     }
  230.     for (int c:countedPrices) {
  231.         sum = sum + c;
  232.     }
  233.     double average = (sum/countedPrices.size());
  234.     cout << "Average price information for " << countedPrices.size() << " sets: $" << average << endl;
  235.     int min = countedPrices[0];
  236.     for (unsigned int i = 0; i < countedPrices.size(); i++) {
  237.         if (min > countedPrices[i]) {
  238.             min = countedPrices[i];
  239.         }
  240.     }
  241.     for (unsigned int i = 0; i < countedPrices.size(); i++) {
  242.         if (min == countedPrices[i]) {
  243.             cout << "Set with the minimum price:" << endl;
  244.             cout << "Name: " << fileData[i].name << endl;
  245.             cout << "Number: " << fileData[i].number << endl;
  246.             cout << "Theme: " << fileData[i].theme << endl;
  247.             cout << "Price: $" << fileData[i].price << endl;
  248.             cout << "Minifigures: " << fileData[i].minifigs << endl;
  249.             cout << "Piece count: " << fileData[i].pieces << endl;
  250.         }
  251.     }
  252.     int max = countedPrices[0];
  253.     for (unsigned int i = 0; i < countedPrices.size(); i++) {
  254.         if (max < countedPrices[i]) {
  255.             max = countedPrices[i];
  256.         }
  257.     }
  258.     for (unsigned int i = 0; i < countedPrices.size(); i++) {
  259.         if (max == countedPrices[i]) {
  260.             cout << "Set with the maximum price:" << endl;
  261.             cout << "Name: " << fileData[i].name << endl;
  262.             cout << "Number: " << fileData[i].number << endl;
  263.             cout << "Theme: " << fileData[i].theme << endl;
  264.             cout << "Price: $" << fileData[i].price << endl;
  265.             cout << "Minifigures: " << fileData[i].minifigs << endl;
  266.             cout << "Piece Count: " << fileData[i].pieces << endl;
  267.         }
  268.     }
  269.    }
  270.    
  271.    
  272. }
  273.  
  274. void printMinifigData(string chosenFile, vector<Data>& fileData) {
  275.    if (chosenFile != "All Files") {
  276.     double sum = 0;
  277.     vector<int> countedMinifigs;
  278.     for (unsigned int i = 0; i < fileData.size(); i++) {
  279.         int oneSet = stoi(fileData[i].minifigs);
  280.         countedMinifigs.push_back(oneSet);
  281.     }
  282.     for (int c:countedMinifigs) {
  283.         sum = sum + c;
  284.     }
  285.     double average = (sum/countedMinifigs.size());
  286.     cout << "Average number of minifigurines: " << average << endl;
  287.     int max = countedMinifigs[0];
  288.     for (unsigned int i = 0; i < countedMinifigs.size(); i++) {
  289.         if (max < countedMinifigs[i]) {
  290.             max = countedMinifigs[i];
  291.         }
  292.     }
  293.     for (unsigned int i = 0; i < countedMinifigs.size(); i++) {
  294.         if (max == countedMinifigs[i]) {
  295.             cout << "Set with the most minifigurines:" << endl;
  296.             cout << "Name: " << fileData[i].name << endl;
  297.             cout << "Number: " << fileData[i].number << endl;
  298.             cout << "Theme: " << fileData[i].theme << endl;
  299.             cout << "Price: $" << fileData[i].price << endl;
  300.             cout << "Minifigures: " << fileData[i].minifigs << endl;
  301.             cout << "Piece Count: " << fileData[i].pieces << endl;
  302.         }
  303.     }
  304.    }
  305.    
  306. }
  307.  
  308. void ifYouBought(vector<Data>& fileData) {
  309.     int sum = 0;
  310.     vector<int> countedPrices;
  311.     for (unsigned int i = 0; i < fileData.size(); i++) {
  312.         int oneSet = stoi(fileData[i].price);
  313.         countedPrices.push_back(oneSet);
  314.     }
  315.     for (int c:countedPrices) {
  316.         sum = sum + c;
  317.     }
  318.     cout << "It would cost: $" << sum << endl;
  319.     int pieceSum = 0;
  320.     vector<int> countedPieces;
  321.     for (unsigned int i = 0; i < fileData.size(); i++) {
  322.         int setPieces = stoi(fileData[i].pieces);
  323.         countedPieces.push_back(setPieces);
  324.     }
  325.     for (int p:countedPieces) {
  326.         pieceSum = pieceSum + p;
  327.     }
  328.     cout << "You would have " << pieceSum << " pieces in your collection" << endl;
  329.     int minifigSum = 0;
  330.     vector<int> countedMinifigs;
  331.     for (unsigned int i = 0; i < fileData.size(); i++) {
  332.         int setMinifig = stoi(fileData[i].minifigs);
  333.         countedMinifigs.push_back(setMinifig);
  334.     }
  335.     for (int m:countedMinifigs) {
  336.         minifigSum = minifigSum + m;
  337.     }
  338.     cout << "You would have an army of " << minifigSum << " mini_figurines!" << endl;
  339.    
  340. }
  341.  
  342. void taskChoice(int choice) {
  343.     if (choice == 1) {
  344.         mostExp(fileData);
  345.     }
  346.     else if (choice == 2) {
  347.         pieceCount(chosenFile, fileData);
  348.     }
  349.     else if (choice == 3) {
  350.         searchName(chosenFile, fileData);
  351.     }
  352.     else if (choice == 4) {
  353.         searchTheme(chosenFile, fileData);
  354.     }
  355.     else if (choice == 5) {
  356.         avgCount(chosenFile, fileData);
  357.     }
  358.     else if (choice == 6) {
  359.         avgPrice(chosenFile, fileData);
  360.     }
  361.     else if (choice == 7) {
  362.         printMinifigData(chosenFile, fileData);
  363.     }
  364.     else if (choice == 8) {
  365.         ifYouBought(fileData);
  366.     }
  367.     else {
  368.         exit(0);
  369.     }
  370. }
  371.  
  372. int main()
  373. {
  374.     cout << std::fixed << setprecision(2);
  375.     cout << "Which file(s) to open?\n";
  376.     cout << "1. lego1.csv" << endl;
  377.     cout << "2. lego2.csv" << endl;
  378.     cout << "3. lego3.csv" << endl;
  379.     cout << "4. All 3 files" << endl;
  380.     cin >> option;
  381.  
  382.  
  383.    if (option < 4) {
  384.    readFileData(option, readDataFileChoice(option), fileData);
  385.    
  386.    }
  387.  
  388.    
  389.    
  390.  
  391.  
  392.    
  393.    
  394.    cout << "1. Most Expensive set" << endl;
  395.     cout << "2. Largest piece count" << endl;
  396.     cout << "3. Search for set name containing..." << endl;
  397.     cout << "4. Search themes..." << endl;
  398.     cout << "5. Part count information" << endl;
  399.     cout << "6. Price information" << endl;
  400.     cout << "7. Minifigure information" << endl;
  401.     cout << "8. If you bought one of everything..." << endl;
  402.     cout << "0. Exit" << endl;
  403.    
  404.     int choice;
  405.     cin >> choice;
  406.    cin.get();  // Clear newline character for any later input
  407.    
  408.    taskChoice(choice);
  409.  
  410.      
  411.    
  412.    
  413.      
  414.     return 0;
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement