--- a/UnitConverter.st Wed Aug 06 00:57:59 1997 +0200
+++ b/UnitConverter.st Thu Aug 07 12:55:32 1997 +0200
@@ -48,6 +48,7 @@
Transcript showCR:
(UnitConverter convert:1 from:#liter to:#floz)
[exEnd]
+
or, how many square-meters an acre is:
[exBegin]
Transcript showCR:
@@ -64,11 +65,12 @@
[exEnd]
- how fast do I drive ?
+ how fast do I drive ? :-)
[exBegin]
Transcript showCR:(
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr')
[exEnd]
+ how fast does Chris drive ? :-)
[exBegin]
Transcript showCR:(
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr')
@@ -86,6 +88,29 @@
[exEnd]
+ distances:
+
+ [exBegin]
+ Transcript showCR:(
+ UnitConverter convert:1 from:#'lightsecond' to:#'kilometer')
+ [exEnd]
+ thats the same:
+ [exBegin]
+ Transcript showCR:(
+ UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer')
+ [exEnd]
+ a days travel ...
+ [exBegin]
+ Transcript showCR:(
+ UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer')
+ [exEnd]
+ a year travel ...
+ [exBegin]
+ Transcript showCR:(
+ UnitConverter convert:1 from:#'lightyear' to:#'kilometer')
+ [exEnd]
+
+
real estate buyers might want to know, how many acres
a german ar is:
[exBegin]
@@ -98,7 +123,12 @@
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2')
[exEnd]
-
+ how many tea spoons are there in a cubic meter ?
+ (roughly, since a teaspoon is not a standard unit)
+ [exBegin]
+ Transcript showCR:
+ (UnitConverter convert:1 from:#'meter^3' to:#teaspoon)
+ [exEnd]
how wide is a US page in inches:
[exBegin]
Transcript showCR:
@@ -163,6 +193,11 @@
Scaling at:#atto put:(1/1000000000000000000).
+ "/ ---------- velocity -------------
+
+ Constants at:#lightSpeed put:#(2.997925E8 #'m/s').
+
+
"/ -------------- length -------------
Aliases at:#km put:#kilometer.
@@ -191,6 +226,7 @@
"/ nautic
self addConversion:1852 from:#'nautical-mile' to:#meter.
+ self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'.
"/ ---------- time -------------
@@ -200,11 +236,6 @@
self addConversion:365.24219879 from:#yr to:#dy.
- "/ ---------- velocity -------------
-
- Constants at:#lightSpeed put:#(2.997925E8 #'m/s').
-
-
"/ ---------- printing -------------
"/ inch to (roughly) a typesetter point
@@ -381,8 +412,45 @@
UnitConverter initialize
"
- "Modified: 29.3.1997 / 19:11:52 / cg"
"Created: 22.7.1997 / 13:56:40 / cg"
+ "Modified: 6.8.1997 / 17:16:00 / cg"
+! !
+
+!UnitConverter class methodsFor:'accessing'!
+
+scalings
+ "return the set of known scaling prefixes"
+
+ ^ Scaling keys
+
+ "
+ UnitConverter scalings
+ "
+
+ "Created: 6.8.1997 / 16:53:19 / cg"
+ "Modified: 6.8.1997 / 16:58:11 / cg"
+!
+
+units
+ "return the set of known units"
+
+ |setOfUnits|
+
+ setOfUnits := IdentitySet new.
+ Conversions keysAndValuesDo:[:srcUnit :conversionInfo |
+ setOfUnits add:srcUnit.
+ conversionInfo keysDo:[:targetUnit |
+ setOfUnits add:targetUnit
+ ]
+ ].
+ ^ setOfUnits
+
+ "
+ UnitConverter units
+ "
+
+ "Created: 6.8.1997 / 16:57:08 / cg"
+ "Modified: 6.8.1997 / 16:57:47 / cg"
! !
!UnitConverter class methodsFor:'conversions'!
@@ -397,6 +465,8 @@
sF1 sF2 dF1 dF2
s d|
+"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit).
+
Conversions isNil ifTrue:[
self initializeConversions
].
@@ -467,6 +537,11 @@
^ self convert:(howMany*val) from:unit to:destUnit
].
+ "/ compounds (^ , / or *) are very naively parsed
+ "/ need a full expression parser (tree) for full power.
+ "/ I leave that as an excercise to you ...
+
+
"/ working with squares or cubics ?
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[
@@ -564,6 +639,36 @@
].
].
+ "/ if working with a product, try each component
+
+ (sourceUnit includes:$*) ifTrue:[
+ i := sourceUnit indexOf:$*.
+ sF1 := sourceUnit copyTo:(i - 1).
+ sF2 := sourceUnit copyFrom:(i + 1).
+
+ "/ see what we have ...
+
+ self units do:[:aUnit |
+ |pref iUnit factor2 rslt|
+
+ pref := sF1 , '*'.
+ (aUnit startsWith:pref) ifTrue:[
+ "/ ok; want a/b -> x
+ "/ found a/c -> any
+ "/ what about c->b ?
+
+ iUnit := aUnit copyFrom:pref size + 1.
+ factor2 := self convert:1 from:sF2 to:iUnit.
+ factor2 notNil ifTrue:[
+ "/ good ...
+ rslt := self convert:(factor2 * howMany)
+ from:aUnit to:destUnit.
+ rslt notNil ifTrue:[^ rslt].
+ ]
+ ]
+ ].
+ ].
+
^ self noConversionFrom:sourceUnit to:destUnit.
"direct - how many meters are there in two inches:
@@ -594,7 +699,7 @@
"
"Created: 31.5.1996 / 16:23:38 / cg"
- "Modified: 22.7.1997 / 13:57:20 / cg"
+ "Modified: 6.8.1997 / 18:10:22 / cg"
!
inchToMillimeter:inches
@@ -709,5 +814,5 @@
!UnitConverter class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.10 1997-07-22 11:58:06 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.11 1997-08-07 10:55:32 cg Exp $'
! !