#include #include "ListReader.h" #include "SortedList.h" #include "Node.h" using namespace std; istream* ListReader::isr = &cin; bool ListReader::eof = false; int ListReader::lineLimit = -1; int ListReader::lineCount = 0; void ListReader::buildList(SortedList& list) { if (eof || (lineLimit!=-1 && lineCount>=lineLimit)) { throw 0; } list.header = new Node(random()%200-500,NULL,NULL); list.trailer = new Node(random()%200-500,NULL,NULL); list.header->next = list.trailer; list.trailer->prev = list.header; NodePtr current = list.header; cout << "Enter a list...\n"; int value; int last=-1; (*isr) >> value; while (true) { lineCount++; if ((*isr).eof()) { // must be EOF if (isr != &cin) { // switch from file to cin isr = &cin; continue; } else { cout << "Unexpected end of input reached.\n"; throw 0; } } if (!(*isr)) { // non-numeric input (*isr).clear(); (*isr).ignore(numeric_limits::max(),'\n'); cout << "error on input line " << lineCount << ". Non-numeric data ignored.\n"; } else if (value<0 || value > 999) { if (value==-1) { eof = true; break; } if (value==1000) break; // invalid cout << "error on input line " << lineCount << ". Invalid value " << value << " ignored.\n"; } else { if (value < last) { // decreasing cout << "error on input line " << lineCount << ". Decreasing value " << value << " ignored.\n"; } else { // valid last = value; NodePtr v = new Node(value,current,current->next); v->next->prev = v; v->prev->next = v; current = v; } } if (lineLimit!=-1 && lineCount>=lineLimit) { break; } (*isr) >> value; } if (lineLimit!=-1 && lineCount>=lineLimit) { cout << "ListReader reached the limit of "<< lineLimit << " lines of input.\n"; } }