Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.58 KB | None | 0 0
  1. #include <iostream>
  2.     #include <iomanip>
  3.     #include <cmath>
  4.     #include <fstream>
  5.     #include <string>
  6.     #include <cstdlib>
  7.     #include <sstream>
  8.     #include <vector>
  9.     #include <stdexcept>
  10.  
  11. using namespace std;
  12. int main(int argc, char** argv)
  13. {
  14.     ifstream inFile;
  15.     ofstream outFile;
  16.     char inputName[1024] = { '\0' };
  17.     char outputName[1024] = { '\0' };
  18.     unsigned int nline = 0;
  19.     int count = 0;
  20.     int tries = 0;
  21.     int firstint = 0;
  22.     int weight = 0;
  23.     int diameter = 0;
  24.     int sensor_num = 0;
  25.     string bentorusable;
  26.     string imagefaces;
  27.     int lines = 0;
  28.     string noBlank;
  29.     tries = 0;
  30.     string name;
  31.     string nextline;
  32.     string wholeline = "";
  33.  
  34.     //coins
  35.     int nickle = 0;
  36.     int dimes = 0;
  37.     int quarters = 0;
  38.     int loonies = 0;
  39.     int twonies = 0;
  40.  
  41.     int rolls_of_nickels = 0;
  42.     int rolls_of_dimes = 0;
  43.     int rolls_of_quarters = 0;
  44.     int rolls_of_loonies = 0;
  45.     int rolls_of_toonies = 0;
  46.  
  47.     int nickel_amount = 0;
  48.     int dimes_amount = 0;
  49.     int quarters_amount = 0;
  50.     int loonies_amount = 0;
  51.     int toonies_amount = 0;
  52.  
  53.     //nickel max and min weight and diameter
  54.     const double min_nickel_weight = 3.60;
  55.     const double max_nickel_weight = 4.30;
  56.     const double min_nickel_diameter = 20.2;
  57.     const double max_nickel_diameter = 20.8;
  58.     //dime max and min weight and diameter
  59.     const double min_dime_weight = 1.30;
  60.     const double max_dime_weight = 2.20;
  61.     const double min_dime_diameter = 17.3;
  62.     const double max_dime_diameter = 18.7;
  63.     //quarter max and min weight and diameter
  64.     const double min_quarter_weight = 4.00;
  65.     const double max_quarter_weight = 4.80;
  66.     const double min_quarter_diameter = 22.80;
  67.     const double max_quarter_diameter = 24.60;
  68.     //loonie max and min weight and diameter
  69.     const double min_loonie_weight = 6.50;
  70.     const double max_loonie_weight = 7.50;
  71.     const double min_loonie_diameter = 25.00;
  72.     const double max_loonie_diameter = 27.00;
  73.     //toonie max and min weight and diameter
  74.     const double min_toonie_weight = 6.75;
  75.     const double max_toonie_weight = 7.85;
  76.     const double min_toonie_diameter = 26.90;
  77.     const double max_toonie_diameter = 29.10;
  78.  
  79.     //coins per roll
  80.     const int nickels_per_roll = 40;
  81.     const int dimes_per_roll = 50;
  82.     const int quarters_per_roll = 40;
  83.     const int loonies_per_roll = 25;
  84.     const int toonies_per_roll = 25;
  85.     //other
  86.     double weight_coin = 0.0;
  87.     double diameter_coin = 0.0;
  88.     double other_coin_weight_total = 0.0;
  89.     double other_coin_num_total = 0.0;
  90.     int container_amount = 0.0;
  91.     double  max_othercontainter_weight = 0.0;
  92.     double weight_other_container = 0.0;
  93.     double bent_container_weight = 0.0;
  94.     double bent_coin_weight_total = 0.0;
  95.     const double max_bent_container_weight = 200.0;
  96.     int bent_container_amount = 0;
  97.     double other_amount = 0.0;
  98.     double other_weight = 0.0;
  99.     //Enter inputName of the file
  100.     do
  101.     {
  102.         cout << "Type the name of the input file containing sensor readings: " << endl;
  103.         cin >> inputName;
  104.         inFile.open(inputName);
  105.  
  106.         if (!inFile.is_open()) //Checks if the file is not open
  107.         {
  108.             cerr << "ERROR: File " << inputName << " could not be opened for input" << endl;
  109.             tries++;
  110.         }
  111.         if (tries >= 3)
  112.         {
  113.             cerr << "ERROR: You exceeded maximum number of tries allowed" << endl;
  114.             cerr << "while entering the input file name";
  115.             inFile.close();
  116.  
  117.         }
  118.     } while (!inFile.is_open() && tries < 3);
  119.  
  120.     tries = 0; //Resets tries to 0
  121.  
  122.     //Enter the outputName of the file
  123.     do
  124.     {
  125.         cout << "Type the name of the output file which will hold the simulation results" << endl;
  126.         cin >> outputName;
  127.         outFile.open(outputName);
  128.  
  129.         if (!outFile.is_open()) //Checks if the file is not open
  130.         {
  131.             cerr << "ERROR: File " << outputName << " could not be opened for output" << endl;
  132.             tries++;
  133.         }
  134.     } while (outFile.fail() && tries < 3);
  135.  
  136.     if (tries >= 3)
  137.     {
  138.         cerr << "ERROR: You exceeded maximum number of tries allowed" << endl;
  139.         cerr << "while entering the output file name" << endl;
  140.         return 1;
  141.     }
  142.  
  143.     if (!(inFile >> lines))
  144.     {
  145.         nline++;
  146.         if (inFile.eof())
  147.         {
  148.             cerr << "ERROR: Input data file is empty" << endl;
  149.             inFile.close();
  150.             outFile.close();
  151.             return 1;
  152.         }
  153.         else
  154.         {
  155.             cerr << "ERROR: First piece of data in the file is not an integer" << endl;
  156.             inFile.close();
  157.             outFile.close();
  158.             return 1;
  159.         }
  160.     }
  161.     if (lines <= 0 || lines > 5000)
  162.     {
  163.         cerr << "ERROR: The number of sensor readings is out of range" << endl;
  164.         inFile.close();
  165.         outFile.close();
  166.         return 1;
  167.  
  168.     }
  169.  
  170.     std::string input = " ";
  171.     std::string slot;
  172.  
  173.  
  174.     while (std::getline(inFile, input))
  175.     {
  176.         std::vector<std::string> vec;
  177.         std::stringstream ss(input);
  178.  
  179.         while (ss >> slot)
  180.         {
  181.             vec.push_back(slot);
  182.         }
  183.  
  184.         switch (vec.size())
  185.         {
  186.         case 0:
  187.         {
  188.             if (inFile.eof())
  189.             {
  190.                 cerr << "ERROR: No more data" << endl;
  191.                 cerr << "Simulation completed early before line " << nline << " of input" << endl;
  192.                 break;
  193.             }
  194.             else
  195.             {
  196.                 continue;
  197.             }
  198.         }
  199.         break;
  200.  
  201.         case 1:
  202.         {
  203.             int elem = stoi(vec.at(0));
  204.             try
  205.             {
  206.                 int elem = stoi(vec.at(0));
  207.             }
  208.  
  209.             catch (...)
  210.             {
  211.                 cerr << "ERROR: Weight sensor value read on line " << nline << " is not an integer" << endl;
  212.                 cerr << "Simulation terminated early : Please correct your data file" << endl;
  213.                 nline++;
  214.             }
  215.  
  216.             if (!(elem <= 255 && elem >= 0))
  217.             {
  218.                 cerr << "ERROR: Sensor reading out of range, ignoring line " << nline << " in the input file" << endl;
  219.             }
  220.             else
  221.             {
  222.                 cout << "ERROR: Weight sensor measurement only" << endl;
  223.                 cout << "ERROR: Ignore line " << nline << " of the input file" << endl;
  224.                 nline++;
  225.             }
  226.         } break;
  227.  
  228.         case 2:
  229.         {
  230.             int second_elem = std::stoi(vec.at(1));
  231.             try
  232.             {
  233.                 int second_elem = std::stoi(vec.at(1));
  234.             }
  235.             catch (...)
  236.             {
  237.                 cerr << "ERROR: Diameter sensor value read on line " << nline << " is not an integer" << endl;
  238.                 cerr << "Simulation terminated early : Please correct your data file" << endl;
  239.                 nline++;
  240.             }
  241.  
  242.             if (!(second_elem <= 255 && second_elem >= 0))
  243.             {
  244.                 cerr << "ERROR: Sensor reading out of range, ignoring line " << nline << " in the input file" << endl;
  245.                 nline++;
  246.             }
  247.             else
  248.             {
  249.                 cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
  250.                 cerr << "Ignoring line " << nline << " of the input file" << endl;
  251.                 nline++;
  252.             }
  253.  
  254.         }
  255.         break;
  256.  
  257.         case 3:
  258.         {
  259.             std::string third_elem = vec.at(2);
  260.             if (third_elem != "bent" && third_elem != "usable")
  261.             {
  262.                 cerr << "ERROR: Result of test to determine if coin is bent at line " << nline << " is invalid" << endl;
  263.                 cerr << "Ignoring this line of data" << endl;
  264.                 nline++;
  265.             }
  266.             else
  267.             {
  268.                 cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
  269.                 cerr << "Ignoring line " << nline << " of the input file" << endl;
  270.                 nline++;
  271.             }
  272.         }
  273.         break;
  274.  
  275.         case 4:
  276.         {
  277.             string fourth_elem = vec.at(3);
  278.             if (fourth_elem != "BothMatch" && fourth_elem != "OneMatch" && fourth_elem != "NoMatch")
  279.             {
  280.                 cerr << "ERROR: image processing result at line " << nline << " is invalid" << endl;
  281.                 cerr << "Ignoring this line of data" << endl;
  282.                 nline++;
  283.             }
  284.             else
  285.             {
  286.                 nline++;
  287.             }
  288.         } break;
  289.        
  290.  
  291.         default:
  292.         {
  293.             cerr << "ERROR: Extra data at line " << nline << ". Ignoring extra data" << endl;
  294.             nline++;
  295.             continue;
  296.         }
  297.         break;
  298.  
  299.         }
  300.     }
  301. }
  302.  
  303.         //other stuff
  304.  
  305.             /*if (vec.at(2) != "bent")
  306.             {
  307.                 if (vec.at(3) == "BothMatch")
  308.                 {
  309.                     if (((weight_coin >= min_nickel_weight && weight_coin <= max_nickel_weight) && (diameter_coin >= min_nickel_diameter && diameter_coin <= max_nickel_diameter)))
  310.                     {
  311.                         nickel_amount++;
  312.                         cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
  313.                         if (nickel_amount == nickels_per_roll)
  314.                         {
  315.                             rolls_of_nickels++;
  316.                             nickel_amount = 0;
  317.  
  318.                             cout << "The nickel wrapper is now full" << endl;
  319.                             cout << "The nickel wrapper has now been replaced" << endl;
  320.                         }
  321.                         else
  322.                         {
  323.                             cout << "There are now " << nickel_amount << " in the nickels wrapper" << endl;
  324.                         }
  325.                     }
  326.                     else if ((weight_coin >= min_dime_weight && weight_coin <= max_dime_weight) && (diameter_coin >= min_dime_diameter && diameter_coin <= max_dime_diameter))
  327.                     {
  328.                         dimes_amount++;
  329.                         cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
  330.                         if (dimes_amount == dimes_per_roll)
  331.                         {
  332.                             rolls_of_dimes++;
  333.                             dimes_amount = 0;
  334.                             cout << "The dime wrapper is now full" << endl;
  335.                             cout << "The dime wrapper has now been replaced" << endl;
  336.                             break;
  337.                         }
  338.                         else
  339.                         {
  340.                             cout << "There are now " << dimes_amount << " coins in the dimes wrapper" << endl;
  341.                         }
  342.                     }
  343.                     else if ((weight_coin >= min_quarter_weight && weight_coin <= max_quarter_weight) && (diameter_coin >= min_quarter_diameter && diameter_coin <= max_quarter_diameter))
  344.                     {
  345.                         quarters_amount++;
  346.                         cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
  347.                         if (quarters_amount == rolls_of_quarters)
  348.                         {
  349.                             rolls_of_quarters++;
  350.                             quarters_amount = 0;
  351.  
  352.                             cout << "The quarter wrapper is now full" << endl;
  353.                             cout << "The quarter wrapper has now been replaced" << endl;
  354.                         }
  355.                         else
  356.                         {
  357.                             cout << "There are now " << quarters_amount << " in the quarters wrapper" << endl;
  358.                         }
  359.                     }
  360.                     else if ((weight_coin >= min_loonie_weight && weight_coin <= max_loonie_weight) && (diameter_coin >= min_loonie_diameter && diameter_coin <= max_loonie_diameter))
  361.                     {
  362.                         loonies_amount++;
  363.                         cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
  364.                         if (loonies_amount == rolls_of_loonies)
  365.                         {
  366.                             rolls_of_loonies++;
  367.                             loonies_amount = 0;
  368.  
  369.                             cout << "The loonie wrapper is now full" << endl;
  370.                             cout << "The loonie wrapper has now been replaced" << endl;
  371.                         }
  372.                         else
  373.                         {
  374.                             cout << "There are now " << loonies_amount << " in the loonies wrapper" << endl;
  375.                         }
  376.                     }
  377.                     else if ((weight_coin >= min_toonie_weight && weight_coin <= max_toonie_weight) && (diameter_coin >= min_toonie_diameter && diameter_coin <= max_toonie_diameter))
  378.                     {
  379.                         toonies_amount++;
  380.                         cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
  381.                         if (toonies_amount == rolls_of_toonies)
  382.                         {
  383.                             rolls_of_toonies++;
  384.                             toonies_amount = 0;
  385.  
  386.                             cout << "The toonie wrapper is now full" << endl;
  387.                             cout << "The toonie wrapper has now been replaced" << endl;
  388.                         }
  389.                         else
  390.                         {
  391.                             cout << "There are now " << toonies_amount << " in the toonies wrapper" << endl;
  392.                         }
  393.                     }
  394.                 }
  395.                 else
  396.                 {
  397.                     other_coin_weight_total += weight_coin;
  398.                     other_coin_num_total++;
  399.  
  400.                     if (weight_other_container > max_othercontainter_weight)
  401.                     {
  402.                         weight_other_container = 0;
  403.                         weight_other_container += weight_coin;
  404.                         container_amount++;
  405.                         cout << "This coin does not fit in the other coin container" << endl;
  406.                         cout << "The other coin container has been replaced" << endl;
  407.                         cout << "The coin in the new other coin container weighs " << weight_other_container << " grams" << endl;
  408.                     }
  409.                     else
  410.                     {
  411.                         weight_other_container += weight_coin;
  412.  
  413.                         cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  414.                         cout << "The coins in the other coin container now weigh " << weight_other_container << " grams" << endl;
  415.                     }
  416.                 }
  417.             }
  418.             else
  419.             {
  420.                 bent_container_weight += weight_coin;
  421.                 bent_coin_weight_total += weight_coin;
  422.                 if (bent_container_weight > max_bent_container_weight)
  423.                 {
  424.                     bent_container_weight = 0;
  425.                     bent_container_weight += weight_coin;
  426.                     bent_container_amount++;
  427.  
  428.                     cout << "This coin does not fit in the bent coin container" << endl;
  429.                     cout << "The bent coin container has been replaced" << endl;
  430.                     cout << "The coin in the new bent coin container weighs " << bent_container_weight << " grams" << endl;
  431.                 }
  432.                 else
  433.                 {
  434.                     cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  435.                     cout << "The coins in the bent coin container now weighs " << bent_container_weight << " grams" << endl;
  436.                 }
  437.             }
  438.         }
  439.  
  440.         cout << "SUMMARY" << endl;
  441.         cout << "The Coin Sorter made " << rolls_of_nickels << " rolls of nickels." << endl;
  442.         cout << "    There are " << nickel_amount << " nickels in the partially full roll." << endl;
  443.         cout << "The Coin Sorter made " << rolls_of_dimes << " rolls of dimes." << endl;
  444.         cout << "    There are " << dimes_amount << " dimes in the partially full roll." << endl;
  445.         cout << "The Coin Sorter made " << rolls_of_quarters << " rolls of quarters." << endl;
  446.         cout << "    There are " << quarters_amount << " quarters in the partially full roll." << endl;
  447.         cout << "The Coin Sorter made " << rolls_of_loonies << " rolls of loonies." << endl;
  448.         cout << "    There are " << loonies_amount << " loonies in the partially full roll." << endl;
  449.         cout << "The Coin Sorter made " << rolls_of_toonies << " rolls of toonies." << endl;
  450.         cout << "    There are " << toonies_amount << " toonies in the partially full roll." << endl;
  451.         cout << "The Coin Sorter processed " << other_coin_num_total << " other coins." << endl;
  452.  
  453.         cout << "    The other coins completely filled " << container_amount << " containers" << endl;
  454.  
  455.         cout << "    There were " << other_amount << " other coins in the partially full container" << endl;
  456.         cout << "    The total weight of the other coins was " << fixed << setprecision(3) << other_weight << " grams" << endl;
  457.         cout << "The Coin Sorter processed " << fixed << setprecision(4) << bent_coin_weight_total << " g of bent coins" << endl;
  458.     }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement