Last active
December 24, 2015 08:39
-
-
Save RMcGee/6771626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
Char menuItem; | |
Console.WriteLine("Welcome to ze calculator"); | |
menuItem = GetMenuItem(); | |
while (menuItem != 'X') | |
{ | |
ProcessMenuItem(menuItem); | |
menuItem = GetMenuItem(); | |
}// while | |
Console.WriteLine("Goodbye, user"); | |
Console.ReadLine(); | |
}// Main | |
// OUT IN | |
static Char GetMenuItem() | |
{ | |
Char menuItem; | |
DisplayMenu(); | |
menuItem = Char.ToUpper(Char.Parse(Console.ReadLine())); | |
while (menuItem != 'F' && menuItem != 'C' && menuItem != 'I' && menuItem != 'X') | |
{ | |
Console.WriteLine("Invalid menu selection"); | |
DisplayMenu(); | |
menuItem = Char.ToUpper(Char.Parse(Console.ReadLine())); | |
}// while | |
return menuItem; | |
}// GetMenuItem | |
// OUT IN | |
static void DisplayMenu() | |
{ | |
Console.WriteLine(); // Writes a blank line | |
Console.WriteLine("Enter one of the following items: "); | |
Console.WriteLine("F - Factorial\nC - Combination\nI - Fibbonacci\nX - Exit\n"); | |
}// DisplayMenu | |
// OUT IN | |
static void ProcessMenuItem(Char MenuItem) | |
{ | |
switch (MenuItem) | |
{ | |
case 'F': | |
ProcessFactorial(); | |
break; | |
case 'I': | |
ProcessFibonacci(); | |
break; | |
case 'C': | |
ProcessCombination(); | |
break; | |
}//switch | |
//Console.WriteLine("ProcessMenuItem"); | |
}//ProcessMenuItem | |
// OUT IN | |
static void ProcessFactorial() | |
{ | |
Int32 n, nFactorial; | |
n = GetPositiveInteger("Enter positive n value"); | |
nFactorial = Factorial(n); | |
Console.WriteLine(n + " factorial is " + nFactorial); | |
}//ProcessFactorial | |
// OUT IN | |
static void ProcessFibonacci() | |
{ | |
Int32 n, nthFibonacci; | |
n = GetPositiveInteger("Enter position in sequence"); | |
nthFibonacci = Fibonacci(n); | |
Console.WriteLine("The " + n + " number in the sequence is " + nthFibonacci); | |
}//ProcessFibonacci | |
// OUT IN | |
static void ProcessCombination() | |
{ | |
Int32 n, r, nChooseR; | |
n = GetPositiveInteger("Enter number of objects to choose from"); | |
r = GetPositiveInteger("Enter number of objects to choose"); | |
while (r > n) | |
{ | |
Console.WriteLine("Error - must be less than or equal to " + n); | |
}//while | |
nChooseR = Combination(n, r); | |
Console.WriteLine("There are " + nChooseR + " ways to choose " + r + " of " + n + " objects"); | |
}//ProcessCombination | |
// OUT IN | |
static Int32 Factorial(Int32 n) | |
{ | |
Int32 nFactorial = 1; | |
for (; n > 1; n--) | |
nFactorial *= n; | |
return nFactorial; | |
}//Factorial | |
// OUT IN | |
static Int32 Fibonacci(Int32 n) | |
{ | |
Int32 count = 0, current = 0, previous = 1, previousPrevious = 0; | |
for (; count < n; count++) | |
{ | |
previousPrevious = previous; | |
previous = current; | |
current = previous + previousPrevious; | |
} | |
return current; | |
}//Fibonacci | |
// OUT IN | |
static Int32 Combination(Int32 n, Int32 r) | |
{ | |
return Factorial(n) / (Factorial(r) * Factorial(n - r)); | |
}//Combination | |
static Int32 GetPositiveInteger(String prompt) | |
{ | |
Int32 n; | |
Console.WriteLine(prompt); | |
n = Int32.Parse(Console.ReadLine()); | |
while (n < 0) | |
{ | |
Console.WriteLine("Input Error - Input must be positive"); | |
Console.WriteLine(prompt); | |
n = Int32.Parse(Console.ReadLine()); | |
}//while | |
return n; | |
}//GetPositiveInteger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment