Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SecondWeeklyChallenge
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region PositiveNumberStorage
- //Making our very own storage
- Console.WriteLine("Please type in the capacity of your storage.");
- uint capacity = NumberInputAndOutput();
- PositiveNumberStorage storage = new PositiveNumberStorage(capacity);
- //Trying out the element placement
- Console.WriteLine("Please provide an index (between 0 and capacity-1) you want to put an element in. ");
- uint index = NumberInputAndOutput();
- Console.WriteLine("Please provide a value (a positive number) you want to put in the storage. ");
- uint value = NumberInputAndOutput();
- bool didItWork = storage.ElementPlacement(index, value);
- if (didItWork)
- {
- Console.WriteLine("You successfully placed an element in.");
- }
- else
- {
- Console.WriteLine("The element placement failed.");
- }
- //Taking out an element from the storage
- Console.WriteLine("Please provide an index you want to take out an element from. ");
- index = NumberInputAndOutput();
- uint numberTaken = storage.ElementTakeOut(index);
- if (numberTaken == 0)
- {
- Console.WriteLine($"You could not take out an element from index {index}");
- }
- else
- {
- Console.WriteLine($"You have successfully taken out number {numberTaken} from the storage. Now index {index} is empty.");
- }
- //Search for an element
- Console.WriteLine("Please provide a value you want to search for. ");
- uint element = NumberInputAndOutput();
- bool theElementIsThere = storage.ElementSearch(element);
- if (theElementIsThere)
- {
- Console.WriteLine($"The element is there, the storage contains {element}");
- }
- else
- {
- Console.WriteLine($"The storage does not contain {element}");
- }
- //Checking if the storage is full
- bool storageIsFull = storage.StorageIsFull();
- if (storageIsFull)
- {
- Console.WriteLine("The storage is full.");
- }
- else
- {
- Console.WriteLine("The storage is not full.");
- }
- //Emptying the storage
- storage.ClearStorage();
- #endregion
- #region ObjectStorage
- //Making our very own storage
- Console.WriteLine("Please type in the capacity of your storage.");
- uint objectStorageCapacity = NumberInputAndOutput();
- ObjectStorage objectStorage = new ObjectStorage(capacity);
- //Trying out the element placement with string
- Console.WriteLine("Please provide an index (between 0 and capacity-1) you want to put an element in. ");
- index = NumberInputAndOutput();
- Console.Write("Please provide a string you want to put in the storage: ");
- string inputString = Console.ReadLine();
- didItWork = objectStorage.ElementPlacement(index, inputString);
- if (didItWork)
- {
- Console.WriteLine("You successfully placed an element in.");
- }
- else
- {
- Console.WriteLine("The element placement failed.");
- }
- //Taking out an element from the storage
- Console.WriteLine("Please provide an index you want to take out an element from. ");
- index = NumberInputAndOutput();
- Object objectTaken = objectStorage.ElementTakeOut(index);
- if (numberTaken == 0)
- {
- Console.WriteLine($"You could not take out an element from index {index}");
- }
- else
- {
- Console.WriteLine($"You have successfully taken out an element from the storage. Now index {index} is empty.");
- }
- //Search for an element, trying out with a string
- Console.Write("Please provide the element you want to search for: ");
- string stringElementToSearchFor = Console.ReadLine();
- theElementIsThere = objectStorage.ElementSearch(element);
- if (theElementIsThere)
- {
- Console.WriteLine("The element is in the storage.");
- }
- else
- {
- Console.WriteLine("The storage does not contain this element.");
- }
- //Checking if the storage is full
- storageIsFull = objectStorage.StorageIsFull();
- if (storageIsFull)
- {
- Console.WriteLine("The storage is full.");
- }
- else
- {
- Console.WriteLine("The storage is not full.");
- }
- //Emptying the storage
- objectStorage.ClearStorage();
- #endregion
- #region stack
- //Making our very own stack
- Console.WriteLine("Please type in the capacity of your storage.");
- capacity = NumberInputAndOutput();
- PositiveNumberStack stack = new PositiveNumberStack(capacity);
- //Trying out the element placement
- Console.WriteLine("Please provide a value (a positive number) you want to put on the top of the storage. ");
- value = NumberInputAndOutput();
- didItWork = stack.ElementPlacement(value); ;
- if (didItWork)
- {
- Console.WriteLine("You successfully placed an element in.");
- }
- else
- {
- Console.WriteLine("The element placement failed.");
- }
- //Taking out an element from the stack
- numberTaken = stack.ElementTakeOut();
- if (numberTaken == 0)
- {
- Console.WriteLine("You could not take out an element from the storage");
- }
- else
- {
- Console.WriteLine($"You have successfully taken out number {numberTaken} from the storage.");
- }
- //Check the stacks top for an element
- Console.WriteLine("Please provide a value you want to check if it is on the top of the stack. ");
- element = NumberInputAndOutput();
- theElementIsThere = stack.ElementTopCheck(element);
- if (theElementIsThere)
- {
- Console.WriteLine($"The top element is {element}.");
- }
- else
- {
- Console.WriteLine($"The top element is not {element}.");
- }
- //Checking if the stack is full
- storageIsFull = stack.StorageHasElement();
- if (storageIsFull)
- {
- Console.WriteLine("The storage is full.");
- }
- else
- {
- Console.WriteLine("The storage is not full.");
- }
- //Emptying the storage
- stack.ClearStorage();
- #endregion
- Console.ReadLine();
- }
- //Simple algorithm for values for the uint storage questions.
- //If the user makes a mistake here, the methods in the Storage Class still ask him for a valid value
- public static uint NumberInputAndOutput()
- {
- bool itIsNumber = false;
- uint number = 0;
- do
- {
- Console.Write("Please put the number in: ");
- itIsNumber = uint.TryParse(Console.ReadLine(), out number);
- } while (itIsNumber == false);
- return number;
- }
- }
- #region PositiveNumberStorage class
- class PositiveNumberStorage
- {
- public uint Capacity { get; }
- uint[] storage;
- //Makes the storage
- public PositiveNumberStorage(uint capacity)
- {
- if (capacity == 0)
- {
- bool capacityIsNumber = false;
- do
- {
- Console.Write("Invalid input, please provide a positive number: ");
- capacityIsNumber = uint.TryParse(Console.ReadLine(), out capacity);
- } while (capacityIsNumber == false || capacity == 0);
- }
- Capacity = capacity;
- storage = new uint[capacity];
- }
- //Places a given element to the given index
- public bool ElementPlacement(uint index, uint number)
- {
- bool success = false;
- if (index < 0 || index > Capacity - 1 || storage[index] != 0 || number < 1)
- {
- Console.WriteLine("Invalid input.");
- }
- else
- {
- storage[index] = number;
- success = true;
- }
- return success;
- }
- //Takes out an element
- public uint ElementTakeOut(uint index)
- {
- uint element = 0;
- if (index < 0 || index > Capacity - 1 || storage[index] == 0)
- {
- }
- else
- {
- element = storage[index];
- storage[index] = 0;
- }
- return element;
- }
- //Searches for an element in the storage
- public bool ElementSearch(uint element)
- {
- bool success = false;
- if (element == 0)
- {
- }
- else
- {
- for (int i = 0; i < Capacity; i++)
- {
- if (storage[i] == element)
- {
- success = true;
- break;
- }
- }
- }
- return success;
- }
- //Checks if the storage is full
- public bool StorageIsFull()
- {
- bool success = true;
- for (int i = 0; i < Capacity; i++)
- {
- if (storage[i] == 0)
- {
- success = false;
- break;
- }
- }
- return success;
- }
- //Clears the storage
- public void ClearStorage()
- {
- for (int i = 0; i < Capacity; i++)
- {
- storage[i] = 0;
- }
- }
- }
- #endregion
- #region PositiveNumberStack class
- class PositiveNumberStack
- {
- public uint Capacity { get; }
- uint[] stack;
- //Makes the stack
- public PositiveNumberStack(uint capacity)
- {
- if (capacity == 0)
- {
- bool capacityIsNumber = false;
- do
- {
- Console.Write("Invalid input, please provide a positive number: ");
- capacityIsNumber = uint.TryParse(Console.ReadLine(), out capacity);
- } while (capacityIsNumber == false || capacity == 0);
- }
- Capacity = capacity;
- stack = new uint[capacity];
- }
- //Places an element to the top
- public bool ElementPlacement(uint number)
- {
- bool success = false;
- if (number < 1)
- {
- Console.WriteLine("Invalid input.");
- }
- else if (stack[stack.Length - 1] != 0)
- {
- Console.WriteLine("The storage is full, you must empty it or remove an element from it.");
- }
- else
- {
- for (int i = 1; i < stack.Length; i++)
- {
- stack[i] = stack[i - 1];
- }
- stack[0] = number;
- success = true;
- }
- return success;
- }
- //Takes out the top element
- public uint ElementTakeOut()
- {
- uint element = stack[0];
- for (int i = 0; i < stack.Length - 1; i++)
- {
- stack[i] = stack[i + 1];
- }
- return element;
- }
- //Checks if the top element is what you think it is
- public bool ElementTopCheck(uint element)
- {
- bool success = false;
- if (element == 0)
- {
- }
- else if (stack[0] == element)
- {
- success = true;
- }
- return success;
- }
- //Checks if there is any element in the stack
- public bool StorageHasElement()
- {
- bool success = true;
- if (stack[0] == 0)
- {
- success = false;
- }
- return success;
- }
- //Clears the stack
- public void ClearStorage()
- {
- for (int i = 1; i < Capacity; i++)
- {
- stack[i - 1] = stack[i];
- stack[0] = 0;
- }
- }
- }
- #endregion
- #region ObjectStorage
- class ObjectStorage
- {
- public uint Capacity { get; }
- private Object[] storage;
- //Makes the storage
- public ObjectStorage(uint capacity)
- {
- if (capacity == 0)
- {
- bool capacityIsNumber = false;
- do
- {
- Console.Write("Invalid input, please provide a positive number: ");
- capacityIsNumber = uint.TryParse(Console.ReadLine(), out capacity);
- } while (capacityIsNumber == false || capacity == 0);
- }
- Capacity = capacity;
- storage = new Object[capacity];
- }
- //Places an element in the storage
- public bool ElementPlacement(uint index, Object element)
- {
- bool success = false;
- if (index < 0 || index > Capacity - 1)
- {
- }
- else
- {
- storage[index] = element;
- success = true;
- }
- return success;
- }
- //Takes out an element from the storage
- public Object ElementTakeOut(uint index)
- {
- Object element = 0;
- if (index < 0 || index > Capacity - 1 || storage[index] == null)
- {
- }
- else
- {
- element = storage[index];
- storage[index] = 0;
- }
- return element;
- }
- //Searches for an element in the storage
- public bool ElementSearch(Object element)
- {
- bool success = false;
- if (element == null)
- {
- }
- else
- {
- for (int i = 0; i < Capacity; i++)
- {
- if (storage[i] == element)
- {
- success = true;
- break;
- }
- }
- }
- return success;
- }
- //Checks if the storage is full
- public bool StorageIsFull()
- {
- bool success = true;
- for (int i = 0; i < Capacity; i++)
- {
- if (storage[i] == null)
- {
- success = false;
- break;
- }
- }
- return success;
- }
- //Clears the storage
- public void ClearStorage()
- {
- for (int i = 0; i < Capacity; i++)
- {
- storage[i] = null;
- }
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement