Просмотр исходного кода

Add weight units and convenience functions to Units

Neal Wilson 11 лет назад
Родитель
Сommit
99198ac580
1 измененных файлов: 128 добавлений и 1 удалений
  1. 128
    1
      src/units.w

+ 128
- 1
src/units.w Просмотреть файл

@@ -22,11 +22,17 @@ class Units: public QObject
22 22
 			Fahrenheit = 10144,
23 23
 			Celsius = 10143,
24 24
 			Kelvin = 10325,
25
-			Rankine = 10145
25
+			Rankine = 10145,
26
+			Pound = 15876,
27
+			Kilogram = 15877,
28
+			Ounce = 1,
29
+			Gram = 2
26 30
 		};
27 31
 		static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
28 32
 		static double convertRelativeTemperature(double value, Unit fromUnit, Unit toUnit);
29 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 38
 #endif
@@ -175,6 +181,9 @@ double Units::convertRelativeTemperature(double value, Unit fromUnit, Unit toUni
175 181
 				case Rankine:
176 182
 					return value;
177 183
 					break;
184
+				default:
185
+					return 0;
186
+					break;
178 187
 			}
179 188
 			break;
180 189
 		case Celsius:
@@ -192,6 +201,9 @@ double Units::convertRelativeTemperature(double value, Unit fromUnit, Unit toUni
192 201
 				case Rankine:
193 202
 					return value * (9.0 / 5.0);
194 203
 					break;
204
+				default:
205
+					return 0;
206
+					break;
195 207
 			}
196 208
 			break;
197 209
 		case Kelvin:
@@ -209,6 +221,9 @@ double Units::convertRelativeTemperature(double value, Unit fromUnit, Unit toUni
209 221
 				case Rankine:
210 222
 					return value * (9.0 / 5.0);
211 223
 					break;
224
+				default:
225
+					return 0;
226
+					break;
212 227
 			}
213 228
 			break;
214 229
 		case Rankine:
@@ -226,6 +241,9 @@ double Units::convertRelativeTemperature(double value, Unit fromUnit, Unit toUni
226 241
 				case Rankine:
227 242
 					return value;
228 243
 					break;
244
+				default:
245
+					return 0;
246
+					break;
229 247
 			}
230 248
 			break;
231 249
 		default:
@@ -235,6 +253,115 @@ double Units::convertRelativeTemperature(double value, Unit fromUnit, Unit toUni
235 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 365
 @ This class is exposed to the host environment. Note the lack of constructor.
239 366
 We do not wish to create any instances, just have access to the |Unit|
240 367
 enumeration.

Загрузка…
Отмена
Сохранить