3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
CircularArray/CircularArray.cpp@64:8ada3e0b2048, 2017-04-09 (annotated)
- Committer:
- Netaphous
- Date:
- Sun Apr 09 23:25:26 2017 +0000
- Branch:
- feature/listOptimisation
- Revision:
- 64:8ada3e0b2048
- Child:
- 65:3723d2729b68
Created and tested Circular Array class. Needs implementing
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 | } |
Netaphous | 64:8ada3e0b2048 | 21 | int nextSpace = firstValue + currentSize; |
Netaphous | 64:8ada3e0b2048 | 22 | nextSpace = nextSpace % maxSize; |
Netaphous | 64:8ada3e0b2048 | 23 | array[nextSpace] = _measure; |
Netaphous | 64:8ada3e0b2048 | 24 | currentSize++; |
Netaphous | 64:8ada3e0b2048 | 25 | } |
Netaphous | 64:8ada3e0b2048 | 26 | void CircularArray::listX(int x) |
Netaphous | 64:8ada3e0b2048 | 27 | { |
Netaphous | 64:8ada3e0b2048 | 28 | if(x > currentSize) |
Netaphous | 64:8ada3e0b2048 | 29 | { |
Netaphous | 64:8ada3e0b2048 | 30 | x = currentSize; |
Netaphous | 64:8ada3e0b2048 | 31 | printf("Changed print amount to currentSize"); |
Netaphous | 64:8ada3e0b2048 | 32 | } |
Netaphous | 64:8ada3e0b2048 | 33 | int currentElement = nextSpace() - x; |
Netaphous | 64:8ada3e0b2048 | 34 | if(currentElement < 0) |
Netaphous | 64:8ada3e0b2048 | 35 | { |
Netaphous | 64:8ada3e0b2048 | 36 | currentElement += maxSize; |
Netaphous | 64:8ada3e0b2048 | 37 | } |
Netaphous | 64:8ada3e0b2048 | 38 | for(int i = 0; i < x; i++) |
Netaphous | 64:8ada3e0b2048 | 39 | { |
Netaphous | 64:8ada3e0b2048 | 40 | char *ptr = array[currentElement].date.ToString(); |
Netaphous | 64:8ada3e0b2048 | 41 | printf("\n\r%i. %s T: %f | H: %f | P: %f |",i,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure); |
Netaphous | 64:8ada3e0b2048 | 42 | currentElement++; |
Netaphous | 64:8ada3e0b2048 | 43 | currentElement = currentElement % maxSize; |
Netaphous | 64:8ada3e0b2048 | 44 | } |
Netaphous | 64:8ada3e0b2048 | 45 | } |
Netaphous | 64:8ada3e0b2048 | 46 | void CircularArray::listAll() |
Netaphous | 64:8ada3e0b2048 | 47 | { |
Netaphous | 64:8ada3e0b2048 | 48 | listX(currentSize); |
Netaphous | 64:8ada3e0b2048 | 49 | } |
Netaphous | 64:8ada3e0b2048 | 50 | void CircularArray::deleteX(int x) |
Netaphous | 64:8ada3e0b2048 | 51 | { |
Netaphous | 64:8ada3e0b2048 | 52 | if(x >= maxSize) |
Netaphous | 64:8ada3e0b2048 | 53 | { |
Netaphous | 64:8ada3e0b2048 | 54 | deleteAll(); |
Netaphous | 64:8ada3e0b2048 | 55 | } |
Netaphous | 64:8ada3e0b2048 | 56 | else |
Netaphous | 64:8ada3e0b2048 | 57 | { |
Netaphous | 64:8ada3e0b2048 | 58 | firstValue += x; |
Netaphous | 64:8ada3e0b2048 | 59 | currentSize -= x; |
Netaphous | 64:8ada3e0b2048 | 60 | firstValue = firstValue % maxSize; |
Netaphous | 64:8ada3e0b2048 | 61 | if(currentSize <= 0) |
Netaphous | 64:8ada3e0b2048 | 62 | { |
Netaphous | 64:8ada3e0b2048 | 63 | deleteAll(); |
Netaphous | 64:8ada3e0b2048 | 64 | } |
Netaphous | 64:8ada3e0b2048 | 65 | } |
Netaphous | 64:8ada3e0b2048 | 66 | } |
Netaphous | 64:8ada3e0b2048 | 67 | void CircularArray::deleteAll() |
Netaphous | 64:8ada3e0b2048 | 68 | { |
Netaphous | 64:8ada3e0b2048 | 69 | currentSize = 0; |
Netaphous | 64:8ada3e0b2048 | 70 | } |
Netaphous | 64:8ada3e0b2048 | 71 | int CircularArray::getSize() |
Netaphous | 64:8ada3e0b2048 | 72 | { |
Netaphous | 64:8ada3e0b2048 | 73 | return currentSize; |
Netaphous | 64:8ada3e0b2048 | 74 | } |
Netaphous | 64:8ada3e0b2048 | 75 | int CircularArray::nextSpace() |
Netaphous | 64:8ada3e0b2048 | 76 | { |
Netaphous | 64:8ada3e0b2048 | 77 | int nextSpace = firstValue + currentSize; |
Netaphous | 64:8ada3e0b2048 | 78 | nextSpace = nextSpace % maxSize; |
Netaphous | 64:8ada3e0b2048 | 79 | return nextSpace; |
Netaphous | 64:8ada3e0b2048 | 80 | } |