![](/media/cache/profiles/ba5be68d3d541f2ca043d77fc54fe702.jpg.50x50_q85.jpg)
3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
Diff: CircularArray/CircularArray.cpp
- Branch:
- feature/listOptimisation
- Revision:
- 64:8ada3e0b2048
- Child:
- 65:3723d2729b68
diff -r cb7b9d2ccfa5 -r 8ada3e0b2048 CircularArray/CircularArray.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CircularArray/CircularArray.cpp Sun Apr 09 23:25:26 2017 +0000 @@ -0,0 +1,80 @@ +#include "CircularArray.h" +#include <stdio.h> +#include <ctype.h> +/* + Constructor +*/ +CircularArray::CircularArray(int limit) +{ + maxSize = limit; + array = new Measure[maxSize]; + firstValue = 0; + currentSize = 0; +} +void CircularArray::pushValue(Measure _measure) +{ + if(currentSize >= maxSize) + { + firstValue++; + currentSize--; + } + int nextSpace = firstValue + currentSize; + nextSpace = nextSpace % maxSize; + array[nextSpace] = _measure; + currentSize++; +} +void CircularArray::listX(int x) +{ + if(x > currentSize) + { + x = currentSize; + printf("Changed print amount to currentSize"); + } + int currentElement = nextSpace() - x; + if(currentElement < 0) + { + currentElement += maxSize; + } + for(int i = 0; i < x; i++) + { + char *ptr = array[currentElement].date.ToString(); + printf("\n\r%i. %s T: %f | H: %f | P: %f |",i,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure); + currentElement++; + currentElement = currentElement % maxSize; + } +} +void CircularArray::listAll() +{ + listX(currentSize); +} +void CircularArray::deleteX(int x) +{ + if(x >= maxSize) + { + deleteAll(); + } + else + { + firstValue += x; + currentSize -= x; + firstValue = firstValue % maxSize; + if(currentSize <= 0) + { + deleteAll(); + } + } +} +void CircularArray::deleteAll() +{ + currentSize = 0; +} +int CircularArray::getSize() +{ + return currentSize; +} +int CircularArray::nextSpace() +{ + int nextSpace = firstValue + currentSize; + nextSpace = nextSpace % maxSize; + return nextSpace; +} \ No newline at end of file