Ejemplo n.º 1
0
int main(int argc, char* argv[]) {

  DLList* list;
  stringstream ss;
  string strValue;
  int intValue;

  if (argc != 2) {
    cout << "USAGE: " << argv[0] << " FILENAME" << endl;

  } else {
    string currentLine;
    char input;
    cout << "PROCESSING FILE: " << argv[1] << endl;

    ifstream theFile(argv[1]);
    if (theFile.good()) {

      while (getline(theFile, currentLine)) {
        ss.clear();
        intValue = 0;
        strValue = "";
        bool activeList = false;

        input = currentLine.at(0);

          switch (input) {
            
            case 'C':
              if (activeList == false)
              list = new DLList;
              else if (activeList == true) {
              delete list; 
              list = new DLList; 
              }
              activeList = true;
              cout << "LIST CREATED" << endl;
              break;
              
            case 'X':
              list->clear();
              cout << "LIST CLEARED" << endl;
              break;

            case 'D':
              delete list;
              list = NULL;
              cout << "LIST DELETED" << endl;
              break;

             case 'I':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              cout << "VALUE " << intValue << " INSERTED" << endl;
              list->insert(intValue);
              break;

            case 'F':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              cout << "VALUE " << intValue << " ADDED TO HEAD" << endl;
              list->pushFront(intValue);
              break;

            case 'B':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              cout << "VALUE " << intValue << " ADDED TO TAIL" << endl;
              list->pushBack(intValue);
              break;

            case 'A':
              cout << "VALUE " << list->getFront() << " AT HEAD" << endl;
              break;

            case 'Z':
              cout << "VALUE " << list->getBack() << " AT TAIL" << endl;
              break;

            case 'T':
              list->popFront();
              cout << "REMOVED HEAD" << endl;
              break;

            case 'K':
              list->popBack();
              cout << "REMOVED TAIL" << endl;
              break;

            case 'E':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              if (list->removeAll(intValue))
                cout << "VALUE " << intValue << " ELIMINATED" << endl;
              else
                cout << "VALUE " << intValue << " NOT FOUND" << endl;
              break;

              case 'G':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              if (list->get(intValue))
                cout << "VALUE " << intValue << " FOUND" << endl;
              else
                cout << "VALUE " << intValue << " NOT FOUND" << endl;
              break;

             case 'P':
              cout << list->toString() << endl;
              break;

            case 'N':
              cout << "LIST SIZE IS " << list->getSize() << endl;
              break;

            case 'R':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              if (list->removeFirst(intValue))
                cout << "VALUE " << intValue << " REMOVED" << endl;
              else
                cout << "VALUE " << intValue << " NOT FOUND" << endl;
              break;

            default:
              cout << "UNKNOWN COMMAND" << endl;
              break;

          }
        }
      }
     theFile.close();

  }

  return 0;
}
Ejemplo n.º 2
0
void unittest2 ()
{
	cout << "\nSTARTING UNIT TEST\n\n";
	
	DLList list;
	
	try {
		evaluate(list.getSize() == 0);
		cout << "Passed TEST 1: default constructor (size) \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 1: default constructor (size) #\n";
	}
	
	try {
		evaluate(list.toString() == "");
		cout << "Passed TEST 2: toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 2: toString #\n";
	}
	
	list.insert(10);
	try {
		evaluate(list.getSize() == 1 && list.toString() == "10");
		cout << "Passed TEST 3: insert(10)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 3: insert(10)/getSize/toString #\n";
	}
	
	list.insert(50);
	try {
		evaluate(list.getSize() == 2 && list.toString() == "10,50");
		cout << "Passed TEST 4: insert(50)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 4: insert(50)/getSize/toString #\n";
	}
	
	list.insert(30);
	try {
		evaluate(list.getSize() == 3 && list.toString() == "10,30,50");
		cout << "Passed TEST 5: insert(30)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 5: insert(30)/getSize/toString #\n";
	}
	
	list.insert(5);
	try {
		evaluate(list.getSize() == 4 && list.toString() == "5,10,30,50");
		cout << "Passed TEST 6: insert(5)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 6: insert(5)/getSize/toString #\n";
	}
	
	list.insert(55);
	try {
		evaluate(list.getSize() == 5 && list.toString() == "5,10,30,50,55");
		cout << "Passed TEST 7: insert(55)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 7: insert(55)/getSize/toString #\n";
	}
	
	list.insert(20);
	try {
		evaluate(list.getSize() == 6 && list.toString() == "5,10,20,30,50,55");
		cout << "Passed TEST 8: insert(20)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 8: insert(20)/getSize/toString #\n";
	}
	
	list.insert(40);
	try {
		evaluate(list.getSize() == 7 && list.toString() == "5,10,20,30,40,50,55");
		cout << "Passed TEST 9: insert(40)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 9: insert(40)/getSize/toString #\n";
	}
	
	list.insert(30);
	try {
		evaluate(list.getSize() == 8 && list.toString() == "5,10,20,30,30,40,50,55");
		cout << "Passed TEST 10: insert(30)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 10: insert(30)/getSize/toString #\n";
	}
	
	list.insert(5);
	try {
		evaluate(list.getSize() == 9 && list.toString() == "5,5,10,20,30,30,40,50,55");
		cout << "Passed TEST 11: insert(5)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 11: insert(5)/getSize/toString #\n";
	}
	
	try {
		evaluate(list.removeFirst(1) == false);
		cout << "Passed TEST 12: removeFirst(1) \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 12: removeFirst(1) #\n";
	}
	
	try {
		evaluate(list.removeFirst(5) == true && list.getSize() == 8 && list.toString() == "5,10,20,30,30,40,50,55");
		cout << "Passed TEST 13: removeFirst(5)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 13: removeFirst(5)/getSize/toString #\n";
	}
	
	try {
		evaluate(list.removeFirst(30) == true && list.getSize() == 7 && list.toString() == "5,10,20,30,40,50,55");
		cout << "Passed TEST 14: removeFirst(30)/getSize/toString \n";
	} catch (MyException e){
		cout << "# FAILED TEST 14: removeFirst(30)/getSize/toString #\n";
	}
	
	try {
		evaluate(list.removeFirst(30) == true && list.getSize() == 6 && list.toString() == "5,10,20,40,50,55");
		cout << "Passed TEST 15: removeFirst(30)/getSize/toString \n";
	} catch (MyException e){
		cout << "# FAILED TEST 15: removeFirst(30)/getSize/toString #\n";
	}
	
	try {
		evaluate(list.removeFirst(55) == true && list.getSize() == 5 && list.toString() == "5,10,20,40,50");
		cout << "Passed TEST 16: removeFirst(55)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 16: removeFirst(55)/getSize/toString #\n";
	}
	
	try {
		evaluate(list.removeFirst(10) == true && list.getSize() == 4 && list.toString() == "5,20,40,50");
		cout << "Passed TEST 17: removeFirst(10)/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 17: removeFirst(10)/getSize/toString #\n";
	}
	
	list.popFront();
	try {
		evaluate(list.getSize() == 3 && list.toString() == "20,40,50");
		cout << "Passed TEST 18: removeHead/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 18: removeHead/getSize/toString #\n";
	}
	
	list.popBack();
	try {
		evaluate(list.getSize() == 2 && list.toString() == "20,40");
		cout << "Passed TEST 19: removeTail/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 19: removeTail/getSize/toString #\n";
	}
	
	list.clear();
	try {
		evaluate(list.getSize() == 0 && list.toString() == "");
		cout << "Passed TEST 20: clear/getSize/toString \n";
	} catch (MyException e) {
		cout << "# FAILED TEST 20: clear/getSize/toString #\n";
	}
	
	cout << "\nUNIT TEST COMPLETE\n\n";
	
	
}
Ejemplo n.º 3
0
void processFile (string filename) {
    stringstream ss;
    int value = 0;
    DLList* list = NULL;
    char firstCharacter;
    ifstream fin(filename.c_str());
    if(!fin.fail()) {
        string nextline;
        while(!fin.eof()) {
            getline(fin, nextline);
            firstCharacter = nextline[0];
            switch(firstCharacter) {
            case '#':
                break;
            case 'C':
                if(list != NULL) { //if there was a previous list delete it
                    delete list;
                }
                else {
                    //creates a new list of type DLList
                    list = new DLList();
                    cout << "LIST CREATED" << endl;
                }
                break;
            case 'X':
                //if no list was created yet create list first
                if(list == NULL) {
                    cout << "MUST CREAT LIST INSTANCE" << endl;
                } //if there was a list clear it with the clear function
                else {
                    list->clear();
                    cout << "LIST CLEARED" << endl;
                }
                break;
            case 'D':
                //if there is a list deleted it
                if(list!= NULL) {
                    delete list;
                    cout << "LIST DELETED" << endl;
                }//if there isn't show warning
                else
                    cout << "MUST CREAT LIST INSTANCE" << endl;
                break;
            case 'I':
                if(list == NULL) {
                    cout << "MUST CREAT LIST INSTANCE" << endl;
                }
                else {
                    ss.str(nextline.substr(2));//gets the next whole value after the first value on a line
                    ss >> value;//puts the from input into an int
                    list->insert(value);//inserts the value into a node on the list
                    cout << "VALUE " << value << " INSERTED" << endl;
                    ss.clear();
                    ss.str("");
                }
                break;
            case 'F':
                ss.str(nextline.substr(2));
                ss >> value;
                list->insertHead(value);//inserts the value from input into the first node
                cout << "VALUE " << value << " ADDED TO HEAD" << endl;
                ss.clear();
                ss.str("");
                break;
            case 'B':
                ss.str(nextline.substr(2));
                ss >> value;
                list->insertTail(value);
                cout << "VALUE " << value << " ADDED TO TAIL" << endl;
                ss.clear();
                ss.str("");
                break;
            case 'A':
                if(list == NULL) {
                    cout << "VALUE NULL AT HEAD" << endl;
                }
                else {
                    list->getHead();
                    cout << "VALUE " << list->getHead() << " AT HEAD" << endl;
                }
                break;
            case 'Z':
                list->getTail();
                cout << "VALUE "  << list->getTail() << " AT TAIL" << endl;
                break;
            case 'T':
                if(list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                }
                else if (list->getSize() > 0) {
                    list->removeHead();
                    cout << "REMOVED HEAD" << endl;
                }
                else {
                    cout << "LIST EMPTY" << endl;

                }
                break;
            case 'K':
                if(list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                }
                else if(list->getSize() > 0) {
                    list->removeTail();
                    cout << "REMOVED TAIL" << endl;
                }
                else
                    cout << "LIST EMPTY" << endl;
                break;
            case 'E':
                ss.str(nextline.substr(2));
                ss >> value;
                list->removeFirst(value);
                cout << "VALUE " << value << " ELIMINATED" << endl;
                ss.clear();
                ss.str("");
                break;
            case 'R':
                break;
            case 'G':
                break;
            case 'N':
                break;
            case 'P':
                list->toString();
                break;
            }
        }
        cout << "INPUT FINISHED" << endl;
        fin.close();
    }