Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Nicholas Jacks
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- class bigInt{
- private:
- vector<int> digits;
- public:
- bigInt(){ //default constructor vector set to a size of 1; digits = {0};
- digits.push_back(0);
- }
- explicit bigInt(string s){ //converts string type numbers to int type numbers and places them digits 0-9 in their own position within a vector of digits; ex: "123" = {1, 2, 3}
- for(int i=0; i<=s.length()-1; i++){
- digits.push_back(int(s[i]-'0'));
- }
- }
- explicit bigInt(int n) { //constructor for int to bigInt;
- int count=0;
- while (n != 0) {
- count++;
- digits.push_back(n % 10);
- n /= 10;
- }
- int i = 0; //reflector 005 --> 500;
- int j = count - 1;
- while (j >= count/2){
- int temp = digits[j];
- digits[j] = digits[i];
- digits[i] = temp;
- j--;
- i++;
- }
- }
- static void swap(bigInt & a, bigInt & b){ //swaps bigInt types;
- bigInt temp = a;
- a = b;
- b = temp;
- }
- static bigInt subtraction(bigInt & op1, bigInt & op2){
- bigInt C;
- int i = 1;
- bool negative = false;
- if(op1.digits.size()<op2.digits.size()){
- swap(op1,op2);
- negative = true;
- }
- for(int y = i; y<op1.digits.size(); y++){
- C.digits.push_back(0);
- }
- int j = op2.digits.size()-1;
- int pos = C.digits.size()-1;
- for(int k = op1.digits.size()-1; k>=0; k--){
- if(op1.digits[k]>=op2.digits[j] && j>=0){
- C.digits[pos] = op1.digits[k]-op2.digits[j];
- }
- else if(op1.digits[k]<op2.digits[j] && j>=0){
- int temp = k-1;
- bool found = false;
- while(!found){
- if(op1.digits[temp]>0){
- op1.digits[temp]-=1;
- op1.digits[k]+=10;
- C.digits[pos] = op1.digits[k]-op2.digits[j];
- found = true;
- }
- else{
- op1.digits[temp]+=9;
- found = false;
- temp--;
- }
- }
- }
- else{
- C.digits[pos] = op1.digits[k];
- }
- j--;
- pos--;
- }
- while(C.digits[0]==0){ //avoids 0 in first place; ex: {1, 2}*{1, 2}={0, 1, 4, 4} ---> {1, 4, 4}
- bigInt fix;
- fix.digits[0] = C.digits[1];
- for (int f = 2; f <= C.digits.size() - 1; f++) {
- fix.digits.push_back(C.digits[f]);
- }
- C = fix;
- }
- return(C);
- }
- static bigInt addition(bigInt & op1, bigInt & op2){
- bigInt C;
- if(op2.digits.size()>op1.digits.size()){ //op1 size must be >= than op1 size to properly work; op1*op2 <--- good op2*op1 <--- fail
- swap(op1,op2);
- }
- for(int i = 1; i<op1.digits.size()+op2.digits.size()-1; i++){
- C.digits.push_back(0);
- }
- int pos = C.digits.size()-1;
- int carry = 0;
- int j = op2.digits.size()-1;
- int res = 0;
- for(int i=op1.digits.size()-1; i>=0; i--){
- if(j<0){
- res = carry+op1.digits[i];
- }
- else{
- res = carry+op1.digits[i]+op2.digits[j];
- }
- C.digits[pos] = res%10;
- carry = res/10;
- j--;
- pos--;
- if(i==0&&carry>0){
- C.digits[pos] = carry;
- }
- }
- while(C.digits[0]==0){ //avoids 0 in first place; ex: {1, 2}*{1, 2}={0, 1, 4, 4} ---> {1, 4, 4}
- bigInt fix;
- fix.digits[0] = C.digits[1];
- for (int i = 2; i <= C.digits.size() - 1; i++) {
- fix.digits.push_back(C.digits[i]);
- }
- C = fix;
- }
- return(C);
- }
- static bigInt multiply(bigInt & op1, bigInt & op2){ //multiply's two bigInts;
- bigInt C;
- if(op2.digits.size()>op1.digits.size()){ //op1 size must be >= than op1 size to properly work; op1*op2 <--- good op2*op1 <--- fail
- swap(op1,op2);
- }
- for(int k=1; k<op1.digits.size()+op2.digits.size(); k++){ //sets the result vector to max amount of possible digits due to multiplication with 0 in each place; op1
- C.digits.push_back(0); //{1 , 2, 3}*{3, 2, 1} = {0, 0, 0, 0, 0, 0}
- }
- for(int i=op2.digits.size()-1; i>=0; i--){ //main for() loop running the iteration of multiplication for op2;
- int carry = 0;
- int carry2 = 0;
- for(int j=op1.digits.size()-1;j>=0; j--){ //nested for() loop running the iteration of multiplication for op1;
- int pos = i+j+1;
- int res = op1.digits[j]*op2.digits[i]+carry;
- int res2 = C.digits[pos] + res%10 + carry2;
- carry=res/10;
- carry2=res2/10;
- C.digits[pos] = res2%10;
- if(j==0&&(res>=10||res2>=10)){
- C.digits[pos-1] = res/10+carry2;
- }
- }
- }
- if(C.digits[0] == 0){ //avoids 0 in first place; ex: {1, 2}*{1, 2}={0, 1, 4, 4} ---> {1, 4, 4}
- bigInt fix;
- fix.digits[0] = C.digits[1];
- for(int i = 2; i<=C.digits.size()-1; i++){
- fix.digits.push_back(C.digits[i]);
- }
- C = fix;
- }
- return(C); //return bigInt product;
- }
- void print(){ //prints a bigInt number;
- int x = digits.size()-1;
- for(int i = 0; i<=x; i++){
- cout << digits[i];
- }
- }
- static bigInt factorial(int n){
- bigInt result(1);
- for(int i=2; i<=n; i++){
- bigInt num2(i);
- result = bigInt::multiply(result,num2);
- }
- return(result);
- }
- };
- int main(){
- string s1;
- string s2;
- bigInt op1;
- bigInt op2;
- int n=1;
- char operand;
- cout << "Welcome to the Big Int calculator!\n" << endl;
- cout << "Inset an operand. You may pick (+ ! * -)\n";
- cin >> operand;
- if(operand=='!'){
- cout << "Insert a reasonable sized integer" << endl;
- cin >> n;
- }
- else {
- cout << "Insert op1\n";
- cin >> s1;
- cout << "Inset op2\n";
- cin >> s2;
- bigInt temp(s1);
- bigInt temp2(s2);
- op1 = temp;
- op2 = temp2;
- }
- bigInt result;
- if(operand == '*'){
- result = bigInt::multiply(op1,op2);
- cout << "Answer = ";
- result.print();
- }
- else if(operand == '!'){
- cout << "Answer = ";
- result = bigInt::factorial(n);
- result.print();
- }
- else if(operand == '+'){
- cout << "Answer = ";
- result = bigInt::addition(op1,op2);
- result.print();
- }
- else if(operand == '-'){
- cout << "Answer = ";
- result = bigInt::subtraction(op1,op2);
- result.print();
- }
- else{
- cout << "error with selection";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement