Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SecondWeeklyChallenge
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. #region PositiveNumberStorage
  14. //Making our very own storage
  15. Console.WriteLine("Please type in the capacity of your storage.");
  16. uint capacity = NumberInputAndOutput();
  17.  
  18. PositiveNumberStorage storage = new PositiveNumberStorage(capacity);
  19.  
  20. //Trying out the element placement
  21. Console.WriteLine("Please provide an index (between 0 and capacity-1) you want to put an element in. ");
  22. uint index = NumberInputAndOutput();
  23.  
  24. Console.WriteLine("Please provide a value (a positive number) you want to put in the storage. ");
  25. uint value = NumberInputAndOutput();
  26.  
  27. bool didItWork = storage.ElementPlacement(index, value);
  28. if (didItWork)
  29. {
  30. Console.WriteLine("You successfully placed an element in.");
  31. }
  32. else
  33. {
  34. Console.WriteLine("The element placement failed.");
  35. }
  36.  
  37. //Taking out an element from the storage
  38. Console.WriteLine("Please provide an index you want to take out an element from. ");
  39. index = NumberInputAndOutput();
  40. uint numberTaken = storage.ElementTakeOut(index);
  41.  
  42. if (numberTaken == 0)
  43. {
  44. Console.WriteLine($"You could not take out an element from index {index}");
  45. }
  46. else
  47. {
  48. Console.WriteLine($"You have successfully taken out number {numberTaken} from the storage. Now index {index} is empty.");
  49. }
  50.  
  51. //Search for an element
  52. Console.WriteLine("Please provide a value you want to search for. ");
  53. uint element = NumberInputAndOutput();
  54.  
  55. bool theElementIsThere = storage.ElementSearch(element);
  56. if (theElementIsThere)
  57. {
  58. Console.WriteLine($"The element is there, the storage contains {element}");
  59. }
  60. else
  61. {
  62. Console.WriteLine($"The storage does not contain {element}");
  63. }
  64.  
  65. //Checking if the storage is full
  66. bool storageIsFull = storage.StorageIsFull();
  67. if (storageIsFull)
  68. {
  69. Console.WriteLine("The storage is full.");
  70. }
  71. else
  72. {
  73. Console.WriteLine("The storage is not full.");
  74. }
  75.  
  76. //Emptying the storage
  77. storage.ClearStorage();
  78. #endregion
  79.  
  80. #region ObjectStorage
  81. //Making our very own storage
  82. Console.WriteLine("Please type in the capacity of your storage.");
  83. uint objectStorageCapacity = NumberInputAndOutput();
  84.  
  85. ObjectStorage objectStorage = new ObjectStorage(capacity);
  86.  
  87. //Trying out the element placement with string
  88. Console.WriteLine("Please provide an index (between 0 and capacity-1) you want to put an element in. ");
  89. index = NumberInputAndOutput();
  90.  
  91. Console.Write("Please provide a string you want to put in the storage: ");
  92. string inputString = Console.ReadLine();
  93.  
  94. didItWork = objectStorage.ElementPlacement(index, inputString);
  95. if (didItWork)
  96. {
  97. Console.WriteLine("You successfully placed an element in.");
  98. }
  99. else
  100. {
  101. Console.WriteLine("The element placement failed.");
  102. }
  103.  
  104. //Taking out an element from the storage
  105. Console.WriteLine("Please provide an index you want to take out an element from. ");
  106. index = NumberInputAndOutput();
  107. Object objectTaken = objectStorage.ElementTakeOut(index);
  108.  
  109. if (numberTaken == 0)
  110. {
  111. Console.WriteLine($"You could not take out an element from index {index}");
  112. }
  113. else
  114. {
  115. Console.WriteLine($"You have successfully taken out an element from the storage. Now index {index} is empty.");
  116. }
  117.  
  118. //Search for an element, trying out with a string
  119. Console.Write("Please provide the element you want to search for: ");
  120. string stringElementToSearchFor = Console.ReadLine();
  121.  
  122. theElementIsThere = objectStorage.ElementSearch(element);
  123. if (theElementIsThere)
  124. {
  125. Console.WriteLine("The element is in the storage.");
  126. }
  127. else
  128. {
  129. Console.WriteLine("The storage does not contain this element.");
  130. }
  131.  
  132. //Checking if the storage is full
  133. storageIsFull = objectStorage.StorageIsFull();
  134. if (storageIsFull)
  135. {
  136. Console.WriteLine("The storage is full.");
  137. }
  138. else
  139. {
  140. Console.WriteLine("The storage is not full.");
  141. }
  142.  
  143. //Emptying the storage
  144. objectStorage.ClearStorage();
  145. #endregion
  146.  
  147. #region stack
  148.  
  149. //Making our very own stack
  150. Console.WriteLine("Please type in the capacity of your storage.");
  151. capacity = NumberInputAndOutput();
  152.  
  153. PositiveNumberStack stack = new PositiveNumberStack(capacity);
  154.  
  155. //Trying out the element placement
  156. Console.WriteLine("Please provide a value (a positive number) you want to put on the top of the storage. ");
  157. value = NumberInputAndOutput();
  158.  
  159.  
  160. didItWork = stack.ElementPlacement(value); ;
  161. if (didItWork)
  162. {
  163. Console.WriteLine("You successfully placed an element in.");
  164. }
  165. else
  166. {
  167. Console.WriteLine("The element placement failed.");
  168. }
  169.  
  170. //Taking out an element from the stack
  171. numberTaken = stack.ElementTakeOut();
  172.  
  173. if (numberTaken == 0)
  174. {
  175. Console.WriteLine("You could not take out an element from the storage");
  176. }
  177. else
  178. {
  179. Console.WriteLine($"You have successfully taken out number {numberTaken} from the storage.");
  180. }
  181.  
  182. //Check the stacks top for an element
  183. Console.WriteLine("Please provide a value you want to check if it is on the top of the stack. ");
  184. element = NumberInputAndOutput();
  185.  
  186. theElementIsThere = stack.ElementTopCheck(element);
  187. if (theElementIsThere)
  188. {
  189. Console.WriteLine($"The top element is {element}.");
  190. }
  191. else
  192. {
  193. Console.WriteLine($"The top element is not {element}.");
  194. }
  195.  
  196. //Checking if the stack is full
  197. storageIsFull = stack.StorageHasElement();
  198. if (storageIsFull)
  199. {
  200. Console.WriteLine("The storage is full.");
  201. }
  202. else
  203. {
  204. Console.WriteLine("The storage is not full.");
  205. }
  206.  
  207. //Emptying the storage
  208. stack.ClearStorage();
  209. #endregion
  210.  
  211. Console.ReadLine();
  212. }
  213.  
  214. //Simple algorithm for values for the uint storage questions.
  215. //If the user makes a mistake here, the methods in the Storage Class still ask him for a valid value
  216. public static uint NumberInputAndOutput()
  217. {
  218. bool itIsNumber = false;
  219. uint number = 0;
  220.  
  221. do
  222. {
  223. Console.Write("Please put the number in: ");
  224. itIsNumber = uint.TryParse(Console.ReadLine(), out number);
  225. } while (itIsNumber == false);
  226.  
  227. return number;
  228. }
  229. }
  230.  
  231. #region PositiveNumberStorage class
  232. class PositiveNumberStorage
  233. {
  234. public uint Capacity { get; }
  235. uint[] storage;
  236.  
  237. //Makes the storage
  238. public PositiveNumberStorage(uint capacity)
  239. {
  240. if (capacity == 0)
  241. {
  242. bool capacityIsNumber = false;
  243. do
  244. {
  245. Console.Write("Invalid input, please provide a positive number: ");
  246. capacityIsNumber = uint.TryParse(Console.ReadLine(), out capacity);
  247. } while (capacityIsNumber == false || capacity == 0);
  248. }
  249. Capacity = capacity;
  250. storage = new uint[capacity];
  251.  
  252. }
  253.  
  254. //Places a given element to the given index
  255. public bool ElementPlacement(uint index, uint number)
  256. {
  257. bool success = false;
  258.  
  259. if (index < 0 || index > Capacity - 1 || storage[index] != 0 || number < 1)
  260. {
  261. Console.WriteLine("Invalid input.");
  262. }
  263. else
  264. {
  265. storage[index] = number;
  266. success = true;
  267. }
  268.  
  269. return success;
  270. }
  271.  
  272. //Takes out an element
  273. public uint ElementTakeOut(uint index)
  274. {
  275. uint element = 0;
  276.  
  277. if (index < 0 || index > Capacity - 1 || storage[index] == 0)
  278. {
  279.  
  280. }
  281. else
  282. {
  283. element = storage[index];
  284. storage[index] = 0;
  285. }
  286.  
  287. return element;
  288. }
  289.  
  290. //Searches for an element in the storage
  291. public bool ElementSearch(uint element)
  292. {
  293. bool success = false;
  294.  
  295. if (element == 0)
  296. {
  297.  
  298. }
  299. else
  300. {
  301. for (int i = 0; i < Capacity; i++)
  302. {
  303. if (storage[i] == element)
  304. {
  305. success = true;
  306. break;
  307. }
  308. }
  309. }
  310. return success;
  311. }
  312.  
  313. //Checks if the storage is full
  314. public bool StorageIsFull()
  315. {
  316. bool success = true;
  317.  
  318. for (int i = 0; i < Capacity; i++)
  319. {
  320. if (storage[i] == 0)
  321. {
  322. success = false;
  323. break;
  324. }
  325. }
  326.  
  327. return success;
  328. }
  329.  
  330. //Clears the storage
  331. public void ClearStorage()
  332. {
  333. for (int i = 0; i < Capacity; i++)
  334. {
  335. storage[i] = 0;
  336. }
  337. }
  338. }
  339. #endregion
  340.  
  341. #region PositiveNumberStack class
  342. class PositiveNumberStack
  343. {
  344. public uint Capacity { get; }
  345. uint[] stack;
  346.  
  347.  
  348. //Makes the stack
  349. public PositiveNumberStack(uint capacity)
  350. {
  351. if (capacity == 0)
  352. {
  353. bool capacityIsNumber = false;
  354. do
  355. {
  356. Console.Write("Invalid input, please provide a positive number: ");
  357. capacityIsNumber = uint.TryParse(Console.ReadLine(), out capacity);
  358. } while (capacityIsNumber == false || capacity == 0);
  359. }
  360. Capacity = capacity;
  361. stack = new uint[capacity];
  362.  
  363. }
  364.  
  365. //Places an element to the top
  366. public bool ElementPlacement(uint number)
  367. {
  368. bool success = false;
  369. if (number < 1)
  370. {
  371. Console.WriteLine("Invalid input.");
  372. }
  373. else if (stack[stack.Length - 1] != 0)
  374. {
  375. Console.WriteLine("The storage is full, you must empty it or remove an element from it.");
  376. }
  377. else
  378. {
  379. for (int i = 1; i < stack.Length; i++)
  380. {
  381. stack[i] = stack[i - 1];
  382. }
  383. stack[0] = number;
  384. success = true;
  385. }
  386.  
  387. return success;
  388. }
  389.  
  390. //Takes out the top element
  391. public uint ElementTakeOut()
  392. {
  393. uint element = stack[0];
  394.  
  395. for (int i = 0; i < stack.Length - 1; i++)
  396. {
  397. stack[i] = stack[i + 1];
  398. }
  399.  
  400. return element;
  401. }
  402.  
  403. //Checks if the top element is what you think it is
  404. public bool ElementTopCheck(uint element)
  405. {
  406. bool success = false;
  407.  
  408. if (element == 0)
  409. {
  410.  
  411. }
  412. else if (stack[0] == element)
  413. {
  414. success = true;
  415. }
  416.  
  417. return success;
  418. }
  419.  
  420. //Checks if there is any element in the stack
  421. public bool StorageHasElement()
  422. {
  423. bool success = true;
  424.  
  425. if (stack[0] == 0)
  426. {
  427. success = false;
  428. }
  429.  
  430. return success;
  431. }
  432.  
  433. //Clears the stack
  434. public void ClearStorage()
  435. {
  436. for (int i = 1; i < Capacity; i++)
  437. {
  438. stack[i - 1] = stack[i];
  439. stack[0] = 0;
  440. }
  441.  
  442. }
  443. }
  444. #endregion
  445.  
  446. #region ObjectStorage
  447. class ObjectStorage
  448. {
  449. public uint Capacity { get; }
  450. private Object[] storage;
  451.  
  452.  
  453. //Makes the storage
  454. public ObjectStorage(uint capacity)
  455. {
  456. if (capacity == 0)
  457. {
  458. bool capacityIsNumber = false;
  459. do
  460. {
  461. Console.Write("Invalid input, please provide a positive number: ");
  462. capacityIsNumber = uint.TryParse(Console.ReadLine(), out capacity);
  463. } while (capacityIsNumber == false || capacity == 0);
  464. }
  465. Capacity = capacity;
  466. storage = new Object[capacity];
  467.  
  468. }
  469.  
  470. //Places an element in the storage
  471. public bool ElementPlacement(uint index, Object element)
  472. {
  473. bool success = false;
  474.  
  475. if (index < 0 || index > Capacity - 1)
  476. {
  477.  
  478. }
  479. else
  480. {
  481. storage[index] = element;
  482. success = true;
  483. }
  484.  
  485. return success;
  486. }
  487.  
  488. //Takes out an element from the storage
  489. public Object ElementTakeOut(uint index)
  490. {
  491. Object element = 0;
  492.  
  493. if (index < 0 || index > Capacity - 1 || storage[index] == null)
  494. {
  495.  
  496. }
  497. else
  498. {
  499. element = storage[index];
  500. storage[index] = 0;
  501. }
  502.  
  503. return element;
  504. }
  505.  
  506. //Searches for an element in the storage
  507. public bool ElementSearch(Object element)
  508. {
  509. bool success = false;
  510.  
  511. if (element == null)
  512. {
  513.  
  514. }
  515. else
  516. {
  517. for (int i = 0; i < Capacity; i++)
  518. {
  519. if (storage[i] == element)
  520. {
  521. success = true;
  522. break;
  523. }
  524. }
  525. }
  526. return success;
  527. }
  528.  
  529. //Checks if the storage is full
  530. public bool StorageIsFull()
  531. {
  532. bool success = true;
  533.  
  534. for (int i = 0; i < Capacity; i++)
  535. {
  536. if (storage[i] == null)
  537. {
  538. success = false;
  539. break;
  540. }
  541. }
  542.  
  543. return success;
  544. }
  545.  
  546. //Clears the storage
  547. public void ClearStorage()
  548. {
  549. for (int i = 0; i < Capacity; i++)
  550. {
  551. storage[i] = null;
  552. }
  553. }
  554. }
  555. #endregion
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement