![](/media/cache/profiles/ba5be68d3d541f2ca043d77fc54fe702.jpg.50x50_q85.jpg)
3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
CircularArray/CircularArray.cpp
- Committer:
- aburch1
- Date:
- 2017-05-11
- Revision:
- 82:668b51a39148
- Parent:
- 81:996c0a3319b4
- Child:
- 83:0d3572a8a851
File content as of revision 82:668b51a39148:
#include "CircularArray.h" #include <stdio.h> #include <ctype.h> /* Constructor */ CircularArray::CircularArray(int limit, MessageLogger *newLogger) { maxSize = limit; array = new Measure[maxSize]; firstValue = 0; currentSize = 0; logger = newLogger; } void CircularArray::pushValue(Measure _measure) { if(currentSize >= maxSize) { firstValue++; currentSize--; } int next = nextSpace(); array[next] = _measure; currentSize++; } Measure CircularArray::pullValue() { return array[firstValue]; } char temp2[256]; void CircularArray::readX(int x) { if(x > currentSize) { x = currentSize; snprintf(temp2, 256, "Not enough measurements taken. Printing all %i measurement(s): \r\n", x); } else { snprintf(temp2, 256, "Printing %i measurement(s): \r\n", x); } logger->SendMessage(temp2); int currentElement = nextSpace() - x; if(currentElement < 0) { currentElement += maxSize; } char* ptr; for(int i = 0; i < x; i++) { ptr = array[currentElement].date.ToString(); snprintf(temp2, 256, "%i. %s, T: %f, H: %f, P: %f\n\r",i + 1,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure); logger->SendMessage(temp2); currentElement++; currentElement = currentElement % maxSize; } } void CircularArray::readAll() { logger->SendMessage("Printing all measurementss performed so far: \r\n"); readX(currentSize); } void CircularArray::deleteX(int x) { if(x >= maxSize) { deleteAll(); } else { firstValue += x; currentSize -= x; firstValue = firstValue % maxSize; } } void CircularArray::deleteAll() { snprintf(temp2, 256, "Deleted %i records.", currentSize); logger->SendMessage(temp2); currentSize = 0; } int CircularArray::getSize() { return currentSize; } int CircularArray::nextSpace() { int nextSpace = firstValue + currentSize; nextSpace = nextSpace % maxSize; return nextSpace; }