Biguint == operator

I need to create a == operator overload with this biguint class that I created and have no clue how can someone help? I have been trying and used my compare function can someone help with that also? If someone can help me with this part I have done the +,+=,-,-+ operators but cant figure out how to access the numbers or if my compare is right for the == and with <?

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

#include "biguint.h"
#include <string>
#include <cstdlib>
#include <iostream>
biguint::biguint()
{
  for(size_t i = 0; i < CAPACITY; i++){
    data_[i]=0;
  }
}

biguint::biguint(const std::string &s){
  int templeng = s.length();
  for(size_t i = 0; i < templeng; i++){
    data_[i] = s[templeng -i - 1] -'0';
  }
  for(size_t i = templeng; i < CAPACITY; i++){
    data_[i]=0;
  }
}

unsigned short biguint::operator [](std::size_t pos) const{
  unsigned short i = 0;
  if(pos >= 0 && pos < CAPACITY){
    i = data_[pos];
  }
  return i;
}
std::ostream& operator <<(std::ostream& out, const biguint& b)
{
  for (int i = biguint::CAPACITY; i > 0; i--) {
    out<<b[i-1];
  }
  return out;
}
void biguint::operator += (const biguint & b){
  int k = 0;
  int temp = 0;
  for (size_t i = 0; i < CAPACITY; i++) {
    temp = data_[i]+b.data_[i];
    if(temp >= 10){
      data_[i] = temp-10;
      data_[i+1]+=1;
    }
    else{
      data_[i]=temp;
    }
  }
}
biguint operator + (const biguint & lhs, const biguint & rhs)
{
  biguint result(lhs);
  result += rhs;
  return result;
}
int biguint::compare(const biguint & b) const
{
  return this->toStdString().compare(b.toStdString());
}
std::string biguint::toStdString() const
{
    bool start = false;
    std::string result = "";

    for (int i = (int) this->CAPACITY - 1; i >= 0; i--) {
        if (this->data_[i] == 0 && !start) {
            if (this->CAPACITY == 1)
                result += std::to_string(this->data_[i]);
            continue;
        }

        else {
            start = true;
            result += std::to_string(this->data_[i]);
        }
    }

    return result;
}

bool operator == (const biguint & lhs, const biguint & rhs)
{
  return lhs.compare(rhs);
}



void biguint::operator -= (const biguint & b){
  int k = 0;
  int temp = 0;
  for (size_t i = 0; i < CAPACITY; i++) {
    temp = data_[i]-b.data_[i];
    if(temp < 0){
      data_[i] = temp+10;
      data_[i+1]-=1;
    }
    else{
      data_[i]=temp;
    }
  }
}
biguint operator - (const biguint & lhs, const biguint & rhs)
{
  biguint result(lhs);
  result -= rhs;
  return result;
}
Last edited on
std::string::compare() returns an int, not a bool. It returns a positive int if the string is lexicographically after the argument, a negative int if it's before, and 0 if it's equal. So you need to return lhs.compare(rhs) == 0.
@dutch thanks for the reply how about if i wanted to do a < and <= operator?
dutch
Last edited on
std::string::compare returns a negative value for <. So

1
2
3
4
5
6
7
8
9
bool operator < (const biguint & lhs, const biguint & rhs)
{
  return lhs.compare(rhs) < 0;
}

bool operator <= (const biguint & lhs, const biguint & rhs)
{
  return lhs.compare(rhs) <= 0;
}


This all assumes that a lexicographical comparison is appropriate, which I kind of doubt now that I think about it. It would need the leading zeroes to work properly. Maybe you should make a compare function that works on the numeric data directly. It seems that you are storing single digits in ints (or maybe shorts). I would've just used a string in the first place. And you seem to have a maximum CAPACITY for the numbers, too, which is not really necessary.
Hi dutch thanks for your help
Ya i set capacity to 20 i will try to revise on those errors
Dutch do you have any ideas how to do this with dynamic arrays on my other post?
== should be easy and fast for most objects.
is the size the same? if not, not equal.
if size is the same, can you memcmp the data chunk and return that answer...
Topic archived. No new replies allowed.