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
#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(constchar*);
Int(const Int&);
~Int();
Int operator/(const Int&);
Int operator%(const Int&);
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 = newshort[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(constchar* string)
{
cout << "Int(const char*) called" << endl;
number = newshort[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 = newshort[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
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.
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).
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.