I was wondering if someone had made a unit converter widget , a widget that can convert the input from a unit (say meters) to a relative unit (say millimeters) i am more interested on the algorithm that they used to support lots of units
baray98
Printable View
I was wondering if someone had made a unit converter widget , a widget that can convert the input from a unit (say meters) to a relative unit (say millimeters) i am more interested on the algorithm that they used to support lots of units
baray98
I haven't done a general widget for unit conversion , so can't paste the code here.
But i had tried to generalize unit conversions. My skills were limited when i did this after Jacek had suggested me in this post.
I had come out with this some time back to help me convert between different units.
If some one has better idea, please do help so that i can improve my code too.
unit.h
Code:
#define MU0 12.566370614e-7 /* magnetic constant */ #define C0 299792458.0 /* speed of light in vacuum */ #define ZF0 376.73031346958504364963 /* wave resistance in vacuum */ namespace Units { enum UnitType { Frequency=0, Length, Resistance, Angle, None = -1 }; enum FrequencyUnits { GHz=0,Hz,KHz,MHz }; enum LengthUnits { mil=0,cm,mm,m,um,in,ft }; enum ResistanceUnits { Ohm=0,kOhm }; enum AngleUnits { Deg=0,Rad }; int toInt(const QString& unit); double convert(double value, Units::UnitType ut, int fromUnit, int toUnit); };
unit.cpp
Code:
namespace Units { // Unit conversion array for length. double lengthConversionTable[7][7] = { { 1.0, 2.54e-3, 2.54e-2, 2.54e-5, 25.4, 1.e-3, 1./12000}, {1./2.54e-3, 1.0, 10.0, 1.e-2, 1.e4, 1./2.54, 1./30.48}, {1./2.54e-2, 1./10., 1.0, 1.e-3, 1.e3, 1./25.4, 1./304.8}, {1./2.54e-5, 1.e2, 1.e3, 1.0, 1.e6, 1./2.54e-2, 1./0.3048}, {1./25.4, 1.e-4, 1.e-3, 1.e-6, 1.0, 1./2.54e4, 1./3.048e5}, {1.e3, 2.54, 25.4, 2.54e-2, 2.54e4, 1.0, 1./12.}, {1.2e4, 30.48, 304.8, 0.3048, 3.048e5, 12.0, 1.0} }; // Unit conversion array for frequencies. double frequencyConversionTable[4][4] = { { 1.0, 1.e9, 1.e6, 1.e3}, { 1.e-9, 1.0, 1.e-3, 1.e-6}, { 1.e-6, 1.e3, 1.0, 1.e-3}, { 1.e-3, 1.e6, 1.e3, 1.0} }; // Unit conversion array for resistances. double resistanceConversionTable[2][2] = { {1.0, 1.e-3}, {1.e3, 1.0} }; // Unit conversion array for angles. double angleConversionTable[2][2] = { {1.0, M_PI/180.0}, {180.0/M_PI, 1.0} }; { return Units::freqList[int(f)]; } { return Units::lenList[int(l)]; } { return Units::resList[int(r)]; } { return Units::angleList[int(a)]; } { switch(t) { case Frequency: return toString(FrequencyUnits(u)); case Length: return toString(LengthUnits(u)); case Resistance: return toString(ResistanceUnits(u)); case Angle: return toString(AngleUnits(u)); default: }; } double convert(double value, Units::UnitType ut, int fromUnit, int toUnit) { double cnv = value; switch(ut) { case Frequency: cnv *= frequencyConversionTable[int(fromUnit)][toUnit]; break; case Length: cnv *= lengthConversionTable[int(fromUnit)][toUnit]; break; case Resistance: cnv *= resistanceConversionTable[int(fromUnit)][toUnit]; break; case Angle: cnv *= angleConversionTable[int(fromUnit)][toUnit]; break; default: break; }; return cnv; } int toInt(const QString& unit) { if(unit == "NA") return None; for(int i=0; i<freqList.size(); ++i) { if(unit == freqList[i]) return i; } for(int i=0; i<lenList.size(); ++i) { if(unit == lenList[i]) return i; } for(int i=0; i<resList.size(); ++i) { if(unit == resList[i]) return i; } for(int i=0; i<angleList.size(); ++i) { if(unit == angleList[i]) return i; } return None; } };
I think the best way would be to have a class (but not a widget) that reads definitions and formulas from an XML or ini file. Then it's just a matter of finding a proper formula, parsing and executing it. You can even keep scripts in the file and execute them using the script engine Qt provides.
Gopala I am lost in generating a temperatureConversion table
I need it in Celsius , Farenheit, Kelvin
hope you can help me out
baray98
I had done this some time before. May be this can be improved.
Code:
enum TemperatureUnit { Celsius, Farenheit, Kelvin }; double convert(double val, TemperatureUnit from, TemperatureUnit to) { if(from == Celsius) { if(to == Kelvin) return val + 273.15; else if(to == Farenheit) return 1.8*val + 32; else return val; } else if(from == Farenheit) { if(to == Celsius) return (val - 32)*1.8; else if(to == Kelvin) return (val - 32)*1.8 + 273.15; else return val; } else { if(to == Celsius) return val - 273.15; else if(to == Farenheit) return (val-273.15)*1.8 + 32; else return val; } }
Isn't this by any chance an academic/school problem?
I'd even say this is not a programming issue, this should be posted on a school math forum.Quote:
Isn't this by any chance an academic/school problem?
Once you have the formula, what is there to do? "hack" the "code"?
Come on...
I tought there was a way of converting temp using some kinda table but, thank you
baray98
I made a Quantity class that did conversions for me, with Weight and Volume subclasses. It was based on Fowler's Quantity Analysis Pattern. You can get it at <http://www.usermode.org/code/quantity-0.1.tar.gz>.
For a defined range and resolution - sure!Quote:
Do you think is it ever possible ?
Actually used by time critical applications, since its only a LUT lookup instead of a calculation.
Like a multiplication table from school ;)
i want to know what is the main code for mm to cm to m widget .. gopala krishna can you help me with that.
Really? You think digging up an 8 year old thread and asking one of the posters is going to help?Quote:
gopala krishna can you help me with that.
1 m = 100 cm = 1000 mm
It's a couple of lines of code - what would be the point of a widget to do that? Maybe you should be a little more clear about what it is you actually want to do.
Is this a homework project by any chance?