Source File: Input: Output: Value: Integer data is usually represented in a single word on a computer. The number of bit
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Source File: Input: Output: Value: Integer data is usually represented in a single word on a computer. The number of bit
#include <iostream>#include <string>#include <deque>
using namespace std;
typedef enum {NEGATIVE, ZERO, POSITIVE} Sign;
bool isInt(string s);
class BigInt{ friend ostream& operator<<( ostream& output,const BigInt& ); friend istream& operator>>( istream& input,BigInt& ); public: BigInt(); // constructor; digits = 0 BigInt( int num ); //constructor; digits = num BigInt( const string str ); // constructor; digits =str BigInt( const BigInt& other ); // copy constructor
bool operator==( const BigInt& rhs )const; // Equality bool operator< ( const BigInt& rhs )const; // Less Than
private: Sign sign; // Sign of # deque<char> digits; //Deque of digits of #};
#endif-----------------------------------------------------------------------------------------
#include <lab45.h>#include <cstdlib>#include <iomanip>#include <regex>
using namespace std;
void compareZeroes(const BigInt& a, const BigInt& b,const BigInt& c, const BigInt& d, const BigInt& e, const BigInt&f, string v1, string v2, string v3, string v4, string v5, string v6);
int main(){ BigInt a, b(-507), c("abc"), d("275"), e(" -1111111111222222222233333333334444 "); BigInt f(e), g(" 0 "), h(" -0 "); BigInt i(" +0 "), j(+0),k(-0);
cout << boolalpha; cout << "a = " << a << endl; cout << "g = " << g << endl; cout << "h = " << h << endl; cout << "i = " << i << endl; cout << "j = " << j << endl; cout << "k = " << k << endl;
compareZeroes(a, g, h, i, j, k, "a", "g", "h", "i", "j","k"); compareZeroes(g, h, i, j, k, a, "g", "h", "i", "j", "k","a"); compareZeroes(h, i, j, k, a, g, "h", "i", "j", "k", "a","g"); compareZeroes(i, j, k, a, g, h, "i", "j", "k", "a", "g","h"); compareZeroes(j, k, a, g, h, i, "j", "k", "a", "g", "h","i"); compareZeroes(k, a, g, h, i, j, "k", "a", "g", "h", "i","j");
cout << "a = " << a << endl << "b= " << b << endl; cout << "c = " << c << endl << "d =" << d << endl; cout << "e = " << e << endl << "f =" << f << endl; c = a = b; cout << "a = " << a << endl << "b =" << b << endl; cout << "c = " << c << endl; cout << "a == b = " << (a == b) <<endl; cout << "c == b = " << (c == b) <<endl;
while (cin >> a >> b) { cout << "a = " << a << " b = "<< b << endl; cout << a << " == " << b<< " = " << (a == b) << endl; cout << a << " < " << b << " = " << (a < b) <<endl; }
return EXIT_SUCCESS;}
void compareZeroes(const BigInt& a, const BigInt& b,const BigInt& c, const BigInt& d, const BigInt& e, const BigInt&f, string v1, string v2, string v3, string v4, string v5, string v6){ cout << v1 << " == " << v1 << " = "<< (a == a) << endl; cout << v1 << " == " << v2 << " = "<< (a == b) << endl; cout << v1 << " == " << v3 << " = "<< (a == c) << endl; cout << v1 << " == " << v4 << " = "<< (a == d) << endl; cout << v1 << " == " << v5 << " = "<< (a == e) << endl; cout << v1 << " == " << v6 << " = "<< (a == f) << endl;}
bool isInt(string s){ regex pattern {R"(^\s*[-+]?\d+\s*$)"};
return regex_match(s, pattern);}
istream& operator>>(istream& input, BigInt&num){ string s;
input >> s; num = BigInt(s);
return input;}
ostream& operator<<(ostream& output, constBigInt& num){ deque<char>::const_reverse_iterator itr;
if (num.sign == NEGATIVE) output << '-';
for (itr = num.digits.crbegin(); itr !=num.digits.crend(); ++itr) output << *itr;
return output;}----------------------------------------------------------------------------------------------
#include <lab45.h>#include <cctype> // How can I write this page
BigInt::BigInt() // constructor;digits = 0 : BigInt ("0"){ }
BigInt::BigInt( int num) //constructor; digits = num : BigInt (to_string(num)){ }
BigInt::BigInt( const string str) // constructor;digits = str{ if (isInt(str)) { string::const_reverse_iterator ritr, end =str.crend(); sign = POSITIVE; for (ritr = str.crbegin(); ritr < end;++ritr) { if (isspace(*ritr)) ; else if (isdigit(*ritr)) digits.emplace_back(*ritr); else if (*ritr == '-') sign = NEGATIVE; } while (!digits.empty() &&digits.back() == '0') { digits.pop_back(); } if (digits.empty()) { //deque is empty, we are representing a true 0 sign = ZERO; digits.emplace_back('0'); } } else { sign = ZERO; digits.emplace_back('0'); } }BigInt::BigInt( const BigInt& other) //copyconstructor{ this->sign = other.sign; this->digits = other.digits;}bool BigInt::operator== ( const BigInt& rhs) const//Equality{ return true;}bool BigInt::operator< ( const BigInt& rhs)const //Less than{ return true;}
Source File: Input: Output: Value: Integer data is usually represented in a single word on a computer. The number of bits in a word determines the range of integers representable. For example, a 16-bit word allows integers in the range from -32768 through 32767. Doubling the word size to 32 bits increases the range from -2147483648 through 2147483647. In this latter case, results that are representable within ten digits are possible. It is desirable to be able to represent integral results that are larger (in absolute value) than this. In this assignment you are to develop a class for representing large integers. The large integer will be implemented as a deque of chars. Each element in the deque will contain a digit of the large integer. The large integer is to be stored such that the least significant digit is contained in the first element, the next-to-least significant digit in the second element, and so on. No leading zeroes should be stored with the large integer. The number zero (0) should be stored as a single digit of zero (0). For this assignment, you are to create several constructors and overloaded operators that can be used for comparing large integers. A header file is shown in Figure 1, a sample main function for testing your implementation is shown in Figure 2, and a sample execution sequence is shown in Figure 3. To use the Makefile as distributed in class, add a target of lab45 to targets2srcfiles. For the constructor that takes a string parameter, if the isInt function returns false, the number should be stored as zero. using namespace std; 10 typedef enum {NEGATIVE, ZERO, POSITIVE) Sign; 11 7 9 12 13 14 #include #include 6 #include 15 24 17 18 19 * 20 21 Lab 45 24 25 61 26 Page 2 63 64 BigInt(int num); BigInt( const string str); 22 BigInt( const BigInt& other ); = Lab 45 27 CS 2336 Data Structures and Algorithms Lab 45 Page 4 30 31 32 Lab 45 28 29 deque digits; #ifndef LAB45_H #define LAB45_H bool is Int (string s); class BigInt { public: g 55 h = 0 56 i = 0 57 0 }; j = 58 k = 0 BigInt (); private: bool operator==(const BigInt& rhs ) const; // Equality bool operator<(const BigInt& rhs) const; // Less Than a #endif CS 2336 Lab 45 5 1 #include 2 #include 6 friend ostream& operator<<(ostream& output, const BigInt& ); friend istream& operator>>(istream& input, BigInt& ); 3 #include #include 8 10 9 11 14 { 16 17 Sign sign; 18 19 15 BigInt a, b(-507), c("abc"), d("275"), e(" BigInt f(e), g(" BigInt i(" +0 22 25 23 27 29 30 35 43 46 49 56 */2336/45/1ab45. (CICPP| cpp|c++|cc|cxx| cp) under control of main function under control of main function 4 57 39 cout << "a = 62 63 64 69 73 32 compareZeroes (j, k, a, g, h, i, "j", "k", "a", "g", "h", "i"); compareZeroes (k, a, g, h, i, j, "k", "a", "g" "h", "₁", "j"); 74 44 while (cin >> a >> b) { cout << "C = " 41 cout << "a == b = " << (a == b) << endl; 42 cout << "c == b = " << (c == b) << endl; 68 { 83 80 51 return EXIT_SUCCESS; } 88 87 int main() 75 { 90 93 using namespace std; void compareZeroes (const BigInt& a, const BigInt& b, const BigInt& c, const BigInt& d, const BigInt& e, const BigInt& f, string v1, string v2, string v3, string v4, string v5, string v6); 91 95 { } CS 2336 Data Structures and Algorithms cout << v1 << " == " << v1 << " = " << (a == a) << endl; 60 cout << v1 << " == " << v2 << " = " << (a == b) << endl; 61 cout << v1 << " == " << v3 << " = " << (a == c) << endl; 71 return regex_match(s, pattern); 72 } 78 input >> s; Figure 1. /usr/local/2336/include/lab45.h (Part 1 of 2) cout << boolalpha; cout << "a = " << a << endl; cout << "g = " << g << endl; cout << "h= " << h << endl; cout << "i = " << i << endl; cout << "j = " << j << endl; cout << "k = " << k << endl; } 96 } } cout << "a = " << a << endl << "b = "1 cout << "C = " cout << "e = c = a = b; 103 Figure 1. /usr/local/2336/include/lab45.h (Part 2 of 2) 101 compareZeroes (a, g, h, i, j, k, "a", "g", "h", "i", "j", "k"); compareZeroes (g, h, i, j, k, a, "g", "h", "i", "j", "k", "a"); compareZeroes (h, i, j, k, a, g, "h", "i", "j", "k", "a", "g"); compareZeroes (i, j, k, a, g, h, "i", "j", "k", "a", "g", "h"); bool isInt (s ing s) regex pattern (R" (^\s* [-+]?\d+\s*$)"}; 1 newuser@csunix "> cd 2336 2 newuser@csunix /2336> ./getlab.ksh 45 * Checking to *Creating a * Checking = true void compareZeroes (const BigInt& a, const BigInt& b, const BigInt& c, const BigInt& d, const BigInt& e, const BigInt& f, = true 115 117 94 return output; string s; istream& operator>>(istream& input, BigInt& num) 59 a == 60 a == g = true a == h = true. a == 1 return input; ostream& operator<<(ostream& output, const BigInt& num) 85 { 86 deque :: const_reverse_iterator itr; Figure 2. /usr/local/2336/src/lab45main. C (Part 1 of 3) input num = BigInt(s); *Copying from folder /usr/local/2336/data/45 to folder ./45 C d -12345678901234567890 a === true a == k = true. c cout << "a = " << a << " " << b << endl; cout << a << " == " << b << " = " << (a == b) << endl; cout << a << " < " << b << " << (a < b) << endl; cout << v1 << " == " << v4 << " = " << (a == d) << endl; cout << v1 << " == " < 191 newuser@csunix /2336/45> 192 newuser@csunix /2336/45> // constructor; digits = 0 // constructor; digits = num // constructor; digits = str // copy constructor lab45.h 42 43 44 45 // Sign of # // Deque of digits of # 46 47 48 49 50 51 65 66 67 68 Figure 3. Commands to Compile, Link, & Run Lab 45 (Part 1 of 3) 74 38 99999999999999999998 99999999999999999999 12345678901234567890 98765432109876543210 40 98765432109876543210 12345678901234567890 41 1234567890123456789 == g 60 60 g== Due Date: See Blackboard. Data Structures and Algorithms CS 2336 Data Structures and Algorithms -0 h ==i == j = true 69 g== k = true = true 71 h == h = true 70 g== a 72 hi= true 73 h=j= true true ==k= = true h == a 118 "); 124 75 76 h == g = true. 77 i ====1= true 130 137 Due Date: See Blackboard 139 g= true = true = true 148 "); 154 1234567890123456789 == 1234= false 159 1234567890123456789 < 1234 false 160 a = 1234 b = 1234567890123456789 161 1234 1234567890123456789 = 162 1234 < 1234567890123456789 = true false 163 a = 0 b 164 0 == 0 = true. 0 < 0 = false 166 a = 0 true 167 00= 0 false 168. 0 169 a = 0 b = 0 172 170 0 0= true 171 0 0 false a = 0 b = 0 173 0 0= true 174 0 0 false. a = 0 b = 0 176 0 0= true 177 0 0 false 178 a = 0 b = 0 179 0 0= true < 0 = false 175 180 0 = 0 181 a b = 0 182 0 0 true 0 false 183 0 184 a = 0 b = 0 185 00= true 186 0 < 0 = false 187 a = 0 b = 0 188 0 0= true 0 false 189 0 Due Date: See Blackboard lab45main. C 157 Page 1 Due Date: See Blackboard. +0 -0 0 +0 Page 3 1234 1234567890123456789 -0 0 +0 -0 0 119 12345678901234567890 == -12345678901234567890 = false 120 12345678901234567890 < -12345678901234567890 = false 121 = 0 a = 12345678901234567890 b 122 12345678901234567890 == 0 = false 123 12345678901234567890 < 0 = false a = 0 b = 0 125 0 0= true 0 0 false. 126 127 a = -98765432109876543210 b = -12345678901234567890 128 -98765432109876543210 == -12345678901234567890 = false 129 -98765432109876543210 < -12345678901234567890 = true 131 -99999999999999999999 == -99999999999999999998 = false 132 -99999999999999999999 < -99999999999999999998= true 133 a = -99999999999999999998 b = -99999999999999999999 134 -99999999999999999998 == -99999999999999999999 = false 135 -99999999999999999998 < -99999999999999999999 = false 136 a = -1234567890123456789 a = 12345678901234567890 b = -12345678901234567890 -1234567890123456789 == -1234 = false 138 -1234567890123456789 < -1234 = true a = -1234 b = -1234567890123456789 140 -1234-1234567890123456789 = false 141 -1234 < -1234567890123456789 = false 142 a = 12345678901234567890 b = 12345678901234567890 143 12345678901234567890 == 12345678901234567890 = true 144 1234567890123456789012345678901234567890 = false 1234 Page 5 a = -99999999999999999999 b = -99999999999999999998 ± ± ± o o o ó ó ó +0 b = -1234 145 146 9999999999999999999999999999999999999998 = false 9999999999999999999999999999999999999998 = false a = 99999999999999999999 b = 99999999999999999998 149 99999999999999999998 ==99999999999999999999 = false 150 99999999999999999998 < 99999999999999999999 = true a = 99999999999999999998 b = 99999999999999999999 152 12345678901234567890 == 98765432109876543210 = 153 12345678901234567890 Figure 3. Commands to Compile, Link, & Run Lab 45 (Part 2 of 3) cat 01.dat ./lab45 > my.out diff 01.out my.out a = 12345678901234567890 b = 98765432109876543210 Figure 3. Commands to Compile, Link, & Run Lab 45 (Part 3 of 3) 155 98765432109876543210 == 12345678901234567890 = false 156 98765432109876543210 < 12345678901234567890 = false a = 1234567890123456789 b = 1234 false 98765432109876543210 = true a = 98765432109876543210 b = 12345678901234567890 Due Date: See Blackboard Lab 45 Due Date: See Blackboard