Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "LongInt.h"
- /* LongInt >> operation overloader
- *
- * Params: istream, LongInt
- * Returns: istream containing input of LongInt object
- *
- */
- istream& operator>>( istream &in, LongInt &rhs ){
- string tempStr;
- in >> tempStr;
- for(int i = 0; i < tempStr.length(); i++){
- switch(tempStr[i])
- {
- case '-':
- rhs.negative = true;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- rhs.digits.addFront(tempStr[i]);
- break;
- default:
- break;
- }
- }
- return in;
- }
- ostream &operator<<( ostream &out, const LongInt &rhs ){
- string stringOutput;
- char tmp;
- LongInt LongIntTemp;
- LongIntTemp = rhs;
- while(!LongIntTemp.digits.isEmpty())
- {
- tmp = LongIntTemp.digits.getBack();
- switch(tmp){
- case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
- stringOutput.push_back(tmp);
- break;
- default:
- break;
- }
- LongIntTemp.digits.removeBack();
- }
- if(rhs.negative == true)
- {
- stringOutput = '-' + stringOutput;
- }
- if(stringOutput.length() == 0)
- {
- stringOutput = '0';
- }
- out << stringOutput;
- return out;
- }
- // Constructors
- LongInt::LongInt(const string str ){
- negative = false;
- for(int i = 0; i < str.length(); i++){
- digits.addFront(str[i]);
- if(str[i] == '-')
- {
- negative = true;
- }
- }
- void remove0s( );
- if(digits.isEmpty()){
- digits.addFront('0');
- }
- //cout << "inside the Longint(string) constructor" << endl;
- }
- LongInt::LongInt(const LongInt &rhs ){
- digits = rhs.digits;
- if(rhs.negative == true)
- {
- negative = true;
- }
- else
- {
- negative = false;
- }
- //cout << "inside the Longint(LongInt) constructor" << endl;
- }
- LongInt::LongInt( ){
- digits.addFront('0');
- negative = false;
- //cout << "inside the Longint() constructor" << endl;
- }
- // Destructor
- LongInt::~LongInt( ){
- digits.clear();
- }
- // Arithmetic binary operators
- LongInt LongInt::operator+( const LongInt &rhs ) const {
- return rhs;
- }
- LongInt LongInt::operator-( const LongInt &rhs ) const {
- return rhs;
- }
- // assignment operators
- const LongInt& LongInt::operator=( const LongInt &rhs ) {
- digits = rhs.digits;
- if(rhs.negative == true)
- {
- negative = true;
- }
- while(digits.getBack() == '-' || digits.getBack() == '0')
- {
- digits.removeBack();
- }
- return rhs;
- }
- // Logical binary operators
- bool LongInt::operator< ( const LongInt & rhs ) const {
- return true;
- }
- bool LongInt::operator<=( const LongInt & rhs ) const {
- return true;
- }
- bool LongInt::operator> ( const LongInt & rhs ) const {
- return true;
- }
- bool LongInt::operator>=( const LongInt & rhs ) const {
- return true;
- }
- bool LongInt::operator==( const LongInt & rhs ) const {
- return true;
- }
- bool LongInt::operator!=( const LongInt & rhs ) const {
- return true;
- }
- void LongInt::remove0s( ){
- bool firstCharNonZero;
- char tmp;
- Deque<char> dequeTemp;
- dequeTemp = digits;
- firstCharNonZero = false;
- digits.clear();
- while(!dequeTemp.isEmpty())
- {
- tmp = dequeTemp.getFront();
- //if((tmp == '0' || tmp == '-') && !firstCharNonZero){
- if(tmp == '0' && !firstCharNonZero){
- firstCharNonZero = false;
- }
- else
- {
- firstCharNonZero = true;
- }
- dequeTemp.removeFront();
- if(firstCharNonZero == true){
- digits.addFront(tmp);
- }
- }
- }
Add Comment
Please, Sign In to add comment