mardi 28 septembre 2021

Handling Input/Output when creating own Bigint class

I'm trying to create my own BigInt class that can handle numbers up to 256 digits. Going off of this main file:

#include <iostream>
#include "Bigint.h"

using namespace std;


int main() {
    Bigint n1, n2;
    char op;
    
    while (cin >> n1 >> n2 >> op) {
        switch (op) {
            case '+' :
                cout << n1 + n2 << endl;
                break;
            case '-' :
                cout << n1 - n2 << endl;
                break;
            case '*' :
                cout << n1 * n2 << endl;
                break;
            case '/' :
                cout << n1 / n2 << endl;
                break;

        }
    }

    return 0;
}

I understand the best way to do this would be to overload the input operator so that it converts whatever number is entered as input into a digit array. And then from there I could then overload the binary operators. In terms of the class 'Bigint' my header is as follows:

#ifndef BIGINT_BIGINT_H
#define BIGINT_BIGINT_H

#include <iostream>

#define BIGINT_SIZE 256

class Bigint {
public:
        friend std::istream& operator>> (std::istream& in, Bigint& n);

        friend std::ostream& operator<< (std::ostream& out, const Bigint& n);

        // the binary + operator
        friend Bigint operator+ (const Bigint& n1, const Bigint& n2);

        // the binary - operator
        friend Bigint operator- (const Bigint& n1, const Bigint& n2);

        // the binary * operator
        friend Bigint operator* (const Bigint& n1, const Bigint& n2);

       // the binary / operator
       friend Bigint operator/ (const Bigint& n1, const Bigint& n2);

};


#endif //BIGINT_BIGINT_H

But I'm lost as to how I would actually go about overloading the input and output operator in the class cpp file.

std::istream& operator>> (std::istream& in, Bigint& n){
        
}
std::ostream& operator<< (std::ostream& out, const Bigint& n) {
   
}

I think I'm confusing myself, but I don't quite understand what std::istream& in, std::ostream out and Bigint& n are in the context of this. How would I handle these variables when writing something that converts the istream into a digit array? Thank you

Aucun commentaire:

Enregistrer un commentaire