Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const long LEN = 1000004;//troche wiecej, by dmuchac na zimno
- struct readResult{
- bool isABigger;
- bool isANegative;
- bool isBNegative;
- };
- void readN(char *n, string s){
- long ss = s.size();
- long comma = 0;
- for(long i=1; i<=ss; i++){
- if(s[ss-i] == ',')
- comma = i;
- }
- for(long i=1; i<=ss; i++){
- if(s[ss-i] == ',')
- continue;
- n[LEN-102+comma-(i>comma)-i] = s[ss-i] - '0';
- }
- }
- readResult read2Nums(char *a, char *b){
- string sa, sb;
- cin >> sa >> sb;
- bool isANegative = (sa[0] == '-');
- bool isBNegative = (sb[0] == '-');
- readN(a, sa.substr(isANegative || (sa[0] == '+'), string::npos));
- readN(b, sb.substr(isBNegative || (sb[0] == '+'), string::npos));
- bool isABigger = false;
- for(long i=0; i<LEN; i++){
- if(a[i] > b[i]){
- isABigger = true;
- break;
- }else if(b[i] > a[i]){
- break;
- }
- }
- return {
- isABigger,
- isANegative,
- isBNegative
- };
- }
- void add(char *a, char *b){
- char r = 0;
- for(long i=LEN-1; i>=0; i--){
- a[i] = r + a[i] + b[i];
- r = a[i] / 10;
- a[i]%= 10;
- }
- }
- void substract(char *a, char *b){
- long j;
- for(long i=LEN-1; i>=0; i--){
- if(a[i] < b[i]){
- a[i]+= 10;
- j = 1;
- while(true){
- if(a[i-j] == 0){
- a[i-j] = 9;
- }else{
- a[i-j]--;
- break;
- }
- j++;
- }
- }
- a[i]-= b[i];
- }
- }
- bool isZero(char *n){
- for(long i=0; i<LEN; i++){
- if(n[i] != 0) return false;
- }
- return true;
- }
- int main(){
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- char a[LEN] = {0};
- char b[LEN] = {0};
- readResult result = read2Nums(a, b);
- char *bigger = (result.isABigger) ? a : b;
- char *smaller = (result.isABigger) ? b : a;
- if(!result.isANegative && !result.isBNegative){
- add(bigger, smaller);
- }else if(result.isANegative && result.isBNegative){
- add(bigger, smaller);
- if(!isZero(bigger)) cout << '-';
- }else{
- substract(bigger, smaller);
- if(!isZero(bigger)){
- if(result.isABigger){
- if(result.isANegative) cout << '-';
- }else{
- if(result.isBNegative) cout << '-';
- }
- }
- }
- bool flag = false;
- for(long i=0; i<LEN; i++){
- if(!flag){
- if(bigger[i] != 0){
- flag = true;
- cout << int(bigger[i]);
- }
- } else{
- cout << int(bigger[i]);
- }
- }
- if(!flag) cout << 0;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement