Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     // Initialize and declear all Variables
  14.     ifstream myinputstream;
  15.     ofstream myoutputstream;
  16.     char inputname[1024] = { '\0' };
  17.     char outputname[1024] = { '\0' };
  18.     char inFile[1024] = { '\0' };
  19.     int i = 0; //number of tries
  20.     const int maxtries = 2; //maximum number of tires
  21.     int numlines = 0; //number of lines in a file
  22.     const int minlines = 1; //minimum number of lines in the file
  23.     const int maxlines = 5000; // maximum number of lines in the file
  24.     int weight = 0; // weight of a coin
  25.     int diameter = 0;
  26.  
  27.     //double diametermm = 0;
  28.     //double weightgrams = 0;
  29.  
  30.     string weightstring;
  31.     string diameterstring;
  32.     string wholeline;
  33.  
  34.     int NNN = 0; // count line
  35.     int count1 = 0;
  36.  
  37.     string usable;
  38.     string match;
  39.     string extrastuff;
  40.    
  41.  
  42.     double measuringDiameter = 0.0;
  43.     double measuringWeight = 0.0;
  44.     int numNICKEL = 0;
  45.     int numTWONIES = 0;
  46.     int numLOONIES = 0;
  47.     int numDIMES = 0;
  48.     int numQUARTERS = 0;
  49.     int numOTHER = 0;
  50.  
  51.     int nickle = 0;
  52.     int twonies = 0;
  53.     int loonies = 0;
  54.     int dimes = 0;
  55.     int quarters = 0;
  56.  
  57.     int numRollsNickels = 0;
  58.     int numRollsDimes = 0;
  59.     int numRollsQuarter = 0;
  60.     int numRollsLoonies = 0;
  61.     int numRollsToonies = 0;
  62.  
  63.     double weightBentContainer = 0.0;
  64.     double totalBentCoinWeight = 0.0;
  65.     int numBentContainer = 0;
  66.     double weightOtherContainer = 0.0;
  67.     int totalOtherCoinsNum = 0;
  68.     int numOtherContainer = 0;
  69.     double totalOtherCoinWeight = 0.0;
  70.  
  71.  
  72.  
  73.  
  74.     //declear constants
  75.     //nickel max and min weight and diameter
  76.     const double minNICKELweight = 3.60;
  77.     const double maxNICKELweight = 4.30;
  78.     const double minNICKELdiameter = 20.2;
  79.     const double maxNICKELdiameter = 20.8;
  80.     //dime max and min weight and diameter
  81.     const double minDIMEweight = 1.30;
  82.     const double maxDIMEweight = 2.20;
  83.     const double minDIMEdiameter = 17.3;
  84.     const double maxDIMEdiameter = 18.7;
  85.     //quarter max and min weight and diameter
  86.     const double minQUARTERweight = 4.00;
  87.     const double maxQUARTERweight = 4.80;
  88.     const double minQUARTERdiameter = 22.80;
  89.     const double maxQUARTERdiameter = 24.60;
  90.     //loonie max and min weight and diameter
  91.     const double minLOONIEweight = 6.50;
  92.     const double maxLOONIEweight = 7.50;
  93.     const double minLOONIEdiameter = 25.00;
  94.     const double maxLOONIEdiameter = 27.00;
  95.     //toonie max and min weight and diameter
  96.     const double minTWONIEweight = 6.75;
  97.     const double maxTWONIEweight = 7.85;
  98.     const double minTWONIEdiameter = 26.90;
  99.     const double maxTWONIEdiameter = 29.10;
  100.  
  101.     //Coins per Role
  102.     const int NICKELSPERROLE = 40;
  103.     const int DIMESPERROLE = 50;
  104.     const int QUARTERSPERROLE = 40;
  105.     const int LOONIESPERROLE = 25;
  106.     const int TOONIESPERROLE = 25;
  107.  
  108.     const double maxBENTcontainerWEIGHT = 100.0;
  109.     const double maxOTHERcontainerWEIGHT = 200.0;
  110.  
  111.     double coinWEIGHT = 0.0;
  112.     double coinDIAMETER = 0.0;
  113.     double otherWEIGHT = 0.0;
  114.  
  115.     cout.setf(ios::fixed);
  116.     cout.setf(ios::showpoint);
  117.  
  118.    
  119.     do
  120.     {
  121.         cout << "Type the name of the input file containing sensor readings:\n";
  122.         cin >> inputname;
  123.         myinputstream.open(inputname);
  124.  
  125.         if (myinputstream.fail())
  126.         {
  127.             cerr << "ERROR: file " << inputname << " could not be opened for input" << endl;
  128.  
  129.             i++;
  130.         }
  131.         if (i > maxtries)
  132.         {
  133.             cout << "ERROR: You exceeded maximum number of tries allowed \nwhile entering the input file name";
  134.             return 1;
  135.         }
  136.  
  137.     } while (myinputstream.fail() && i <= maxtries);
  138.  
  139.  
  140.     i = 0;   //reset i back to zero
  141.  
  142.     //error for outputfiles
  143.     do
  144.     {
  145.         cout << "Type the name of the output file which will hold the simulation results: \n";
  146.         cin >> outputname;
  147.         myoutputstream.open(outputname, ios::in);
  148.        
  149.  
  150.  
  151.         if (myoutputstream.fail())
  152.         {
  153.             cerr << "ERROR: file " << outputname << " could not be opened for output" << endl;
  154.  
  155.             i++;
  156.         }
  157.         if (i > maxtries)
  158.         {
  159.             cerr << "ERROR: You exceeded maximum number of tries allowed \nwhile entering the output file name";
  160.             return 1;
  161.         }
  162.  
  163.  
  164.     } while (myoutputstream.fail() && i <= maxtries);
  165.  
  166.  
  167.  
  168.  
  169.     if (!(myinputstream >> numlines))
  170.     {
  171.         if (myinputstream.eof())
  172.         {
  173.             cerr << "ERROR: Input data file is empty" << endl;
  174.             myinputstream.close();
  175.             myoutputstream.close();
  176.             return 1;
  177.         }
  178.         else
  179.         {
  180.             cerr << "ERROR: First piece of data in the file is not an integer\n";
  181.             myinputstream.close();
  182.             myoutputstream.close();
  183.             return 1;
  184.         }
  185.     }
  186.  
  187.  
  188.     if (numlines < minlines || numlines > maxlines)
  189.     {
  190.         cerr << "ERROR: The number of sensor readings is out of range" << endl;
  191.         myinputstream.close();
  192.         return 1;
  193.     }
  194.    
  195.  
  196.  
  197.     while(NNN <= numlines)
  198.     {
  199.         NNN++;
  200.        
  201.         while (!myinputstream.eof()) { //If all of the above runs fine, it will begin processing the data of the text file. This while loop runs only until there are no lines left in the code
  202.  
  203.             getline(myinputstream, wholeline, '\n'); //This begins processing information through the getfile function
  204.  
  205.             if (wholeline.empty()) { //This will skip any empty lines in the code before it adds the line to the count
  206.                 continue;
  207.             }
  208.             NNN++; //This will display the amount of lines
  209.  
  210.                 /*cout << wholeline << '\n';*/ //This line is used only to test and check for any errors in each line
  211.  
  212.             istringstream iss(wholeline);
  213.  
  214.  
  215.             getline(iss, weightstring, ' '); //This will process our value for the weight of the coin. The value should be within the sensor reading ( 0 <= weight <= 255)
  216.  
  217.             stringstream weightstringtoint(weightstring);
  218.             weightstringtoint >> weight; //This will convert the string into an integer
  219.  
  220.             if (weightstring.empty())
  221.             { //If there is no value read from the line for weight, it will run this error
  222.                 cerr << "ERROR: No more data" << endl;
  223.                 cerr << "Simulation completed early before line " << NNN << " of input" << endl;
  224.                 continue;
  225.             }
  226.  
  227.             if (!weight) { //If the first value read on the line is not an integer (i.e. a value with decimals, letters, etc.), it will run this error
  228.                 cerr << "ERROR: Weight sensor value read on line " << NNN << " is not an integer" << endl;
  229.                 cerr << "Simulation terminated early: Please correct your data file" << endl;
  230.                 continue;
  231.  
  232.             }
  233.  
  234.             getline(iss, diameterstring, ' '); //The second value read on the line will be the diameter of the coin. The value should also be within the sensor reading (0 <= diameter <= 255)
  235.             stringstream diametertoint(diameterstring);
  236.             diametertoint >> diameter; //This will convert the string into an integer
  237.             if (diameterstring.empty()) { //If there is no second value read on the line for diameter, it will run this error
  238.                 cerr << "ERROR: Weight sensor measurement only" << endl;
  239.                 cerr << "Ignoring line " << NNN << " of the input file" << endl;
  240.                 continue;
  241.             }
  242.  
  243.             if (!diameter) { //If the value read on the line is not an integer (i.e. a value with decimals, letters, etc.), it will run this error
  244.                 cerr << "ERROR: Diameter sensor value read on line " << NNN << " is not an integer" << endl;
  245.                 cerr << "Simulation terminated early: Please correct your data file" << endl;
  246.                 continue;
  247.  
  248.             }
  249.  
  250.             getline(iss, usable, ' '); //The third value read on the line will determine whether the coin is bent or still usable, with the only texts allowed being "bent" and "usable"
  251.  
  252.             if (usable.empty()) { //If there is no third value read on the line for bent, it will run this error
  253.                 cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
  254.                 cerr << "Ignoring line " << NNN << " of the input file" << endl;
  255.                 continue;
  256.             }
  257.  
  258.  
  259.             if (usable != "bent" && usable != "usable") { //If the value read on the line is neither "bent" or "usable", it will run this error
  260.                 cerr << "ERROR: Result of test to determine if coin is bent at line " << NNN << " is invalid" << endl;
  261.                 cerr << "Ignoring this line of data" << endl;
  262.                 continue;
  263.             }
  264.  
  265.             getline(iss, match, ' '); //The fourth value on the line will determine whether the coin has any matches with canadian coins, named faceimage. The only values that it should read is "BothMatch", "OneMatch", or "NoMatch"
  266.  
  267.             if (match.empty()) { //If there is no fourth value read on the line for faceimage, it will run this error
  268.                 cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
  269.                 cerr << "Ignoring line " << NNN << " of the input file" << endl;
  270.                 continue;
  271.             }
  272.             if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch") { //If the value read on this line is neither "BothMatch", "OneMatch", or "NoMatch", it will run this error
  273.                 cerr << "ERROR: image processing result at line " << NNN << " is invalid" << endl;
  274.                 cerr << "Ignoring this line of data" << endl;
  275.                 continue;
  276.  
  277.             }
  278.  
  279.             getline(iss, extrastuff, ' '); //This will read if there is any fifth value on the line that we do not want, named extrastuff
  280.  
  281.             if (!extrastuff.empty()) { //If there is a fifth value read on the line, it will run this error
  282.                 cerr << "ERROR: Extra data at line " << NNN << ". Ignoring extra data" << endl;
  283.                 extrastuff.clear(); //Resets the string value
  284.                 continue;
  285.  
  286.             }
  287.  
  288.             if (weight < 0 || weight > 255 || diameter < 0 || diameter > 255) { //If either the weight or diameter are outside of the sensor reading ( 0 <= weight/diameter <= 255), it will run this error
  289.                 cerr << "ERROR: Sensor reading is out of range, ignoring line " << NNN << " in the input file" << endl;
  290.                 continue;
  291.             }
  292.             coinWEIGHT = static_cast<double>(weight) * ((double)(2) / (double)(51));
  293.             coinDIAMETER = static_cast<double>(diameter) * ((double)(30) / (double)(255) + (double)(10));
  294.             cout.precision(2);
  295.  
  296.             if (usable == "usable")
  297.             {
  298.                 if (match == "BothMatch")
  299.                 {
  300.                     if ((coinWEIGHT >= minNICKELweight && coinWEIGHT <= maxNICKELweight) && (coinDIAMETER >= minNICKELdiameter && coinDIAMETER <= maxNICKELdiameter))
  301.                     {
  302.  
  303.                         numNICKEL++;
  304.                         cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
  305.                         if (numNICKEL == NICKELSPERROLE)
  306.                         {
  307.                             numRollsNickels++;
  308.                             numNICKEL = 0;
  309.                             cout << "The nickel wrapper is now full" << endl;
  310.                             cout << "The nickel wrapper has now been replaced" << endl;
  311.  
  312.                             continue
  313.                         }
  314.                         else
  315.                         {
  316.                             cout << "There are now " << numNICKEL << " in the nickels wrapper" << endl;
  317.                         }
  318.  
  319.                     }
  320.                     else if ((coinWEIGHT >= minDIMEweight && coinWEIGHT <= maxDIMEweight) && (coinDIAMETER >= minDIMEdiameter && coinDIAMETER <= maxDIMEdiameter))
  321.                     {
  322.                         numDIMES++;
  323.                         cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
  324.                         if (numDIMES == DIMESPERROLE)
  325.                         {
  326.                             numRollsDimes++;
  327.                             numDIMES = 0;
  328.                             cout << "The dime wrapper is now full" << endl;
  329.                             cout << "The dime wrapper has now been replaced" << endl;
  330.                             break;
  331.                         }
  332.                         else
  333.                         {
  334.                             cout << "There are now " << numDIMES << " coins in the dimes wrapper" << endl;
  335.  
  336.                         }
  337.  
  338.                     }
  339.                     else if ((coinWEIGHT >= minQUARTERweight && coinWEIGHT <= maxQUARTERweight) && (coinDIAMETER >= minQUARTERdiameter && coinDIAMETER <= maxQUARTERdiameter))
  340.                     {
  341.                         numQUARTERS++;
  342.                         cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
  343.                         if (numQUARTERS == QUARTERSPERROLE)
  344.                         {
  345.                             numRollsQuarter++;
  346.                             numQUARTERS = 0;
  347.  
  348.                             cout << "The quarter wrapper is now full" << endl;
  349.                             cout << "The quarter wrapper has now been replaced" << endl;
  350.  
  351.  
  352.                         }
  353.                         else
  354.                         {
  355.                             cout << "There are now " << numQUARTERS << " in the quarters wrapper" << endl;
  356.                         }
  357.                     }
  358.                     else if ((coinWEIGHT >= minLOONIEweight && coinWEIGHT <= maxLOONIEweight) && (coinDIAMETER >= minLOONIEdiameter && coinDIAMETER <= maxLOONIEdiameter))
  359.                     {
  360.                         numLOONIES++;
  361.                         cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
  362.                         if (numLOONIES == LOONIESPERROLE)
  363.                         {
  364.                             numRollsLoonies++;
  365.                             numLOONIES = 0;
  366.  
  367.                             cout << "The loonie wrapper is now full" << endl;
  368.                             cout << "The loonie wrapper has now been replaced" << endl;
  369.  
  370.  
  371.                         }
  372.                         else
  373.                         {
  374.                             cout << "There are now " << numLOONIES << " in the loonies wrapper" << endl;
  375.                         }
  376.                     }
  377.                     else if ((coinWEIGHT >= minTWONIEweight && coinWEIGHT <= maxTWONIEweight) && (coinDIAMETER >= minTWONIEdiameter && coinDIAMETER <= maxTWONIEdiameter))
  378.                     {
  379.                         numTWONIES++;
  380.                         cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
  381.                         if (numTWONIES == TOONIESPERROLE)
  382.                         {
  383.                             numRollsToonies++;
  384.                             numTWONIES = 0;
  385.  
  386.                             cout << "The toonie wrapper is now full" << endl;
  387.                             cout << "The toonie wrapper has now been replaced" << endl;
  388.  
  389.  
  390.                         }
  391.                         else
  392.                         {
  393.                             cout << "There are now " << numTWONIES << " in the toonies wrapper" << endl;
  394.                         }
  395.                     }
  396.                 }
  397.                 else
  398.                 {
  399.                     totalOtherCoinWeight += coinWEIGHT;
  400.                     totalOtherCoinsNum++;
  401.  
  402.                     if (weightOtherContainer > maxOTHERcontainerWEIGHT)
  403.                     {
  404.                         weightOtherContainer = 0;
  405.                         weightOtherContainer += coinWEIGHT;
  406.                         numOtherContainer++;
  407.  
  408.                         cout << "This coin does not fit in the other coin container" << endl;
  409.                         cout << "The other coin container has been replaced" << endl;
  410.                         cout << "The coin in the new other coin container weighs " << weightOtherContainer << " grams" << endl;
  411.                     }
  412.                     else
  413.                     {
  414.                         weightOtherContainer += coinWEIGHT;
  415.  
  416.                         cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  417.                         cout << "The coins in the other coin container now weigh " << weightOtherContainer << " grams" << endl;
  418.                     }
  419.  
  420.  
  421.                 }
  422.  
  423.             }
  424.             if (usable == "bent")
  425.             {
  426.                 weightBentContainer += coinWEIGHT;
  427.                 totalBentCoinWeight += coinWEIGHT;
  428.  
  429.                 if (weightBentContainer > maxBENTcontainerWEIGHT)
  430.                 {
  431.                     weightBentContainer = 0;
  432.                     weightBentContainer += coinWEIGHT;
  433.                     numBentContainer++;
  434.  
  435.                     cout << "This coin does not fit in the bent coin container" << endl;
  436.                     cout << "The bent coin container has been replaced" << endl;
  437.                     cout << "The coin in the new bent coin container weighs " << weightBentContainer << " grams" << endl;
  438.                 }
  439.  
  440.                 cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  441.                 cout << "The coins in the bent coin container now weighs " << weightBentContainer << " grams" << endl;
  442.  
  443.             }
  444.  
  445.             /*if (!(myinputstream >> weight))
  446.             {
  447.                 if (myinputstream.eof())
  448.                 {
  449.                     cerr << "ERROR: No more data" << endl;
  450.                     cerr << "Simulation completed early before line " << NNN << " of input" << endl;
  451.                     break;
  452.                 }
  453.                 else
  454.                 {
  455.                     cerr << "ERROR: Weight sensor value read on line " << NNN << " is not an integer" << endl;
  456.                     cerr << "Simulation terminated early : Please correct your data file" << endl;
  457.                     return 1;
  458.  
  459.                 }
  460.             }
  461.  
  462.             char space;
  463.  
  464.             space = myinputstream.peek();
  465.  
  466.             while (space == ' ' || space == '\t')
  467.             {
  468.                 space = myinputstream.get();
  469.             }
  470.  
  471.             if (space == '\n') continue;
  472.  
  473.  
  474.  
  475.  
  476.             if (space == '\n')
  477.             {
  478.                 cout << "ERROR: Weight sensor measurement only" << endl;
  479.                 cout << "ERROR: Ignore line " << NNN << " of the input file" << endl;
  480.                 continue;
  481.             }
  482.  
  483.             if (!(myinputstream >> diameter))
  484.             {
  485.                 cerr << "ERROR: Diameter sensor value read on line " << NNN << " is not an integer" << endl;
  486.                 cerr << "Simulation terminated early : Please correct your data file" << endl;
  487.                 return 1;
  488.             }
  489.  
  490.             while (space == ' ' || space == '\t')
  491.             {
  492.                 myinputstream.get();
  493.  
  494.             }
  495.  
  496.  
  497.  
  498.             if (space == '\n')
  499.             {
  500.                 cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
  501.                 cerr << "Ignoring line " << NNN << " of the input file" << endl;
  502.                 return 1;
  503.             }
  504.  
  505.  
  506.             myinputstream >> usable;
  507.  
  508.             if (usable != "bent" && usable != "usable")
  509.             {
  510.                 cerr << "ERROR: Result of test to determine if coin is bent at line " << NNN << " is invalid" << endl;
  511.                 cerr << "Ignoring this line of data" << endl;
  512.                 while (!(myinputstream.peek() == '\n'))
  513.                 {
  514.                     myinputstream.get();
  515.                 }
  516.                 continue;
  517.             }
  518.             while (space == ' ' || space == '\t')
  519.             {
  520.                 myinputstream.get();
  521.  
  522.             }
  523.  
  524.  
  525.             if (space == '\n');
  526.             {
  527.                 cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
  528.                 cerr << "Ignoring line " << NNN << " of the input file" << endl;
  529.                 continue;
  530.             }
  531.  
  532.             myinputstream >> match;
  533.  
  534.             if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch")
  535.             {
  536.                 cerr << "ERROR: image processing result at line " << NNN << " is invalid" << endl;
  537.                 cerr << "Ignoring this line of data" << endl;
  538.  
  539.                 continue;
  540.             }
  541.             while (space == ' ' || space == '\t');
  542.             {
  543.                 myinputstream.get();
  544.  
  545.                 if (space != '\n' && space == '\t' && space != ' ')
  546.                 {
  547.                     cerr << "ERROR: Extra data at line " << NNN << ". Ignoring extra data" << endl;
  548.                 }
  549.                 while (space != ' ' && space == '\n');
  550.                 {
  551.                     myinputstream.get();
  552.                 }
  553.  
  554.  
  555.             }
  556.  
  557.             coinWEIGHT = static_cast<double>(weight) * ((double)(2) / (double)(51));
  558.             coinDIAMETER = static_cast<double>(diameter) * ((double)(30) / (double)(255) + (double)(10));
  559.             cout.precision(2);
  560.  
  561.             cout << " why is this not working";
  562.  
  563.             if ((coinWEIGHT >= minNICKELweight && coinWEIGHT <= maxNICKELweight) && (coinDIAMETER >= minNICKELdiameter && coinDIAMETER <= maxNICKELdiameter))
  564.             {
  565.  
  566.                 numNICKEL++;
  567.                 cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
  568.                 if (numNICKEL == NICKELSPERROLE)
  569.                 {
  570.                     numRollsNickels++;
  571.                     numNICKEL = 0;
  572.                     cout << "The nickel wrapper is now full" << endl;
  573.                     cout << "The nickel wrapper has now been replaced" << endl;
  574.  
  575.                 }
  576.                 else
  577.                 {
  578.                     cout << "There are now " << numNICKEL << " in the nickels wrapper" << endl;
  579.                 }
  580.  
  581.             }
  582.             else if ((coinWEIGHT >= minDIMEweight && coinWEIGHT <= maxDIMEweight) && (coinDIAMETER >= minDIMEdiameter && coinDIAMETER <= maxDIMEdiameter))
  583.             {
  584.                 numDIMES++;
  585.                 cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
  586.                 if (numDIMES == DIMESPERROLE)
  587.                 {
  588.                     numRollsDimes++;
  589.                     numDIMES = 0;
  590.                     cout << "The dime wrapper is now full" << endl;
  591.                     cout << "The dime wrapper has now been replaced" << endl;
  592.                     break;
  593.                 }
  594.                 else
  595.                 {
  596.                     cout << "There are now " << numDIMES << " coins in the dimes wrapper" << endl;
  597.  
  598.                 }
  599.  
  600.             }
  601.             else if ((coinWEIGHT >= minQUARTERweight && coinWEIGHT <= maxQUARTERweight) && (coinDIAMETER >= minQUARTERdiameter && coinDIAMETER <= maxQUARTERdiameter))
  602.             {
  603.                 numQUARTERS++;
  604.                 cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
  605.                 if (numQUARTERS == QUARTERSPERROLE)
  606.                 {
  607.                     numRollsQuarter++;
  608.                     numQUARTERS = 0;
  609.  
  610.                     cout << "The quarter wrapper is now full" << endl;
  611.                     cout << "The quarter wrapper has now been replaced" << endl;
  612.  
  613.  
  614.                 }
  615.                 else
  616.                 {
  617.                     cout << "There are now " << numQUARTERS << " in the quarters wrapper" << endl;
  618.                 }
  619.             }
  620.             else if ((coinWEIGHT >= minLOONIEweight && coinWEIGHT <= maxLOONIEweight) && (coinDIAMETER >= minLOONIEdiameter && coinDIAMETER <= maxLOONIEdiameter))
  621.             {
  622.                 numLOONIES++;
  623.                 cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
  624.                 if (numLOONIES == LOONIESPERROLE)
  625.                 {
  626.                     numRollsLoonies++;
  627.                     numLOONIES = 0;
  628.  
  629.                     cout << "The loonie wrapper is now full" << endl;
  630.                     cout << "The loonie wrapper has now been replaced" << endl;
  631.  
  632.  
  633.                 }
  634.                 else
  635.                 {
  636.                     cout << "There are now " << numLOONIES << " in the loonies wrapper" << endl;
  637.                 }
  638.             }
  639.             else if ((coinWEIGHT >= minTWONIEweight && coinWEIGHT <= maxTWONIEweight) && (coinDIAMETER >= minTWONIEdiameter && coinDIAMETER <= maxTWONIEdiameter))
  640.             {
  641.                 numTWONIES++;
  642.                 cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
  643.                 if (numTWONIES == TOONIESPERROLE)
  644.                 {
  645.                     numRollsToonies++;
  646.                     numTWONIES = 0;
  647.  
  648.                     cout << "The toonie wrapper is now full" << endl;
  649.                     cout << "The toonie wrapper has now been replaced" << endl;
  650.  
  651.  
  652.                 }
  653.                 else
  654.                 {
  655.                     cout << "There are now " << numTWONIES << " in the toonies wrapper" << endl;
  656.                 }
  657.             }
  658.  
  659.  
  660.             string x = " "; //whatever is in the file
  661.             string y;
  662.  
  663.             i = 0;
  664.  
  665.             while (getline(myinputstream, x))
  666.             {
  667.                 vector<string> vec;
  668.                 stringstream buf(x);
  669.  
  670.                 while (buf >> y)
  671.                 {
  672.                     vec.push_back(y);
  673.                 }
  674.  
  675.                 switch (vec.size())
  676.                 {
  677.                 case 0:
  678.                 {
  679.                     if (myinputstream.eof())
  680.                     {
  681.                         cerr << "ERROR: No more data" << endl;
  682.                         cerr << "Simulation completed early before line " << NNN << " of input" << endl;
  683.                         break;
  684.                     }
  685.                     else
  686.                     {
  687.                         continue;
  688.                     }
  689.                 }
  690.                 break;
  691.  
  692.                 case 1:
  693.                 {
  694.                     int weight = stoi(vec.at(0));
  695.                     try
  696.                     {
  697.                         int weight = stoi(vec.at(0));
  698.                     }
  699.  
  700.                     catch (...)
  701.                     {
  702.                         cerr << "ERROR: Weight sensor value read on line " << NNN << " is not an integer" << endl;
  703.                         cerr << "Simulation terminated early : Please correct your data file" << endl;
  704.                         NNN++;
  705.                     }
  706.  
  707.                     if (!(weight <= 255 && weight >= 0))
  708.                     {
  709.                         cerr << "ERROR: Sensor reading out of range, ignoring line " << NNN << " in the input file" << endl;
  710.                     }
  711.                     else
  712.                     {
  713.                         cout << "ERROR: Weight sensor measurement only" << endl;
  714.                         cout << "ERROR: Ignore line " << NNN << " of the input file" << endl;
  715.                         NNN++;
  716.                     }
  717.                 }   break;
  718.  
  719.                 case 2:
  720.                 {
  721.                     int diameter = stoi(vec.at(1));
  722.                     try
  723.                     {
  724.                         int diameter = stoi(vec.at(1));
  725.                     }
  726.                     catch (...)
  727.                     {
  728.                         cerr << "ERROR: Diameter sensor value read on line " << NNN << " is not an integer" << endl;
  729.                         cerr << "Simulation terminated early : Please correct your data file" << endl;
  730.                         NNN++;
  731.                     }
  732.  
  733.                     if (!(diameter <= 255 && diameter >= 0))
  734.                     {
  735.                         cerr << "ERROR: Sensor reading out of range, ignoring line " << NNN << " in the input file" << endl;
  736.                         NNN++;
  737.                     }
  738.                     else
  739.                     {
  740.                         cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
  741.                         cerr << "Ignoring line " << NNN << " of the input file" << endl;
  742.                         NNN++;
  743.                     }
  744.  
  745.                 }
  746.                 break;
  747.  
  748.                 case 3:
  749.                 {
  750.                     string usable = vec.at(2);
  751.                     if (usable != "bent" && usable != "usable")
  752.                     {
  753.                         cerr << "ERROR: Result of test to determine if coin is bent at line " << NNN << " is invalid" << endl;
  754.                         cerr << "Ignoring this line of data" << endl;
  755.                         NNN++;
  756.                     }
  757.                     else
  758.                     {
  759.                         cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
  760.                         cerr << "Ignoring line " << NNN << " of the input file" << endl;
  761.                         NNN++;
  762.                     }
  763.                 }
  764.                 break;
  765.  
  766.                 case 4:
  767.                 {
  768.                     string match = vec.at(3);
  769.                     if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch")
  770.                     {
  771.                         cerr << "ERROR: image processing result at line " << NNN << " is invalid" << endl;
  772.                         cerr << "Ignoring this line of data" << endl;
  773.                         NNN++;
  774.                     }
  775.                     else
  776.                     {
  777.                         NNN++;
  778.                         coinWEIGHT = static_cast<double>(weight) * ((double)(2) / (double)(51));
  779.                         coinDIAMETER = static_cast<double>(diameter) * ((double)(30) / (double)(255) + (double)(10));
  780.                         cout.precision(2);
  781.  
  782.                         if (usable == "usable")
  783.                         {
  784.                             if (match == "BothMatch")
  785.                             {
  786.                                 if ((coinWEIGHT >= minNICKELweight && coinWEIGHT <= maxNICKELweight) && (coinDIAMETER >= minNICKELdiameter && coinDIAMETER <= maxNICKELdiameter))
  787.                                 {
  788.  
  789.                                     numNICKEL++;
  790.                                     cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
  791.                                     if (numNICKEL == NICKELSPERROLE)
  792.                                     {
  793.                                         numRollsNickels++;
  794.                                         numNICKEL = 0;
  795.                                         cout << "The nickel wrapper is now full" << endl;
  796.                                         cout << "The nickel wrapper has now been replaced" << endl;
  797.  
  798.  
  799.                                     }
  800.                                     else
  801.                                     {
  802.                                         cout << "There are now " << numNICKEL << " in the nickels wrapper" << endl;
  803.                                     }
  804.  
  805.                                 }
  806.                                 else if ((coinWEIGHT >= minDIMEweight && coinWEIGHT <= maxDIMEweight) && (coinDIAMETER >= minDIMEdiameter && coinDIAMETER <= maxDIMEdiameter))
  807.                                 {
  808.                                     numDIMES++;
  809.                                     cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
  810.                                     if (numDIMES == DIMESPERROLE)
  811.                                     {
  812.                                         numRollsDimes++;
  813.                                         numDIMES = 0;
  814.                                         cout << "The dime wrapper is now full" << endl;
  815.                                         cout << "The dime wrapper has now been replaced" << endl;
  816.                                         break;
  817.                                     }
  818.                                     else
  819.                                     {
  820.                                         cout << "There are now " << numDIMES << " coins in the dimes wrapper" << endl;
  821.  
  822.                                     }
  823.  
  824.                                 }
  825.                                 else if ((coinWEIGHT >= minQUARTERweight && coinWEIGHT <= maxQUARTERweight) && (coinDIAMETER >= minQUARTERdiameter && coinDIAMETER <= maxQUARTERdiameter))
  826.                                 {
  827.                                     numQUARTERS++;
  828.                                     cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
  829.                                     if (numQUARTERS == QUARTERSPERROLE)
  830.                                     {
  831.                                         numRollsQuarter++;
  832.                                         numQUARTERS = 0;
  833.  
  834.                                         cout << "The quarter wrapper is now full" << endl;
  835.                                         cout << "The quarter wrapper has now been replaced" << endl;
  836.  
  837.  
  838.                                     }
  839.                                     else
  840.                                     {
  841.                                         cout << "There are now " << numQUARTERS << " in the quarters wrapper" << endl;
  842.                                     }
  843.                                 }
  844.                                 else if ((coinWEIGHT >= minLOONIEweight && coinWEIGHT <= maxLOONIEweight) && (coinDIAMETER >= minLOONIEdiameter && coinDIAMETER <= maxLOONIEdiameter))
  845.                                 {
  846.                                     numLOONIES++;
  847.                                     cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
  848.                                     if (numLOONIES == LOONIESPERROLE)
  849.                                     {
  850.                                         numRollsLoonies++;
  851.                                         numLOONIES = 0;
  852.  
  853.                                         cout << "The loonie wrapper is now full" << endl;
  854.                                         cout << "The loonie wrapper has now been replaced" << endl;
  855.  
  856.  
  857.                                     }
  858.                                     else
  859.                                     {
  860.                                         cout << "There are now " << numLOONIES << " in the loonies wrapper" << endl;
  861.                                     }
  862.                                 }
  863.                                 else if ((coinWEIGHT >= minTWONIEweight && coinWEIGHT <= maxTWONIEweight) && (coinDIAMETER >= minTWONIEdiameter && coinDIAMETER <= maxTWONIEdiameter))
  864.                                 {
  865.                                     numTWONIES++;
  866.                                     cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
  867.                                     if (numTWONIES == TOONIESPERROLE)
  868.                                     {
  869.                                         numRollsToonies++;
  870.                                         numTWONIES = 0;
  871.  
  872.                                         cout << "The toonie wrapper is now full" << endl;
  873.                                         cout << "The toonie wrapper has now been replaced" << endl;
  874.  
  875.  
  876.                                     }
  877.                                     else
  878.                                     {
  879.                                         cout << "There are now " << numTWONIES << " in the toonies wrapper" << endl;
  880.                                     }
  881.                                 }
  882.                             }
  883.                             else
  884.                             {
  885.                                 totalOtherCoinWeight += coinWEIGHT;
  886.                                 totalOtherCoinsNum++;
  887.  
  888.                                 if (weightOtherContainer > maxOTHERcontainerWEIGHT)
  889.                                 {
  890.                                     weightOtherContainer = 0;
  891.                                     weightOtherContainer += coinWEIGHT;
  892.                                     numOtherContainer++;
  893.  
  894.                                     cout << "This coin does not fit in the other coin container" << endl;
  895.                                     cout << "The other coin container has been replaced" << endl;
  896.                                     cout << "The coin in the new other coin container weighs " << weightOtherContainer << " grams" << endl;
  897.                                 }
  898.                                 else
  899.                                 {
  900.                                     weightOtherContainer += coinWEIGHT;
  901.  
  902.                                     cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  903.                                     cout << "The coins in the other coin container now weigh " << weightOtherContainer << " grams" << endl;
  904.                                 }
  905.  
  906.  
  907.                             }
  908.  
  909.                         }
  910.                         if(usable == "bent")
  911.                         {
  912.                             weightBentContainer += coinWEIGHT;
  913.                             totalBentCoinWeight += coinWEIGHT;
  914.  
  915.                             if (weightBentContainer > maxBENTcontainerWEIGHT)
  916.                             {
  917.                                 weightBentContainer = 0;
  918.                                 weightBentContainer += coinWEIGHT;
  919.                                 numBentContainer++;
  920.  
  921.                                 cout << "This coin does not fit in the bent coin container" << endl;
  922.                                 cout << "The bent coin container has been replaced" << endl;
  923.                                 cout << "The coin in the new bent coin container weighs " << weightBentContainer << " grams" << endl;
  924.                             }
  925.  
  926.                             cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  927.                             cout << "The coins in the bent coin container now weighs " << weightBentContainer << " grams" << endl;
  928.  
  929.                         }
  930.  
  931.                     }
  932.                 } break;
  933.                 default:
  934.                 {
  935.                     cerr << "ERROR: Extra data at line " << NNN << ". Ignoring extra data" << endl;
  936.                     NNN++;
  937.                     continue;
  938.                 }
  939.                 break;
  940.                 }
  941.  
  942.             }*/
  943.  
  944.         }
  945.     }
  946.  
  947.     cout << endl << endl << endl;
  948.     cout << "SUMMARY" << endl;
  949.     cout << "The Coin Sorter made " << numRollsNickels << " rolls of nickels." << endl;
  950.     cout << "    There are " << numNICKEL << " nickels in the partially full roll." << endl;
  951.     cout << "The Coin Sorter made " << numRollsDimes << " rolls of dimes." << endl;
  952.     cout << "    There are " << numDIMES << " dimes in the partially full roll." << endl;
  953.     cout << "The Coin Sorter made " << numRollsQuarter << " rolls of quarters." << endl;
  954.     cout << "    There are " << numQUARTERS << " quarters in the partially full roll." << endl;
  955.     cout << "The Coin Sorter made " << numRollsLoonies << " rolls of loonies." << endl;
  956.     cout << "    There are " << numLOONIES << " loonies in the partially full roll." << endl;
  957.     cout << "The Coin Sorter made " << numRollsToonies << " rolls of toonies." << endl;
  958.     cout << "    There are " << numTWONIES << " toonies in the partially full roll." << endl;
  959.     cout << "The Coin Sorter processed " << totalOtherCoinsNum << " other coins." << endl;
  960.  
  961.     cout << "    The other coins completely filled " << numOtherContainer << " containers" << endl;
  962.  
  963.     cout << "    There were " << numOTHER << " other coins in the partially full container" << endl;
  964.     cout << "    The total weight of the other coins was " << fixed << setprecision(3) << otherWEIGHT << " grams" << endl;
  965.     cout << "The Coin Sorter processed " << fixed << setprecision(4) << totalBentCoinWeight << " g of bent coins" << endl;
  966.  
  967.    
  968.  
  969.     return 0;
  970.  
  971.  
  972. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement