bool < operator overloading

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
#include "dbiguint.h"
#include <string>
#include <cstdlib>
#include <iostream>
dbiguint::dbiguint()
{
    capacity_ = 1;
    data_=new unsigned short [capacity_];
    data_[0]=0;


}

dbiguint::dbiguint(const std::string &s){
  int templeng = s.length();
  capacity_ = templeng;
  data_=new unsigned short [capacity_];
  for(size_t i = 0; i < capacity_; i++){
    data_[i] = s[templeng -i - 1] -'0';
  }
}
  std::size_t dbiguint::size() const
  {
    return capacity_;
  }

unsigned short dbiguint::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 dbiguint & b)
{
  for (int i = b.size(); i > 0; i--) {
    out<<b[i-1];
  }
  return out;
}
void dbiguint::reserve(std::size_t newcapacity_)
{
  if(newcapacity_<capacity_){
    return;
  }
  else{
    unsigned short *tempdata;
    tempdata = new unsigned short [newcapacity_];
    for (size_t q = 0; q < capacity_; q++) {
      tempdata[q]=data_[q];
    }
    for(size_t i = capacity_; i < newcapacity_; i++){
      tempdata[i]=0;
    }
    capacity_ = newcapacity_;
    delete [] data_;
    data_ = tempdata;
    tempdata = nullptr;
  }
}

void dbiguint::operator += (const dbiguint & b){
  int temp = 0;
  if(capacity_ < b.size()){
    reserve(b.size());
  }
  for (size_t i = 0; i < capacity_; i++) {
    temp = data_[i]+b[i];

    if(temp >= 10){
      if(i == (capacity_ - 1)){
        reserve((capacity_ +1));
      }
      data_[i] = temp-10;
      data_[i+1]++;
    }
    else{
      data_[i]=temp;
    }
  }
}
int dbiguint::compare(const dbiguint & b) const
{
  if(capacity_<b.size())
  {
    return b.size();

  }
  else if(capacity_>b.size())
  {
    return capacity_;
  }
  else{
  return this->toStdString().compare(b.toStdString());
  }
}

bool operator > ( dbiguint & lhs, dbiguint & rhs)
{
  return lhs.compare(rhs) > 0;
}
std::string dbiguint::toStdString() const
 {
    bool first = false;
    std::string result = "";

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

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

    return result;
}

 dbiguint::~dbiguint()
 {
   delete [] data_;
   data_ = nullptr;
   capacity_ = 0;
 }


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
dbiguint.h
#ifndef DBIGUINT_H
#define DBIGUINT_H
#include <iostream>
#include <cstdlib>
#include <string>

class dbiguint
{
public:
    // pre: none
    // post: creates a dynamic bigint value 0
    dbiguint();

    // pre: s[0], ..., s[s.size()-1] are digits
    // post: creates a dbiguint whose digits are given in s
    dbiguint(const std::string & s);

/*We aren't implementing this constructor yet
    // pre:  none    //NEW
    // post: copy constructor: creates a new dynamic bigint which is
    //                         a copy of given dynamic bigint
    dbiguint(const dbiguint &);
*/

    // pre: none    //NEW
    // post: returns dynamically allocated memory to heap
    ~dbiguint();

    // pre: none    //NEW
    // post: makes this dynamic bigint a copy of given dynamic bigint
    //void operator =(const dbiguint &);

    // pre: none    //NEW
    // post: returns the size of the memory block of this dbiguint
    std::size_t size() const;

    // pre: none
    // post: returns the digit at given pos (0 if does not exist)
    //       pos 0 is the least significant (units) digit
    unsigned short operator [](std::size_t pos) const;

    // pre: none
    // post: returns 0 if this dbiguint equals given dbiguint
    //               1 if this dbiguint >      given dbiguint
    //              -1 otherwise
    int compare(const dbiguint &) const;
    std::string toStdString() const;


    // pre: none
    // post: returns a string containing the digits and sign of this dbiguint
  //  std::string toStdString() const;

    // pre: none
    // post: adds/subtracts given dbiguint to this dbiguint
     void operator +=(const dbiguint &);
  //  void operator -=(const dbiguint &);
  //  void operator *=(const dbiguint &);

    // pre: none
    // post: if newcapacity_ <= capacity_ then do nothing (cannot shrink)
    //       else allocate a new block with size newcapacity_
    //       copy existing digits and fill the rest with 0
    void reserve(std::size_t newcapacity_);

private:
    unsigned short *data_;
    std::size_t capacity_;

    // INVARIANTS:
    //    data_ points to (has the address of) a dynamic array
    //    of capacity_ digits
    //    data_[0] = least significant (units) digits
    //    data_[k] = digit at position k (or 0 if not used)


};


// nonmember functions
/*
dbiguint operator +(const dbiguint &, const dbiguint &);
dbiguint operator -(const dbiguint &, const dbiguint &);
dbiguint operator *(const dbiguint &, const dbiguint &);


bool operator <= (dbiguint &, dbiguint &);
bool operator != (dbiguint &, dbiguint &);
bool operator == (dbiguint &, dbiguint &);
bool operator >= (dbiguint &, dbiguint &);
*/
bool operator < (dbiguint &, dbiguint &);
bool operator > ( dbiguint &,  dbiguint &);

std::ostream & operator << (std::ostream &, const dbiguint &);

//std::istream & operator >> (std::istream &, dbiguint &);

#endif // DBIGUINT_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
main.cpp

#include "dbiguint.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
  dbiguint one("500");
  dbiguint two("4000");
  dbiguint three("1000");
  dbiguint four("290");
  cout<<(one>two)<<endl;
  cout<<(three<four)<<endl;



}


Hi so my code for < operator works but It has an issue when
dbiguint one("1234");
dbiguint two("234");
one<two
it will return true and not false because I think it only check the first number how can i Fix it to make sure it works?
Anyone have any idea what I have to do to fix this
Last edited on
Compare the lengths of the strings first.

Only if the strings have the same length do you need to go on with the standard string comparator.
lastchance how do i do that? In my compare function?
can i say if data_size()>b.size()?
Last edited on
If a.size() < b.size() then the number represented by a is smaller and you can return appropriately.
If b.size() < a.size() then the number represented by b is smaller and you can return appropriately.
Once you have done those comparisons, if you have still not returned from the function then you know the sizes are the same and you can do your test for a < b.
lastchance this is what i put now it works until i do one<two when two is like 20 and when one is 100


if(capacity_<b.size())
{
return capacity_;

}
else if(capacity_>b.size())
{
return b.size();
}
Last edited on
@andy101,
Are you trying to return a bool or an int? Which function are you referring to? The answer is almost certainly ... no.

If you provide complete, compileable, runnable code, I will endeavour to give you an answer. At the moment I can't see exactly what you are doing.
lastchance Im using a compare function to compare two dbiguints its supposed to return a boolean. I updated my code so it works now
dbiguint one("5000");
dbiguint two("600");
dbiguint three("1000");
dbiguint four("290");
cout<<(one<two)<<endl;
like for example this returns true even though it should be false
Last edited on
@andy101,
Please show complete, compileable, runnable code. Otherwise it is impossible for me to see to what you are referring.

If you don't do that then I'm exiting this thread, as I couldn't give you a reliable answer.
Last edited on
@lastchance i fixed it i added all my files
Last edited on
It doesn't compile. You haven't defined an operator <

Please don't change code further back up the thread; it just confuses things.

Why don't you use std::string rather than dynamic char arrays?
for this assignment, we are supposed to use dynamic arrays.
whoops heres my code for <
bool operator < ( dbiguint & lhs, dbiguint & rhs)
{
return lhs.compare(rhs) < 0;
}
dbiguint one("60");
dbiguint two("5000");
cout<<(one>two)<<endl;
this returns true
Last edited on
But you have std::string throughout your code ... except in the places where it would be most useful!

I'm sorry, @andy101, but you have memory leaks, I can't even work out whether you are using chars or unsigned shorts, and you keep converting to and from std::string when you might as well use std::string throughout to represent your big integers and make life straightforward.

I'll have to let somebody else have a go at this.
This is what you document for compare():
1
2
3
4
5
// pre: none
// post: returns 0 if this dbiguint equals given dbiguint
//               1 if this dbiguint >      given dbiguint
//              -1 otherwise
int compare(const dbiguint &) const;


And this is what it actually returns:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int dbiguint::compare(const dbiguint & b) const
{
    if(capacity_<b.size())
    {
        return b.size();
    }

    else if(capacity_>b.size())
    {
        return capacity_;
    }

    else{
        return this->toStdString().compare(b.toStdString());
    }
}

Can you spot any inconsistency?
:)
Enoizat oh yes I shouldn't put that in my compare function that should go in my < operator definition.
Last edited on
¿what's the value of `b.size()'?
¿is it 0? ¿is it 1? ¿is it -1? ¿may ever be -1? ¿why are you returning .size() instead of 1, 0, or -1?

same thing for capacity_
Topic archived. No new replies allowed.