-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigIntCpp.cpp
135 lines (112 loc) · 3.2 KB
/
BigIntCpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<bits/stdc++.h>
#include <string>
using namespace std;
void line();
void bigintadd();
string findSum(string , string );
int main()
{
int val=0;
char ch;
while(val!=1){
cout<<"BigInt Operation in C++"<<endl;
line();
cout<<"1. Addition" <<endl;
cout<<"2. Subtraction" <<endl;
cout<<"3. Multiplication" <<endl;
cout<<"4. Exponentiation " <<endl;
cout<<"5. GCD " <<endl;
cout<<"6. Factorial " <<endl;
cout<<"7. Testing: print 3000 char on screen " <<endl;
cout<<"0. EXIT " <<endl;
cout<<"Enter Choice: Choose one and press enter : ";
cin>>ch;
line();
switch (ch)
{
case '0':
cout<<" Exiting from program"<<endl;
val=1;
break;
case '1': cout<<"1. Addition" <<endl;
bigintadd();
cout<<" END of Addition" <<endl;
break;
case '2': cout <<" WARNING:- NOT IMPLEMENTED"<<endl; break;
case '3': cout <<" WARNING: NOT IMPLEMENTED"<<endl;break;
case '4': cout <<" WARNING: NOT IMPLEMENTED"<<endl;break;
case '5': cout <<" WARNING: NOT IMPLEMENTED"<<endl;break;
case '6': cout <<" WARNING: NOT IMPLEMENTED"<<endl;break;
case '7': for(int i=0; i<3000;i++){cout<<"X";}
default:
cout<<"ERROR: Choice doesn't match any case: Try Again:"<<endl;
line();
//cout<<"Try Again:"<<endl;
continue;
}
cout<<" Exiting from loop";
} //end while loop
return 0;
}
string* takeinput(){
//string str[2];
string* str = new string[2];
cout <<" Enter Operand 1 "<<endl; cin >> str[0];
cout <<" Enter Operand 2 "<<endl; cin >> str[1];
return str;
}
void bigintadd(){
//string str1 = "12";
//string str2 = "198111";
string *str2;
str2=takeinput();
//cout<<str2[0]<<endl;
//cout<<str2[1]<<endl;
cout << "RESULT=" <<findSum(str2[0], str2[1])<<endl;
}
string findSum(string str1, string str2)
{
// Before proceeding further, make sure length
// of str2 is larger.
if (str1.length() > str2.length())
swap(str1, str2);
// Take an empty string for storing result
string str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
// Reverse both of strings
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i=0; i<n1; i++)
{
// Do school mathematics, compute sum of
// current digits and carry
int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
// Calculate carry for next step
carry = sum/10;
}
// Add remaining digits of larger number
for (int i=n1; i<n2; i++)
{
int sum = ((str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
// Add remaining carry
if (carry)
str.push_back(carry+'0');
// reverse resultant string
reverse(str.begin(), str.end());
return str;
}
void line(){
cout<<"-------------------------------------------------"<<endl;
}
void reverse(string str)
{
for (int i=str.length()-1; i>=0; i--)
cout << str[i];
}