Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define boost ios_base::sync_with_stdio(0)
- #define boost1 cin.tie(0)
- #define boost2 cout.tie(0)
- #define ll long long int
- #define ull unsigned long long int
- bool isSmaller(string str1, string str2)
- {
- int n1 = str1.length(), n2 = str2.length();
- if (n1 < n2)
- return true;
- if (n2 < n1)
- return false;
- for (int i=0; i<n1; i++)
- if (str1[i] < str2[i])
- return true;
- else if (str1[i] > str2[i])
- return false;
- return false;
- }
- string findDiff(string str1, string str2)
- {
- if (isSmaller(str1, str2))
- swap(str1, str2);
- string str = "";
- int n1 = str1.length(), n2 = str2.length();
- reverse(str1.begin(), str1.end());
- reverse(str2.begin(), str2.end());
- int carry = 0;
- for (int i=0; i<n2; i++)
- {
- int sub = ((str1[i]-'0')-(str2[i]-'0')-carry);
- if (sub < 0)
- {
- sub = sub + 10;
- carry = 1;
- }
- else
- carry = 0;
- str.push_back(sub + '0');
- }
- for (int i=n2; i<n1; i++)
- {
- int sub = ((str1[i]-'0') - carry);
- if (sub < 0)
- {
- sub = sub + 10;
- carry = 1;
- }
- else
- carry = 0;
- str.push_back(sub + '0');
- }
- reverse(str.begin(), str.end());
- return str;
- }
- string doSum(string a, string b)
- {
- if(a.size() < b.size())
- swap(a, b);
- int j = a.size()-1;
- for(int i=b.size()-1; i>=0; i--, j--)
- a[j]+=(b[i]-'0');
- for(int i=a.size()-1; i>0; i--)
- {
- if(a[i] > '9')
- {
- int d = a[i]-'0';
- a[i-1] = ((a[i-1]-'0') + d/10) + '0';
- a[i] = (d%10)+'0';
- }
- }
- if(a[0] > '9')
- {
- string k;
- k+=a[0];
- a[0] = ((a[0]-'0')%10)+'0';
- k[0] = ((k[0]-'0')/10)+'0';
- a = k+a;
- }
- return a;
- }
- int main()
- {
- boost;boost1;boost2;
- string s1,s2,s3,s4,st1,st2;
- cin>>s3;
- cin>>s4;
- st1=s3;
- st2=s4;
- if(s3[0]=='+' || s3[0]=='-')
- {
- s3=s3.erase(0,1);
- }
- else
- {
- s3=s3;
- }
- if(s4[0]=='+' || s4[0]=='-')
- {
- s4=s4.erase(0,1);
- }
- else
- {
- s4=s4;
- }
- /// cout<<s3<<" "<<s4<<endl;
- if(isSmaller(s3,s4)==true)
- {
- s1=s4;
- s2=s3;
- }
- else
- {
- s1=s3;
- s2=s4;
- }
- /// cout<<s1<<" "<<s2<<endl;
- ///cout<<st1<<" "<<st2<<endl;
- if(st1[0]=='-'&& st2[0]!='-')
- {
- if(isSmaller(s4,s3)==true)
- {
- cout<<"-"<<findDiff(s1,s2)<<endl;
- }
- else
- {
- cout<<findDiff(s1,s2)<<endl;
- }
- }
- else if(st1[0]!='-'&& st2[0]=='-')
- {
- if(isSmaller(s3,s4)==true)
- {
- cout<<"-"<<findDiff(s1,s2)<<endl;
- }
- else
- {
- cout<<findDiff(s1,s2)<<endl;
- }
- }
- else
- {
- if(st1[0]=='-' && st2[0]=='-')
- {
- cout<<"-"<<doSum(s1,s2)<<endl;
- }
- else
- {
- cout<<doSum(s1,s2)<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement