My program is that the user can input any string and choose between two option the bubble sort or the quicksort and after that my program will execute the sorting but I am having a problem with the user input.
I don't know how to call the user input into my BubbleSort and also into my Quicksort.
I have red lines in my code specially on Implementaion and userInput. How will I fix it? I am a newbie in creating a strategy pattern.
Here are my errors:
/Users/xhantan/Projects/SortString/SortString/Program.cs(69,29,69,38): error CS0103: The name 'userInput' does not exist in the current context
/Users/xhantan/Projects/SortString/SortString/Program.cs(70,37,70,46): error CS0103: The name 'userInput' does not exist in the current context
/Users/xhantan/Projects/SortString/SortString/Program.cs(67,23,67,36): error CS0161: 'QuickSort.Implementaion(object)': not all code paths return a value
/Users/xhantan/Projects/SortString/SortString/Program.cs(42,30,42,39): error CS0103: The name 'userInput' does not exist in the current context
/Users/xhantan/Projects/SortString/SortString/Program.cs(39,23,39,37): error CS0161: 'BubbleSort.Implementation(object)': not all code paths return a value
/Users/xhantan/Projects/SortString/SortString/Program.cs(28,40,28,54): error CS7036: There is no argument given that corresponds to the required formal parameter 'data' of 'IStrategy.Implementation(object)'
0 Warning(s)
6 Error(s)
Here is my code:
using System;
namespace SortStrategyPattern
{
class Context
{
private IStrategy _strategy;
public Context()
{ }
public Context(IStrategy strategy)
{
this._strategy = strategy;
}
public void SetStrategy(IStrategy strategy)
{
this._strategy = strategy;
}
public void UserInput()
{
Console.Write("Enter a string: ");
String userInput = Console.ReadLine();
userInput = this._strategy.Implementation();
}
}
public interface IStrategy
{
object Implementation(object data);
}
class BubbleSort : IStrategy
{
public object Implementation(object data)
{
char temp;
char[] charStr = userInput.ToCharArray();
for (int x = 1; x < charStr.Length; x++)
{
for (int y = 0; y < charStr.Length - 1; y++)
{
if (charStr[y] > charStr[y + 1])
{
temp = charStr[y];
charStr[y] = charStr[y + 1];
charStr[y + 1] = temp;
}
}
}
Console.Write("Bubble Sort: ");
foreach (char input in charStr)
{
Console.Write(input + ", ");
}
}
}
class QuickSort : IStrategy
{
public object Implementaion(object data)
{
var charArray = userInput.ToCharArray();
quicksort(charArray, 0, userInput.Length);
Console.Write("Quick Sort: ");
foreach (char s in charArray)
{
Console.Write(s + ", ");
}
}
static void quicksort(char[] userInput, int start, int end)
{
if (start < end)
{
int pivotIndex = partition(userInput, start, end);
quicksort(userInput, start, pivotIndex);
quicksort(userInput, pivotIndex + 1, end);
}
}
static void swap(char[] userInput, int i, int j)
{
char temp = userInput[i];
userInput[i] = userInput[j];
userInput[j] = temp;
}
static int partition(char[] userInput, int start, int end)
{
int pivotIndex = userInput[start];
int swapIndex = start;
for (int i = start + 1; i < end; i++)
{
if (userInput[i] < pivotIndex)
{
swapIndex++;
swap(userInput, i, swapIndex);
}
}
swap(userInput, start, swapIndex);
return swapIndex;
}
public object Implementation(object data)
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
char response;
do
{
Console.WriteLine();
Console.WriteLine("Choose between the two sorting strategies:");
Console.WriteLine(" a) - Bubble Sort");
Console.WriteLine(" b) - Quick Sort");
Console.WriteLine();
Console.Write("Your option: ");
{
response = char.Parse(Console.ReadLine());
}
Console.WriteLine();
switch (response.ToString().ToLower())
{
case "a":
new BubbleSort();
break;
case "b":
new QuickSort();
break;
default:
Console.WriteLine("Invalid answer. Please enter a valid option.");
response = '';
break;
}
} while (response == '');
}
}
}
See Question&Answers more detail:os