Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- #include <sstream>
- #include <vector>
- #include <stdexcept>
- using namespace std;
- int main(int argc, char** argv)
- {
- ifstream inFile;
- ofstream outFile;
- char inputName[1024] = { '\0' };
- char outputName[1024] = { '\0' };
- unsigned int nline = 0;
- int count = 0;
- int tries = 0;
- int firstint = 0;
- int weight = 0;
- int diameter = 0;
- int sensor_num = 0;
- string bentorusable;
- string imagefaces;
- int lines = 0;
- string noBlank;
- tries = 0;
- string name;
- string nextline;
- string wholeline = "";
- //coins
- int nickle = 0;
- int dimes = 0;
- int quarters = 0;
- int loonies = 0;
- int twonies = 0;
- int rolls_of_nickels = 0;
- int rolls_of_dimes = 0;
- int rolls_of_quarters = 0;
- int rolls_of_loonies = 0;
- int rolls_of_toonies = 0;
- int nickel_amount = 0;
- int dimes_amount = 0;
- int quarters_amount = 0;
- int loonies_amount = 0;
- int toonies_amount = 0;
- //nickel max and min weight and diameter
- const double min_nickel_weight = 3.60;
- const double max_nickel_weight = 4.30;
- const double min_nickel_diameter = 20.2;
- const double max_nickel_diameter = 20.8;
- //dime max and min weight and diameter
- const double min_dime_weight = 1.30;
- const double max_dime_weight = 2.20;
- const double min_dime_diameter = 17.3;
- const double max_dime_diameter = 18.7;
- //quarter max and min weight and diameter
- const double min_quarter_weight = 4.00;
- const double max_quarter_weight = 4.80;
- const double min_quarter_diameter = 22.80;
- const double max_quarter_diameter = 24.60;
- //loonie max and min weight and diameter
- const double min_loonie_weight = 6.50;
- const double max_loonie_weight = 7.50;
- const double min_loonie_diameter = 25.00;
- const double max_loonie_diameter = 27.00;
- //toonie max and min weight and diameter
- const double min_toonie_weight = 6.75;
- const double max_toonie_weight = 7.85;
- const double min_toonie_diameter = 26.90;
- const double max_toonie_diameter = 29.10;
- //coins per roll
- const int nickels_per_roll = 40;
- const int dimes_per_roll = 50;
- const int quarters_per_roll = 40;
- const int loonies_per_roll = 25;
- const int toonies_per_roll = 25;
- //other
- double weight_coin = 0.0;
- double diameter_coin = 0.0;
- double other_coin_weight_total = 0.0;
- double other_coin_num_total = 0.0;
- int container_amount = 0.0;
- double max_othercontainter_weight = 0.0;
- double weight_other_container = 0.0;
- double bent_container_weight = 0.0;
- double bent_coin_weight_total = 0.0;
- const double max_bent_container_weight = 200.0;
- int bent_container_amount = 0;
- double other_amount = 0.0;
- double other_weight = 0.0;
- //Enter inputName of the file
- do
- {
- cout << "Type the name of the input file containing sensor readings: " << endl;
- cin >> inputName;
- inFile.open(inputName);
- if (!inFile.is_open()) //Checks if the file is not open
- {
- cerr << "ERROR: File " << inputName << " could not be opened for input" << endl;
- tries++;
- }
- if (tries >= 3)
- {
- cerr << "ERROR: You exceeded maximum number of tries allowed" << endl;
- cerr << "while entering the input file name";
- inFile.close();
- }
- } while (!inFile.is_open() && tries < 3);
- tries = 0; //Resets tries to 0
- //Enter the outputName of the file
- do
- {
- cout << "Type the name of the output file which will hold the simulation results" << endl;
- cin >> outputName;
- outFile.open(outputName);
- if (!outFile.is_open()) //Checks if the file is not open
- {
- cerr << "ERROR: File " << outputName << " could not be opened for output" << endl;
- tries++;
- }
- } while (outFile.fail() && tries < 3);
- if (tries >= 3)
- {
- cerr << "ERROR: You exceeded maximum number of tries allowed" << endl;
- cerr << "while entering the output file name" << endl;
- return 1;
- }
- if (!(inFile >> lines))
- {
- nline++;
- if (inFile.eof())
- {
- cerr << "ERROR: Input data file is empty" << endl;
- inFile.close();
- outFile.close();
- return 1;
- }
- else
- {
- cerr << "ERROR: First piece of data in the file is not an integer" << endl;
- inFile.close();
- outFile.close();
- return 1;
- }
- }
- if (lines <= 0 || lines > 5000)
- {
- cerr << "ERROR: The number of sensor readings is out of range" << endl;
- inFile.close();
- outFile.close();
- return 1;
- }
- std::string input = " ";
- std::string slot;
- while (std::getline(inFile, input))
- {
- std::vector<std::string> vec;
- std::stringstream ss(input);
- while (ss >> slot)
- {
- vec.push_back(slot);
- }
- switch (vec.size())
- {
- case 0:
- {
- if (inFile.eof())
- {
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation completed early before line " << nline << " of input" << endl;
- break;
- }
- else
- {
- continue;
- }
- }
- break;
- case 1:
- {
- int elem = stoi(vec.at(0));
- try
- {
- int elem = stoi(vec.at(0));
- }
- catch (...)
- {
- cerr << "ERROR: Weight sensor value read on line " << nline << " is not an integer" << endl;
- cerr << "Simulation terminated early : Please correct your data file" << endl;
- nline++;
- }
- if (!(elem <= 255 && elem >= 0))
- {
- cerr << "ERROR: Sensor reading out of range, ignoring line " << nline << " in the input file" << endl;
- }
- else
- {
- cout << "ERROR: Weight sensor measurement only" << endl;
- cout << "ERROR: Ignore line " << nline << " of the input file" << endl;
- nline++;
- }
- } break;
- case 2:
- {
- int second_elem = std::stoi(vec.at(1));
- try
- {
- int second_elem = std::stoi(vec.at(1));
- }
- catch (...)
- {
- cerr << "ERROR: Diameter sensor value read on line " << nline << " is not an integer" << endl;
- cerr << "Simulation terminated early : Please correct your data file" << endl;
- nline++;
- }
- if (!(second_elem <= 255 && second_elem >= 0))
- {
- cerr << "ERROR: Sensor reading out of range, ignoring line " << nline << " in the input file" << endl;
- nline++;
- }
- else
- {
- cerr << "ERROR: Weight and diameter sensor measurements only" << endl;
- cerr << "Ignoring line " << nline << " of the input file" << endl;
- nline++;
- }
- }
- break;
- case 3:
- {
- std::string third_elem = vec.at(2);
- if (third_elem != "bent" && third_elem != "usable")
- {
- cerr << "ERROR: Result of test to determine if coin is bent at line " << nline << " is invalid" << endl;
- cerr << "Ignoring this line of data" << endl;
- nline++;
- }
- else
- {
- cerr << "ERROR: Weight and diameter sensor measurements and bent string only" << endl;
- cerr << "Ignoring line " << nline << " of the input file" << endl;
- nline++;
- }
- }
- break;
- case 4:
- {
- string fourth_elem = vec.at(3);
- if (fourth_elem != "BothMatch" && fourth_elem != "OneMatch" && fourth_elem != "NoMatch")
- {
- cerr << "ERROR: image processing result at line " << nline << " is invalid" << endl;
- cerr << "Ignoring this line of data" << endl;
- nline++;
- }
- else
- {
- nline++;
- }
- } break;
- default:
- {
- cerr << "ERROR: Extra data at line " << nline << ". Ignoring extra data" << endl;
- nline++;
- continue;
- }
- break;
- }
- }
- }
- //other stuff
- /*if (vec.at(2) != "bent")
- {
- if (vec.at(3) == "BothMatch")
- {
- if (((weight_coin >= min_nickel_weight && weight_coin <= max_nickel_weight) && (diameter_coin >= min_nickel_diameter && diameter_coin <= max_nickel_diameter)))
- {
- nickel_amount++;
- cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl;
- if (nickel_amount == nickels_per_roll)
- {
- rolls_of_nickels++;
- nickel_amount = 0;
- cout << "The nickel wrapper is now full" << endl;
- cout << "The nickel wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << nickel_amount << " in the nickels wrapper" << endl;
- }
- }
- else if ((weight_coin >= min_dime_weight && weight_coin <= max_dime_weight) && (diameter_coin >= min_dime_diameter && diameter_coin <= max_dime_diameter))
- {
- dimes_amount++;
- cout << "The Coin Sorter has sent one coin to the dimes wrapper" << endl;
- if (dimes_amount == dimes_per_roll)
- {
- rolls_of_dimes++;
- dimes_amount = 0;
- cout << "The dime wrapper is now full" << endl;
- cout << "The dime wrapper has now been replaced" << endl;
- break;
- }
- else
- {
- cout << "There are now " << dimes_amount << " coins in the dimes wrapper" << endl;
- }
- }
- else if ((weight_coin >= min_quarter_weight && weight_coin <= max_quarter_weight) && (diameter_coin >= min_quarter_diameter && diameter_coin <= max_quarter_diameter))
- {
- quarters_amount++;
- cout << "The Coin Sorter has sent one coin to the quarters wrapper" << endl;
- if (quarters_amount == rolls_of_quarters)
- {
- rolls_of_quarters++;
- quarters_amount = 0;
- cout << "The quarter wrapper is now full" << endl;
- cout << "The quarter wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << quarters_amount << " in the quarters wrapper" << endl;
- }
- }
- else if ((weight_coin >= min_loonie_weight && weight_coin <= max_loonie_weight) && (diameter_coin >= min_loonie_diameter && diameter_coin <= max_loonie_diameter))
- {
- loonies_amount++;
- cout << "The Coin Sorter has sent one coin to the loonies wrapper" << endl;
- if (loonies_amount == rolls_of_loonies)
- {
- rolls_of_loonies++;
- loonies_amount = 0;
- cout << "The loonie wrapper is now full" << endl;
- cout << "The loonie wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << loonies_amount << " in the loonies wrapper" << endl;
- }
- }
- else if ((weight_coin >= min_toonie_weight && weight_coin <= max_toonie_weight) && (diameter_coin >= min_toonie_diameter && diameter_coin <= max_toonie_diameter))
- {
- toonies_amount++;
- cout << "The Coin Sorter has sent one coin to the toonies wrapper" << endl;
- if (toonies_amount == rolls_of_toonies)
- {
- rolls_of_toonies++;
- toonies_amount = 0;
- cout << "The toonie wrapper is now full" << endl;
- cout << "The toonie wrapper has now been replaced" << endl;
- }
- else
- {
- cout << "There are now " << toonies_amount << " in the toonies wrapper" << endl;
- }
- }
- }
- else
- {
- other_coin_weight_total += weight_coin;
- other_coin_num_total++;
- if (weight_other_container > max_othercontainter_weight)
- {
- weight_other_container = 0;
- weight_other_container += weight_coin;
- container_amount++;
- cout << "This coin does not fit in the other coin container" << endl;
- cout << "The other coin container has been replaced" << endl;
- cout << "The coin in the new other coin container weighs " << weight_other_container << " grams" << endl;
- }
- else
- {
- weight_other_container += weight_coin;
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coins in the other coin container now weigh " << weight_other_container << " grams" << endl;
- }
- }
- }
- else
- {
- bent_container_weight += weight_coin;
- bent_coin_weight_total += weight_coin;
- if (bent_container_weight > max_bent_container_weight)
- {
- bent_container_weight = 0;
- bent_container_weight += weight_coin;
- bent_container_amount++;
- 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 " << bent_container_weight << " grams" << endl;
- }
- else
- {
- cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
- cout << "The coins in the bent coin container now weighs " << bent_container_weight << " grams" << endl;
- }
- }
- }
- cout << "SUMMARY" << endl;
- cout << "The Coin Sorter made " << rolls_of_nickels << " rolls of nickels." << endl;
- cout << " There are " << nickel_amount << " nickels in the partially full roll." << endl;
- cout << "The Coin Sorter made " << rolls_of_dimes << " rolls of dimes." << endl;
- cout << " There are " << dimes_amount << " dimes in the partially full roll." << endl;
- cout << "The Coin Sorter made " << rolls_of_quarters << " rolls of quarters." << endl;
- cout << " There are " << quarters_amount << " quarters in the partially full roll." << endl;
- cout << "The Coin Sorter made " << rolls_of_loonies << " rolls of loonies." << endl;
- cout << " There are " << loonies_amount << " loonies in the partially full roll." << endl;
- cout << "The Coin Sorter made " << rolls_of_toonies << " rolls of toonies." << endl;
- cout << " There are " << toonies_amount << " toonies in the partially full roll." << endl;
- cout << "The Coin Sorter processed " << other_coin_num_total << " other coins." << endl;
- cout << " The other coins completely filled " << container_amount << " containers" << endl;
- cout << " There were " << other_amount << " other coins in the partially full container" << endl;
- cout << " The total weight of the other coins was " << fixed << setprecision(3) << other_weight << " grams" << endl;
- cout << "The Coin Sorter processed " << fixed << setprecision(4) << bent_coin_weight_total << " g of bent coins" << endl;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement