Skip to the content of the web site.

Project R.1: Unit conversions

Create a program for converting between units.

First, you will have to determine a set of units you will use, as you will have to recognize them.

For example, this may include the SI units:

NameSymbol
meterm
gramg
seconds
ampereA
kelvinK
candelacd
molemol
voltV
ohmOmega
teslaT
weberWb
faradF
henryH
siemensS
coulombC
wattW
newtonN
jouleJ
pascalPa
literL

Each of these can have a prefix:

PrefixSymbolFactor
yotta Y $10^{24}$
zetta Z $10^{21}$
exa E $10^{18}$
peta P $10^{15}$
tera T $10^{12}$
giga G $10^{9}$
mega M $10^{6}$
kilo k $10^{3}$
hecto h $10^{2}$
deca da $10^{1}$
deci d $10^{-1}$
centi c $10^{-2}$
milli m $10^{-3}$
micro u $10^{-6}$
nano n $10^{-9}$
pico p $10^{-12}$
femto f $10^{-15}$
atto a $10^{-18}$
zepto z $10^{-21}$
yocto y $10^{-24}$

Your first project should be to write a function that recognizes the symbol and a possible prefix. Thus, you should recognize kg as 1000 grams, and dm as one-tenth of a meter.

There may also be non-SI units:

NameSymbol
footft
yardyd
milemi
gallongal
pintpt
ounceoz
fluid ouncefl oz
graingr

Your program may then work as follows:

Enter a quantity: 3.25 m
Enter a unit to convert to: yd
3.25 m = 3.55424321959755 yd

Enter a quantity: 3 pt
Enter a unit to convert to: L
3 pt = 1.419529419 L