File: quantity.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (486 lines) | stat: -rw-r--r-- 12,078 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/***************************************************************************
  quantity.h
  -------------------
  Generic quantity class
  -------------------
  Copyright 2001-2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include "quantity.h"

// Unit Class ///////////////////////////////////////////////////////////////

// our static map units
QMap<QString,Unit*> Unit::units_;

/**
 * Full constructor. Construct a Unit from name and symbol
 * and add to map.
 */
Unit::Unit(const QString &name, const QString &symbol)
    : name_(name), symbol_(symbol), conversions_()
{
    units_.insert(symbol_, this);
}

/**
 * Destructor. Removes Unit from map.
 */
inline Unit::~Unit()
{
    units_.remove(symbol_);
}

/**
 * Add a conversion function for this Unit.
 */
void Unit::addConversion(const Unit &u, const Conversion c)
{
    conversions_.insert(u.name(), c);
}

/**
 * Convert to amount of other units to this Unit.
 */
double Unit::convert(double amount, const Unit &other) const
{
    // if converting from self...
    if (other.name_ == name_) return amount;
    
    if (conversions_.contains(other.name())) {
        return (conversions_.value(other.name()))(amount);
    }
    // TODO: throw exception instead of returning 0.0
    return 0.0;
}

/**
 * Return Unit associated with symbol.
 */
Unit* Unit::unit(const QString &symbol)
{
    if (units_.contains(symbol))
        return units_.value(symbol);
    else
        return 0;
}

/**
 * Equivalence operator.
 */
bool Unit::operator==(const Unit &u) const
{
    return ((name_ == u.name_) && (symbol_ == u.symbol_));
}

// Quantity Class ///////////////////////////////////////////////////////////

Unit Quantity::generic("unit", "unit");

/**
 * Assignment operator.
 */
Quantity &Quantity::operator=(const Quantity &q)
{
    if (this != &q) {
        amount_ = q.amount_;
        unit_ = q.unit_;
    }
    return *this;
}

/**
 * Equivalence operator.
 */
bool Quantity::operator==(const Quantity &q) const
{
    return ((amount_ == q.amount_) &&
            (unit_->symbol() == q.unit_->symbol()));
}

/**
 * Convert Quantity to unit.
 */
void Quantity::convert(const Unit &u)
{
    amount_ = unit_->convert(amount_, u);
    unit_ = &u;
    // TODO: change to bool return, and return false if not convertable
}

/**
 * Output quantity as a string, output is "xx.xx symbol".
 */
QString Quantity::toString(int prec) const
{
    return (QString::number(amount_, 'f', prec) + " " + unit_->symbol());
}

/**
 * Set quantity from string (assumes format of "%f %s")
 */
void Quantity::fromQString(const QString &from, const Unit &defaultunit)
{
    QString str = from.section(' ', 1, -1);
    unit_ =  Unit::unit(str);
    if (!unit_) unit_ = &defaultunit;

    str = from.section(' ', 0, 0);
    amount_ = str.toDouble();
}

// Addition operators /////////////////

Quantity &Quantity::operator+=(const Quantity &q)
{
    if (unit_->convertable(*q.unit_)) {
        amount_ += q.unit_->convert(q.amount_, *unit_);
    }
    return *this;
}

Quantity operator+(const Quantity &q1, const Quantity &q2)
{
    Quantity temp(q1);
    if (temp.unit_->convertable(*q2.unit_)) {
        temp.amount_ += q2.unit_->convert(q2.amount_, *temp.unit_);
    }
    return temp;
}

Quantity operator+(const Quantity &q, const double d)
{
    Quantity temp(q);
    temp.amount_ += d;
    return temp;
}

// Subtraction operators //////////////

Quantity &Quantity::operator-=(const Quantity &q)
{
    if (unit_->convertable(*q.unit_)) {
        amount_ -= q.unit_->convert(q.amount_, *unit_);
    }
    return *this;
}

Quantity operator-(const Quantity &q1, const Quantity &q2)
{
    Quantity temp(q1);
    if (temp.unit_->convertable(*q2.unit_)) {
        temp.amount_ += q2.unit_->convert(q2.amount_, *temp.unit_);
    }
    return temp;
}

Quantity operator-(const Quantity &q, const double d)
{
    Quantity temp(q);
    temp.amount_ -= d;
    return temp;
}

Quantity operator-(const double d, const Quantity &q)
{
    Quantity temp(q);
    temp.amount_ = d - temp.amount_;
    return temp;
}

// Misc operators /////////////////////

Quantity operator-(const Quantity &q)
{
    Quantity temp(q);
    temp.amount_ *= -1.0;
    return temp;
}

// Weight Class //////////////////////////////////////////////////////////

Unit Weight::gram("gram", "g");
Unit Weight::kilogram("kilogram", "kg");
Unit Weight::ounce("ounce", "oz");
Unit Weight::pound("pound", "lb");

bool Weight::initialized_ = false;

/**
 * Constructor.
 */
Weight::Weight(double amount, const Unit &unit) 
    : Quantity(amount, unit)
{
    if (!initialized_) initialize();
}

/**
 * Conversion constructor. Converts from base Quantity.
 */
Weight::Weight(const Quantity &q)
    : Quantity(q)
{
    if (!initialized_) initialize();
    // TODO: throw if nonconvertable
}

/**
 * Initialize the class units.
 */
void Weight::initialize()
{
    gram.addConversion(kilogram, gram2kilogram);
    gram.addConversion(ounce, gram2ounce);
    gram.addConversion(pound, gram2pound);
    kilogram.addConversion(gram, kilogram2gram);
    kilogram.addConversion(ounce, kilogram2ounce);
    kilogram.addConversion(pound, kilogram2pound);
    ounce.addConversion(gram, ounce2gram);
    ounce.addConversion(kilogram, ounce2kilogram);
    ounce.addConversion(pound, ounce2pound);
    pound.addConversion(gram, pound2gram);
    pound.addConversion(kilogram, pound2kilogram);
    pound.addConversion(ounce, pound2ounce);
    initialized_ = true;
}

// Volume Class //////////////////////////////////////////////////////////

Unit Volume::barrel("barrel", "bbl");
Unit Volume::gallon("gallon", "gal");
Unit Volume::hectoliter("hectoliter", "hL");
Unit Volume::liter("liter", "L");
Unit Volume::milliliter("milliliter", "mL");
Unit Volume::fluidounce("fluid ounce", "fl oz");

bool Volume::initialized_ = false;

/**
 * Constructor.
 */
Volume::Volume(double amount, const Unit &unit) 
    : Quantity(amount, unit)
{
    if (!initialized_) initialize();
}

/**
 * Conversion constructor. Converts from base Quantity.
 */
Volume::Volume(const Quantity &q)
    : Quantity(q)
{
    if (!initialized_) initialize();
    // TODO: throw if nonconvertable
}

/**
 * Initialize the class units.
 */
void Volume::initialize()
{
    barrel.addConversion(fluidounce, barrel2fluidounce);
    barrel.addConversion(gallon, barrel2gallon);
    barrel.addConversion(hectoliter, barrel2hectoliter);
    barrel.addConversion(liter, barrel2liter);
    barrel.addConversion(milliliter, barrel2milliliter);
    fluidounce.addConversion(barrel, fluidounce2barrel);
    fluidounce.addConversion(gallon, fluidounce2gallon);
    fluidounce.addConversion(hectoliter, fluidounce2hectoliter);
    fluidounce.addConversion(liter, fluidounce2liter);
    fluidounce.addConversion(milliliter, fluidounce2milliliter);
    gallon.addConversion(barrel, gallon2barrel);
    gallon.addConversion(fluidounce, gallon2fluidounce);
    gallon.addConversion(hectoliter, gallon2hectoliter);
    gallon.addConversion(liter, gallon2liter);
    gallon.addConversion(milliliter, gallon2milliliter);
    hectoliter.addConversion(barrel, hectoliter2barrel);
    hectoliter.addConversion(fluidounce, hectoliter2fluidounce);
    hectoliter.addConversion(gallon, hectoliter2gallon);
    hectoliter.addConversion(liter, hectoliter2liter);
    hectoliter.addConversion(milliliter, hectoliter2milliliter);
    liter.addConversion(barrel, liter2barrel);
    liter.addConversion(fluidounce, liter2fluidounce);
    liter.addConversion(gallon, liter2gallon);
    liter.addConversion(hectoliter, liter2hectoliter);
    liter.addConversion(milliliter, liter2milliliter);
    milliliter.addConversion(barrel, milliliter2barrel);
    milliliter.addConversion(fluidounce, milliliter2fluidounce);
    milliliter.addConversion(gallon, milliliter2gallon);
    milliliter.addConversion(hectoliter, milliliter2hectoliter);
    milliliter.addConversion(liter, milliliter2liter);
    initialized_ = true;
}

// Temperature Class //////////////////////////////////////////////////////////

Unit Temperature::fahrenheit("fahrenheit", "F");
Unit Temperature::celsius("celsius", "C");

bool Temperature::initialized_ = false;

/**
 * Constructor
 */
Temperature::Temperature(double amount, const Unit &unit) 
    : Quantity(amount, unit)
{
    if (!initialized_) initialize();
}

/**
 * Conversion constructor. Converts from base Quantity.
 */
Temperature::Temperature(const Quantity &q)
    : Quantity(q)
{
    if (!initialized_) initialize();
    // TODO: throw if nonconvertable
}

/**
 * Initialize the class units.
 */
void Temperature::initialize()
{
    fahrenheit.addConversion(celsius, fahrenheit2celsius);
    celsius.addConversion(fahrenheit, celsius2fahrenheit);
}

// Conversion Functions //////////////////////////////////////////////////

// TODO: double check formulas

double gram2kilogram(double value)
    { return value / 1000.0; }

double gram2ounce(double value)
    { return value / 28.349523125; }

double gram2pound(double value)
    { return value / 453.59237; }

double kilogram2gram(double value)
    { return value * 1000.0 ; }

double kilogram2ounce(double value)
    { return value * 35.27396; }

double kilogram2pound(double value)
    { return value * 2.2046226; }

double ounce2gram(double value)
    { return value * 28.349523125; }

double ounce2kilogram(double value)
    { return value / 35.27396; }

double ounce2pound(double value)
    { return value / 16.0; }

double pound2gram(double value)
    { return value * 453.59237; }

double pound2kilogram(double value)
    { return value / 2.2046226; }

double pound2ounce(double value)
    { return value * 16.0; }

double barrel2fluidounce(double value)
    { return value * 3968.0; }

double barrel2gallon(double value)
    { return value * 31.0; }

double barrel2hectoliter(double value)
    { return value * 1.173477658; }

double barrel2liter(double value)
    { return value * 117.3477658; }

double barrel2milliliter(double value)
    { return value * 117347.7658; }

double fluidounce2barrel(double value)
    { return value / 3968.0; }

double fluidounce2gallon(double value)
    { return value / 128.0; }

double fluidounce2hectoliter(double value)
    { return value / 3381.4016; }

double fluidounce2liter(double value)
    { return value / 33.814016; }

double fluidounce2milliliter(double value)
    { return value * 29.5735; }

double gallon2barrel(double value)
    { return value / 31.0; }

double gallon2fluidounce(double value)
    { return value * 128.0; }

double gallon2hectoliter(double value)
    { return value / 26.4172; }

double gallon2liter(double value)
    { return value * 3.7854118; }

double gallon2milliliter(double value)
    { return value * 3785.4118; }

double hectoliter2barrel(double value)
    { return value / 1.173477658; }

double hectoliter2fluidounce(double value)
    { return value * 3381.4016; }

double hectoliter2gallon(double value)
    { return value * 26.4172; }

double hectoliter2liter(double value)
    { return value * 100.0; }

double hectoliter2milliliter(double value)
    { return value * 100000.0; }

double liter2barrel(double value)
    { return value / 117.3477658; }

double liter2fluidounce(double value)
    { return value * 33.814016; }

double liter2gallon(double value)
    { return value / 3.7854118; }

double liter2hectoliter(double value)
    { return value / 100.0; }

double liter2milliliter(double value)
    { return value * 1000.0; }

double milliliter2barrel(double value)
    { return value / 117347.7658; }

double milliliter2fluidounce(double value)
    { return value / 29.5735; }

double milliliter2gallon(double value)
    { return value / 3785.4118; }

double milliliter2hectoliter(double value)
    { return value / 100000.0; }

double milliliter2liter(double value)
    { return value / 1000.0; }

double fahrenheit2celsius(double value)
    { return (value - 32.0) / 1.8; }

double celsius2fahrenheit(double value)
    { return (value * 1.8) + 32.0; }