I've just started a C++ class and my first assignment was to overload the + and * operators using data that is read in from two separate files. The two lists can be of any size, so the temp list copies left over items instead of multiplying them.
Part of my class
I've played around a bit with this which *seems* to work if either list is smaller than the other. I haven't tried it if both lists are equal, so I sense a disasterCode:const int CAPACITY = 10; typedef short bool; #define true 1 #define false !true class sequence { public: friend sequence operator*(const sequence& s1, const sequence& s2); private: double data[CAPACITY]; int index; int used; };
I know that there must be a better alternative, but I am having total brain lock as to how I should code this thing so it reads both lists and figures out which list is larger in order to copy the rest of the list to the temp.Code:sequence operator*(const sequence& s1, const sequence& s2) { sequence temp; int ci = 0; while(ci < s1.size() && ci < s2.size()) { temp.data[ci] = s1.data[ci] * s2.data[ci]; ci++; } switch(ci == s1.size() ? true : false) { case true : while(ci < s2.size()) { temp.data[ci] = s2.data[ci]; ci++; } break; case false: while(ci < s1.size()) { temp.data[ci] = s1.data[ci]; ci++; } break; }; temp.used = ci; return temp; }
If any of you real programmers could throw me a bone, I'd apprciate it.
TIA



LinkBack URL
About LinkBacks




