Using multi source files
Yeah I'm using visual studio.net and I'm trying to make a project that requires I use multiple files, a header file for the class declaration, a class.cpp file for the method definitions, and a main.cpp.
I know you're supposed to include "headername.h" at the top. But it's not working correctly. It's not linking right and saying all my class variables in my main (ex. CLASSNAME a,b,c,d) are undefined and such. Just making sure how do you refer to the class in the classname.cpp and such?
Any ideas, if you want to see the code I can email it to you, because i havent figured out the c++ text to html converter thing yet.
Thanks.
EDIT: here's the code
>#ifndef INTEGER
I know you're supposed to include "headername.h" at the top. But it's not working correctly. It's not linking right and saying all my class variables in my main (ex. CLASSNAME a,b,c,d) are undefined and such. Just making sure how do you refer to the class in the classname.cpp and such?
Any ideas, if you want to see the code I can email it to you, because i havent figured out the c++ text to html converter thing yet.
Thanks.
EDIT: here's the code
>#ifndef INTEGER
<#define INTEGER
#include
#include
using namespace std;
class INT {
int digits[100];
char sign;
INT& check();
public:
INT ( );
INT (int num);
INT (string s);
bool operator<(const INT&) const;
bool operator>(const INT&) const;
bool operator==(const INT&) const;
bool operator!=(const INT&) const;
bool operator <=(const INT&) const;
bool operator >=(const INT&) const;
operator int() const;
operator string() const;
friend INT operator%(const INT&, const INT&);
friend INT operator+(const INT&, const INT&);
friend INT operator-(const INT&, const INT&);
friend INT operator*(const INT&, const INT&);
friend INT operator/(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
#endif
#include
#include "10456904INT.h"
#include
using namespace std;
INT () {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT (string s) {
INT result;
int counts = 99, length = s.length();
for (;length > 0; length--,counts--) {
result[counts] = (s[length - 1] - '0');
}
while (counts >=0) {
result[counts] = 0;
counts--;
}
return result;
return result;
}
bool INT::operator<(const INT& b) const {
INT temp = this
if ((temp.sign == 'n') && (b.sign == 'p'))
return true;
else if ((temp.sign == 'p') && (b.sign == 'n'))
return false;
int counters = 0;
while (counters < 100) {
if ((temp.digits[counters] > b.digits[counters]) && (temp.sign == 'n') && (b.sign =='n')) {
counters++;
return true;
else if ((temp.digits[counters] > b.digits[counters]) && (temp.sign == 'p') && (b.sign =='p')) {
counters++;
return false;
else if ((temp.digits[counters] < b.digits[counters]) && (temp.sign == 'n') && (b.sign =='n')) {
counters++;
return false;
else if ((temp.digits[counters] < b.digits[counters]) && (temp.sign == 'p') && (b.sign =='p')) {
counters++;
return true;
else if (temp.digits[counters] == b.digits[counters])
counters++;
}
return false;
}
bool INT::operator>(const INT& b) const {
if (*this.sign == 'n') && (b.sign == 'p')
return false;
else if (temp.sign == 'p') && (b.sign == 'n')
return true;
int counter = 0;
while (counter < 100) {
if (temp.digits[counter] < b.digits[counter]) {
counter++;
return false;
}
else if ((temp.digits[counter] > b.digits[counter]) && (temp.sign == 'n') && (b.sign =='n')) {
counter++;
return false;
else if ((temp.digits[counter] > b.digits[counter]) && (temp.sign == 'p') && (b.sign =='p')) {
counter++;
return true;
}
else if (temp.digits[counter] == b.digits[counter])
counter++;
}
return false;
}
bool INT::operator==(const INT& b) const{
INT tem = *this;
int count = 0;
if (temp.sign != b.sign)
return false;
while (count < 100) {
if (temp.digits[b] == b.digits[count])
count++;
else
return false;
}
return true;
}
bool INT::operator!=(const INT& b) const {
if (*this == b)
return false;
else
return true;
}
bool INT::operator <=(const INT& b) const {
if ((*this < b) || (*this == b))
return true;
else
return false;
}
bool INT::operator >=(const INT&) const {
if ((*this > b) || (*this == b))
return true;
else
return false;
}
INT::operator string() const {
int digs = 0
string result = "";
if (sign == 'n')
string = string + '-';
for (; digits[digs] == 0; digs++)
while (digs < 100) {
string = string + (digits[digs] + '0')
digs++;
}
return result;
}
INT::ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
INT::istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a [Error: Irreparable invalid markup ('<number.length();>') in entry. Owner must fix manually. Raw contents below.]
Yeah I'm using visual studio.net and I'm trying to make a project that requires I use multiple files, a header file for the class declaration, a class.cpp file for the method definitions, and a main.cpp.
I know you're supposed to include "headername.h" at the top. But it's not working correctly. It's not linking right and saying all my class variables in my main (ex. CLASSNAME a,b,c,d) are undefined and such. Just making sure how do you refer to the class in the classname.cpp and such?
Any ideas, if you want to see the code I can email it to you, because i havent figured out the c++ text to html converter thing yet.
Thanks.
EDIT: here's the code
<lj-cut text="This Appears On
Friends' Pages">>#ifndef INTEGER
<pre><code><#define INTEGER
#include <iostream>
#include <string>
using namespace std;
class INT {
int digits[100];
char sign;
INT& check();
public:
INT ( );
INT (int num);
INT (string s);
bool operator<(const INT&) const;
bool operator>(const INT&) const;
bool operator==(const INT&) const;
bool operator!=(const INT&) const;
bool operator <=(const INT&) const;
bool operator >=(const INT&) const;
operator int() const;
operator string() const;
friend INT operator%(const INT&, const INT&);
friend INT operator+(const INT&, const INT&);
friend INT operator-(const INT&, const INT&);
friend INT operator*(const INT&, const INT&);
friend INT operator/(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
#endif
#include <iostream>
#include "10456904INT.h"
#include <string>
using namespace std;
INT () {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT (string s) {
INT result;
int counts = 99, length = s.length();
for (;length > 0; length--,counts--) {
result[counts] = (s[length - 1] - '0');
}
while (counts >=0) {
result[counts] = 0;
counts--;
}
return result;
return result;
}
bool INT::operator<(const INT& b) const {
INT temp = this
if ((temp.sign == 'n') && (b.sign == 'p'))
return true;
else if ((temp.sign == 'p') && (b.sign == 'n'))
return false;
int counters = 0;
while (counters < 100) {
if ((temp.digits[counters] > b.digits[counters]) && (temp.sign == 'n') && (b.sign =='n')) {
counters++;
return true;
else if ((temp.digits[counters] > b.digits[counters]) && (temp.sign == 'p') && (b.sign =='p')) {
counters++;
return false;
else if ((temp.digits[counters] < b.digits[counters]) && (temp.sign == 'n') && (b.sign =='n')) {
counters++;
return false;
else if ((temp.digits[counters] < b.digits[counters]) && (temp.sign == 'p') && (b.sign =='p')) {
counters++;
return true;
else if (temp.digits[counters] == b.digits[counters])
counters++;
}
return false;
}
bool INT::operator>(const INT& b) const {
if (*this.sign == 'n') && (b.sign == 'p')
return false;
else if (temp.sign == 'p') && (b.sign == 'n')
return true;
int counter = 0;
while (counter < 100) {
if (temp.digits[counter] < b.digits[counter]) {
counter++;
return false;
}
else if ((temp.digits[counter] > b.digits[counter]) && (temp.sign == 'n') && (b.sign =='n')) {
counter++;
return false;
else if ((temp.digits[counter] > b.digits[counter]) && (temp.sign == 'p') && (b.sign =='p')) {
counter++;
return true;
}
else if (temp.digits[counter] == b.digits[counter])
counter++;
}
return false;
}
bool INT::operator==(const INT& b) const{
INT tem = *this;
int count = 0;
if (temp.sign != b.sign)
return false;
while (count < 100) {
if (temp.digits[b] == b.digits[count])
count++;
else
return false;
}
return true;
}
bool INT::operator!=(const INT& b) const {
if (*this == b)
return false;
else
return true;
}
bool INT::operator <=(const INT& b) const {
if ((*this < b) || (*this == b))
return true;
else
return false;
}
bool INT::operator >=(const INT&) const {
if ((*this > b) || (*this == b))
return true;
else
return false;
}
INT::operator string() const {
int digs = 0
string result = "";
if (sign == 'n')
string = string + '-';
for (; digits[digs] == 0; digs++)
while (digs < 100) {
string = string + (digits[digs] + '0')
digs++;
}
return result;
}
INT::ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
INT::istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loc++] = number.at(a) - '0';
return is;
}
INT INT::operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
else {
//CODE TO ADD TWO NUMBERS WITH DIFFERENT SIGNS
int diff, borrow=0, i=0;
INT big, small;
while ( (x.digits[i] == y.digits[i]) && (i < 99) ) i++; // skip to first non-common digit in both x & y
if (x.digits[i] == y.digits[i]) return 0; // if (x = y), then [(x - y) = (y - x) = 0]
if (x.digits[i] > y.digits[i]) { // determine which INT has the highest absolute value
big = x;
small = y;
}
else {
big = y;
small = x;
}
result.sign = big.sign; // retain the sign of the largest number for the result
for (int a=99; a>=0; a--) {
//subtract digit of smaller INT from larger INT and subtract borrow if necessary
diff = big.digits[a] - small.digits[a] - borrow;
if (diff < 0) {
// must 'borrow' from next digit to the left, set borrow flag and add 10
borrow = 1;
diff += 10;
}
else borrow = 0; // reset flag
result.digits[a] = diff;
}
}
return result;
}
INT INT:::operator-(const INT& x, const INT& y) {
//reverse sign of y and add
INT z = y;
if (y.sign=='n') z.sign='p';
else z.sign='n';
return x + z;
}
INT INT::operator*(const INT& x, const INT& y) {
INT result;
int xlen=0, ylen=0, carry=0;
//find number of digits in both x & y
while (x.digits[xlen] == 0) xlen++;
while (y.digits[ylen] == 0) ylen++;
xlen = 100 - xlen;
ylen = 100 - ylen;
//multiply each digit of y with x and add results
for (int i=0; i<ylen; i++) {
INT temp;
for (int j=0; j<=xlen; j++) {
if (i + j > 99) break; // result too large, stop processing before array out-of-bounds
temp.digits[99-(i+j)] = (x.digits[99-j] * y.digits[99-i]) + carry;
carry = temp.digits[99-(i+j)] / 10; // carry tenths place to next iteration
temp.digits[99-(i+j)] %= 10; // put ones place in temp array
}
result = result + temp;
}
if (x.sign != y.sign) result.sign='n'; // if signs are not the same, result is negative
return result;
}
INT INT::operator/(const INT& x, const INT& y) {
INT result(1), xpos=x, ypos=y;
xpos.sign='p'; ypos.sign='p'; // store x & y in temp variables with positive signs
int i = 0;
while ( (x.digits[i] == y.digits[i] ) && (i < 99) ) i++;
if (x.digits[i] < y.digits[i]) return 0; // if y > x then result = 0
// handle division by zero
bool iszero=true;
for (int j=99; j>=0; j--) {
if (y.digits[j] == 0) continue;
iszero = false;
}
if (iszero) {
cout << "Error: division by zero.\n";
exit(1);
}
/* Multiply denominator by 10...2 until it becomes greater than numerator, result
keeps track of multiplication operations */
INT temp=ypos, temp2=ypos;
bool flag = true;
for (int a=10; a>1; a--) {
while (flag) {
if (temp2.digits[0]) break; // prevent from trying to multiply over 100 digits
temp2 = temp * a;
int j = 0;
while ( (x.digits[j] == temp2.digits[j] ) && (j < 99) ) j++;
if (x.digits[j] < temp2.digits[j]) break;
temp = temp2; // stores last result that was less than numerator
result = result * a;
}
}
/* divide recursively [(x - current product) / y] and add to result until no further iterations
can be made */
result = result + (xpos-temp)/ypos;
if (x.sign != y.sign) result.sign='n'; // if signs are not the same, result is negative
return result;
}
INT INT::operator%(const INT& a, const INT& b) {
INT mod, quotient tempb = b, tempa = a;
quotient = tempa/tempb;
tempb = tempb*quotient;
mod = tempa - tempb;
mod.sign ='p';
return mod;
}
#include <iostream>
#include "10456904INT.h"
#include <string>
using namespace std;
int main() {
INT a, b, c, d;
cin >> a;
cin >> b;
c = a + b;
d = a / b;
cout << c << endl;
cout << d << endl;
return 0;
}</pre></code></lj-cut>
I know you're supposed to include "headername.h" at the top. But it's not working correctly. It's not linking right and saying all my class variables in my main (ex. CLASSNAME a,b,c,d) are undefined and such. Just making sure how do you refer to the class in the classname.cpp and such?
Any ideas, if you want to see the code I can email it to you, because i havent figured out the c++ text to html converter thing yet.
Thanks.
EDIT: here's the code
<lj-cut text="This Appears On
Friends' Pages">>#ifndef INTEGER
<pre><code><#define INTEGER
#include <iostream>
#include <string>
using namespace std;
class INT {
int digits[100];
char sign;
INT& check();
public:
INT ( );
INT (int num);
INT (string s);
bool operator<(const INT&) const;
bool operator>(const INT&) const;
bool operator==(const INT&) const;
bool operator!=(const INT&) const;
bool operator <=(const INT&) const;
bool operator >=(const INT&) const;
operator int() const;
operator string() const;
friend INT operator%(const INT&, const INT&);
friend INT operator+(const INT&, const INT&);
friend INT operator-(const INT&, const INT&);
friend INT operator*(const INT&, const INT&);
friend INT operator/(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
#endif
#include <iostream>
#include "10456904INT.h"
#include <string>
using namespace std;
INT () {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT (string s) {
INT result;
int counts = 99, length = s.length();
for (;length > 0; length--,counts--) {
result[counts] = (s[length - 1] - '0');
}
while (counts >=0) {
result[counts] = 0;
counts--;
}
return result;
return result;
}
bool INT::operator<(const INT& b) const {
INT temp = this
if ((temp.sign == 'n') && (b.sign == 'p'))
return true;
else if ((temp.sign == 'p') && (b.sign == 'n'))
return false;
int counters = 0;
while (counters < 100) {
if ((temp.digits[counters] > b.digits[counters]) && (temp.sign == 'n') && (b.sign =='n')) {
counters++;
return true;
else if ((temp.digits[counters] > b.digits[counters]) && (temp.sign == 'p') && (b.sign =='p')) {
counters++;
return false;
else if ((temp.digits[counters] < b.digits[counters]) && (temp.sign == 'n') && (b.sign =='n')) {
counters++;
return false;
else if ((temp.digits[counters] < b.digits[counters]) && (temp.sign == 'p') && (b.sign =='p')) {
counters++;
return true;
else if (temp.digits[counters] == b.digits[counters])
counters++;
}
return false;
}
bool INT::operator>(const INT& b) const {
if (*this.sign == 'n') && (b.sign == 'p')
return false;
else if (temp.sign == 'p') && (b.sign == 'n')
return true;
int counter = 0;
while (counter < 100) {
if (temp.digits[counter] < b.digits[counter]) {
counter++;
return false;
}
else if ((temp.digits[counter] > b.digits[counter]) && (temp.sign == 'n') && (b.sign =='n')) {
counter++;
return false;
else if ((temp.digits[counter] > b.digits[counter]) && (temp.sign == 'p') && (b.sign =='p')) {
counter++;
return true;
}
else if (temp.digits[counter] == b.digits[counter])
counter++;
}
return false;
}
bool INT::operator==(const INT& b) const{
INT tem = *this;
int count = 0;
if (temp.sign != b.sign)
return false;
while (count < 100) {
if (temp.digits[b] == b.digits[count])
count++;
else
return false;
}
return true;
}
bool INT::operator!=(const INT& b) const {
if (*this == b)
return false;
else
return true;
}
bool INT::operator <=(const INT& b) const {
if ((*this < b) || (*this == b))
return true;
else
return false;
}
bool INT::operator >=(const INT&) const {
if ((*this > b) || (*this == b))
return true;
else
return false;
}
INT::operator string() const {
int digs = 0
string result = "";
if (sign == 'n')
string = string + '-';
for (; digits[digs] == 0; digs++)
while (digs < 100) {
string = string + (digits[digs] + '0')
digs++;
}
return result;
}
INT::ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
INT::istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loc++] = number.at(a) - '0';
return is;
}
INT INT::operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
else {
//CODE TO ADD TWO NUMBERS WITH DIFFERENT SIGNS
int diff, borrow=0, i=0;
INT big, small;
while ( (x.digits[i] == y.digits[i]) && (i < 99) ) i++; // skip to first non-common digit in both x & y
if (x.digits[i] == y.digits[i]) return 0; // if (x = y), then [(x - y) = (y - x) = 0]
if (x.digits[i] > y.digits[i]) { // determine which INT has the highest absolute value
big = x;
small = y;
}
else {
big = y;
small = x;
}
result.sign = big.sign; // retain the sign of the largest number for the result
for (int a=99; a>=0; a--) {
//subtract digit of smaller INT from larger INT and subtract borrow if necessary
diff = big.digits[a] - small.digits[a] - borrow;
if (diff < 0) {
// must 'borrow' from next digit to the left, set borrow flag and add 10
borrow = 1;
diff += 10;
}
else borrow = 0; // reset flag
result.digits[a] = diff;
}
}
return result;
}
INT INT:::operator-(const INT& x, const INT& y) {
//reverse sign of y and add
INT z = y;
if (y.sign=='n') z.sign='p';
else z.sign='n';
return x + z;
}
INT INT::operator*(const INT& x, const INT& y) {
INT result;
int xlen=0, ylen=0, carry=0;
//find number of digits in both x & y
while (x.digits[xlen] == 0) xlen++;
while (y.digits[ylen] == 0) ylen++;
xlen = 100 - xlen;
ylen = 100 - ylen;
//multiply each digit of y with x and add results
for (int i=0; i<ylen; i++) {
INT temp;
for (int j=0; j<=xlen; j++) {
if (i + j > 99) break; // result too large, stop processing before array out-of-bounds
temp.digits[99-(i+j)] = (x.digits[99-j] * y.digits[99-i]) + carry;
carry = temp.digits[99-(i+j)] / 10; // carry tenths place to next iteration
temp.digits[99-(i+j)] %= 10; // put ones place in temp array
}
result = result + temp;
}
if (x.sign != y.sign) result.sign='n'; // if signs are not the same, result is negative
return result;
}
INT INT::operator/(const INT& x, const INT& y) {
INT result(1), xpos=x, ypos=y;
xpos.sign='p'; ypos.sign='p'; // store x & y in temp variables with positive signs
int i = 0;
while ( (x.digits[i] == y.digits[i] ) && (i < 99) ) i++;
if (x.digits[i] < y.digits[i]) return 0; // if y > x then result = 0
// handle division by zero
bool iszero=true;
for (int j=99; j>=0; j--) {
if (y.digits[j] == 0) continue;
iszero = false;
}
if (iszero) {
cout << "Error: division by zero.\n";
exit(1);
}
/* Multiply denominator by 10...2 until it becomes greater than numerator, result
keeps track of multiplication operations */
INT temp=ypos, temp2=ypos;
bool flag = true;
for (int a=10; a>1; a--) {
while (flag) {
if (temp2.digits[0]) break; // prevent from trying to multiply over 100 digits
temp2 = temp * a;
int j = 0;
while ( (x.digits[j] == temp2.digits[j] ) && (j < 99) ) j++;
if (x.digits[j] < temp2.digits[j]) break;
temp = temp2; // stores last result that was less than numerator
result = result * a;
}
}
/* divide recursively [(x - current product) / y] and add to result until no further iterations
can be made */
result = result + (xpos-temp)/ypos;
if (x.sign != y.sign) result.sign='n'; // if signs are not the same, result is negative
return result;
}
INT INT::operator%(const INT& a, const INT& b) {
INT mod, quotient tempb = b, tempa = a;
quotient = tempa/tempb;
tempb = tempb*quotient;
mod = tempa - tempb;
mod.sign ='p';
return mod;
}
#include <iostream>
#include "10456904INT.h"
#include <string>
using namespace std;
int main() {
INT a, b, c, d;
cin >> a;
cin >> b;
c = a + b;
d = a / b;
cout << c << endl;
cout << d << endl;
return 0;
}</pre></code></lj-cut>
