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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include "HugeInteger.h"
#include <iostream>
using namespace std;
HugeInteger::HugeInteger(void)
{
for (int i = MAXINT - 1; i >= 0; i--)
this->myArray[i] = 0;
}
HugeInteger::~HugeInteger()
{
}
void HugeInteger::input(int newArray[MAXINT])
{
for (int index = MAXINT - 1; index >= 0; index--)
this->myArray[index] = newArray[index];
}
void HugeInteger::output()
{
for (int index = 0; index < MAXINT; index++)
cout << this->myArray[index];
}
HugeInteger HugeInteger::add(HugeInteger other)
{
HugeInteger result;
for (int i = 0; i < MAXINT; i++)
{
result.myArray[i] = this->myArray[i] + other.myArray[i];
}
return result;
}
HugeInteger HugeInteger::subtract(HugeInteger other)
{
HugeInteger result;
for (int i = 0; i < MAXINT; i++)
{
result.myArray[i] = this->myArray[i] - other.myArray[i];
}
return result;
}
bool HugeInteger::testZero(void)
{
bool result = true;
for (int index = MAXINT - 1; index >= 0; index-- )
if (this->myArray[index] != 0)
result = false;
return result;
}
__________________________________________
NEXT .CCP
#include <iostream>
#include "HugeInteger.h"
using namespace std;
int main()
{
int first[MAXINT] = { 1, 2, 4, 5, 7, 8, 9, 6, 5, 6, 3, 2, 5, 4, 1, 5, 6, 9, 8, 7, 4, 5, 1, 2, 0, 3, 6, 0, 2, 5, 6, 9, 7, 4, 1, 2, 5, 9, 6, 0 };
int second[MAXINT] = { 2, 3, 5, 0, 4, 6, 9, 8, 7, 5, 6, 3, 0, 1, 5, 4, 7, 2, 9, 8, 3, 7, 1, 5, 3, 4, 2, 6, 0, 1, 4, 5, 7, 9, 8, 6, 5, 1, 3, 0 };
int zero[MAXINT] = { 0 };
HugeInteger myHugeInteger1;
HugeInteger myHugeInteger2;
HugeInteger myHugeInteger0;
// Add Objects
HugeInteger myHugeInteger4;
HugeInteger sum2;
// Subtract Objects
HugeInteger myHugeInteger5;
HugeInteger difference2;
myHugeInteger1.input(first);
myHugeInteger2.input(second);
myHugeInteger0.input(zero);
cout << boolalpha << endl;
cout << "This will test if all the elements in myHugeInteger1,\n";
myHugeInteger1.output();
cout << ", are equal to zero: " << endl;
cout << myHugeInteger1.testZero() << endl << endl;
cout << "\n\nThis will test if all the elements in myHugeInteger2,\n";
myHugeInteger2.output();
cout << ", are equal to zero: " << endl;
cout << myHugeInteger2.testZero() << endl << endl;
cout << "\n\nThe will test if all the elements in myHugeInteger0,\n";
myHugeInteger0.output();
cout << ", are equal to zero: " << endl;
cout << myHugeInteger0.testZero() << endl << endl;
// Add test case 1
myHugeInteger4 = myHugeInteger0.add(myHugeInteger2);
cout << "\nThe sum of\t";
myHugeInteger0.output();
cout << endl << " plus\t\t";
myHugeInteger2.output();
cout << endl << " equals\t\t";
myHugeInteger4.output();
cout << endl;
// Add test case 2
sum2 = myHugeInteger1.add(myHugeInteger2);
cout << "\nThe sum of \t";
myHugeInteger1.output();
cout << endl << " plus\t\t";
myHugeInteger2.output();
cout << endl << " equals\t\t";
sum2.output();
cout << endl;
// Subtract test case 1
myHugeInteger5 = myHugeInteger0.subtract(myHugeInteger2);
cout << "\nThe difference of \t";
myHugeInteger0.output();
cout << endl << " minus\t\t";
myHugeInteger2.output();
cout << endl << " equals\t\t";
myHugeInteger5.output();
cout << endl;
// Subtract test case 2
difference2 = myHugeInteger1.subtract(myHugeInteger2);
cout << "\nThe difference of \t";
myHugeInteger1.output();
cout << endl << " minus\t\t";
myHugeInteger2.output();
cout << endl << " equals\t\t";
difference2.output();
cout << endl;
/*
//THIS IS WHAT I NEED
// Equal to test case
cout << "\nThis will test if all the elements in the first array,\t";
myHugeInteger1.output();
cout << endl << " are equal to all elements in the second array: \t\t << endl;
myHugeInteger2.output();
cout << endl << myHugeInteger1.testEqualTo() << endl << endl;
*/
}
________________________________________
HEADER FILE
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H
#include <iostream>
using namespace std;
static const int MAXINT = 40;
//HungeInteger Class
class HugeInteger
{
public:
HugeInteger(void);
~HugeInteger(void);
void HugeInteger::input(int[MAXINT]);
void HugeInteger::output(void);
HugeInteger HugeInteger::add(HugeInteger other);
HugeInteger HugeInteger::subtract(HugeInteger other);
bool testZero(void);
bool testEqualTo(void);
private:
int myArray[MAXINT];
};
#endif
|