c++ - Writing HugeInteger class -
i new c++ , suppose assignment.
create class hugeinteger uses 40-element array of digits store integers large 40 digits each. provide member functions input, output, add , subtract. comparing hugeinteger objects, provide functions isequalto, isnotequalto, isgreaterthan, islessthan, iscreaterthanorequalto , islessthanorequalto - each of these "predicate" function returns true if relationship holds between 2 hugeintegers , returns false if relationship not hold. also, provided predicate function iszero.
recommendations points: provide member functions multiply, divide, , modulus.
i having trouble figuring out how compare 2 objects in class.
my header coding looks this:
#pragma once #include <iostream> using namespace std; static const int maxinteger = 40; //hugeinteger class class hugeinteger { public: hugeinteger(void); //constructor ~hugeinteger(void); //destructor void hugeinteger::input(int[maxinteger]); //input array internal array void hugeinteger::output(void); //write out array screen bool iszero(void); //test see if 0 bool hugeinteger::isequal(hugeinteger other); //test see if objects equal bool hugeinteger::isnotequal(hugeinteger other); //test see if objects not equal bool hugeinteger::isgreaterthan(hugeinteger other); //test see if 1 object greater other bool hugeinteger::islessthan(hugeinteger other); //test see if 1 object less other bool hugeinteger::isgreaterthanorequal(hugeinteger other); //test see if 1 object greater or equal other bool hugeinteger::islessthanorequal(hugeinteger other); //test see if 1 obejct less or equal other hugeinteger hugeinteger::add(hugeinteger other); //adds 2 objects hugeinteger hugeinteger::subtract(hugeinteger other); //subtract 2 objects hugeinteger hugeinteger::multiply(hugeinteger other); //multiply 2 objects hugeinteger hugeinteger::divide(hugeinteger other); //divide 2 objcts private: bool ispositive; //needed when subtraction int hugeintergerone[maxinteger]; //internal array };
my .cpp coding looks this:
#include "hugeinteger.h" #include <iostream> using namespace std; //hugeinteger class functions hugeinteger::hugeinteger(void) //constructor { //zero out internal array (int = maxinteger - 1; >= 0; i--) ->hugeintergerone[i] = 0; } hugeinteger::~hugeinteger() //destructor { //de-allocates internal array } void hugeinteger::input(int newarray[maxinteger]) { //copies 'newarray' internal array (int index = maxinteger - 1; index >= 0; index--) this->hugeintergerone[index] = newarray[index]; } void hugeinteger::output() { //outputs internal array screen (int index = 0; index < maxinteger; index++) cout << this->hugeintergerone[index] << " "; } bool hugeinteger::iszero(void) //test 0 function { bool result = true; //test whether every element of internal array 0 (int index = maxinteger - 1; index >= 0; index--) if (this->hugeintergerone[index] != 0) result = false; return result; } bool hugeinteger::isequal(hugeinteger other) //test equal function { bool result = true; (int = 0; < maxinteger; i++) if (this->hugeintergerone[i] != other.hugeintergerone[i]) bool result = false; return result; } bool hugeinteger::isnotequal(hugeinteger other) //test not equal function { bool result = true; (int index = maxinteger - 1; index >= 0; index--) if (this->hugeintergerone[index] == other.hugeintergerone[index]) bool result = false; return result; } bool hugeinteger::isgreaterthan(hugeinteger other) //test greater function { bool result = false; (int index = maxinteger - 1; index >= 0; index--) if (this->hugeintergerone[index] > other.hugeintergerone[index]) bool result = true; return result; } bool hugeinteger::islessthan(hugeinteger other) //test less function { bool result = false; (int index = maxinteger - 1; index >= 0; index--) if (this->hugeintergerone[index] < other.hugeintergerone[index]) bool result = true; return result; } bool hugeinteger::isgreaterthanorequal(hugeinteger other) //test greater or equal function { bool result = false; (int index = maxinteger - 1; index >= 0; index--) if (this->hugeintergerone[index] >= other.hugeintergerone[index]) bool result = true; return result; } bool hugeinteger::islessthanorequal(hugeinteger other) //test less or equal function { bool result = false; (int index = maxinteger - 1; index >= 0; index--) if (this->hugeintergerone[index] <= other.hugeintergerone[index]) bool result = true; return result; } hugeinteger hugeinteger::add(hugeinteger other) //adds objects { hugeinteger result; (int = 0; < maxinteger; i++) { result.hugeintergerone[i] = -> hugeintergerone[i] + other.hugeintergerone[i]; } return result; } hugeinteger hugeinteger::subtract(hugeinteger other) //subtracts objects { hugeinteger result; (int = 0; < maxinteger; i++) { result.hugeintergerone[i] = this->hugeintergerone[i] - other.hugeintergerone[i]; } return result; } hugeinteger hugeinteger::multiply(hugeinteger other) //multiplies objects { hugeinteger result; (int = 0; < maxinteger; i++) { result.hugeintergerone[i] = this->hugeintergerone[i] * other.hugeintergerone[i]; } return result; } hugeinteger hugeinteger::divide(hugeinteger other) //divides objects { hugeinteger result; (int = 0; < maxinteger; i++) { result.hugeintergerone[i] = this->hugeintergerone[i]/other.hugeintergerone[i]; } return result; }
and main programming code looks this:
#include <iostream> #include "hugeinteger.h" using namespace std; int main() { //create 3 arrays int first[maxinteger] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 }; int second[maxinteger] = { 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; int zero[maxinteger] = { 0 }; //create objects hugeinteger myhugeinteger0; hugeinteger myhugeinteger1; hugeinteger myhugeinteger2; hugeinteger myhugeinteger3; //input arrays objects myhugeinteger1.input(first); myhugeinteger2.input(second); myhugeinteger0.input(zero); //prints out words true or false instead of 1 or 0 cout << boolalpha << endl; //opening statements cout << "welcome!\n" << endl; cout << "we testing bunch of different functions on class objects today.\n" << endl; cout << "i have created 3 class objects 40 element arrays.\n" << endl; system("pause"); cout << "\n" << endl; //prints elements in each object cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "my 3 objecs are: \n" << endl; cout << "myhugeinteger0:\n" << endl; myhugeinteger0.output(); cout << "\n\nmyhugeinteger1:\n" << endl; myhugeinteger1.output(); cout << "\n\nmyhugeinteger2:\n" << endl; myhugeinteger2.output(); cout << "\n" << endl; //intro check if objecs 0 cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "first, test see if of elements in arrays equal \nzero.\n" << endl; system("pause"); cout << endl; //test if of object elements equal 0 cout << "are of elements in myhugeinteger0 equal zero?\n\n"; cout << myhugeinteger0.iszero() << endl << endl; cout << "are of elements in myhugeinteger1 equal zero?\n\n"; cout << myhugeinteger1.iszero() << endl << endl; cout << "are of elements in myhugeinteger2 equal zero?\n\n"; cout << myhugeinteger2.iszero() << endl << endl; //intro adding objects cout << "\n\n" << endl; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "now shall add different arrays together.\n" << endl; system("pause"); cout << endl; //add different objects myhugeinteger3 = myhugeinteger0.add(myhugeinteger1); cout << "\nthe sum of myhugeinteger0 plus myhugeinteger1 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger0.add(myhugeinteger2); cout << "\nthe sum of myhugeinteger0 plus myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger1.add(myhugeinteger2); cout << "\nthe sum of myhugeinteger1 plus myhugeinteger2 equals\n\n"; myhugeinteger3.output(); //intro subtracting objects cout << "\n\n" << endl; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "now shall subtract different arrays.\n" << endl; system("pause"); //subtract different objects myhugeinteger3 = myhugeinteger0.subtract(myhugeinteger1); cout << "\nthe difference of myhugeinteger0 minus myhugeinteger1 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger1.subtract(myhugeinteger0); cout << "\nthe difference of myhugeinteger1 minus myhugeinteger0 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger0.subtract(myhugeinteger2); cout << "\nthe difference of myhugeinteger0 minus myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger2.subtract(myhugeinteger0); cout << "\nthe difference of myhugeinteger2 minus myhugeinteger0 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger1.subtract(myhugeinteger2); cout << "\nthe difference of myhugeinteger1 minus myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger2.subtract(myhugeinteger1); cout << "\nthe difference of myhugeinteger2 minus myhugeinteger1 equals\n\n"; myhugeinteger3.output(); cout << endl; //intro multipling objects cout << "\n\n" << endl; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "now shall multiply different arrays together.\n" << endl; system("pause"); //multiply different objects myhugeinteger3 = myhugeinteger0.multiply(myhugeinteger1); cout << "\nthe product of myhugeinteger0 times myhugeinteger1 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger0.multiply(myhugeinteger2); cout << "\nthe product of myhugeinteger0 times myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger1.multiply(myhugeinteger2); cout << "\nthe product of myhugeinteger1 times myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; //intro dividing objects cout << "\n\n" << endl; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "now shall divide different arrays.\n" << endl; system("pause"); //divide different objects myhugeinteger3 = myhugeinteger0.divide(myhugeinteger1); cout << "\nthe dividen of myhugeinteger0 divided myhugeinteger1 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger0.divide(myhugeinteger2); cout << "\nthe dividen of myhugeinteger0 divided myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger1.divide(myhugeinteger2); cout << "\nthe dividen of myhugeinteger1 divided myhugeinteger2 equals\n\n"; myhugeinteger3.output(); cout << endl; myhugeinteger3 = myhugeinteger2.divide(myhugeinteger1); cout << "\nthe dividen of myhugeinteger2 divided myhugeinteger1 equals\n\n"; myhugeinteger3.output(); cout << endl; //intro comparing objects cout << "\n\n" << endl; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout << "now shall compare different arrays.\n" << endl; system("pause"); cout << endl; //see if objecs equal cout << "is myhugeinteger0 equal myhugeinteger1? \n\n"; cout << myhugeinteger0.isequal(myhugeinteger1) << endl << endl; cout << "is myhugeinteger0 equal myhugeinteger2? \n\n"; cout << myhugeinteger0.isequal(myhugeinteger2) << endl << endl; cout << "is myhugeinteger1 equal myhugeinteger2? \n\n"; cout << myhugeinteger1.isequal(myhugeinteger2) << endl << endl; //see if objects not equal cout << "is myhugeinteger0 not equal myhugeinteger1? \n\n"; cout << myhugeinteger0.isnotequal(myhugeinteger1) << endl << endl; cout << "is myhugeinteger0 not equal myhugeinteger2? \n\n"; cout << myhugeinteger0.isnotequal(myhugeinteger2) << endl << endl; cout << "is myhugeinteger1 not equal myhugeinteger2? \n\n"; cout << myhugeinteger1.isnotequal(myhugeinteger2) << endl << endl; //see if objects greater cout << "\nthat today! thank watching!\n" << endl; system("pause"); return 0; }
every time run program when goes compare 2 objects returns true no matter what. can not figure out doing wrong or how fix it. appreciated! thank you!
you creating new local bool result
inside condition in loop result
return outerscoped 1 set true.
to prevent these types of errors , make easier read, recommend providing braces around conditions , loops if technically not required.
bool hugeinteger::isequal(hugeinteger other) //test equal function { bool result = true; (int = 0; < maxinteger; i++) { if (this->hugeintergerone[i] != other.hugeintergerone[i]) { result = false; // had: bool result = false; } } return result; }
edit - further explanation
in these lines of code create new bool called result
exist until scope of if statement exited. if add braces see scope ends. because of this, set local scoped result
false, function scoped result
still maintains initial value of true gave @ function beginning.
if (this->hugeintergerone[i] != other.hugeintergerone[i]) bool result = false;
if write lines braces more clear scope is:
if (this->hugeintergerone[i] != other.hugeintergerone[i]) { bool result = false; // in scope within surrounding braces }
edit 2 - coding practice suggestion
pointed out jww
in comments, comparison functions copying hugeinteger
class each time call 1 of them. without getting details (you can or read text book details), best practice pass const &
object second copy of object not made when calling function. practice mark functions const whenever possible. function const indicates not change state of object itself. has no "side effects". important when talk threading , thread safety of objects.
bool hugeinteger::isequal(const hugeinteger& other) const //test equal function { bool result = true; (int = 0; < maxinteger; i++) { if (this->hugeintergerone[i] != other.hugeintergerone[i]) { result = false; // had: bool result = false; } } return result; }
Comments
Post a Comment