PDA

View Full Version : Unit Converter Widget



baray98
6th January 2008, 00:57
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

Gopala Krishna
6th January 2008, 08:22
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 (http://www.qtcentre.org/forum/f-qt-programming-2/t-dialog-and-code-design-issue-3766.html).

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


#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
};
const QStringList freqList(QStringList() << "GHz" << "Hz" << "kHz" << "MHz");
const QStringList resList(QStringList() << "Ohm" << "kOhm");
const QStringList lenList(QStringList() << "mil" << "cm" << "mm" << "m" << "um" << "in" << "ft");
const QStringList angleList(QStringList() << "deg" << "rad");

QString toString(FrequencyUnits f);
QString toString(LengthUnits l);
QString toString(ResistanceUnits r);
QString toString(AngleUnits a);
QString toString(int un,UnitType t);
int toInt(const QString& unit);
double convert(double value, Units::UnitType ut, int fromUnit, int toUnit);
};



unit.cpp


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}
};

QString toString(FrequencyUnits f)
{
return Units::freqList[int(f)];
}
QString toString(LengthUnits l)
{
return Units::lenList[int(l)];
}
QString toString(ResistanceUnits r)
{
return Units::resList[int(r)];
}
QString toString(AngleUnits a)
{
return Units::angleList[int(a)];
}

QString toString(int u, UnitType t)
{
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:
return QString();
};
}

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;
}
};

wysota
6th January 2008, 09:55
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.

baray98
7th January 2008, 07:52
Gopala I am lost in generating a temperatureConversion table

I need it in Celsius , Farenheit, Kelvin


hope you can help me out

baray98

Gopala Krishna
7th January 2008, 11:00
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.

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;
}
}

wysota
7th January 2008, 12:38
Isn't this by any chance an academic/school problem?

high_flyer
7th January 2008, 16:59
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.
Once you have the formula, what is there to do? "hack" the "code"?
Come on...

baray98
7th January 2008, 17:37
I tought there was a way of converting temp using some kinda table but, thank you

baray98

Brandybuck
7th January 2008, 19:21
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>.

Gopala Krishna
8th January 2008, 09:05
I tought there was a way of converting temp using some kinda table but, thank you

baray98

Do you think is it ever possible ? :rolleyes:

high_flyer
8th January 2008, 12:21
Do you think is it ever possible ?
For a defined range and resolution - sure!
Actually used by time critical applications, since its only a LUT lookup instead of a calculation.

wysota
8th January 2008, 14:43
Like a multiplication table from school ;)

Dumidu
17th December 2016, 03:00
i want to know what is the main code for mm to cm to m widget .. gopala krishna can you help me with that.

d_stranz
17th December 2016, 16:06
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?

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?