I'm trying to make a metric system converter where you input a number, the unit of measurement and the desired output unit (what to convert it to), and I also separated the converter as a different method.
The program takes the input and the only thing I can think of how to convert it is by converting the given number back to meter by dividing the input number by the original unit that is equivalent to 1 mile then multiply it by the desired output unit that is equivalent to 1 meter.
double m_Out = 1; // These are all equivalent to 1 meter //meter
double mm_Out = 1000; //millimeters
double cm_Out = 100; //centimeters
double mi_Out = 0.000621371192; //miles
double in_Out = 39.3700787; //inches
double km_Out = 0.001; //kilometers
double ft_Out = 3.2808399; //feet
double yd_Out = 1.0936133; //yard
I am also using a 'double' variable type to prevent exceptions when an input number has a decimal.
I'm wondering if there is another way of doing it other than long lines of if statements. Currently I'm using multiple if statements to check what the input unit is so that we know what unit it is and what to do with it then goes to another if statement to see what the desired output unit is.
This is the code I'm going for now:
using System;
namespace num4
{
internal class Program
{
static void Converter(double firstNum, string firstUnit, string unitOut)
{
double m_Out = 1;
double mm_Out = 1000;
double cm_Out = 100;
double mi_Out = 0.000621371192;
double in_Out = 39.3700787;
double km_Out = 0.001;
double ft_Out = 3.2808399;
double yd_Out = 1.0936133;
//convert input back to miles (dividing by its own unit value equivalent to 1 mile) before comverting to desired output unit
string errornotfound = "Input Unit is not recognized, make sure the units are found on the given list.";
if (firstUnit == "m") // if the input is m
{
string unit = "m";
if (unitOut == "mm")
{
double res = firstNum * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = firstNum * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = firstNum * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = firstNum * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = firstNum * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = firstNum * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = firstNum * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = firstNum * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "mm") // If the input is mm
{
if (unitOut == "mm")
{
double res = (firstNum / mm_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / mm_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / mm_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / mm_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / mm_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / mm_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / mm_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / mm_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "cm")
{
if (unitOut == "mm")
{
double res = (firstNum / cm_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / cm_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / cm_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / cm_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / cm_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / cm_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / cm_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / mm_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "mi")
{
if (unitOut == "mm")
{
double res = (firstNum / mi_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / mi_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / mi_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / mi_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / mi_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / mi_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / mi_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / mi_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "in")
{
if (unitOut == "mm")
{
double res = (firstNum / in_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / in_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / in_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / in_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / in_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / in_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / in_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / in_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "km")
{
if (unitOut == "mm")
{
double res = (firstNum / km_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / km_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / km_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / km_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / km_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / km_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / km_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / km_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "ft")
{
if (unitOut == "mm")
{
double res = (firstNum / ft_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / ft_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / ft_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / ft_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / ft_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / ft_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / ft_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / ft_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else if (firstUnit == "yd")
{
if (unitOut == "mm")
{
double res = (firstNum / yd_Out) * mm_Out;
Console.WriteLine(res);
}
else if (unitOut == "m")
{
double res = (firstNum / yd_Out) * m_Out;
Console.WriteLine(res);
}
else if (unitOut == "cm")
{
double res = (firstNum / yd_Out) * cm_Out;
Console.WriteLine(res);
}
else if (unitOut == "mi")
{
double res = (firstNum / yd_Out) * mi_Out;
Console.WriteLine(res);
}
else if (unitOut == "in")
{
double res = (firstNum / yd_Out) * in_Out;
Console.WriteLine(res);
}
else if (unitOut == "km")
{
double res = (firstNum / yd_Out) * km_Out;
Console.WriteLine(res);
}
else if (unitOut == "ft")
{
double res = (firstNum / yd_Out) * ft_Out;
Console.WriteLine(res);
}
else if (unitOut == "yd")
{
double res = (firstNum / yd_Out) * yd_Out;
Console.WriteLine(res);
}
else
{
Console.WriteLine(errornotfound);
}
}
else
{
Console.WriteLine(errornotfound);
}
}
static void Main(string[] args)
{
/*Metric Converter
* Write a program that can convert a given input distance between 8 units of measurement
* [m,mm,cm,mi,in,km,ft,yd]
*
*
* input
* '''
* meter(m)
*
* sample input
* '''m
* number to convert (6)a
* type of unit (km)
* output unit to convert to (ft)r
* [6km >> ???ft]k
* -----------------------------
* start
* input for num1
* input for unit1
* input for out_uni1
* convert unit>>>>out_uni1
* print out_uni1
* end
* -----------------------------
*/
string border = "--------------------------------------------------------------";
Console.WriteLine(border);
Console.WriteLine(" Metric Converter");
Console.WriteLine(border);
Console.Write("What number would you like to convert? ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("");
Console.WriteLine(border);
Console.WriteLine("Units: [m], [mm], [cm], [mi], [in], [km], [ft], [yd]");
Console.WriteLine(border);
Console.Write("What is the original unit? ");
string inputUnit = Console.ReadLine();
Console.Write("What unit would you like to convert it to? ");
string inputOutUnit = Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine(border);
Console.Write("The result is: ");
Converter(num1, inputUnit, inputOutUnit);
Console.ReadKey();
}
}
}
Freely add your suggestions as I would like to hear alternatives.
/*Metric Converter
* Write a program that can convert a given input distance between 8 units of measurement
* [m,mm,cm,mi,in,km,ft,yd]
*
*
* input
* '''
* meter(m)
*
* sample input
* '''
* number to convert (6)
* type of unit (km)
* output unit to convert to (ft)
* [6km >> ???ft]
* -----------------------------mjgs
* start
* input for num1
* input for unit1
* input for out_uni1
* convert unit>>>>out_uni1
* print out_uni1
* end
* -----------------------------mjgs
*/
CodePudding user response:
I think you can just do switch case on it.
switch (firstUnit)
{
case "m":
switch (unit)
{
case "mm":
res = 0.0;
break;
default:
break;
}
break;
case "mm":
switch (unit)
{
case "cm":
res = 1.0;
break;
default:
break;
}
break;
default:
break;
}
CodePudding user response:
If you're using C#8 or above, you can use the new switch expression to shorten this significantly. The above code would then become:
static void Converter ( double firstNum, string firstUnit, string unitOut )
{
double m_Out = 1;
double mm_Out = 1000;
double cm_Out = 100;
double mi_Out = 0.000621371192;
double in_Out = 39.3700787;
double km_Out = 0.001;
double ft_Out = 3.2808399;
double yd_Out = 1.0936133;
//convert input back to miles (dividing by its own unit value equivalent to 1 mile) before comverting to desired output unit
string errornotfound = "Input Unit is not recognized, make sure the units are found on the given list.";
try
{
var firstCalculation = firstNum / ( firstUnit switch
{
"m" => m_Out,
"mm" => mm_Out,
"cm" => cm_Out,
"mi" => mi_Out,
"in" => in_Out,
"km" => km_Out,
"ft" => ft_Out,
"yd" => yd_Out,
_ => throw new ArgumentException("First unit")
} );
var result = firstCalculation * ( unitOut switch
{
"m" => mm_Out,
"mm" => mm_Out,
"cm" => cm_Out,
"mi" => mi_Out,
"in" => in_Out,
"km" => km_Out,
"ft" => ft_Out,
"yd" => yd_Out,
_ => throw new ArgumentException("Output unit")
} );
Console.WriteLine (result);
}
catch
{
Console.WriteLine ( errornotfound );
}
}
Please note: I'm only answering the question "How do I shorten my if statements in c sharp?". I haven't commented on whether this is the best method or not.
UPDATE: as per the comment by @jmcilhinney, a Dictionary<string, double> would reduce the code even further:
internal class Program
{
const string NOTFOUND = "Input Unit is not recognized, make sure the units are found on the given list.";
private static Dictionary<string, double> values = new Dictionary<string, double>
{
["m"] = 1,
["mm"] = 1000,
["cm"] = 100,
["mi"] = 0.000621371192,
["in"] = 39.3700787,
["km"] = 0.001,
["ft"] = 3.2808399,
["yd"] = 1.0936133,
};
static string Converter ( double firstNum, string firstUnit, string unitOut )
{
//convert input back to miles (dividing by its own unit value equivalent to 1 mile) before comverting to desired output unit
if ( !values.TryGetValue ( firstUnit, out var value ) )
return NOTFOUND;
var firstCalculation = firstNum / value;
if ( !values.TryGetValue ( unitOut, out value ) )
return NOTFOUND;
return ( firstCalculation * value ).ToString ( );
}
// ...
}
CodePudding user response:
Define enum
enum Unit
{
Meter,
Millimetre,
Centimeter,
Mile,
Inch,
Kilometer,
Foot,
Yard
}
Extend its functionality
static class Extensions
{
public static double InMeters(this Unit unit)
{
switch (unit)
{
default:
return 1;
case Unit.Millimetre:
return 0.001;
case Unit.Centimeter:
return 0.01;
case Unit.Mile:
return 1609.34;
case Unit.Inch:
return 0.0254;
case Unit.Kilometer:
return 1_000;
case Unit.Foot:
return 0.3048;
case Unit.Yard:
return 0.9144;
}
}
}
Then use
static void Main(string[] args)
{
double value = 100;
Unit inUnit = Unit.Centimeter;
Unit outUnit = Unit.Meter;
var result = value * inUnit.InMeters() / outUnit.InMeters();
Console.WriteLine(result);
Console.ReadKey();
}