Ovako nešto:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Parsing_Resistor_Value
{
class Program
{
static void Main(string[] args)
{
var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "Silver", "Gold" };
var factorDictionary = new Dictionary<char, int>() { { 'R', 1 }, { 'K', 1000 }, { 'M', 1000000 }, { 'G', 1000000000 } };
string res = string.Empty;
while (string.IsNullOrEmpty(res))
{
Console.Write("Enter resistor value (use R, K, M ,G notation for .): ");
res = Console.ReadLine();
}
var lastChar = res.Last();
var isUnitCorrect = factorDictionary.ContainsKey(lastChar);
var value = res.Substring(0, res.Length - 1);
var isValueCorrect = !value.Any(x => !char.IsDigit(x));
if (isUnitCorrect && isValueCorrect)
{
int mul = factorDictionary[lastChar];
double val = double.Parse(value) * mul;
int third = 0;
if (val < 1)
{
val *= 100;
third = 9;
}
else if (val < 10)
{
val *= 10;
third = 10;
}
res = val.ToString();
if (res.Count() > 11)
Console.WriteLine("Invalid value");
else
Console.WriteLine(colours[res[0] - '0'] + " " + colours[res[1] - '0'] + " " + colours[third != 0 ? third : res.Count() - 2]);
}
else
Console.WriteLine("Invalid value!");
Console.WriteLine("\nPress any key to exit.");
Console.ReadKey();
}
}
}
|