Advertisement
JamesDamico

Mystery

Oct 11th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | None | 0 0
  1.  
  2. // (c) 2013-2019 David Gerstl, all rights reserved
  3. // For the use of my C++ students to use as a base to implement
  4. // dynamic arrays, exceptions and operator overloading, and templates
  5.  
  6.  
  7. // Class farmingdale::stack: General integer stack implementation based on array.
  8. // We will add dynamic arrays (for unlimited size), templates (to allow multiple types) etc.
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <fstream>
  13. #include <stdlib.h>
  14. #include <climits>
  15. #include <random>
  16. #include "mysteryStack.h"
  17. #include "BCS370_arrayBasedStack.h"
  18.  
  19.  
  20.  
  21. //MysteryStackTest
  22. bool normalTest();
  23. bool copyCtorTest();
  24.  
  25. //ArrayBasedStackTest
  26. bool normalTest(int testSize);
  27. bool copyCtorTest(int test);
  28.  
  29.  
  30. //TestStream
  31. int testStream(std::string fileName);
  32.  
  33. //RandomStream
  34. void randomStream(std::string fileName, int length);
  35.  
  36. int main() {
  37.  
  38. //MysteryStackTest
  39. bool failed = false;
  40. failed = normalTest();
  41. if (!failed) {
  42. copyCtorTest();
  43. }
  44. if (false == failed) {
  45. std::cout << "MysterStack: Passed the [non-exhaustive] test." << std::endl;
  46. }
  47.  
  48. //ArrayBasedStackTest
  49. failed = false;
  50. failed = normalTest(100);
  51. if (!failed) {
  52. copyCtorTest(100);
  53. }
  54. if (false == failed) {
  55. std::cout << "ArrayBasedStack: Passed the [non-exhaustive] test." << std::endl;
  56. }
  57.  
  58. char selection;
  59.  
  60.  
  61. std::string fileName;
  62. int length = 0;
  63. int failLine = 0;
  64. do
  65. {
  66. std::cout << " Main Menu" << std::endl;
  67. std::cout << " ====================================" << std::endl;
  68. std::cout << " 1. Exit" << std::endl;
  69. std::cout << " 2. RandomStream" << std::endl;
  70. std::cout << " 3. TestStream" << std::endl;
  71. std::cout << " 4. RandomStream & TestStream" << std::endl;
  72. std::cout << "\n" << std::endl;
  73. std::cout << " X. Exit\n" << std::endl;
  74. std::cout << " ====================================" << std::endl;
  75. std::cout << " Enter your selection: " << std::endl;
  76. std::cin >> selection;
  77. std::cout << std::endl;
  78. system("cls");
  79.  
  80. switch (selection)
  81. {
  82. case '1':
  83. std::cout << "Goodbye.\n";
  84. system("pause");
  85. return 0;
  86. break;
  87.  
  88. case '2':
  89.  
  90. std::cout << "Please input a filename:" << std::endl;
  91. std::cin >> fileName;
  92.  
  93. std::cout << "How many iterations:" << std::endl;
  94. std::cin >> length;
  95.  
  96. randomStream(fileName, length);
  97.  
  98. system("pause");
  99. system("cls");
  100. break;
  101.  
  102. case '3':
  103.  
  104. std::cout << "Please input a filename you would like to run testStream on:" << std::endl;
  105. std::cin >> fileName;
  106.  
  107. int failLine;
  108. failLine = testStream(fileName);
  109.  
  110. if (failLine != 0)
  111. {
  112. std::cout << "Error on line " << failLine << std::endl;
  113. }
  114.  
  115.  
  116. system("pause");
  117. system("cls");
  118. break;
  119.  
  120. case '4':
  121. std::cout << "Please input a filename:" << std::endl;
  122. std::cin >> fileName;
  123.  
  124. std::cout << "How many iterations:" << std::endl;
  125. std::cin >> length;
  126.  
  127. randomStream(fileName, length);
  128.  
  129. failLine = testStream(fileName);
  130.  
  131. if (failLine != 0)
  132. {
  133. std::cout << "Error on line " << failLine << std::endl;
  134. }
  135.  
  136. system("pause");
  137. system("cls");
  138. break;
  139.  
  140. default:
  141. system("cls");
  142. std::cout << selection << " is not a valid menu item.\n";
  143.  
  144. }
  145.  
  146. } while (selection != 1);
  147.  
  148. return 0;
  149.  
  150. }
  151.  
  152.  
  153. void randomStream(std::string fileName, int length)
  154. {
  155. std::ofstream outFile;
  156. outFile.open(fileName);
  157.  
  158. if (outFile)
  159. {
  160. std::random_device rd;
  161. std::mt19937 gen(rd());
  162. std::uniform_int_distribution<> dis(0, 5);
  163.  
  164. for (int i = 0; i < length; ++i)
  165. {
  166. int randNum = dis(gen);
  167.  
  168. std::uniform_int_distribution<> dis(INT_MIN, INT_MAX);
  169. int minMax = 0;
  170.  
  171. if (randNum == 1) {
  172. std::uniform_int_distribution<> dis(INT_MIN, INT_MAX);
  173. int minMax = dis(gen);
  174. outFile << "A " << minMax << std::endl;
  175. }
  176. else if (randNum == 2) {
  177. outFile << "D" << std::endl;
  178. }
  179. else if (randNum == 3) {
  180. outFile << "P" << std::endl;
  181. }
  182. else if (randNum == 4) {
  183. outFile << "E" << std::endl;
  184. }
  185. else if (randNum == 5) {
  186. outFile << "C" << std::endl;
  187. }
  188. else {
  189. outFile << "S" << std::endl;
  190. }
  191. }
  192. }
  193. }
  194.  
  195. int testStream(std::string fileName)
  196. {
  197. //Open file reader
  198. std::ifstream inFile;
  199. inFile.open(fileName);
  200. if (!inFile)
  201. {
  202. std::cout << "Error opening file!" << std::endl;
  203. system("pause");
  204. }
  205.  
  206. //Create the stacks
  207. farmingdale::stack s1;
  208. farmingdale::stack s2;
  209. mystery::stack m1(0);
  210.  
  211. char x;
  212. std::string num;
  213. int lineNum = 1;
  214. std::string pe1, pe2, pe3, po1, po2, po3;
  215. bool failed;
  216. int failCount = 0;
  217. while (inFile >> x)
  218. {
  219. switch (x)
  220. {
  221. case 'A':
  222. failCount = 0;
  223.  
  224. inFile >> num;
  225.  
  226.  
  227. failed = s1.push(num);
  228. if (false == failed) {
  229. failCount++;
  230. }
  231.  
  232.  
  233. failed = s2.push(num);
  234. if (false == failed) {
  235. failCount++;
  236. }
  237.  
  238. failed = m1.push(num);
  239. if (false == failed) {
  240. failCount++;
  241. }
  242.  
  243. if (failCount == 1 || failCount == 2)
  244. {
  245. return lineNum;
  246. }
  247.  
  248. // std::cout << num << std::endl;
  249. break;
  250. case 'P':
  251. failCount = 0;
  252.  
  253. failed = s1.peek(pe1);
  254. if (false == failed) {
  255. failCount++;
  256. }
  257.  
  258. failed = s2.peek(pe2);
  259. if (false == failed) {
  260. failCount++;
  261. }
  262.  
  263. failed = m1.peek(pe3);
  264. if (false == failed) {
  265. failCount++;
  266. }
  267.  
  268. if (failCount == 1 || failCount == 2 || (pe1 != pe2) || (pe1 != pe3) || (pe2 != pe3))
  269. {
  270. std::cout << failCount << std::endl;
  271. return lineNum;
  272. }
  273.  
  274.  
  275. break;
  276. case 'D':
  277. failCount = 0;
  278.  
  279. failed = s1.pop(po1);
  280. if (false == failed) {
  281. failCount++;
  282. }
  283.  
  284.  
  285. failed = s2.pop(po2);
  286. if (false == failed) {
  287. failCount++;
  288. }
  289.  
  290.  
  291. failed = m1.pop(po3);
  292. if (false == failed) {
  293. failCount++;
  294. }
  295.  
  296. if (failCount == 1 || failCount == 2 || (po1 != po2) || (po1 != po3) || (po2 != po3))
  297. {
  298. return lineNum;
  299. }
  300. break;
  301. case 'C':
  302. // std::cout << x << std::endl;
  303. break;
  304. case 'S':
  305. // std::cout << x << std::endl;
  306. break;
  307. case 'E':
  308. // std::cout << x << std::endl;
  309. break;
  310. }
  311.  
  312. lineNum++;
  313. }
  314. std::string pee;
  315. s1.peek(pee);
  316. std::cout << pee << std::endl;
  317. s2.peek(pee);
  318. std::cout << pee << std::endl;
  319. m1.peek(pee);
  320. std::cout << pee << std::endl;
  321.  
  322. return 0;
  323. }
  324.  
  325.  
  326.  
  327.  
  328. //MysteryStackTest
  329. bool normalTest() {
  330. mystery::stack s1(16);
  331. for (int i = 0; i < s1.stackCapacity(); ++i) {
  332. if (mystery::FAILURE == s1.push(std::to_string(i + 1000))) {
  333. std::cerr << "Error on line " << __LINE__ << std::endl;
  334. return true;
  335. }
  336. }
  337. if (mystery::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
  338. std::cerr << "Error on line " << __LINE__ << std::endl;
  339. return true;
  340. }
  341. std::string j;
  342. std::string t;
  343. for (int i = 0; i < s1.stackCapacity(); ++i) {
  344. if (mystery::FAILURE == s1.peek(t) || mystery::FAILURE == s1.pop(j) ||
  345. t != j || j != std::to_string(1000 + s1.stackCapacity() - i - 1)) {
  346. std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
  347. << " and t is " << t << std::endl;
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. //MysteryStackTest
  354. bool copyCtorTest() {
  355. mystery::stack s1(16);
  356. for (int i = 0; i < s1.stackCapacity(); ++i) {
  357. if (mystery::FAILURE == s1.push(std::to_string(i + 1000))) {
  358. std::cerr << "Error on line " << __LINE__ << std::endl;
  359. return true;
  360. }
  361. }
  362. if (mystery::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
  363. std::cerr << "Error on line " << __LINE__ << std::endl;
  364. return true;
  365. }
  366. // copy s1 into s2
  367. mystery::stack s2(s1);
  368. std::string j;
  369. std::string t;
  370. for (int i = 0; i < s2.stackCapacity(); ++i) {
  371. if (mystery::FAILURE == s2.peek(t) || mystery::FAILURE == s2.pop(j) ||
  372. t != j || j != std::to_string(1000 + s2.stackCapacity() - i - 1)) {
  373. std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
  374. << " and t is " << t << std::endl;
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. //ArrayBasedStackTest
  381. bool normalTest(int testSize) {
  382. farmingdale::stack s1;
  383. for (int i = 0; i < testSize; ++i) {
  384. if (farmingdale::FAILURE == s1.push(std::to_string(i + 1000))) {
  385. std::cerr << "Error on line " << __LINE__ << std::endl;
  386. return true;
  387. }
  388. }
  389. // if (farmingdale::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
  390. // std::cerr << "Error on line " << __LINE__ << std::endl;
  391. // return true;
  392. // }
  393. std::string j;
  394. std::string t;
  395. for (int i = 0; i < testSize; ++i) {
  396. if (farmingdale::FAILURE == s1.peek(t) || farmingdale::FAILURE == s1.pop(j) ||
  397. t != j || j != std::to_string(1000 + testSize - i - 1)) {
  398. std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
  399. << " and t is " << t << std::endl;
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405.  
  406. //ArrayBasedStackTest
  407. bool copyCtorTest(int testSize) {
  408. farmingdale::stack s1;
  409. for (int i = 0; i < testSize; ++i) {
  410. if (farmingdale::FAILURE == s1.push(std::to_string(i + 1000))) {
  411. std::cerr << "Error on line " << __LINE__ << std::endl;
  412. return true;
  413. }
  414. }
  415. // if (farmingdale::SUCCESS == s1.push(std::to_string(s1.stackCapacity() + 1000))) {
  416. // std::cerr << "Error on line " << __LINE__ << std::endl;
  417. // return true;
  418. // }
  419. // copy s1 into s2
  420. farmingdale::stack s2(s1);
  421.  
  422. if (s1 != s2) {
  423. std::cerr << "Error on line " << __LINE__ << std::endl;
  424. return true;
  425. }
  426.  
  427.  
  428.  
  429. std::string j;
  430. std::string t;
  431. for (int i = 0; i < testSize; ++i) {
  432. if (farmingdale::FAILURE == s2.peek(t) || farmingdale::FAILURE == s2.pop(j) ||
  433. t != j || j != std::to_string(1000 + testSize - i - 1)) {
  434. std::cerr << "Error on line " << __LINE__ << " j is " << j << " and i is " << i
  435. << " and t is " << t << std::endl;
  436. return true;
  437. }
  438. }
  439. return false;
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement