Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <sstream>
  6. using namespace std;
  7. int main() {
  8. ifstream inputData;
  9. fstream outputData;
  10. char inputFileName[5000];
  11. char outputFileName[5000];
  12. int firstValue = 0;
  13. int weightSensorReading = 0;
  14. int diameterSensorReading = 0;
  15. string usability;
  16. string match;
  17. int numberOfLines = 0;
  18. int counter4 = 0;
  19. const double weightConstant = 0.039215676;
  20. const double diameterConstant = 0.117647059;
  21. double weight = 0;
  22. double diameter = 0;
  23. const double nickelWeight = 3.95;
  24. const double nickelWeightVAR = 0.35;
  25. const double nickelDiameter = 21;
  26. const double nickelDiameterVAR = 0.8;
  27. const double dimeWeight = 1.75;
  28. const double dimeWeightVAR = 0.45;
  29. const double dimeDiameter = 18;
  30. const double dimeDiameterVAR = 0.7;
  31. const double quarterWeight = 4.4;
  32. const double quarterWeightVAR = 0.4;
  33. const double quarterDiameter = 23.7;
  34. const double quarterDiameterVAR = 0.9;
  35. const double loonieWeight = 7;
  36. const double loonieWeightVAR = 0.5;
  37. const double loonieDiameter = 26;
  38. const double loonieDiameterVAR = 1;
  39. const double toonieWeight = 7.3;
  40. const double toonieWeightVAR = 0.55;
  41. const double toonieDiameter = 28;
  42. const double toonieDiameterVAR = 1.1;
  43. const int nickelPerRoll = 40;
  44. const int dimePerRoll = 50;
  45. const int quarterPerRoll = 40;
  46. const int looniePerRoll = 25;
  47. const int tooniePerRoll = 25;
  48. const int bentCoinWeight = 100;
  49. const int otherCoinWeight = 200;
  50. int nickelCount = 0;
  51. int dimeCount = 0;
  52. int quarterCount = 0;
  53. int loonieCount = 0;
  54. int toonieCount = 0;
  55. double bentWeightCount = 0;
  56. double otherWeightCount = 0;
  57. int nickelBagCount = 0;
  58. int dimeBagCount = 0;
  59. int quarterBagCount = 0;
  60. int loonieBagCount = 0;
  61. int toonieBagCount = 0;
  62. int bentBagCount = 0;
  63. int otherBagCount = 0;
  64. double totalOtherWeightCount = 0;
  65. int otherCoinCount = 0;
  66. int fullOtherCoin = 0;
  67. int fullOtherCoinCounter = 0;
  68. double totalBentWeightCount = 0;
  69. char ch;
  70. bool input = false;
  71.  
  72. for (int counter = 0; counter < 3;) {
  73. if (counter < 3) {
  74. cout << "Type the name of the input file containing sensor readings: \n";
  75. cin >> inputFileName;
  76. inputData.open(inputFileName);
  77. if (inputData.fail()) {
  78. cerr << "ERROR: File " << inputFileName << " could not be opened for input";
  79. ++counter;
  80. }
  81. if (counter >= 3) {
  82. cerr << endl << "ERROR: You exceeded maximum number of tries allowed" << endl << "while entering the input file name";
  83. inputData.close();
  84. return(1);
  85. }
  86. if (!inputData.fail())
  87. {
  88. break;
  89. }
  90. }
  91. }
  92. for (int counter2 = 0; counter2 < 3;) {
  93. cout << "Type the name of the output file which will hold the simulation results: \n";
  94. cin >> outputFileName;
  95. outputData.open(outputFileName);
  96. if (outputData.fail()) {
  97. cerr << "ERROR: File " << outputFileName << " could not be opened for output";
  98. ++counter2;
  99. }
  100. if (counter2 >= 3) {
  101. cerr << endl << "ERROR: You exceeded maximum number of tries allowed" << endl << "while entering the output file name";
  102. outputData.close();
  103. return(2);
  104. }
  105. if (!outputData.fail()) {
  106. break;
  107. }
  108. }
  109. if (!(inputData >> firstValue)) {
  110. if (inputData.eof()) {
  111. cerr << "ERROR: Input data file is empty";
  112. inputData.close();
  113. return(3);
  114. }
  115. else {
  116. cerr << "ERROR: First piece of data in the file is not an integer";
  117. inputData.close();
  118. return(4);
  119. }
  120. }
  121. if (firstValue <= 0 || firstValue > 5000) {
  122. cerr << "ERROR: The number of sensor readings is out of range";
  123. inputData.close();
  124. return(5);
  125. }
  126.  
  127. for (int counter3 = 1; counter3 < firstValue; counter3++) {
  128. if (!(inputData >> weightSensorReading)) {
  129. cerr << "ERROR: Weight sensor value read on line " << counter3 << " is not an integer" << endl;
  130. cerr << "Simulation terminated early: Please correct your data file";
  131. break;
  132. if (inputData.eof()) {
  133. cerr << "ERROR: No more data" << endl;
  134. cerr << "Simulation completed early before the line " << counter3 << " of input";
  135. break;
  136. }
  137. else {
  138. cerr << "ERROR: Weight sensor value read on line " << counter3 << " is not an integer" << endl;
  139. cerr << "Simulation terminated early: Please correct your data file";
  140. break;
  141. }
  142. break;
  143. }
  144. else {
  145. ch = inputData.get();
  146. ch = inputData.peek();
  147. while (ch == '\t' || ch == ' ') {
  148. ch = inputData.peek();
  149. if (ch == '\n' || inputData.eof()) {
  150. cerr << "ERROR: Weight sensor measurement only " << endl << "Ignoring line " << counter3 << " of the input line";
  151. break;
  152. input = true;
  153. }
  154. }
  155. if (input == true) {
  156. continue;
  157. }
  158. }
  159. if (!(inputData >> diameterSensorReading)) {
  160. cerr << "ERROR: Diameter sensor value read on line " << counter3 << " is not an integer" << endl;
  161. cerr << "Simulation terminated early: Please correct your data file";
  162. break;
  163. if (inputData.eof()) {
  164. cerr << "ERROR: No more data" << endl;
  165. cerr << "Simulation terminated early: Please correct your data file";
  166. break;
  167. }
  168. else {
  169. cerr << "ERROR: Diameter sensor value read on line " << counter3 << " is not an integer" << endl;
  170. cerr << "Simulation terminated early: Please correct your data file";
  171. break;
  172. }
  173. break;
  174. }
  175. else {
  176. ch = inputData.get();
  177. ch = inputData.peek();
  178. while (ch == '\t' || ch == ' ') {
  179. ch = inputData.peek();
  180. if (ch == '\n' || inputData.eof()) {
  181. cerr << "ERROR: Weight and diameter sensor measurement only " << endl << "Ignoring line " << counter3 << " of the input line";
  182. break;
  183. input = true;
  184. }
  185. }
  186. if (input == true) {
  187. continue;
  188. }
  189. }
  190. if (!(inputData >> usability) && inputData.eof()) {
  191. cerr << "ERROR: No more data" << endl;
  192. cerr << "Simulation terminated early: Please correct your data file";
  193. break;
  194. }
  195. if (usability != "bent" && usability != "usable") {
  196. cerr << "ERROR: Result of test to determine if coin is bent at line " << counter3 << " is invalid" << endl;
  197. cerr << "Ignoring this line of data";
  198. continue;
  199. }
  200. else {
  201. ch = inputData.get();
  202. ch = inputData.peek();
  203. while (ch == '\t' || ch == ' ') {
  204. ch = inputData.peek();
  205. if (ch == '\n' || inputData.eof()) {
  206. cerr << "ERROR: Weight and diameter sensor measurements and bent string only " << endl << "Ignoring line " << counter3 << " of the input line";
  207. break;
  208. input = true;
  209. }
  210. }
  211. if (input == true) {
  212. continue;
  213. }
  214. }
  215.  
  216. if (!(inputData >> match) && inputData.eof()) {
  217. cerr << "ERROR: No more data" << endl;
  218. cerr << "Simulation terminated early: Please correct your data file";
  219. break;
  220. if (match != "BothMatch" && match != "OneMatch" && match != "NoMatch") {
  221. cerr << "ERROR: image processing resullt at line " << counter3 << " is invalid" << endl << "Ignoring this line of data";
  222. continue;
  223. }
  224. else {
  225. while (ch == '\t' || ch == ' ') {
  226. ch = inputData.get();
  227. ch = inputData.peek();
  228. if (ch == !'\n' || inputData.eof()) {
  229. cerr << "ERROR: Extra data at line " << counter3 << ". Ignoring extra data";
  230. while (ch == !'\n' || inputData.eof()) {
  231. ch = inputData.peek();
  232. }
  233. break;
  234. input = true;
  235. }
  236. }
  237. if (input == true) {
  238. continue;
  239. }
  240. }
  241. }
  242. if (weightSensorReading >= 0 && weightSensorReading <= 255 && diameterSensorReading >= 0 && diameterSensorReading <= 255 ) {
  243. weight = weightConstant * weightSensorReading;
  244. diameter = diameterConstant * diameterSensorReading + 10;
  245. if (usability == "usable") {
  246. if (weight <= nickelWeight + nickelWeightVAR && weight >= nickelWeight - nickelWeightVAR &&
  247. diameter <= nickelDiameter + nickelDiameterVAR && diameter >= nickelDiameter - nickelDiameterVAR && match == "BothMatch") {
  248. nickelCount++;
  249. if (nickelCount <= nickelPerRoll) {
  250. cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "There are now ";
  251. cout << setw(2) << nickelCount << " coins in the nickels wrapper" << endl;
  252. outputData << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "There are now ";
  253. outputData << setw(2) << nickelCount << " coins in the nickels wrapper" << endl;
  254. }
  255. if (nickelCount > nickelPerRoll) {
  256. cout << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "The nickel wrapper is now full";
  257. cout << endl << "The nickel wrapper has now been replaced" << endl;
  258. outputData << "The Coin Sorter has sent one coin to the nickels wrapper" << endl << "The nickel wrapper is now full";
  259. outputData << endl << "The nickel wrapper has now been replaced" << endl;
  260. nickelCount = 0;
  261. nickelBagCount++;
  262. }
  263. }
  264.  
  265. else if (weight <= dimeWeight + dimeWeightVAR && weight >= dimeWeight - dimeWeightVAR &&
  266. diameter <= dimeDiameter + dimeDiameterVAR && diameter >= dimeDiameter - dimeDiameterVAR && match == "BothMatch") {
  267. dimeCount++;
  268. if (dimeCount <= dimePerRoll) {
  269. cout << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "There are now ";
  270. cout << setw(2) << dimeCount << " coins in the dime wrapper" << endl;
  271. outputData << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "There are now ";
  272. outputData << setw(2) << dimeCount << " coins in the dime wrapper" << endl;
  273. }
  274. if (dimeCount > dimePerRoll) {
  275. cout << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "The dime wrapper is now full";
  276. cout << endl << "The dime wrapper has now been replaced" << endl;
  277. outputData << "The Coin Sorter has sent one coin to the dime wrapper" << endl << "The dime wrapper is now full";
  278. outputData << endl << "The dime wrapper has now been replaced" << endl;
  279. dimeCount = 0;
  280. dimeBagCount++;
  281. }
  282. }
  283.  
  284. else if (weight <= quarterWeight + quarterWeightVAR && weight >= quarterWeight - quarterWeightVAR &&
  285. diameter <= quarterDiameter + quarterDiameterVAR && diameter >= quarterDiameter - quarterDiameterVAR && match == "BothMatch") {
  286. quarterCount++;
  287. if (quarterCount <= quarterPerRoll) {
  288. cout << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "There are now ";
  289. cout << setw(2) << quarterCount << " coins in the quarter wrapper" << endl;
  290. outputData << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "There are now ";
  291. outputData << setw(2) << quarterCount << " coins in the quarter wrapper" << endl;
  292. }
  293. if (quarterCount > quarterPerRoll) {
  294. cout << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "The quarter wrapper is now full";
  295. cout << endl << "The quarter wrapper has now been replaced" << endl;
  296. outputData << "The Coin Sorter has sent one coin to the quarter wrapper" << endl << "The quarter wrapper is now full";
  297. outputData << endl << "The quarter wrapper has now been replaced" << endl;
  298. quarterCount = 0;
  299. quarterBagCount++;
  300. }
  301. }
  302.  
  303. else if (weight <= loonieWeight + loonieWeightVAR && weight >= loonieWeight - loonieWeightVAR &&
  304. diameter <= loonieDiameter + loonieDiameterVAR && diameter >= loonieDiameter - loonieDiameterVAR && match == "BothMatch") {
  305. loonieCount++;
  306. if (loonieCount <= looniePerRoll) {
  307. cout << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "There are now ";
  308. cout << setw(2) << loonieCount << " coins in the loonie wrapper" << endl;
  309. outputData << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "There are now ";
  310. outputData << setw(2) << loonieCount << " coins in the loonie wrapper" << endl;
  311. }
  312. if (loonieCount > looniePerRoll) {
  313. cout << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "The loonie wrapper is now full";
  314. cout << endl << "The loonie wrapper has now been replaced" << endl;
  315. outputData << "The Coin Sorter has sent one coin to the loonie wrapper" << endl << "The loonie wrapper is now full";
  316. outputData << endl << "The loonie wrapper has now been replaced" << endl;
  317. loonieCount = 0;
  318. loonieBagCount++;
  319. }
  320. }
  321.  
  322. else if (weight <= toonieWeight + toonieWeightVAR && weight >= toonieWeight - toonieWeightVAR &&
  323. diameter <= toonieDiameter + toonieDiameterVAR && diameter >= toonieDiameter - toonieDiameterVAR && match == "BothMatch") {
  324. toonieCount++;
  325. if (toonieCount <= tooniePerRoll) {
  326. cout << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "There are now ";
  327. cout << setw(2) << toonieCount << " coins in the toonie wrapper" << endl;
  328. outputData << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "There are now ";
  329. outputData << setw(2) << toonieCount << " coins in the toonie wrapper" << endl;
  330. }
  331. if (toonieCount > tooniePerRoll) {
  332. cout << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "The toonie wrapper is now full";
  333. cout << endl << "The toonie wrapper has now been replaced" << endl;
  334. outputData << "The Coin Sorter has sent one coin to the toonie wrapper" << endl << "The toonie wrapper is now full" << endl;
  335. outputData << endl << "The toonie wrapper has now been replaced" << endl;
  336. toonieCount = 0;
  337. toonieBagCount++;
  338. }
  339. }
  340. else {
  341. if (otherWeightCount + weight <= otherCoinWeight) {
  342. totalOtherWeightCount += weight;
  343. otherWeightCount += weight;
  344. otherCoinCount++;
  345. fullOtherCoinCounter++;
  346. cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  347. cout << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams" << endl;
  348. outputData << "The Coin Sorter has sent this coin to the other coin container" << endl;
  349. outputData << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams" << endl;
  350. }
  351. if (otherWeightCount + weight > otherCoinWeight) {
  352. cout << "The Coin Sorter has sent this coin to the other coin container" << endl;
  353. cout << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams." << endl;
  354. outputData << "The Coin Sorter has sent this coin to the other coin container" << endl;
  355. outputData << "The coins in the other coin container now weigh " << setw(3) << fixed << setprecision(2) << otherWeightCount << " grams." << endl;
  356. otherCoinCount = 0;
  357. totalOtherWeightCount += weight;
  358. otherWeightCount = weight;
  359. otherBagCount++;
  360. fullOtherCoin += fullOtherCoinCounter;
  361. }
  362. }
  363. }
  364.  
  365. if (usability == "bent") {
  366. if (bentWeightCount + weight < bentCoinWeight) {
  367. bentWeightCount += weight;
  368. cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  369. cout << "The coins in the bent coin container now weigh " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
  370. outputData << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  371. outputData << "The coins in the bent coin container now weigh " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
  372. totalBentWeightCount += weight;
  373. }
  374. if (bentWeightCount + weight > bentCoinWeight) {
  375. cout << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  376. cout << "This coin does not fit in the bent coin container" << endl;
  377. cout << "The bent coin container has been replaced" << endl;
  378. cout << "The coin in the new bent coin container weighs " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
  379. outputData << "The Coin Sorter has sent this coin to the bent coin container" << endl;
  380. outputData << "This coin does not fit in the bent coin container" << endl;
  381. outputData << "The bent coin container has been replaced" << endl;
  382. outputData << "The coin in the new bent coin container weighs " << setw(3) << fixed << setprecision(2) << bentWeightCount << " grams" << endl;
  383. bentWeightCount = weight;
  384. bentBagCount++;
  385. totalBentWeightCount += weight;
  386. }
  387. }
  388.  
  389. }
  390. }
  391. inputData.close();
  392. cout << endl << endl << endl;
  393. cout << "The Coin Sorter made " << setw(3) << nickelBagCount << " rolls of nickels" << endl;
  394. cout << "There are " << setw(2) << nickelCount << " nickels in the partially full roll" << endl;
  395. cout << "The Coin Sorter made " << setw(3) << dimeBagCount << " rolls of nickels" << endl;
  396. cout << "There are " << setw(2) << dimeCount << " dimes in the partially full roll" << endl;
  397. cout << "The Coin Sorter made " << setw(3) << quarterBagCount << " rolls of quarters" << endl;
  398. cout << "There are " << setw(2) << quarterCount << " quarters in the partially full roll" << endl;
  399. cout << "The Coin Sorter made " << setw(3) << loonieBagCount << " rolls of loonies" << endl;
  400. cout << "There are " << setw(2) << loonieCount << " loonies in the partially full roll" << endl;
  401. cout << "The Coin Sorter made " << setw(3) << toonieBagCount << " rolls of toonies" << endl;
  402. cout << "There are " << setw(2) << toonieCount << " toonies in the partially full roll" << endl;
  403. cout << "The Coin Sorter processed " << setw(5) << otherCoinCount << " other coins" << endl;
  404. cout << "The other coins completely filled " << setw(3) << otherBagCount << " containers" << endl;
  405. cout << "There were " << otherCoinCount << " other coins in the partially full container" << endl;
  406. cout << "The total weight of the other coins was " << setw(9) << setprecision(3) << totalOtherWeightCount << " grams" << endl;
  407. cout << "The Coin Sorter processed " << setw(11) << setprecision(4) << totalBentWeightCount << " g of bent coins" << endl;
  408. outputData << endl << endl << endl;
  409. outputData << "The Coin Sorter made " << setw(3) << nickelBagCount << " rolls of nickels" << endl;
  410. outputData << "There are " << setw(2) << nickelCount << " nickels in the partially full roll" << endl;
  411. outputData << "The Coin Sorter made " << setw(3) << dimeBagCount << " rolls of nickels" << endl;
  412. outputData << "There are " << setw(2) << dimeCount << " dimes in the partially full roll" << endl;
  413. outputData << "The Coin Sorter made " << setw(3) << quarterBagCount << " rolls of quarters" << endl;
  414. outputData << "There are " << setw(2) << quarterCount << " quarters in the partially full roll" << endl;
  415. outputData << "The Coin Sorter made " << setw(3) << loonieBagCount << " rolls of loonies" << endl;
  416. outputData << "There are " << setw(2) << loonieCount << " loonies in the partially full roll" << endl;
  417. outputData << "The Coin Sorter made " << setw(3) << toonieBagCount << " rolls of toonies" << endl;
  418. outputData << "There are " << setw(2) << toonieCount << " toonies in the partially full roll" << endl;
  419. outputData << "The Coin Sorter processed " << setw(5) << otherCoinCount << " other coins" << endl;
  420. outputData << "The other coins completely filled " << setw(3) << otherBagCount << " containers" << endl;
  421. outputData << "There were " << otherCoinCount << " other coins in the partially full container" << endl;
  422. outputData << "The total weight of the other coins was " << setw(9) << setprecision(3) << totalOtherWeightCount << " grams" << endl;
  423. outputData << "The Coin Sorter processed " << setw(11) << setprecision(4) << totalBentWeightCount << " g of bent coins" << endl;
  424.  
  425. return(0);
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement