Skip to content

Instantly share code, notes, and snippets.

@richseviora
Last active December 3, 2015 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richseviora/780f941dee3f33ec25af to your computer and use it in GitHub Desktop.
Save richseviora/780f941dee3f33ec25af to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnitsExample;
namespace Program
{
class Program
{
static void Main()
{
var MultipleList = new List<Multiple>
{
Multiple.Milli, Multiple.Micro, Multiple.Nano, Multiple.Pico, Multiple.Singular
};
while (true)
{
// Collect User Input
Log("SI converter!\nPlease, enter value: ");
double entryValue = Convert.ToInt32(Console.ReadLine());
// Create the Factor List
Log("\nFactors: ");
var choiceString = "";
for (var i = 0; i < MultipleList.Count; i++)
{
var multiple = MultipleList[i];
choiceString += $"\n{i}) {multiple.Name}";
}
// Collect the First Factor Selection
Log(choiceString);
Log("Enter the first factor: ");
var userSelection = Convert.ToInt32(Console.ReadLine());
// Collect the Second Factor Selection
Log(choiceString);
Log("Enter the second factor");
// Calculate Values
UnitValue value = new UnitValue(MultipleList[userSelection], entryValue);
var secondFactorSelection = Convert.ToInt32(Console.ReadLine());
Multiple secondFactor = MultipleList[secondFactorSelection];
// Submit Answer
Log("The answer is : " + value.ConvertTo(secondFactor).PrefixedValue);
// Check for Exit Request
Log("Go again?\nY / N");
var ans = char.Parse(Console.ReadLine());
if (ans == 'N')
{
break;
}
}
}
private static void Log(string message)
{
Console.WriteLine(message);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnitsExample
{
public struct Multiple
{
public Multiple(string name, int exponent)
{
Name = name;
Exponent = exponent;
}
/// <summary>
/// The name of the multiple/prefix.
/// </summary>
public string Name { get; }
/// <summary>
/// The base 10 power of the prefix. 0 is no prefix, 1 is 10, -1 is -1.
/// </summary>
public int Exponent { get; }
/// <summary>
/// Converts the exponent into a multipler for use in conversion.
/// </summary>
public double Multipler => Math.Pow(10, Exponent);
/// <summary>
/// Converts the provided value to the base value (sans prefix).
/// </summary>
/// <param name="inputValue">Value to be converted into the multiple's unit.</param>
/// <returns>Base value of input.</returns>
public double ConvertToBaseValue(double inputValue)
{
return inputValue/Multipler;
}
/// <summary>
/// Converts the provided value to the base value (sans prefix).
/// </summary>
/// <param name="inputValue">Value to be converted to base.</param>
/// <returns>Base value of input.</returns>
public double ConvertFromBaseValue(double inputValue)
{
return inputValue*Multipler;
}
/// <summary>
/// Creates a UnitValue with the unit provided.
/// </summary>
/// <param name="inputValue">The input value </param>
/// <returns></returns>
public UnitValue CreateValue(double inputValue)
{
var newBaseValue = ConvertToBaseValue(inputValue);
return new UnitValue(this, newBaseValue);
}
public static Multiple Singular => new Multiple(null, 0);
public static Multiple Milli => new Multiple("Milli", -3);
public static Multiple Micro => new Multiple("Micro", -6);
public static Multiple Nano => new Multiple("Nano", -9);
public static Multiple Pico => new Multiple("Pico", -12);
}
/// <summary>
/// This class are representations of values.
/// </summary>
public class UnitValue
{
public UnitValue(Multiple multiple, double baseValue)
{
Multiple = multiple;
BaseValue = baseValue;
}
public Multiple Multiple { get; }
/// <summary>
/// Handles conversion to/from the base value.
/// </summary>
public double PrefixedValue
{
get { return Multiple.ConvertFromBaseValue(BaseValue); }
set { BaseValue = Multiple.ConvertToBaseValue(value); }
}
/// <summary>
/// Stores the actual base value.
/// </summary>
public double BaseValue { get; set; }
/// <summary>
/// Allows for quick conversion between units.
/// </summary>
/// <param name="multiple">The new multiple being used.</param>
/// <returns>The value, represented with the new multiple.</returns>
public UnitValue ConvertTo(Multiple multiple)
{
return new UnitValue(multiple, BaseValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment