Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- #include <sstream>
- using namespace std;
- int main() {
- ifstream inputData;
- fstream outputData;
- char inputFileName[5000];
- char outputFileName[5000];
- int firstValue = 0;
- int weightSensorReading = 0;
- int diameterSensorReading = 0;
- string usability;
- string match;
- int numberOfLines = 0;
- int counter4 = 0;
- const double weightConstant = 0.039215676;
- const double diameterConstant = 0.117647059;
- double weight = 0;
- double diameter = 0;
- const double nickelWeight = 3.95;
- const double nickelWeightVAR = 0.35;
- const double nickelDiameter = 21;
- const double nickelDiameterVAR = 0.8;
- const double dimeWeight = 1.75;
- const double dimeWeightVAR = 0.45;
- const double dimeDiameter = 18;
- const double dimeDiameterVAR = 0.7;
- const double quarterWeight = 4.4;
- const double quarterWeightVAR = 0.4;
- const double quarterDiameter = 23.7;
- const double quarterDiameterVAR = 0.9;
- const double loonieWeight = 7;
- const double loonieWeightVAR = 0.5;
- const double loonieDiameter = 26;
- const double loonieDiameterVAR = 1;
- const double toonieWeight = 7.3;
- const double toonieWeightVAR = 0.55;
- const double toonieDiameter = 28;
- const double toonieDiameterVAR = 1.1;
- const int nickelPerRoll = 40;
- const int dimePerRoll = 50;
- const int quarterPerRoll = 40;
- const int looniePerRoll = 25;
- const int tooniePerRoll = 25;
- const int bentCoinWeight = 100;
- const int otherCoinWeight = 200;
- int nickelCount = 0;
- int dimeCount = 0;
- int quarterCount = 0;
- int loonieCount = 0;
- int toonieCount = 0;
- double bentWeightCount = 0;
- double otherWeightCount = 0;
- int nickelBagCount = 0;
- int dimeBagCount = 0;
- int quarterBagCount = 0;
- int loonieBagCount = 0;
- int toonieBagCount = 0;
- int bentBagCount = 0;
- int otherBagCount = 0;
- double totalOtherWeightCount = 0;
- int otherCoinCount = 0;
- int fullOtherCoin = 0;
- int fullOtherCoinCounter = 0;
- double totalBentWeightCount = 0;
- char ch;
- bool input = false;
- for (int counter = 0; counter < 3;) {
- if (counter < 3) {
- cout << "Type the name of the input file containing sensor readings: \n";
- cin >> inputFileName;
- inputData.open(inputFileName);
- if (inputData.fail()) {
- cerr << "ERROR: File " << inputFileName << " could not be opened for input";
- ++counter;
- }
- if (counter >= 3) {
- cerr << endl << "ERROR: You exceeded maximum number of tries allowed" << endl << "while entering the input file name";
- inputData.close();
- return(1);
- }
- if (!inputData.fail())
- {
- break;
- }
- }
- }
- for (int counter2 = 0; counter2 < 3;) {
- cout << "Type the name of the output file which will hold the simulation results: \n";
- cin >> outputFileName;
- outputData.open(outputFileName);
- if (outputData.fail()) {
- cerr << "ERROR: File " << outputFileName << " could not be opened for output";
- ++counter2;
- }
- if (counter2 >= 3) {
- cerr << endl << "ERROR: You exceeded maximum number of tries allowed" << endl << "while entering the output file name";
- outputData.close();
- return(2);
- }
- if (!outputData.fail()) {
- break;
- }
- }
- if (!(inputData >> firstValue)) {
- if (inputData.eof()) {
- cerr << "ERROR: Input data file is empty";
- inputData.close();
- return(3);
- }
- else {
- cerr << "ERROR: First piece of data in the file is not an integer";
- inputData.close();
- return(4);
- }
- }
- if (firstValue <= 0 || firstValue > 5000) {
- cerr << "ERROR: The number of sensor readings is out of range";
- inputData.close();
- return(5);
- }
- for (int counter3 = 1; counter3 < firstValue; counter3++) {
- if (!(inputData >> weightSensorReading)) {
- cerr << "ERROR: Weight sensor value read on line " << counter3 << " is not an integer" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- if (inputData.eof()) {
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation completed early before the line " << counter3 << " of input";
- break;
- }
- else {
- cerr << "ERROR: Weight sensor value read on line " << counter3 << " is not an integer" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- }
- break;
- }
- else {
- ch = inputData.get();
- ch = inputData.peek();
- while (ch == '\t' || ch == ' ') {
- ch = inputData.peek();
- if (ch == '\n' || inputData.eof()) {
- cerr << "ERROR: Weight sensor measurement only " << endl << "Ignoring line " << counter3 << " of the input line";
- break;
- input = true;
- }
- }
- if (input == true) {
- continue;
- }
- }
- if (!(inputData >> diameterSensorReading)) {
- cerr << "ERROR: Diameter sensor value read on line " << counter3 << " is not an integer" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- if (inputData.eof()) {
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- }
- else {
- cerr << "ERROR: Diameter sensor value read on line " << counter3 << " is not an integer" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- }
- break;
- }
- else {
- ch = inputData.get();
- ch = inputData.peek();
- while (ch == '\t' || ch == ' ') {
- ch = inputData.peek();
- if (ch == '\n' || inputData.eof()) {
- cerr << "ERROR: Weight and diameter sensor measurement only " << endl << "Ignoring line " << counter3 << " of the input line";
- break;
- input = true;
- }
- }
- if (input == true) {
- continue;
- }
- }
- if (!(inputData >> usability) && inputData.eof()) {
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- }
- if (usability != "bent" && usability != "usable") {
- cerr << "ERROR: Result of test to determine if coin is bent at line " << counter3 << " is invalid" << endl;
- cerr << "Ignoring this line of data";
- continue;
- }
- else {
- ch = inputData.get();
- ch = inputData.peek();
- while (ch == '\t' || ch == ' ') {
- ch = inputData.peek();
- if (ch == '\n' || inputData.eof()) {
- cerr << "ERROR: Weight and diameter sensor measurements and bent string only " << endl << "Ignoring line " << counter3 << " of the input line";
- break;
- input = true;
- }
- }
- if (input == true) {
- continue;
- }
- }
- if (!(inputData >> match) && inputData.eof()) {
- cerr << "ERROR: No more data" << endl;
- cerr << "Simulation terminated early: Please correct your data file";
- break;
- if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch") {
- cerr << "ERROR: image processing resullt at line " << counter3 << " is invalid" << endl << "Ignoring this line of data";
- continue;
- }
- else {
- while (ch == '\t' || ch == ' ') {
- ch = inputData.get();
- ch = inputData.peek();
- if (ch == !'\n' || inputData.eof()) {
- cerr << "ERROR: Extra data at line " << counter3 << ". Ignoring extra data";
- while (ch == !'\n' || inputData.eof()) {
- ch = inputData.peek();
- }
- break;
- input = true;
- }
- }
- if (input == true) {
- continue;
- }
- }
- }
- if (weightSensorReading >= 0 && weightSensorReading <= 255 && diameterSensorReading >= 0 && diameterSensorReading <= 255 ) {
- weight = weightConstant * weightSensorReading;
- diameter = diameterConstant * diameterSensorReading + 10;
- if (usability == "usable") {
- if (weight <= nickelWeight + nickelWeightVAR && weight >= nickelWeight - nickelWeightVAR &&
- diameter <= nickelDiameter + nickelDiameterVAR && diameter >= nickelDiameter - nickelDiameterVAR && match == "BothMatch") {
- nickelCount++;
- if (nickelCount <= nickelPerRoll) {
- cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "There are now ";
- cout << setw(2) << nickelCount << " coins in the nickels wrapper" << endl;
- outputData << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "There are now ";
- outputData << setw(2) << nickelCount << " coins in the nickels wrapper" << endl;
- }
- if (nickelCount > nickelPerRoll) {
- cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "The nickel wrapper is now full";
- cout << endl << "The nickel wrapper has now been replaced" << endl;
- outputData << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "The nickel wrapper is now full";
- outputData << endl << "The nickel wrapper has now been replaced" << endl;
- nickelCount = 0;
- nickelBagCount++;
- }
- }
- else if (weight <= dimeWeight + dimeWeightVAR && weight >= dimeWeight - dimeWeightVAR &&
- diameter <= dimeDiameter + dimeDiameterVAR && diameter >= dimeDiameter - dimeDiameterVAR && match == "BothMatch") {
- dimeCount++;
- if (dimeCount <= dimePerRoll) {
- cout << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "There are now ";
- cout << setw(2) << dimeCount << " coins in the dime wrapper" << endl;
- outputData << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "There are now ";
- outputData << setw(2) << dimeCount << " coins in the dime wrapper" << endl;
- }
- if (dimeCount > dimePerRoll) {
- cout << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "The dime wrapper is now full";
- cout << endl << "The dime wrapper has now been replaced" << endl;
- outputData << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "The dime wrapper is now full";
- outputData << endl << "The dime wrapper has now been replaced" << endl;
- dimeCount = 0;
- dimeBagCount++;
- }
- }
- else if (weight <= quarterWeight + quarterWeightVAR && weight >= quarterWeight - quarterWeightVAR &&
- diameter <= quarterDiameter + quarterDiameterVAR && diameter >= quarterDiameter - quarterDiameterVAR && match == "BothMatch") {
- quarterCount++;
- if (quarterCount <= quarterPerRoll) {
- cout << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "There are now ";
- cout << setw(2) << quarterCount << " coins in the quarter wrapper" << endl;
- outputData << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "There are now ";
- outputData << setw(2) << quarterCount << " coins in the quarter wrapper" << endl;
- }
- if (quarterCount > quarterPerRoll) {
- cout << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "The quarter wrapper is now full";
- cout << endl << "The quarter wrapper has now been replaced" << endl;
- outputData << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "The quarter wrapper is now full";
- outputData << endl << "The quarter wrapper has now been replaced" << endl;
- quarterCount = 0;
- quarterBagCount++;
- }
- }
- else if (weight <= loonieWeight + loonieWeightVAR && weight >= loonieWeight - loonieWeightVAR &&
- diameter <= loonieDiameter + loonieDiameterVAR && diameter >= loonieDiameter - loonieDiameterVAR && match == "BothMatch") {
- loonieCount++;
- if (loonieCount <= looniePerRoll) {
- cout << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "There are now ";
- cout << setw(2) << loonieCount << " coins in the loonie wrapper" << endl;
- outputData << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "There are now ";
- outputData << setw(2) << loonieCount << " coins in the loonie wrapper" << endl;
- }
- if (loonieCount > looniePerRoll) {
- cout << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "The loonie wrapper is now full";
- cout << endl << "The loonie wrapper has now been replaced" << endl;
- outputData << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "The loonie wrapper is now full";
- outputData << endl << "The loonie wrapper has now been replaced" << endl;
- loonieCount = 0;
- loonieBagCount++;
- }
- }
- else if (weight <= toonieWeight + toonieWeightVAR && weight >= toonieWeight - toonieWeightVAR &&
- diameter <= toonieDiameter + toonieDiameterVAR && diameter >= toonieDiameter - toonieDiameterVAR && match == "BothMatch") {
- toonieCount++;
- if (toonieCount <= tooniePerRoll) {
- cout << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "There are now ";
- cout << setw(2) << toonieCount << " coins in the toonie wrapper" << endl;
- outputData << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "There are now ";
- outputData << setw(2) << toonieCount << " coins in the toonie wrapper" << endl;
- }
- if (toonieCount > tooniePerRoll) {
- cout << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "The toonie wrapper is now full";
- cout << endl << "The toonie wrapper has now been replaced" << endl;
- outputData << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "The toonie wrapper is now full" << endl;
- outputData << endl << "The toonie wrapper has now been replaced" << endl;
- toonieCount = 0;
- toonieBagCount++;
- }
- }
- else {
- if (otherWeightCount + weight <= otherCoinWeight) {
- totalOtherWeightCount += weight;
- otherWeightCount += weight;
- otherCoinCount++;
- fullOtherCoinCounter++;
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams" << endl;
- outputData << "The Coin Sorter has sent this coin to the other coin container" << endl;
- outputData << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams" << endl;
- }
- if (otherWeightCount + weight > otherCoinWeight) {
- cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
- cout << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams." << endl;
- outputData << "The Coin Sorter has sent this coin to the other coin container" << endl;
- outputData << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams." << endl;
- otherCoinCount = 0;
- totalOtherWeightCount += weight;
- otherWeightCount = weight;
- otherBagCount++;
- fullOtherCoin += fullOtherCoinCounter;
- }
- }
- }
- if (usability == "bent") {
- if (bentWeightCount + weight < bentCoinWeight) {
- bentWeightCount += weight;
- cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
- cout << "The coins in the bent coin container now weigh " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
- outputData << "The Coin Sorter has sent this coin to the bent coin container" << endl;
- outputData << "The coins in the bent coin container now weigh " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
- totalBentWeightCount += weight;
- }
- if (bentWeightCount + weight > bentCoinWeight) {
- cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
- 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 " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
- outputData << "The Coin Sorter has sent this coin to the bent coin container" << endl;
- outputData << "This coin does not fit in the bent coin container" << endl;
- outputData << "The bent coin container has been replaced" << endl;
- outputData << "The coin in the new bent coin container weighs " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
- bentWeightCount = weight;
- bentBagCount++;
- totalBentWeightCount += weight;
- }
- }
- }
- }
- inputData.close();
- cout << endl << endl << endl;
- cout << "The Coin Sorter made " << setw(3) << nickelBagCount << " rolls of nickels" << endl;
- cout << "There are " << setw(2) << nickelCount << " nickels in the partially full roll" << endl;
- cout << "The Coin Sorter made " << setw(3) << dimeBagCount << " rolls of nickels" << endl;
- cout << "There are " << setw(2) << dimeCount << " dimes in the partially full roll" << endl;
- cout << "The Coin Sorter made " << setw(3) << quarterBagCount << " rolls of quarters" << endl;
- cout << "There are " << setw(2) << quarterCount << " quarters in the partially full roll" << endl;
- cout << "The Coin Sorter made " << setw(3) << loonieBagCount << " rolls of loonies" << endl;
- cout << "There are " << setw(2) << loonieCount << " loonies in the partially full roll" << endl;
- cout << "The Coin Sorter made " << setw(3) << toonieBagCount << " rolls of toonies" << endl;
- cout << "There are " << setw(2) << toonieCount << " toonies in the partially full roll" << endl;
- cout << "The Coin Sorter processed " << setw(5) << otherCoinCount << " other coins" << endl;
- cout << "The other coins completely filled " << setw(3) << otherBagCount << " containers" << endl;
- cout << "There were " << otherCoinCount << " other coins in the partially full container" << endl;
- cout << "The total weight of the other coins was " << setw(9) << setprecision(3) << totalOtherWeightCount << " grams" << endl;
- cout << "The Coin Sorter processed " << setw(11) << setprecision(4) << totalBentWeightCount << " g of bent coins" << endl;
- outputData << endl << endl << endl;
- outputData << "The Coin Sorter made " << setw(3) << nickelBagCount << " rolls of nickels" << endl;
- outputData << "There are " << setw(2) << nickelCount << " nickels in the partially full roll" << endl;
- outputData << "The Coin Sorter made " << setw(3) << dimeBagCount << " rolls of nickels" << endl;
- outputData << "There are " << setw(2) << dimeCount << " dimes in the partially full roll" << endl;
- outputData << "The Coin Sorter made " << setw(3) << quarterBagCount << " rolls of quarters" << endl;
- outputData << "There are " << setw(2) << quarterCount << " quarters in the partially full roll" << endl;
- outputData << "The Coin Sorter made " << setw(3) << loonieBagCount << " rolls of loonies" << endl;
- outputData << "There are " << setw(2) << loonieCount << " loonies in the partially full roll" << endl;
- outputData << "The Coin Sorter made " << setw(3) << toonieBagCount << " rolls of toonies" << endl;
- outputData << "There are " << setw(2) << toonieCount << " toonies in the partially full roll" << endl;
- outputData << "The Coin Sorter processed " << setw(5) << otherCoinCount << " other coins" << endl;
- outputData << "The other coins completely filled " << setw(3) << otherBagCount << " containers" << endl;
- outputData << "There were " << otherCoinCount << " other coins in the partially full container" << endl;
- outputData << "The total weight of the other coins was " << setw(9) << setprecision(3) << totalOtherWeightCount << " grams" << endl;
- outputData << "The Coin Sorter processed " << setw(11) << setprecision(4) << totalBentWeightCount << " g of bent coins" << endl;
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement