3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
CircularArray/CircularArray.cpp
- Committer:
- Netaphous
- Date:
- 2017-04-09
- Branch:
- feature/listOptimisation
- Revision:
- 64:8ada3e0b2048
- Child:
- 65:3723d2729b68
File content as of revision 64:8ada3e0b2048:
#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; }