Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- using namespace std;
- //declear constants
- const int maxtries = 2; //maximum number of tires
- const int minlines = 1; //minimum number of lines in the file
- const int maxlines = 5000; // maximum number of lines in the file
- //nickel max and min weight and diameter
- const double minNICKELweight = 3.60;
- const double maxNICKELweight = 4.30;
- const double minNICKELdiameter = 20.2;
- const double maxNICKELdiameter = 20.8;
- //dime max and min weight and diameter
- const double minDIMEweight = 1.30;
- const double maxDIMEweight = 2.20;
- const double minDIMEdiameter = 17.3;
- const double maxDIMEdiameter = 18.7;
- //quarter max and min weight and diameter
- const double minQUARTERweight = 4.00;
- const double maxQUARTERweight = 4.80;
- const double minQUARTERdiameter = 22.80;
- const double maxQUARTERdiameter = 24.60;
- //loonie max and min weight and diameter
- const double minLOONIEweight = 6.50;
- const double maxLOONIEweight = 7.50;
- const double minLOONIEdiameter = 25.00;
- const double maxLOONIEdiameter = 27.00;
- //toonie max and min weight and diameter
- const double minTWONIEweight = 6.75;
- const double maxTWONIEweight = 7.85;
- const double minTWONIEdiameter = 26.90;
- const double maxTWONIEdiameter = 29.10;
- //Coins per Role
- const int NICKELSPERROLE = 40;
- const int DIMESPERROLE = 50;
- const int QUARTERSPERROLE = 40;
- const int LOONIESPERROLE = 25;
- const int TOONIESPERROLE = 25;
- //container weight for bent and other coins
- const double maxBENTcontainerWEIGHT = 100.0;
- const double maxOTHERcontainerWEIGHT = 200.0;
- int main()
- {
- //Initialize and declear all Variables
- ifstream myinputstream;
- ofstream myoutputstream;
- char inputname[1024] = { '\0' };
- char outputname[1024] = { '\0' };
- int i = 0; //number of tries
- int numlines = 0; //number of lines in a file
- int NNN = 0; // count line
- int weight = 0;
- int diameter = 0;
- int nickle = 0;
- int twonies = 0;
- int loonies = 0;
- int dimes = 0;
- int quarters = 0;
- int numNICKEL = 0;
- int numTWONIES = 0;
- int numLOONIES = 0;
- int numDIMES = 0;
- int numQUARTERS = 0;
- int numOTHER = 0;
- int numRollsNickels = 0;
- int numRollsDimes = 0;
- int numRollsQuarter = 0;
- int numRollsLoonies = 0;
- int numRollsToonies = 0;
- int totalOtherCoinsNum = 0;
- int numOtherContainer = 0;
- int numBentContainer = 0;
- double coinWEIGHT = 0.0;
- double coinDIAMETER = 0.0;
- double otherWEIGHT = 0.0;
- double measuringDiameter = 0.0;
- double measuringWeight = 0.0;
- double weightBentContainer = 0.0;
- double totalBentCoinWeight = 0.0;
- double weightOtherContainer = 0.0;
- double totalOtherCoinWeight = 0.0;
- string weightstring; //saves weigth value in a string
- string diameterstring;
- string wholeline;
- string usable;
- string match;
- string extrastuff;
- cout.setf(ios::fixed);
- cout.setf(ios::showpoint);
- do
- {
- cout << "Type the name of the input file containing sensor readings:\n";
- cin >> inputname;
- myinputstream.open(inputname);
- if (myinputstream.fail())
- {
- cerr << "ERROR: file " << inputname << " could not be opened for input" << endl;
- i++;
- }
- if (i > maxtries)
- {
- cout << "ERROR: You exceeded maximum number of tries allowed \nwhile entering the input file name";
- return 1;
- }
- } while (myinputstream.fail() && i <= maxtries);
- i = 0; //reset i back to zero
- //error for outputfiles
- do
- {
- cout << "Type the name of the output file which will hold the simulation results: \n";
- cin >> outputname;
- myoutputstream.open(outputname, ios::in);
- if (myoutputstream.fail())
- {
- cerr << "ERROR: file " << outputname << " could not be opened for output" << endl;
- i++;
- }
- if (i > maxtries)
- {
- cerr << "ERROR: You exceeded maximum number of tries allowed \nwhile entering the output file name";
- return 1;
- }
- } while (myoutputstream.fail() && i <= maxtries);
- if (!(myinputstream >> numlines))
- {
- if (myinputstream.eof())
- {
- cerr << "ERROR: Input data file is empty" << endl;
- myinputstream.close();
- myoutputstream.close();
- return 1;
- }
- else
- {
- cerr << "ERROR: First piece of data in the file is not an integer\n";
- myinputstream.close();
- myoutputstream.close();
- return 1;
- }
- }
- else
- {
- if (numlines < minlines || numlines > maxlines)
- {
- cerr << "ERROR: The number of sensor readings is out of range" << endl;
- myinputstream.close();
- return 1;
- }
- else
- {
- while (!myinputstream.eof())
- {
- getline(myinputstream, wholeline, '\n'); //This begins processing information through the getfile function
- if (wholeline.empty()) // skip empty lines
- {
- continue;
- }
- NNN++; //Show the amount of lines
- //cout << wholeline << '\n'; //This line is used only to test and check for any errors in each line
- istringstream iss(wholeline);
- getline(iss, weightstring, ' '); //get the weight of the coin from the file
- stringstream weightstringtoint(weightstring);
- weightstringtoint >> weight; //Convert the string into an integer
- if (weightstring.empty()) // If there is not a weight value
- {
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation completed early before line " << NNN << " of input" << endl;
- break;
- }
- if (!weight) // If the first element is not an integer
- {
- cerr << "ERROR: Weight sensor value read on line " << NNN << " is not an integer" << endl;
- cerr << "Simulation terminated early: Please correct your data file" << endl;
- continue;
- }
- getline(iss, diameterstring, ' '); //gets the second element (diameter) from the input file
- stringstream diametertoint(diameterstring);
- diametertoint >> diameter; //This will convert the string into an integer
- if (diameterstring.empty()) // if ther is no diameter value
- {
- cerr << "ERROR: Weight sensor measurement only" << endl;
- cerr << "Ignoring line " << NNN << " of the input file" << endl;
- continue;
- }
- if (!diameter) //if the second element is not an integer
- {
- cerr << "ERROR: Diameter sensor value read on line " << NNN << " is not an integer" << endl;
- cerr << "Simulation terminated early: Please correct your data file" << endl;
- continue;
- }
- getline(iss, usable, ' '); // gets the third element (usable or bent) from the input file
- if (usable.empty()) // if thether is no thrid element
- {
- cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
- cerr << "Ignoring line " << NNN << " of the input file" << endl;
- continue;
- }
- if (usable != "bent" && usable != "usable") // if the third element is neither "bent" or "usable"
- {
- cerr << "ERROR: Result of test to determine if coin is bent at line " << NNN << " is invalid" << endl;
- cerr << "Ignoring this line of data" << endl;
- continue;
- }
- getline(iss, match, ' '); //gets the fourth element (if the coin matches the images of Canadian coin) from the file
- if (match.empty()) // if there is no fourth element
- {
- cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
- cerr << "Ignoring line " << NNN << " of the input file" << endl;
- continue;
- }
- if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch") //if the fourth element is neither "BothMatch", "OneMatch", or "NoMatch"
- {
- cerr << "ERROR: image processing result at line " << NNN << " is invalid" << endl;
- cerr << "Ignoring this line of data" << endl;
- continue;
- }
- getline(iss, extrastuff, ' '); //This will read if there is any fifth value on the line that we do not want, named extrastuff
- if (!extrastuff.empty())
- { //If there is a fifth value read on the line, it will run this error
- cerr << "ERROR: Extra data at line " << NNN << ". Ignoring extra data" << endl;
- extrastuff.clear(); //Resets the string value
- continue;
- }
- 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
- cerr << "ERROR: Sensor reading is out of range, ignoring line " << NNN << " in the input file" << endl;
- continue;
- }
- coinWEIGHT = static_cast<double>(weight) * 2 / 51;
- coinDIAMETER = static_cast<double>(diameter) * 30 / 255 + 10;
- cout.precision(2);
- if (usable == "bent")
- {
- weightBentContainer += coinWEIGHT;
- totalBentCoinWeight += coinWEIGHT;
- if (weightBentContainer > maxBENTcontainerWEIGHT)
- {
- weightBentContainer = 0;
- weightBentContainer += coinWEIGHT;
- cout << "This coin does not fit in the bent coin container" << endl;
- cout << "The bent coin container has been replaced" << endl;
- cout << "The coin in the new bent coin container weighs " << weightBentContainer << " grams" << endl;
- }
- cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
- cout << "The coins in the bent coin container now weighs " << weightBentContainer << " grams" << endl;
- }
- if (usable == "usable")
- {
- if (match == "BothMatch")
- {
- if ((coinWEIGHT >= minNICKELweight && coinWEIGHT <= maxNICKELweight) && (coinDIAMETER >= minNICKELdiameter && coinDIAMETER <= maxNICKELdiameter))
- {
- numNICKEL++;
- cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
- if (numNICKEL == NICKELSPERROLE)
- {
- numRollsNickels++;
- numNICKEL = 0;
- cout << "The nickel wrapper is now full" << endl;
- cout << "The nickel wrapper has now been replaced" << endl;
- continue;
- }
- else
- {
- cout << "There are now " << numNICKEL << " in the nickels wrapper" << endl;
- }
- }
- else if ((coinWEIGHT >= minDIMEweight && coinWEIGHT <= maxDIMEweight) && (coinDIAMETER >= minDIMEdiameter && coinDIAMETER <= maxDIMEdiameter))
- {
- numDIMES++;
- cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
- if (numDIMES == DIMESPERROLE)
- {
- numRollsDimes++;
- numDIMES = 0;
- cout << "The dime wrapper is now full" << endl;
- cout << "The dime wrapper has now been replaced" << endl;
- continue;
- }
- else
- {
- cout << "There are now " << numDIMES << " coins in the dimes wrapper" << endl;
- }
- }
- else if ((coinWEIGHT >= minQUARTERweight && coinWEIGHT <= maxQUARTERweight) && (coinDIAMETER >= minQUARTERdiameter && coinDIAMETER <= maxQUARTERdiameter))
- {
- numQUARTERS++;
- cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
- if (numQUARTERS == QUARTERSPERROLE)
- {
- numRollsQuarter++;
- numQUARTERS = 0;
- cout << "The quarter wrapper is now full" << endl;
- cout << "The quarter wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << numQUARTERS << " in the quarters wrapper" << endl;
- }
- }
- else if ((coinWEIGHT >= minLOONIEweight && coinWEIGHT <= maxLOONIEweight) && (coinDIAMETER >= minLOONIEdiameter && coinDIAMETER <= maxLOONIEdiameter))
- {
- numLOONIES++;
- cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
- if (numLOONIES == LOONIESPERROLE)
- {
- numRollsLoonies++;
- numLOONIES = 0;
- cout << "The loonie wrapper is now full" << endl;
- cout << "The loonie wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << numLOONIES << " in the loonies wrapper" << endl;
- }
- }
- else if ((coinWEIGHT >= minTWONIEweight && coinWEIGHT <= maxTWONIEweight) && (coinDIAMETER >= minTWONIEdiameter && coinDIAMETER <= maxTWONIEdiameter))
- {
- numTWONIES++;
- cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
- if (numTWONIES == TOONIESPERROLE)
- {
- numRollsToonies++;
- numTWONIES = 0;
- cout << "The toonie wrapper is now full" << endl;
- cout << "The toonie wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << numTWONIES << " in the toonies wrapper" << endl;
- }
- }
- else
- {
- totalOtherCoinsNum++;
- totalOtherCoinWeight += coinWEIGHT;
- weightOtherContainer += coinWEIGHT;
- if (weightOtherContainer >= maxOTHERcontainerWEIGHT)
- {
- numOTHER = 0;
- weightOtherContainer = 0;
- weightOtherContainer += coinWEIGHT;
- numOtherContainer++;
- numOTHER++;
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coin in the other coin container now weighs " << weightOtherContainer << " grams" << endl;
- }
- else
- {
- numOTHER++;
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coins in the other coin container now weigh " << weightOtherContainer << " grams" << endl;
- }
- }
- }
- if (match == "OneMatch" || match == "NoMatch")
- {
- totalOtherCoinsNum++;
- totalOtherCoinWeight += coinWEIGHT;
- weightOtherContainer += coinWEIGHT;
- if (weightOtherContainer >= maxOTHERcontainerWEIGHT)
- {
- numOTHER = 0;
- weightOtherContainer = 0;
- weightOtherContainer += coinWEIGHT;
- numOtherContainer++;
- numOTHER++;
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coin in the other coin container now weighs " << weightOtherContainer << " grams" << endl;
- }
- else
- {
- numOTHER++;
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coins in the other coin container now weigh " << weightOtherContainer << " grams" << endl;
- }
- }
- }
- }
- 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
- ++NNN;
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation completed early before line " << NNN << " of input" << endl << endl << endl;
- }
- }
- }
- cout << endl << endl << endl;
- cout << "SUMMARY" << endl;
- cout << "The Coin Sorter made " << numRollsNickels << " rolls of nickels." << endl;
- cout << " There are " << numNICKEL << " nickels in the partially full roll." << endl;
- cout << "The Coin Sorter made " << numRollsDimes << " rolls of dimes." << endl;
- cout << " There are " << numDIMES << " dimes in the partially full roll." << endl;
- cout << "The Coin Sorter made " << numRollsQuarter << " rolls of quarters." << endl;
- cout << " There are " << numQUARTERS << " quarters in the partially full roll." << endl;
- cout << "The Coin Sorter made " << numRollsLoonies << " rolls of loonies." << endl;
- cout << " There are " << numLOONIES << " loonies in the partially full roll." << endl;
- cout << "The Coin Sorter made " << numRollsToonies << " rolls of toonies." << endl;
- cout << " There are " << numTWONIES << " toonies in the partially full roll." << endl;
- cout << "The Coin Sorter processed " << totalOtherCoinsNum << " other coins." << endl;
- cout << " The other coins completely filled " << numOtherContainer << " containers" << endl;
- cout << " There were " << numOTHER << " other coins in the partially full container" << endl;
- cout << " The total weight of the other coins was " << fixed << setprecision(3) << totalOtherCoinWeight << " grams" << endl;
- cout << "The Coin Sorter processed " << fixed << setprecision(4) << totalBentCoinWeight << " g of bent coins" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement