3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
CircularArray/CircularArray.cpp@74:749727490f44, 2017-04-24 (annotated)
- Committer:
- aburch1
- Date:
- Mon Apr 24 13:15:51 2017 +0000
- Revision:
- 74:749727490f44
- Parent:
- 72:ef4a4e3089c1
- Child:
- 79:4e6b53eb678b
Created a message logger and set up the framework ready for implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Netaphous | 64:8ada3e0b2048 | 1 | #include "CircularArray.h" |
Netaphous | 64:8ada3e0b2048 | 2 | #include <stdio.h> |
Netaphous | 64:8ada3e0b2048 | 3 | #include <ctype.h> |
Netaphous | 64:8ada3e0b2048 | 4 | /* |
Netaphous | 64:8ada3e0b2048 | 5 | Constructor |
Netaphous | 64:8ada3e0b2048 | 6 | */ |
Netaphous | 64:8ada3e0b2048 | 7 | CircularArray::CircularArray(int limit) |
Netaphous | 64:8ada3e0b2048 | 8 | { |
Netaphous | 64:8ada3e0b2048 | 9 | maxSize = limit; |
Netaphous | 64:8ada3e0b2048 | 10 | array = new Measure[maxSize]; |
Netaphous | 64:8ada3e0b2048 | 11 | firstValue = 0; |
Netaphous | 64:8ada3e0b2048 | 12 | currentSize = 0; |
Netaphous | 64:8ada3e0b2048 | 13 | } |
Netaphous | 64:8ada3e0b2048 | 14 | void CircularArray::pushValue(Measure _measure) |
Netaphous | 64:8ada3e0b2048 | 15 | { |
Netaphous | 64:8ada3e0b2048 | 16 | if(currentSize >= maxSize) |
Netaphous | 64:8ada3e0b2048 | 17 | { |
Netaphous | 64:8ada3e0b2048 | 18 | firstValue++; |
Netaphous | 64:8ada3e0b2048 | 19 | currentSize--; |
Netaphous | 64:8ada3e0b2048 | 20 | } |
aburch1 | 72:ef4a4e3089c1 | 21 | int next = nextSpace(); |
aburch1 | 72:ef4a4e3089c1 | 22 | array[next] = _measure; |
Netaphous | 64:8ada3e0b2048 | 23 | currentSize++; |
Netaphous | 64:8ada3e0b2048 | 24 | } |
Netaphous | 65:3723d2729b68 | 25 | void CircularArray::readX(int x) |
Netaphous | 64:8ada3e0b2048 | 26 | { |
Netaphous | 64:8ada3e0b2048 | 27 | if(x > currentSize) |
Netaphous | 64:8ada3e0b2048 | 28 | { |
Netaphous | 64:8ada3e0b2048 | 29 | x = currentSize; |
Netaphous | 64:8ada3e0b2048 | 30 | printf("Changed print amount to currentSize"); |
Netaphous | 64:8ada3e0b2048 | 31 | } |
Netaphous | 64:8ada3e0b2048 | 32 | int currentElement = nextSpace() - x; |
Netaphous | 64:8ada3e0b2048 | 33 | if(currentElement < 0) |
Netaphous | 64:8ada3e0b2048 | 34 | { |
Netaphous | 64:8ada3e0b2048 | 35 | currentElement += maxSize; |
Netaphous | 64:8ada3e0b2048 | 36 | } |
Netaphous | 64:8ada3e0b2048 | 37 | for(int i = 0; i < x; i++) |
Netaphous | 64:8ada3e0b2048 | 38 | { |
Netaphous | 64:8ada3e0b2048 | 39 | char *ptr = array[currentElement].date.ToString(); |
aburch1 | 74:749727490f44 | 40 | printf("\n\r%i. %s T: %f | H: %f | P: %f |",i + 1,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure); |
Netaphous | 64:8ada3e0b2048 | 41 | currentElement++; |
Netaphous | 64:8ada3e0b2048 | 42 | currentElement = currentElement % maxSize; |
Netaphous | 64:8ada3e0b2048 | 43 | } |
Netaphous | 64:8ada3e0b2048 | 44 | } |
Netaphous | 65:3723d2729b68 | 45 | void CircularArray::readAll() |
Netaphous | 64:8ada3e0b2048 | 46 | { |
Netaphous | 65:3723d2729b68 | 47 | readX(currentSize); |
Netaphous | 64:8ada3e0b2048 | 48 | } |
Netaphous | 64:8ada3e0b2048 | 49 | void CircularArray::deleteX(int x) |
Netaphous | 64:8ada3e0b2048 | 50 | { |
Netaphous | 64:8ada3e0b2048 | 51 | if(x >= maxSize) |
Netaphous | 64:8ada3e0b2048 | 52 | { |
Netaphous | 64:8ada3e0b2048 | 53 | deleteAll(); |
Netaphous | 64:8ada3e0b2048 | 54 | } |
Netaphous | 64:8ada3e0b2048 | 55 | else |
Netaphous | 64:8ada3e0b2048 | 56 | { |
Netaphous | 64:8ada3e0b2048 | 57 | firstValue += x; |
Netaphous | 64:8ada3e0b2048 | 58 | currentSize -= x; |
Netaphous | 64:8ada3e0b2048 | 59 | firstValue = firstValue % maxSize; |
Netaphous | 64:8ada3e0b2048 | 60 | } |
Netaphous | 64:8ada3e0b2048 | 61 | } |
Netaphous | 64:8ada3e0b2048 | 62 | void CircularArray::deleteAll() |
Netaphous | 64:8ada3e0b2048 | 63 | { |
Netaphous | 64:8ada3e0b2048 | 64 | currentSize = 0; |
Netaphous | 64:8ada3e0b2048 | 65 | } |
Netaphous | 64:8ada3e0b2048 | 66 | int CircularArray::getSize() |
Netaphous | 64:8ada3e0b2048 | 67 | { |
Netaphous | 64:8ada3e0b2048 | 68 | return currentSize; |
Netaphous | 64:8ada3e0b2048 | 69 | } |
Netaphous | 64:8ada3e0b2048 | 70 | int CircularArray::nextSpace() |
Netaphous | 64:8ada3e0b2048 | 71 | { |
Netaphous | 64:8ada3e0b2048 | 72 | int nextSpace = firstValue + currentSize; |
Netaphous | 64:8ada3e0b2048 | 73 | nextSpace = nextSpace % maxSize; |
Netaphous | 64:8ada3e0b2048 | 74 | return nextSpace; |
Netaphous | 64:8ada3e0b2048 | 75 | } |