#include #include #include "LinkedList.h" #define DELETEL // Remark: if the program is crashing, comment the above line // (causing memory leaks) and substract 0.5 points from the score // produced by the test file. // In general, C++ version is reporting partial points, // take the last partial points before crash // (C++ programs are proned to crash when not written correctly). using namespace std; class Character : public Object // C++ does not provide wrapper classes // so we create one { private: char c[2]; public: Character(char newc) { c[0]=newc; c[1]=0; } operator const char*() const { return c; } }; class Integer : public Object { private: char c[10]; // string representation of integer public: Integer(int i) { itoa(i,c,10); } operator const char*() const { return c; } int intValue() const { return atoi(c); } }; int main() { double points=0.0; double maxpoints=0.0; // test empty (0.1 point) ListReferenceBased* L=new ListReferenceBased; if (L->isEmpty()) points+=0.05; if (L->size()==0) points+=0.05; maxpoints+=0.1; cout <<"Points="<add(0,new Integer(10)); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.05; } #ifdef DELETEL delete L; #endif L=new ListReferenceBased; try { L->remove(0); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } #ifdef DELETEL delete L; #endif L=new ListReferenceBased; try { L->get(0); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } maxpoints+=0.15; cout <<"Points="<add(2,new Integer(10)); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } #ifdef DELETEL delete L; #endif L=new ListReferenceBased; try { L->remove(1); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } #ifdef DELETEL delete L; #endif L=new ListReferenceBased; try { L->get(1); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } maxpoints+=0.15; cout <<"Points="<add(1,new Integer(i+1)); if (L->size()==max) points+=0.05; for (int i=0; iget(i+1))->intValue()==max-i) points+=0.2/max; } catch (logic_error e) { // no logic_error expected } maxpoints+=0.25; cout <<"Points="<add(i+1,new Integer(i+1)); if (L->size()==max) points+=0.05; for (int i=0; iget(i+1))->intValue()==i+1) points+=0.2/max; } catch (logic_error e) { // no logic_error expected } maxpoints+=0.25; cout <<"Points="<add(i/2+1,new Integer(i+1)); if (L->size()==max) points+=0.1; for (int i=0; iget(i+1))->intValue()==2*(i+1)) points+=0.2/max; for (int i=max/2; iget(i+1))->intValue()==2*max-(2*i+1)) points+=0.2/max; } catch (logic_error e) { // no logic_error expected } maxpoints+=0.3; cout <<"Points="<add(1,new Character('A')); L->add(2,new Character('B')); L->removeAll(); if (L->isEmpty()) points+=0.05; if (L->size()==0) points+=0.05; maxpoints+=0.1; cout <<"Points="<add(1,new Integer(i+1)); try { L->get(-1); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } try { L->get(max); } catch (ListIndexOutOfRangeException e) { points+=0.1; } catch (logic_error e) { // wrong type of exception points+=0.05; } } catch (logic_error e) { // no logic_error expected } maxpoints+=0.15; cout <<"Points="<add(1,new Integer(i+1)); try { L->add(-1,new Object()); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } try { L->add(max,new Object()); } catch (ListIndexOutOfRangeException e) { points+=0.1; } catch (logic_error e) { // wrong type of exception points+=0.05; } } catch (logic_error e) { // no logic_error expected } maxpoints+=0.15; cout <<"Points="<add(1,new Integer(i+1)); try { L->remove(-1); } catch (ListIndexOutOfRangeException e) { points+=0.05; } catch (logic_error e) { // wrong type of exception points+=0.025; } try { L->remove(max); } catch (ListIndexOutOfRangeException e) { points+=0.1; } catch (logic_error e) { // wrong type of exception points+=0.05; } } catch (logic_error e) { // no logic_error expected } maxpoints+=0.15; cout <<"Points="<add(i+1,new Integer(i+1)); for (int i=1; iremove(i+1); if (L->size()==max/2) points+=0.1; for (int i=0; iget(i+1))->intValue()==2*i+1) points+=0.3/max; } catch (logic_error e) { // no logic_error expected } maxpoints+=0.25; cout <<"Points="<add_first(new Integer(i+1)); if (((Integer*) L->get_first())->intValue()==i+1) points+=0.1/max; } for (int i=0; iget(i+1))->intValue()==max-i) points+=0.1/max; for (int i=0; iget_first())->intValue()==max-i) points+=0.1/max; L->remove_first(); } } catch (logic_error e) { // no logic_error expected } maxpoints+=0.3; cout <<"Points="<add_last(new Integer(i+1)); if (((Integer*) L->get_last())->intValue()==i+1) points+=0.1/max; } for (int i=0; iget(i+1))->intValue()==i+1) points+=0.1/max; for (int i=0; iget_last())->intValue()==max-i) points+=0.1/max; L->remove_last(); } } catch (logic_error e) { // no logic_error expected } #ifdef DELETEL delete L; #endif maxpoints+=0.3; cout <<"Points="<