Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<string>
- #include<string.h>
- using namespace std;
- #define maxi 200
- string a,b,n;
- char c[200],k[200],x[200];
- int j=0;
- void subtraction()
- {
- string x=string(k);
- while(x.size()<a.size())
- x='0'+x;
- int carry=0,j=0;
- if((a[a.size()-1]+b[b.size()-1])%2!=0)
- a[a.size()-1]=a[a.size()-1]-1;
- for(int i=x.size()-1;i>=0;i--)
- {
- if((a[i]-'0')-(x[i]-'0')+carry<0)
- {
- k[j++]=10+(a[i]-'0')-(x[i]-'0')+'0';
- a[i-1]=a[i-1]-1;
- }
- else
- {
- k[j++]=(a[i]-'0')-(x[i]-'0')+'0';
- }
- }
- int i=strlen(k)-1;
- while(k[i]=='0')
- i=i-1;
- if(i==-1)
- {
- cout<<0<<endl;
- return;
- }
- while(i>=0)
- {
- cout<<k[i];
- i=i-1;
- }
- cout<<endl;
- }
- void division()
- {
- j=0;
- int carry=0;
- for(int i=strlen(c)-1;i>=0;i--)
- {
- int z=c[i]+carry-'0';
- if(z==1)
- z=(z*10)+c[--i]-'0';
- k[j++]=z/2+'0';
- carry=(z%2)*10;
- }
- for(int i=0;i<j;i++)
- cout<<k[i];
- cout<<"\n";
- subtraction();
- }
- void addition()
- {
- j=0;
- while(b.size()<a.size())
- b='0'+b;
- int carry=0;
- for(int i=a.size()-1;i>=0;i--)
- {
- carry=a[i]+b[i]+carry-'0'-'0';
- c[j++]=carry%10+'0';
- carry=carry/10;
- }
- while(carry>0)
- {
- c[j++]=carry%10+'0';
- carry/=10;
- }
- //for(int i=j-1;i>=0;i--)
- //cout<<c[i];
- //cout<<endl;
- division();
- }
- int main()
- {
- int i=0;
- for(i=0;i<10;i++)
- {
- memset(c,0,200);
- memset(k,0,200);
- memset(x,0,200);
- cin>>a>>b;
- addition();
- }
- }
Add Comment
Please, Sign In to add comment