Unit Conversion

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8677 Views - Last Post: 24 November 2010 - 09:09 AM Rate Topic: -----

#1 sharpie349  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 28-October 10

Unit Conversion

Posted 23 November 2010 - 05:13 AM

Hi,
If I wanted to make a program that can convert things like inches, feet, meters, and miles from one another what would be the best design for that?
I have right now two list boxes with one the unit to convert from and the other what unit to convert to. I then have a text box to enter the value. Any ideas?
Thanks so much.
Is This A Good Question/Topic? 0
  • +

Replies To: Unit Conversion

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Unit Conversion

Posted 23 November 2010 - 05:35 AM

Then you have a button to click. When that button is clicked, you do the calculation(s).
Was This Post Helpful? 1
  • +
  • -

#3 sharpie349  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 28-October 10

Re: Unit Conversion

Posted 23 November 2010 - 06:07 AM

Yeah I already have that as a given. It just seems like it would be thousands of lines of code with all the combinations between what the initial unit is and what unit you want to convert it to.
I attached a picture of what my design is like.
Is there any efficient way of being able to do these conversions?
Was This Post Helpful? 0
  • +
  • -

#4 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Unit Conversion

Posted 23 November 2010 - 06:19 AM

View Postsharpie349, on 23 November 2010 - 11:13 AM, said:

Hi,
If I wanted to make a program that can convert things like inches, feet, meters, and miles from one another what would be the best design for that?
I have right now two list boxes with one the unit to convert from and the other what unit to convert to. I then have a text box to enter the value. Any ideas?
Thanks so much.


hi ,
you can use two combobox, a button and textbox for inserting value
Was This Post Helpful? 1
  • +
  • -

#5 sharpie349  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 28-October 10

Re: Unit Conversion

Posted 23 November 2010 - 06:39 AM

Yeah I am fine doing that the thing is the possibility of need hundreds of calculatations.
For example the difference between inches to miles and inches to kilometers.
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Unit Conversion

Posted 23 November 2010 - 07:21 AM

What's your question? Just use two comboBoxes for TO and FROM conversion.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Unit Conversion

Posted 23 November 2010 - 08:28 AM

To answer the question, no, there is no "easy" way of doing 90 conversions between the different units of measurement. It will not be 1000's of lines of code though. If I am not mistaken, most conversions will simply be a one line math statement.

For example, converting 2 miles to inches would be something like this..

decimal miles = 2;
decimal inches = miles * 5280 * 12;


Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Unit Conversion

Posted 23 November 2010 - 08:35 AM

OK, I think he's had enough of the UI advice, even though that was the initial question.

As to your concern about "hundreds of calculations" it'd probably be best not to store the formulas in code. It'd suggest basically creating a calculator (or at least an expression parser) and storing formulas in an external file (perhaps a database) in a parsable format. Then you can parse the expression into steps, replace "variables" with values, and calculate.
Was This Post Helpful? 0
  • +
  • -

#9 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Unit Conversion

Posted 23 November 2010 - 09:10 AM

You have to write all the combinations, that's right.

I would define a Object for the Unit.
Example:
public class Unit() {
   public String Id { get; set; }
   public String Name { get; set; }
   ... // Descriptions
}


Then you have for example 2 Units:
  • 1, Meter
  • 2, Inch


Then you define a Conversion Class
public class Conversion() {
   public String IdInput { get; set; }
   public String IdOutput{ get; set; }
   public ConversionType ConversionType { get; set; }
}


And in this Conversion-Class you can define:
IdInput = 1
IdOutput = 2
ConversionType = MeterToFeed

So and now you define a Class full of CustomConverters in which you have Methods like:
public class MeterToFeed : CustomConverter
{ 
    ...
    public override float Convert(float input) {
        return (float)(source * 3.280839);
    }
}


Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Unit Conversion

Posted 23 November 2010 - 10:39 AM

See, I don't think that's the best of approaches. It's very static. If you want to add a new conversion, you have to recompile, and there's no relationships involved there.

I think that you could do this with a tiny database (or XML file read into a table in memory), and a postfix expression parser. You set up a table of units with IDs, and a table of conversion expressions, with an input unit ID and an output unit ID. That way, you can dynamically generate your drop lists from a select statement (LINQ would be quite good at this). The conversion expression would be a series of postfix operations, with a placeholder for user input.

Then get an input from the user, replace the placeholder, and parse the expression.
Was This Post Helpful? 0
  • +
  • -

#11 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Unit Conversion

Posted 23 November 2010 - 12:33 PM

Instead of coding the conversion hard, it could also be done by adding the factor in the Conversion Class

public class Conversion() {
   public String IdInput { get; set; }
   public String IdOutput{ get; set; }
   public Float Factor { get; set; }
}



All classes are simple ValueObjects and can be stored in a XML (same thought as insertAlias :))
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Unit Conversion

Posted 23 November 2010 - 01:01 PM

Yeah, factors would probably be better than a postfix expression, since you should be able to reduce every conversion to a factor. I hadn't thought that through all the way.
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Unit Conversion

Posted 23 November 2010 - 04:26 PM

I realized that not everything can be reduced to just a factor. Consider the temperature equations:

Tf = (5 / 9) * (Tc - 32)
Tc = ((9 / 5) * Tf) + 32

Since addition is involved, you'd have to have two steps. This is parsable though.

By the way, this got me off on the trail of parsing postfix expressions, so I wrote a little parser class:

class PostfixParser
{
    const string Placeholder = "[x]";

    private static Dictionary<string, Func<double, double, double>> op;

    private static List<string> operators = new List<string>() { "+", "-", "*", "/" };

    static PostfixParser()
    {
        op = new Dictionary<string, Func<double, double, double>>();
        op.Add("+", (x, y) => x + y);
        op.Add("-", (x, y) => x - y);
        op.Add("*", (x, y) => x * y);
        op.Add("/", (x, y) => x / y);
        op.Add("^", (x, y) => Math.Pow(x, y));
    }

    public static double ParseExp(string exp, double? val)
    {
        string[] tokens = exp.Split(new string[] { " " } , StringSplitOptions.RemoveEmptyEntries);
        if (tokens.Contains(Placeholder) && val == null)
            throw new InvalidOperationException("Placeholder token detected in expression, but parameter \"val\" was null.");
        Stack<double> stack = new Stack<double>();
        foreach (string token in tokens)
        {
            if (operators.Contains(token))
            {
                if (stack.Count < 2)
                    throw new InvalidOperationException("Operator imbalance.");
                double y = stack.Pop();
                double x = stack.Pop();
                stack.Push(op[token](x, y));
            }
            else
            {
                double n;
                if (token == Placeholder)
                    n = val.Value;
                else if (!double.TryParse(token, out n))
                    throw new InvalidOperationException("Invalid token detected: " + token);
                stack.Push(n);
            }
        }

        if (stack.Count > 1)
            throw new InvalidOperationException("More than one result on the stack.");
        if (stack.Count < 1)
            throw new InvalidOperationException("No results on the stack.");
        return stack.Pop();
    }

    public static double ParseExp(string exp)
    {
        return ParseExp(exp, null);
    }
}


And it can be used like this:
string fToCExp = "5 9 / [x] 32 - *";
string cToFExp = "9 5 / [x] * 32 + ";
Console.WriteLine("100C converted to F:");
Console.WriteLine(PostfixParser.ParseExp(cToFExp, 100));
Console.WriteLine("32F converted to C:");
Console.WriteLine(PostfixParser.ParseExp(fToCExp, 32));


So if the OP were to do something like this, he could have his conversion expressions laid out in postfix notation, with "[x]" as the placeholder for the input. You can just define these in XML or a DB.
Was This Post Helpful? 1
  • +
  • -

#14 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Unit Conversion

Posted 24 November 2010 - 12:18 AM

You got right insertAlias, I didn't thought about Celsius to Fahrenheit.
But it's very easy to solve :).

Just add a Shift-Factor to the Conversion Unit:
public class Conversion() {
   public String IdInput { get; set; }
   public String IdOutput{ get; set; }
   public Float Factor { get; set; }
   public Float Shift { get; set; }
}


So every Conversion can be done with these 2 values.

This post has been edited by mavarazo: 24 November 2010 - 12:19 AM

Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Unit Conversion

Posted 24 November 2010 - 06:12 AM

Sounds like has a good tutorial or snippet on his hands. That's really nice.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2