Guest User

Untitled

a guest
Jul 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include "LongInt.h"
  2.  
  3. /* LongInt >> operation overloader
  4. *
  5. * Params: istream, LongInt
  6. * Returns: istream containing input of LongInt object
  7. *
  8. */
  9. istream& operator>>( istream &in, LongInt &rhs ){
  10. string tempStr;
  11. in >> tempStr;
  12. for(int i = 0; i < tempStr.length(); i++){
  13. switch(tempStr[i])
  14. {
  15. case '-':
  16. rhs.negative = true;
  17. case '0':
  18. case '1':
  19. case '2':
  20. case '3':
  21. case '4':
  22. case '5':
  23. case '6':
  24. case '7':
  25. case '8':
  26. case '9':
  27. rhs.digits.addFront(tempStr[i]);
  28. break;
  29. default:
  30. break;
  31. }
  32.  
  33. }
  34.  
  35.  
  36. return in;
  37. }
  38.  
  39. ostream &operator<<( ostream &out, const LongInt &rhs ){
  40. string stringOutput;
  41. char tmp;
  42. LongInt LongIntTemp;
  43.  
  44. LongIntTemp = rhs;
  45.  
  46. while(!LongIntTemp.digits.isEmpty())
  47. {
  48. tmp = LongIntTemp.digits.getBack();
  49.  
  50. switch(tmp){
  51. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  52. stringOutput.push_back(tmp);
  53. break;
  54. default:
  55. break;
  56. }
  57.  
  58. LongIntTemp.digits.removeBack();
  59. }
  60.  
  61. if(rhs.negative == true)
  62. {
  63. stringOutput = '-' + stringOutput;
  64. }
  65.  
  66. if(stringOutput.length() == 0)
  67. {
  68. stringOutput = '0';
  69. }
  70.  
  71. out << stringOutput;
  72. return out;
  73. }
  74.  
  75.  
  76.  
  77. // Constructors
  78. LongInt::LongInt(const string str ){
  79. negative = false;
  80. for(int i = 0; i < str.length(); i++){
  81. digits.addFront(str[i]);
  82. if(str[i] == '-')
  83. {
  84. negative = true;
  85. }
  86. }
  87.  
  88.  
  89.  
  90. void remove0s( );
  91. if(digits.isEmpty()){
  92. digits.addFront('0');
  93. }
  94. //cout << "inside the Longint(string) constructor" << endl;
  95. }
  96.  
  97. LongInt::LongInt(const LongInt &rhs ){
  98. digits = rhs.digits;
  99. if(rhs.negative == true)
  100. {
  101. negative = true;
  102. }
  103. else
  104. {
  105. negative = false;
  106. }
  107. //cout << "inside the Longint(LongInt) constructor" << endl;
  108. }
  109.  
  110. LongInt::LongInt( ){
  111.  
  112. digits.addFront('0');
  113. negative = false;
  114. //cout << "inside the Longint() constructor" << endl;
  115. }
  116.  
  117.  
  118. // Destructor
  119. LongInt::~LongInt( ){
  120. digits.clear();
  121.  
  122. }
  123.  
  124. // Arithmetic binary operators
  125. LongInt LongInt::operator+( const LongInt &rhs ) const {
  126.  
  127. return rhs;
  128. }
  129. LongInt LongInt::operator-( const LongInt &rhs ) const {
  130.  
  131. return rhs;
  132. }
  133.  
  134. // assignment operators
  135. const LongInt& LongInt::operator=( const LongInt &rhs ) {
  136.  
  137.  
  138. digits = rhs.digits;
  139. if(rhs.negative == true)
  140. {
  141. negative = true;
  142. }
  143.  
  144. while(digits.getBack() == '-' || digits.getBack() == '0')
  145. {
  146. digits.removeBack();
  147.  
  148. }
  149.  
  150. return rhs;
  151. }
  152.  
  153.  
  154. // Logical binary operators
  155. bool LongInt::operator< ( const LongInt & rhs ) const {
  156. return true;
  157. }
  158.  
  159. bool LongInt::operator<=( const LongInt & rhs ) const {
  160. return true;
  161. }
  162.  
  163. bool LongInt::operator> ( const LongInt & rhs ) const {
  164. return true;
  165. }
  166.  
  167. bool LongInt::operator>=( const LongInt & rhs ) const {
  168. return true;
  169. }
  170.  
  171. bool LongInt::operator==( const LongInt & rhs ) const {
  172. return true;
  173. }
  174.  
  175. bool LongInt::operator!=( const LongInt & rhs ) const {
  176. return true;
  177. }
  178.  
  179.  
  180. void LongInt::remove0s( ){
  181. bool firstCharNonZero;
  182. char tmp;
  183. Deque<char> dequeTemp;
  184.  
  185. dequeTemp = digits;
  186. firstCharNonZero = false;
  187. digits.clear();
  188.  
  189. while(!dequeTemp.isEmpty())
  190. {
  191. tmp = dequeTemp.getFront();
  192. //if((tmp == '0' || tmp == '-') && !firstCharNonZero){
  193. if(tmp == '0' && !firstCharNonZero){
  194. firstCharNonZero = false;
  195. }
  196. else
  197. {
  198. firstCharNonZero = true;
  199. }
  200.  
  201. dequeTemp.removeFront();
  202.  
  203. if(firstCharNonZero == true){
  204. digits.addFront(tmp);
  205. }
  206.  
  207. }
  208. }
Add Comment
Please, Sign In to add comment