Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////
- // Code by DRGC / Arcbinder
- // This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
- //////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Globalization;
- namespace FlagIdentifier
- {
- class Program
- {
- // IDs for localised text
- enum LocalisationID
- {
- EnterValue = 0,
- InvalidInput,
- InputOutOfBounds,
- FlagsSet,
- InputOverflow,
- }
- //Contains strings which the program prints to console (Localise these)
- static string[] LocalisationStrings =
- {
- "Enter flags value (-1 to Exit):",
- "Input must be a non-zero, positive integer, try again",
- "Entered value ( {0} ) higher than largest flag ( {1} ). Try again",
- "These flags are set:",
- "Input out of bounds of type {0}",
- };
- // This array contains the names for your flags
- static string[] FlagNames =
- {
- "Flag Not Named", // 2^0 = 1 = 1 << 0
- "Flag Not Named", // 2^1 = 2 = 1 << 1
- "Flag Not Named", // 2^2 = 4 = 1 << 2
- "Flag Not Named", // 2^3 = 8 = 1 << 3
- "Flag Not Named", // 2^4 = 16 = 1 << 4
- "Flag Not Named", // 2^5 = 32 = 1 << 5
- "Flag Not Named", // 2^6 = 64 = 1 << 6
- "Flag Not Named", // 2^7 = 128 = 1 << 7
- "Flag Not Named", // 2^8 = 256 = 1 << 8
- "Flag Not Named", // 2^9 = 512 = 1 << 9
- "Flag Not Named", // 2^10 = 1024 = 1 << 10
- };
- static int Main()
- {
- // The flag that has been entered
- int flagInt = 0;
- // Highest valid flag (will change as flags are added to (or removed from) FlagNames)
- int highestFlag = 1 << FlagNames.Length;
- // Loop until quit
- while (flagInt == 0)
- {
- // Error flag - set to true if input not in bounds of flagInt's type
- bool overflow = false;
- // Prompt to enter flags value
- Console.Write(LocalisationStrings[(int)LocalisationID.EnterValue]);
- // try to parse input
- try
- {
- flagInt = int.Parse(Console.ReadLine(), CultureInfo.CurrentCulture);
- }
- catch (OverflowException) // Input is out of bounds of a 32-bit integer
- {
- overflow = true;
- flagInt = 0xDEAD;
- }
- catch // Input is not valid integer
- {
- flagInt = 0;
- }
- if (flagInt <= 0)
- {
- if (flagInt != -1)
- Console.WriteLine(LocalisationStrings[(int)LocalisationID.InvalidInput]);
- else
- return 0;
- }
- else if (flagInt > highestFlag || overflow)
- {
- if (!overflow)
- Console.WriteLine(string.Format(CultureInfo.CurrentCulture, LocalisationStrings[(int)LocalisationID.InputOutOfBounds], flagInt, highestFlag));
- else
- Console.WriteLine(string.Format(CultureInfo.CurrentCulture, LocalisationStrings[(int)LocalisationID.InputOverflow], flagInt.GetType().ToString()));
- flagInt = 0;
- }
- else
- {
- Console.WriteLine(LocalisationStrings[(int)LocalisationID.FlagsSet]);
- // Generate flags list
- List<String> flags = new List<string>();
- for (int i = FlagNames.Length - 1; i >= 0; i--)
- {
- int flagValue = 1 << i;
- if (flagInt / flagValue != 0)
- {
- flagInt -= flagValue;
- flags.Insert(0, i + ": " + FlagNames[i]);
- }
- }
- // Print flags
- Console.WriteLine();
- foreach (string s in flags)
- {
- Console.WriteLine(s);
- }
- Console.WriteLine();
- }
- flagInt = 0;
- }
- return -1; // Shouldn't be able to get here
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement