Jump to content

C++ Calculator for very large numbers.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
TheZea

TheZea

    Newbie

  • Members
  • Pip
  • 4 posts
Hello all,

Firstly let me say yes, i have searched the forums, secondly No! i am not asking for you to do my homework, i realize how much you all hate that =P

I am here for some guidance, i have exhausted my researching capabilities and am at the point of getting nowhere, yes i am a noob to this topic, however, i need to get it done, and i am acing the rest bar this section.

The goal is to build a calculator capable of inputing and outputting large integers (up to 20+ characters) I know there are a lot of different threads on this but they are either extremely complex or do not follow my scope.

I need to use arrays
I need to build a Bigint class
I need to do + / - and *
I need to make my own operators
I need Bigint.cpp and Bigint.h files.

I am only in the process of creating the basic files, not much actual code yet. I have not worked with c++ class code before, which is why i'm having trouble.

Questions i am asking:
1. Is my main method legal? can i create Bigint variables and call to them just as i have in my code? assuming i have created the operators << and >> for them?

2. I am definitely missing something in my class of Bigint, are these array declarations allowed?

3. Can i be pointed in the right direction of creating the Bigint constructor?

4. My main problem is with the << and >> operations, totally stuck here!!!

5. With the << >> operations successful, is it plausible that my getInt function, which is called as part of constructing Bigint, that it will gather the input and place them into an array such that:
a[0 0 0 0 0 0 0 0 0 9 8 7 6 5 4 3 2 1 0]?

Bigint.cpp

using namespace std;


int main(void){


    Bigint x, y;

    char op;

    

    cout << "Value x, y and operation?: ";

    cin >> x >> y >> op; //This will work when I write the code for the >> operator.


    switch(op){

        case '+':

            cout << "Result:\n" << x + y << "\n"; //These should work as I write the operators for + - / *.

            break;

        case '-':

            cout << "Result:\n" << x - y << "\n";

            break;

        case '/':

            cout << "Result:\n" << x / y << "\n";

            break;

        case '*':

            cout << "Result:\n" << x * y << "\n";

            break;

    }

    return(0);

}


Bigint.h

using namespace std;


//Begin class

class Bigint {

    friend ostream& operator>> (std::ostream& in, Bigint& x);

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

    friend Bigint operator+(const Bigint& x, const Bigint& y);

    friend Bigint operator-(const Bigint& x, const Bigint& y);

    friend Bigint operator/(const Bigint& x, const Bigint& y);

    friend Bigint operator*(const Bigint& x, const Bigint& y);


public:

    Bigint(long = 0);

    ~Bigint();

    void getInt(unsigned long long int);


private:

    Bigint a[256];

    Bigint b[256];

    Bigint c[256];

//Need some variable array here?


};

//End class



//Begin constructor

Bigint::Bigint(long value) {

    getInt(value);

}


Bigint::~Bigint() {


}


void Bigint::getInt(unsigned long long int value){


    //How do i do this? do i need the in/out operators done first?

    //How would i test the code?

}

//End constructor



//Begin in/out operators

ostream &operator>>(std::ostream &input, const Bigint &x)

{


    //How do i do this?



    return input;

}


ostream &operator<<(std::ostream &output, const Bigint &x)

{



    //How do i do this?



    return output;

}

//End in/out operators



//Begin math operators

Bigint operator+(const Bigint &x, const Bigint &y) {


//    Make several variables to hold values

//    Make a temp array and carry array

//    Make a variable for the result

//

//    Begin writing psuedo code

    

}


Bigint operator*(const Bigint &x, const Bigint &y) {


//    Make several variables to hold values

//    Make a temp array and carry array

//    Make a variable for the result

//

//    Begin writing psuedo code


}


Bigint operator-(const Bigint &x, const Bigint &y) {


//    Make several variables to hold values

//    Make a temp array and carry array

//    Make a variable for the result

//

//    Begin writing psuedo code


}


Bigint operator/(const Bigint &x, const Bigint &y) {


//    Make several variables to hold values

//    Make a temp array and carry array

//    Make a variable for the result

//

//    Begin writing psuedo code


}

//End Math operators



#endif	/* _BIGINT_H */


Thank you for your replies it is much appreciated. I will be fine writing the calculator itself, its more getting the values into the array that is baffling me!!!

cheers,

Zea

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your Bigint class has three Bigint array members? That just sounds bad. You'll end up using all memory on your system when you try to declare one.

You need to decide how you will store your bigint using non-Bigint variables. Common solutions include strings and vector<int>.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
TheZea

TheZea

    Newbie

  • Members
  • Pip
  • 4 posts
I have seen many uses of 'vectors' before, funny we haven't learned anything about them in class, which is why i was 'afraid' to use them.

So i should declare one string/vector which will be used to construct my Bigint variables yes?

Thank you for the reply :).

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
vector is just like an array, only it has a lot of protection against buffer overruns.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Hmm, if you just cast it as a bigint, will it use the same amount of memory :) ?
if not, maybe thats a solution.

#6
TheZea

TheZea

    Newbie

  • Members
  • Pip
  • 4 posts
Hmmm I will look into vectors, if theres too much to learn in a short time i will just use a string then use overloaded methods on it. However, my problem is still how do i 'take input' then pass it to 'Bigint variable' to be acted upon...

#7
TheZea

TheZea

    Newbie

  • Members
  • Pip
  • 4 posts
There is an error to do with my >> operators in either:
a) class itself
b) constructor
c) >> operator function

Error:
    //SHOULD be placing the value passed to the Bigint(num) string in reverse
    //as per the >> operator in Bigint.h (This is where my errors are recurring
    //well the FIRST error) cant compile any further.
    // -- error: no match for 'operator>>' in 'std::cin >> x' --
    cin >> x >> y >> op;

Hopefully som1 may assist, I have added comments to make the code a little more 'graceful'.

#ifndef _BIGINT_H
#define	_BIGINT_H

#include <iostream>
#include <cctype>
#include <string>
#include "Bigint.h"
#include <vector>
#include <sstream>
using namespace std;

//////////////////////////////////////////////////////////////////////
//Begin class Bigint
//////////////////////////////////////////////////////////////////////
class Bigint {
    friend ostream& operator>> (std::ostream& in, Bigint& x);
    friend ostream& operator<< (std::ostream& out, const Bigint& x);
    friend Bigint operator+(const Bigint& x, const Bigint& y);
    friend Bigint operator-(const Bigint& x, const Bigint& y);
    friend Bigint operator/(const Bigint& x, const Bigint& y);
    friend Bigint operator*(const Bigint& x, const Bigint& y);


public:
    //Default constructor
    Bigint();
    //The constructors we want
    Bigint(const Bigint &x);
    Bigint(string str);

private:
    string num; // <-------- Hold all Bigint's
};
//End class

//////////////////////////////////////////////////////////////////////
//Begin constructor
//////////////////////////////////////////////////////////////////////
Bigint::Bigint(){

    this->num;
}

Bigint::Bigint(string str){
    for(int i = 0; i < str.length(); i++){
        num.push_back((str[i]));
    }
}

Bigint::Bigint(const Bigint& x){
////////////////////////////////////////////////////////
    //Something major missing here, this should be the initial constructor
    //for an unsigned Bigint variable x? I think!!
    //as in Bigint x, y; in Bigint.cpp
    
    this->num = "";


}
//End constructor



//////////////////////////////////////////////////////////////////////
//Begin << and >> operators
//////////////////////////////////////////////////////////////////////
ostream &operator>>(std::ostream &input, Bigint &x)//Inputs the string backwards to be calculated on.
{
    long i = 0;
    for(i = x.num.size(); i > 0; --i){
        input << x.num[i-1];
    }
    return input;
}

//Something very wrong here? or possibly in the class itself.
ostream &operator<<(ostream &output, const Bigint &x)//Outputs the string backwards (correctly).
{
	long i = 0;
	for(i = x.num.size();i > 0; --i){
		output << x.num[i-1];
	}
	return output;
}
//End in/out operators



//////////////////////////////////////////////////////////////////////
//Begin arithmetric operators
//////////////////////////////////////////////////////////////////////
Bigint operator+(const Bigint &x, const Bigint &y) {

    //still to do (testing)
    return x;
}

Bigint operator*(const Bigint &x, const Bigint &y) {

    return x;
}

Bigint operator-(const Bigint &x, const Bigint &y) {

    return y;
}

Bigint operator/(const Bigint &x, const Bigint &y) {

    return y;
}
//End Math operators


#endif	/* _BIGINT_H */

Any help is appreciated.