BigInt

I need to write a big int class starting from these two files that tests <, !=, -=, -, *=, and *. I really don't even know where to begin. Anyone have any advice?

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifndef SIMPLENUM_H_
#define SIMPLENUM_H_

#include <iostream>
#include <exception>
#include <limits>

class SimpleNum
{
public:
   // standard methods
   //------------------

   // post: this SimpleNum is UNKNOWN
   SimpleNum();

   // post: this SimpleNum is a copy of other
   SimpleNum( const SimpleNum & other );

   // post: this SimpleNum is UNKNOWN
   ~SimpleNum();

   // post: this SimpleNum is a copy of other
   const SimpleNum & operator=( const SimpleNum & other );


   // other constructors
   //--------------------

   // post: this SimpleNum has the value x
   // note: exception raised if x is negative
   SimpleNum( const int & x );


   // accessors
   //-----------

   // post: this SimpleNum has been printed to outfile
   void
      print( std::ostream & outfile ) const;

   // post: true has been returned just if this SimpleNum is
   //         valid
   bool
      isValid() const;


   // mutator
   //----------

   // pre:  the value of this SimpleNum is n
   // post: the value of this SimpleNum is n-other
   // note: exception raised if n-other is negative
   const SimpleNum &
      operator-=( const SimpleNum & other );


private:
   // data members
   long store_;
};


// auxilliary functions
//----------------------

// post: number has been written to outfile
std::ostream &
   operator<<( std::ostream & outfile, const SimpleNum & number );


// post: lhs - rhs has been returned
// note: exception raised if lhs-rhs is negative
//       exception raised if either parameter is UNKNOWN
SimpleNum
   operator-( const SimpleNum & lhs, const SimpleNum & rhs );


// Method definitions
//--------------------

// post: this SimpleNum is UNKNOWN
SimpleNum::SimpleNum()
   : store_( -1 )
{
}


// post: this SimpleNum is a copy of other
// note: exception raised if other is INVALID
SimpleNum::SimpleNum( const SimpleNum & other )
{
   if( other.isValid() )
   {
      store_ = other.store_;
   }
   else
   {
      throw std::exception::exception( "INVALID parameter in copy constructor" );
   }
}


// post: this SimpleNum is KNKNOWN
SimpleNum::~SimpleNum()
{
   store_ = -1;
}
  

// post: this SimpleNum is a copy of other
// note: exception raised if other is INVALID
const SimpleNum & 
   SimpleNum::operator=( const SimpleNum & other )
{
   if( ! other.isValid() )
   {
      throw std::exception::exception( "INVALID parameter in assignment operator" );
   }

   if( this != & other )
   {
      store_ = other.store_;
   }

   return *this;
}


// post: this SimpleNum has the value x
// note: this SimpleNum is TOO_SMALL if x is negative
SimpleNum::SimpleNum( const int & x )
{
   if( x < 0 )
   {
      // x is TOO_SMALL
      store_ = - 2;
   }
   else
   {
      store_ = x;
   }
}


// post: this SimpleNum has been printed to outfile
// note: invalid numbers are printed as special strings
void
   SimpleNum::print( std::ostream & outfile ) const
{
   if( store_ == -1 )
   {
         outfile << "#UNK";
   }
   else if( store_ == - 2 )
      {
         outfile << "#NEG";
   }
   else
   {
         outfile << store_;
   }
}


// post: true has been returned just if this SimpleNum is valid
bool
   SimpleNum::isValid() const
{
   return store_ >= 0 ;
}


// pre:  the value of this SimpleNum is n
// post: the value of this SimpleNum is n-other
// note: exception raised if this SimpleNum or other is invalid
//       exception raised if n-other is negative
const SimpleNum &
   SimpleNum::operator-=( const SimpleNum & other ) 
{
   if( isValid() && other.isValid() )
   {
      if( store_ >= other.store_ )
      {
         store_ -= other.store_;
      }
      else
      {
         // The sum is NEGATIVE
         throw std::exception::exception( "NEGATIVE result operator-=" );
      }
   }
   else
   {
      throw std::exception::exception( "INVALID number in operator-=" );
   }
   return *this;
}


// post: number has been written to outfile
std::ostream &
   operator<<( std::ostream & outfile, const SimpleNum & number )
{
   number.print( outfile );
   return outfile;
}


// post: lhs - rhs has been returned
// note: exception raised if lhs-rhs is negative
//       exception raised if either parameter is UNKNOWN
SimpleNum
   operator-( const SimpleNum & lhs, const SimpleNum & rhs )
{
   SimpleNum diff( lhs );
   diff -= rhs;
   return diff;
}

#endif 


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
#include "SimpleNum01.h"

#include <iostream>

using namespace std;


int
   main()
{
   SimpleNum x;
   cout << x << endl;

   SimpleNum neg( -1234 );
   cout << neg << endl;

   x = 24;
   cout << x << endl;

   x = 204;
   x -= 107;
   cout << x << endl;

   while( true )
   {
      cout << endl << "Enter two values: ";
      long one, two;
      cin >> one >> two;

      cout << "diff = snone - sntwo is ";
      try
      {
         SimpleNum snone( one );
         SimpleNum sntwo( two );
         SimpleNum diff = snone - sntwo;

         cout << diff << endl;
      }
      catch ( std::exception & ex )
      {
         cout << ex.what() << endl;
      }

      cout << "snone - sntwo is ";
      try
      {
         SimpleNum snone( one );
         SimpleNum sntwo( two );
         //SimpleNum diff = snone - sntwo;

         cout << snone - sntwo << endl;
      }
      catch ( std::exception & ex )
      {
         cout << ex.what() << endl;
      }

      cout << "snone - two is ";
      try
      {
         SimpleNum snone( one );
         SimpleNum sntwo( two );
         //SimpleNum diff = snone - sntwo;

         cout << snone - two << endl;
      }
      catch ( std::exception & ex )
      {
         cout << ex.what() << endl;
      }

      cout << "one - sntwo is ";
      try
      {
         SimpleNum snone( one );
         SimpleNum sntwo( two );
         //SimpleNum diff = snone - sntwo;

         cout << one - sntwo << endl;
      }
      catch ( std::exception & ex )
      {
         cout << ex.what() << endl;
      }
   }

   return 0;
}
Topic archived. No new replies allowed.