Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.10 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. //declear constants
  12. const int maxtries = 2; //maximum number of tires
  13. const int minlines = 1; //minimum number of lines in the file
  14. const int maxlines = 5000; // maximum number of lines in the file
  15. //nickel max and min weight and diameter
  16. const double minNICKELweight = 3.60;
  17. const double maxNICKELweight = 4.30;
  18. const double minNICKELdiameter = 20.2;
  19. const double maxNICKELdiameter = 20.8;
  20. //dime max and min weight and diameter
  21. const double minDIMEweight = 1.30;
  22. const double maxDIMEweight = 2.20;
  23. const double minDIMEdiameter = 17.3;
  24. const double maxDIMEdiameter = 18.7;
  25. //quarter max and min weight and diameter
  26. const double minQUARTERweight = 4.00;
  27. const double maxQUARTERweight = 4.80;
  28. const double minQUARTERdiameter = 22.80;
  29. const double maxQUARTERdiameter = 24.60;
  30. //loonie max and min weight and diameter
  31. const double minLOONIEweight = 6.50;
  32. const double maxLOONIEweight = 7.50;
  33. const double minLOONIEdiameter = 25.00;
  34. const double maxLOONIEdiameter = 27.00;
  35. //toonie max and min weight and diameter
  36. const double minTWONIEweight = 6.75;
  37. const double maxTWONIEweight = 7.85;
  38. const double minTWONIEdiameter = 26.90;
  39. const double maxTWONIEdiameter = 29.10;
  40. //Coins per Role
  41. const int NICKELSPERROLE = 40;
  42. const int DIMESPERROLE = 50;
  43. const int QUARTERSPERROLE = 40;
  44. const int LOONIESPERROLE = 25;
  45. const int TOONIESPERROLE = 25;
  46. //container weight for bent and other coins
  47. const double maxBENTcontainerWEIGHT = 100.0;
  48. const double maxOTHERcontainerWEIGHT = 200.0;
  49.  
  50. int main()
  51. {
  52.    
  53.     //Initialize and declear all Variables
  54.  
  55.     ifstream myinputstream;
  56.     ofstream myoutputstream;
  57.     char inputname[1024] = { '\0' };
  58.     char outputname[1024] = { '\0' };
  59.  
  60.     int i = 0;            //number of tries
  61.     int numlines = 0;     //number of lines in a file
  62.     int NNN = 0;          // count line
  63.     int weight = 0;  
  64.     int diameter = 0;
  65.     int nickle = 0;
  66.     int twonies = 0;
  67.     int loonies = 0;
  68.     int dimes = 0;
  69.     int quarters = 0;
  70.  
  71.     int numNICKEL = 0;
  72.     int numTWONIES = 0;
  73.     int numLOONIES = 0;
  74.     int numDIMES = 0;
  75.     int numQUARTERS = 0;
  76.     int numOTHER = 0;
  77.     int numRollsNickels = 0;
  78.     int numRollsDimes = 0;
  79.     int numRollsQuarter = 0;
  80.     int numRollsLoonies = 0;
  81.     int numRollsToonies = 0;
  82.     int totalOtherCoinsNum = 0;
  83.     int numOtherContainer = 0;
  84.     int numBentContainer = 0;
  85.  
  86.     double coinWEIGHT = 0.0;
  87.     double coinDIAMETER = 0.0;
  88.     double otherWEIGHT = 0.0;
  89.  
  90.     double measuringDiameter = 0.0;
  91.     double measuringWeight = 0.0;
  92.     double weightBentContainer = 0.0;
  93.     double totalBentCoinWeight = 0.0;
  94.     double weightOtherContainer = 0.0;
  95.     double totalOtherCoinWeight = 0.0;
  96.  
  97.     string weightstring;     //saves weigth value in a string
  98.     string diameterstring;
  99.     string wholeline;
  100.     string usable;
  101.     string match;
  102.     string extrastuff;
  103.  
  104.  
  105.     cout.setf(ios::fixed);
  106.     cout.setf(ios::showpoint);
  107.  
  108.    
  109.     do
  110.     {
  111.         cout << "Type the name of the input file containing sensor readings:\n";
  112.         cin >> inputname;
  113.         myinputstream.open(inputname);
  114.        
  115.         if (myinputstream.fail())
  116.         {
  117.             cerr << "ERROR: file " << inputname << " could not be opened for input" << endl;
  118.  
  119.             i++;
  120.         }
  121.         if (i > maxtries)
  122.         {
  123.             cout << "ERROR: You exceeded maximum number of tries allowed \nwhile entering the input file name";
  124.             return 1;
  125.         }
  126.  
  127.     } while (myinputstream.fail() && i <= maxtries);
  128.  
  129.  
  130.     i = 0;   //reset i back to zero
  131.  
  132.     //error for outputfiles
  133.     do
  134.     {
  135.         cout << "Type the name of the output file which will hold the simulation results: \n";
  136.         cin >> outputname;
  137.         myoutputstream.open(outputname, ios::in);
  138.  
  139.         if (myoutputstream.fail())
  140.         {
  141.             cerr << "ERROR: file " << outputname << " could not be opened for output" << endl;
  142.  
  143.             i++;
  144.         }
  145.         if (i > maxtries)
  146.         {
  147.             cerr << "ERROR: You exceeded maximum number of tries allowed \nwhile entering the output file name";
  148.             return 1;
  149.         }
  150.  
  151.  
  152.     } while (myoutputstream.fail() && i <= maxtries);
  153.  
  154.  
  155.     if (!(myinputstream >> numlines))
  156.     {
  157.         if (myinputstream.eof())
  158.         {
  159.             cerr << "ERROR: Input data file is empty" << endl;
  160.             myinputstream.close();
  161.             myoutputstream.close();
  162.             return 1;
  163.         }
  164.         else
  165.         {
  166.             cerr << "ERROR: First piece of data in the file is not an integer\n";
  167.             myinputstream.close();
  168.             myoutputstream.close();
  169.             return 1;
  170.         }
  171.     }
  172.     else
  173.     {
  174.  
  175.         if (numlines < minlines || numlines > maxlines)
  176.         {
  177.             cerr << "ERROR: The number of sensor readings is out of range" << endl;
  178.             myinputstream.close();
  179.             return 1;
  180.         }
  181.         else
  182.         {
  183.             while (!myinputstream.eof())
  184.             {    
  185.  
  186.                 getline(myinputstream, wholeline, '\n'); //This begins processing information through the getfile function
  187.  
  188.                 if (wholeline.empty()) // skip empty lines
  189.                 {
  190.                     continue;
  191.                 }
  192.                 NNN++; //Show the amount of lines
  193.  
  194.                 //cout << wholeline << '\n'; //This line is used only to test and check for any errors in each line
  195.  
  196.                 istringstream iss(wholeline);
  197.  
  198.                 getline(iss, weightstring, ' '); //get the weight of the coin from the file
  199.  
  200.                 stringstream weightstringtoint(weightstring);
  201.                 weightstringtoint >> weight; //Convert the string into an integer
  202.  
  203.                 if (weightstring.empty()) // If there is not a weight value
  204.                 {
  205.                     cerr << "ERROR: No more data" << endl;
  206.                     cerr << "Simulation completed early before line " << NNN << " of input" << endl;
  207.                     break;
  208.                 }
  209.  
  210.                 if (!weight) // If the first element is not an integer
  211.                 {
  212.                     cerr << "ERROR: Weight sensor value read on line " << NNN << " is not an integer" << endl;
  213.                     cerr << "Simulation terminated early: Please correct your data file" << endl;
  214.                     continue;
  215.  
  216.                 }
  217.  
  218.                 getline(iss, diameterstring, ' ');        //gets the second element (diameter) from the input file
  219.                 stringstream diametertoint(diameterstring);
  220.                 diametertoint >> diameter;                //This will convert the string into an integer
  221.                 if (diameterstring.empty())               // if ther is no diameter value
  222.                 {
  223.                     cerr << "ERROR: Weight sensor measurement only" << endl;
  224.                     cerr << "Ignoring line " << NNN << " of the input file" << endl;
  225.                     continue;
  226.                 }
  227.  
  228.                 if (!diameter) //if the second element is not an integer
  229.                 {
  230.                     cerr << "ERROR: Diameter sensor value read on line " << NNN << " is not an integer" << endl;
  231.                     cerr << "Simulation terminated early: Please correct your data file" << endl;
  232.                     continue;
  233.  
  234.                 }
  235.  
  236.                 getline(iss, usable, ' '); // gets the third element (usable or bent) from the input file
  237.                
  238.                 if (usable.empty()) // if thether is no thrid element
  239.                 {
  240.                     cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
  241.                     cerr << "Ignoring line " << NNN << " of the input file" << endl;
  242.                     continue;
  243.                 }
  244.  
  245.  
  246.                 if (usable != "bent" && usable != "usable") // if the third element is neither "bent" or "usable"
  247.                 {
  248.                     cerr << "ERROR: Result of test to determine if coin is bent at line " << NNN << " is invalid" << endl;
  249.                     cerr << "Ignoring this line of data" << endl;
  250.                     continue;
  251.                 }
  252.  
  253.                 getline(iss, match, ' ');     //gets the fourth element (if the coin matches the images of Canadian coin) from the file
  254.                
  255.  
  256.                 if (match.empty())    // if there is no fourth element
  257.                 {
  258.                     cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
  259.                     cerr << "Ignoring line " << NNN << " of the input file" << endl;
  260.                     continue;
  261.                 }
  262.                 if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch") //if the fourth element is neither "BothMatch", "OneMatch", or "NoMatch"
  263.                 {
  264.                     cerr << "ERROR: image processing result at line " << NNN << " is invalid" << endl;
  265.                     cerr << "Ignoring this line of data" << endl;
  266.                     continue;
  267.  
  268.                 }
  269.  
  270.                 getline(iss, extrastuff, ' '); //This will read if there is any fifth value on the line that we do not want, named extrastuff
  271.  
  272.                 if (!extrastuff.empty())
  273.                 { //If there is a fifth value read on the line, it will run this error
  274.                     cerr << "ERROR: Extra data at line " << NNN << ". Ignoring extra data" << endl;
  275.                     extrastuff.clear(); //Resets the string value
  276.                     continue;
  277.  
  278.                 }
  279.  
  280.                 if (weight < 0 || weight > 255 || diameter < 0 || diameter > 255)
  281.                 { //If either the weight or diameter are outside of the sensor reading ( 0 <= weight/diameter <= 255), it will run this error
  282.                     cerr << "ERROR: Sensor reading is out of range, ignoring line " << NNN << " in the input file" << endl;
  283.                     continue;
  284.                 }
  285.  
  286.                 coinWEIGHT = static_cast<double>(weight) * 2 / 51;
  287.                 coinDIAMETER = static_cast<double>(diameter) * 30 / 255 + 10;
  288.                 cout.precision(2);
  289.  
  290.  
  291.                 if (usable == "bent")
  292.                 {
  293.                     weightBentContainer += coinWEIGHT;
  294.                     totalBentCoinWeight += coinWEIGHT;
  295.  
  296.                     if (weightBentContainer > maxBENTcontainerWEIGHT)
  297.                     {
  298.                         weightBentContainer = 0;
  299.                         weightBentContainer += coinWEIGHT;
  300.  
  301.                         cout << "This coin does not fit in the bent coin container" << endl;
  302.                         cout << "The bent coin container has been replaced" << endl;
  303.                         cout << "The coin in the new bent coin container weighs " << weightBentContainer << " grams" << endl;
  304.                     }
  305.  
  306.                     cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  307.                     cout << "The coins in the bent coin container now weighs " << weightBentContainer << " grams" << endl;
  308.  
  309.                 }
  310.                 if (usable == "usable")
  311.                 {
  312.                     if (match == "BothMatch")
  313.                     {
  314.                         if ((coinWEIGHT >= minNICKELweight && coinWEIGHT <= maxNICKELweight) && (coinDIAMETER >= minNICKELdiameter && coinDIAMETER <= maxNICKELdiameter))
  315.                         {
  316.                        
  317.                             numNICKEL++;
  318.                             cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
  319.                             if (numNICKEL == NICKELSPERROLE)
  320.                             {
  321.                                 numRollsNickels++;
  322.                                 numNICKEL = 0;
  323.                                 cout << "The nickel wrapper is now full" << endl;
  324.                                 cout << "The nickel wrapper has now been replaced" << endl;
  325.  
  326.                                 continue;
  327.                             }
  328.                             else
  329.                             {
  330.                                 cout << "There are now " << numNICKEL << " in the nickels wrapper" << endl;
  331.                             }
  332.  
  333.                         }
  334.                         else if ((coinWEIGHT >= minDIMEweight && coinWEIGHT <= maxDIMEweight) && (coinDIAMETER >= minDIMEdiameter && coinDIAMETER <= maxDIMEdiameter))
  335.                         {
  336.                             numDIMES++;
  337.                             cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
  338.                             if (numDIMES == DIMESPERROLE)
  339.                             {
  340.                                 numRollsDimes++;
  341.                                 numDIMES = 0;
  342.                                 cout << "The dime wrapper is now full" << endl;
  343.                                 cout << "The dime wrapper has now been replaced" << endl;
  344.                                 continue;
  345.                             }
  346.                             else
  347.                             {
  348.                                 cout << "There are now " << numDIMES << " coins in the dimes wrapper" << endl;
  349.  
  350.                             }
  351.  
  352.                         }
  353.                         else if ((coinWEIGHT >= minQUARTERweight && coinWEIGHT <= maxQUARTERweight) && (coinDIAMETER >= minQUARTERdiameter && coinDIAMETER <= maxQUARTERdiameter))
  354.                         {
  355.                             numQUARTERS++;
  356.                             cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
  357.                             if (numQUARTERS == QUARTERSPERROLE)
  358.                             {
  359.                                 numRollsQuarter++;
  360.                                 numQUARTERS = 0;
  361.  
  362.                                 cout << "The quarter wrapper is now full" << endl;
  363.                                 cout << "The quarter wrapper has now been replaced" << endl;
  364.  
  365.  
  366.                             }
  367.                             else
  368.                             {
  369.                                 cout << "There are now " << numQUARTERS << " in the quarters wrapper" << endl;
  370.                             }
  371.                         }
  372.                         else if ((coinWEIGHT >= minLOONIEweight && coinWEIGHT <= maxLOONIEweight) && (coinDIAMETER >= minLOONIEdiameter && coinDIAMETER <= maxLOONIEdiameter))
  373.                         {
  374.                             numLOONIES++;
  375.                             cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
  376.                             if (numLOONIES == LOONIESPERROLE)
  377.                             {
  378.                                 numRollsLoonies++;
  379.                                 numLOONIES = 0;
  380.  
  381.                                 cout << "The loonie wrapper is now full" << endl;
  382.                                 cout << "The loonie wrapper has now been replaced" << endl;
  383.  
  384.  
  385.                             }
  386.                             else
  387.                             {
  388.                                 cout << "There are now " << numLOONIES << " in the loonies wrapper" << endl;
  389.                             }
  390.                         }
  391.                         else if ((coinWEIGHT >= minTWONIEweight && coinWEIGHT <= maxTWONIEweight) && (coinDIAMETER >= minTWONIEdiameter && coinDIAMETER <= maxTWONIEdiameter))
  392.                         {
  393.                             numTWONIES++;
  394.                             cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
  395.                             if (numTWONIES == TOONIESPERROLE)
  396.                             {
  397.                                 numRollsToonies++;
  398.                                 numTWONIES = 0;
  399.  
  400.                                 cout << "The toonie wrapper is now full" << endl;
  401.                                 cout << "The toonie wrapper has now been replaced" << endl;
  402.  
  403.                             }
  404.                             else
  405.                             {
  406.                                 cout << "There are now " << numTWONIES << " in the toonies wrapper" << endl;
  407.                             }
  408.                         }
  409.  
  410.                         else
  411.                         {
  412.                             totalOtherCoinsNum++;
  413.                             totalOtherCoinWeight += coinWEIGHT;
  414.                             weightOtherContainer += coinWEIGHT;
  415.                            
  416.                             if (weightOtherContainer >= maxOTHERcontainerWEIGHT)
  417.                             {
  418.                                
  419.                                 numOTHER = 0;
  420.                                 weightOtherContainer = 0;
  421.                                 weightOtherContainer += coinWEIGHT;
  422.                                 numOtherContainer++;
  423.                                 numOTHER++;
  424.                                
  425.  
  426.                                 cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  427.                                 cout << "The coin in the other coin container now weighs " << weightOtherContainer << " grams" << endl;
  428.                             }
  429.                             else
  430.                             {
  431.                                 numOTHER++;
  432.                                 cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  433.                                 cout << "The coins in the other coin container now weigh " << weightOtherContainer << " grams" << endl;
  434.                             }
  435.                         }
  436.                     }
  437.                     if (match == "OneMatch" || match == "NoMatch")
  438.                     {
  439.                         totalOtherCoinsNum++;
  440.                         totalOtherCoinWeight += coinWEIGHT;
  441.                         weightOtherContainer += coinWEIGHT;
  442.  
  443.                         if (weightOtherContainer >= maxOTHERcontainerWEIGHT)
  444.                         {
  445.                             numOTHER = 0;
  446.                             weightOtherContainer = 0;
  447.                             weightOtherContainer += coinWEIGHT;
  448.                             numOtherContainer++;
  449.                             numOTHER++;
  450.  
  451.                             cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  452.                             cout << "The coin in the other coin container now weighs " << weightOtherContainer << " grams" << endl;
  453.                         }
  454.                         else
  455.                         {
  456.                            
  457.                             numOTHER++;
  458.                             cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  459.                             cout << "The coins in the other coin container now weigh " << weightOtherContainer << " grams" << endl;
  460.                         }
  461.  
  462.  
  463.                     }
  464.  
  465.                 }
  466.  
  467.             }
  468.             if (wholeline.empty()) { //Once the file is read through completely, it will display this error, as well as the amount of lines in the text file
  469.                 ++NNN;
  470.                 cerr << "ERROR: No more data" << endl;
  471.                 cerr << "Simulation completed early before line " << NNN << " of input" << endl << endl << endl;
  472.  
  473.             }
  474.  
  475.         }
  476.     }
  477.  
  478.  
  479.     cout << endl << endl << endl;
  480.     cout << "SUMMARY" << endl;
  481.     cout << "The Coin Sorter made " << numRollsNickels << " rolls of nickels." << endl;
  482.     cout << "    There are " << numNICKEL << " nickels in the partially full roll." << endl;
  483.     cout << "The Coin Sorter made " << numRollsDimes << " rolls of dimes." << endl;
  484.     cout << "    There are " << numDIMES << " dimes in the partially full roll." << endl;
  485.     cout << "The Coin Sorter made " << numRollsQuarter << " rolls of quarters." << endl;
  486.     cout << "    There are " << numQUARTERS << " quarters in the partially full roll." << endl;
  487.     cout << "The Coin Sorter made " << numRollsLoonies << " rolls of loonies." << endl;
  488.     cout << "    There are " << numLOONIES << " loonies in the partially full roll." << endl;
  489.     cout << "The Coin Sorter made " << numRollsToonies << " rolls of toonies." << endl;
  490.     cout << "    There are " << numTWONIES << " toonies in the partially full roll." << endl;
  491.     cout << "The Coin Sorter processed " << totalOtherCoinsNum << " other coins." << endl;
  492.     cout << "    The other coins completely filled " << numOtherContainer << " containers" << endl;
  493.     cout << "    There were " << numOTHER << " other coins in the partially full container" << endl;
  494.     cout << "    The total weight of the other coins was " << fixed << setprecision(3) << totalOtherCoinWeight << " grams" << endl;
  495.     cout << "The Coin Sorter processed " << fixed << setprecision(4) << totalBentCoinWeight << " g of bent coins" << endl;
  496.  
  497.    
  498.  
  499.     return 0;
  500.  
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement