Browse Source

Add weight units and convenience functions to Units

Neal Wilson 11 years ago
parent
commit
99198ac580
1 changed files with 128 additions and 1 deletions
  1. 128
    1
      src/units.w

+ 128
- 1
src/units.w View File

22
 			Fahrenheit = 10144,
22
 			Fahrenheit = 10144,
23
 			Celsius = 10143,
23
 			Celsius = 10143,
24
 			Kelvin = 10325,
24
 			Kelvin = 10325,
25
-			Rankine = 10145
25
+			Rankine = 10145,
26
+			Pound = 15876,
27
+			Kilogram = 15877,
28
+			Ounce = 1,
29
+			Gram = 2
26
 		};
30
 		};
27
 		static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
31
 		static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
28
 		static double convertRelativeTemperature(double value, Unit fromUnit, Unit toUnit);
32
 		static double convertRelativeTemperature(double value, Unit fromUnit, Unit toUnit);
29
 		static bool isTemperatureUnit(Unit unit);
33
 		static bool isTemperatureUnit(Unit unit);
34
+		static double convertWeight(double value, Unit fromUnit, Unit toUnit);
35
+		static bool isWeightUnit(Unit unit);
30
 };
36
 };
31
 
37
 
32
 #endif
38
 #endif
175
 				case Rankine:
181
 				case Rankine:
176
 					return value;
182
 					return value;
177
 					break;
183
 					break;
184
+				default:
185
+					return 0;
186
+					break;
178
 			}
187
 			}
179
 			break;
188
 			break;
180
 		case Celsius:
189
 		case Celsius:
192
 				case Rankine:
201
 				case Rankine:
193
 					return value * (9.0 / 5.0);
202
 					return value * (9.0 / 5.0);
194
 					break;
203
 					break;
204
+				default:
205
+					return 0;
206
+					break;
195
 			}
207
 			}
196
 			break;
208
 			break;
197
 		case Kelvin:
209
 		case Kelvin:
209
 				case Rankine:
221
 				case Rankine:
210
 					return value * (9.0 / 5.0);
222
 					return value * (9.0 / 5.0);
211
 					break;
223
 					break;
224
+				default:
225
+					return 0;
226
+					break;
212
 			}
227
 			}
213
 			break;
228
 			break;
214
 		case Rankine:
229
 		case Rankine:
226
 				case Rankine:
241
 				case Rankine:
227
 					return value;
242
 					return value;
228
 					break;
243
 					break;
244
+				default:
245
+					return 0;
246
+					break;
229
 			}
247
 			}
230
 			break;
248
 			break;
231
 		default:
249
 		default:
235
 	return 0;
253
 	return 0;
236
 }
254
 }
237
 
255
 
256
+@ Units of force are handled similarly to units of temperature.
257
+
258
+@(units.cpp@>=
259
+double Units::convertWeight(double value, Unit fromUnit, Unit toUnit)
260
+{
261
+	if(isWeightUnit(fromUnit) && isWeightUnit(toUnit))
262
+	{
263
+		switch(fromUnit)
264
+		{
265
+			case Pound:
266
+				switch(toUnit)
267
+				{
268
+					case Pound:
269
+						return value;
270
+						break;
271
+					case Kilogram:
272
+						return value / 2.2;
273
+						break;
274
+					case Ounce:
275
+						return value * 16.0;
276
+						break;
277
+					case Gram:
278
+						return value / 0.0022;
279
+						break;
280
+					default:
281
+						return 0;
282
+						break;
283
+				}
284
+				break;
285
+			case Kilogram:
286
+				switch(toUnit)
287
+				{
288
+					case Pound:
289
+						return value * 2.2;
290
+						break;
291
+					case Kilogram:
292
+						return value;
293
+						break;
294
+					case Ounce:
295
+						return value * 35.2;
296
+						break;
297
+					case Gram:
298
+						return value * 1000.0;
299
+						break;
300
+					default:
301
+						return 0;
302
+						break;
303
+				}
304
+				break;
305
+			case Ounce:
306
+				switch(toUnit)
307
+				{
308
+					case Pound:
309
+						return value / 16.0;
310
+						break;
311
+					case Kilogram:
312
+						return value / 35.2;
313
+						break;
314
+					case Ounce:
315
+						return value;
316
+						break;
317
+					case Gram:
318
+						return value / 0.0352;
319
+						break;
320
+					default:
321
+						return 0;
322
+						break;
323
+				}
324
+				break;
325
+			case Gram:
326
+				switch(toUnit)
327
+				{
328
+					case Pound:
329
+						return value * 0.0022;
330
+						break;
331
+					case Kilogram:
332
+						return value / 1000.0;
333
+						break;
334
+					case Ounce:
335
+						return value * 0.0352;
336
+						break;
337
+					case Gram:
338
+						return value;
339
+						break;
340
+					default:
341
+						return 0;
342
+						break;
343
+				}
344
+				break;
345
+			default:
346
+				return 0;
347
+				break;
348
+		}
349
+	}
350
+	return 0;
351
+}
352
+
353
+bool Units::isWeightUnit(Unit unit)
354
+{
355
+	if(unit == Pound ||
356
+	   unit == Kilogram ||
357
+	   unit == Ounce ||
358
+	   unit == Gram)
359
+	{
360
+		return true;
361
+	}
362
+	return false;
363
+}
364
+
238
 @ This class is exposed to the host environment. Note the lack of constructor.
365
 @ This class is exposed to the host environment. Note the lack of constructor.
239
 We do not wish to create any instances, just have access to the |Unit|
366
 We do not wish to create any instances, just have access to the |Unit|
240
 enumeration.
367
 enumeration.

Loading…
Cancel
Save