Overloaded function Modulus

I need big help with figuring out Int operator%(const Int&); i havent started on division just yet because it looks like its going to be more complicated
This is my Class
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
#ifndef INT_H
#define INT_H

#include <iostream>
#include <cmath>

using std::ostream;
using std::istream;
using std::string;

class Int {
private:
    short* number;
    int     size;
    void    computeSize();
    bool negative;

public:
    Int(long = 0);
    Int(const char*);
    Int(const Int&);
    ~Int();
    
    Int operator/(const Int&);
    Int operator%(const Int&);


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
Int Int::operator%(const Int& right)
{
	cout << "operator%(const Int&)" << endl;
        // i need help here not sure at all how to do this please guide me through 
}

Int::Int(long value)
{
	cout << "Int(long) called" << endl;
	this->negative = false;
	size = 0;
	number = new short[30];

	for (int i = 0; i < 30; i++)
		number[i] = 0;

	for (int i = 29; value != 0 && i >= 0; i--) {

		number[i] = value % 10;
		value /= 10;
	}
}
Int::Int(const char* string)
{
	cout << "Int(const char*) called" << endl;
	number = new short[30];
	size = 0;
	this->negative = false;
	for (int i = 0; i < 30; i++)
		number[i] = 0;

	int length = strnlen(string, 30);
	for (int j = 30 - length, k = 0; j <= 29; j++, k++) {

		if (isdigit(string[k]))
			number[j] = string[k] - '0';
	}
}
Int::Int(const Int& copy)
{
	cout << "Int(const Int&) called" << endl;
	number = new short[30];
	size = 0;
	this->negative = false;
	
	for (int i = 29; i >= 0; i--)
		number[i] = copy.number[i];

}


Please please help me, I'm quite new to overloaded operators
Last edited on
Division and remainder are actually extremely similar.

A simple algorithm:
1
2
3
4
5
6
7
remainder = 0
while dividend is not zero
    remainder = remainder * 2 + dividend % 2
    dividend /= 2
    if remainder >= divisor
        remainder -= divisor
return remainder
So i can't follow through with using %, i get an error saying "No operator matches these operands" How can i apply this logic with my code ? if i am taking "right " as a parameter, then right would be my divided ? or would number be my dividend ?
a few observations:
division is usually rather complicated, but multiply usually is not. Granted, both are repeated addition (subtraction is just addition of a negative number) if you want to go brute force at it. If you can rewire division into a multiply, it may be easier to implement.

if you had division, the remainder can be had without loops, just a few short steps:
say you had ... 1023 % 13 for example.
1023/13 is 78 integer division.
13*78 is 1014.
1023-1014 is 9.


Last edited on
So i can't follow through with using %
Of course not. But x % 2 is simply the least significant bit of x, which should be trivial to obtain.
x / 2 is simply all the bits of x shifted down towards the least significant end by 1 digit.
(Obviously x * 2 is all the bits shifted up by 1.)

if i am taking "right " as a parameter, then right would be my divided ? or would number be my dividend ?
*this would be the dividend (the left-hand side of the operation) and the parameter would be the divisor (the other side).
Last edited on
For 'ordinary' numbers division or modulo arithmetic can be carried out by repeated subtraction. So if you can do subtraction (and addition) you can do the rest.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

// dividend/divisor = quotient (+ remainder)

int main()
{
    int dividend{1023};
    int divisor{13};
    int quotient{0};
    int remainder{0};
    
    while( dividend >= divisor)
    {
        quotient ++;
        dividend -= divisor;
    }
    
    remainder = dividend;
    std::cout << quotient << ' ' << remainder << '\n';
    
    return 0;
}
Topic archived. No new replies allowed.