Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- int ai[10], bi[10];
- void binartoint_suma(char a[10], char b[10])
- {
- char ak[10];
- int t = 0;int j = 0;
- for(int i = strlen(a); i >=0 ; i--)
- {
- if(a[i] == '0' && b[i] == '0')
- {
- if(t == 1)
- {
- ak[j] = '1'; t = 0;
- }
- else
- {
- ak[j] = '0'; t = 0;
- }
- }
- if(a[i] == '0' && b[i] == '1')
- {
- if(t == 1)
- {
- ak[j] = '0'; t = 1;
- }
- else
- {
- ak[j] = '1'; t = 0;
- }
- }
- if(a[i] == '1' && b[i] == '0')
- {
- if(t == 1)
- {
- ak[j] = '0'; t = 1;
- }
- else
- {
- ak[j] = '1'; t = 0;
- }
- }
- if(a[i] == '1' && b[i] == '1')
- {
- if(t == 1)
- {
- ak[j] = '1'; t = 1;
- }
- else
- {
- ak[j] = '0'; t = 1;
- if(i == 0) ak[j+1] = '1';
- }
- }
- j++;
- }
- for(int i = strlen(ak)-1; i >= 0; i--)
- {
- cout<<ak[i]<<" ";
- }
- }
- bool imprumuta(char a[10], char b[10], int pozitie)
- {
- bool result = false;
- for(int i = pozitie+1; i <=strlen(a); i++)
- {
- if(a[i] == '1')
- {
- a[i] = '0';
- result = true;
- break;
- }
- }
- return result;
- }
- void binartoint_scadere(char a[10], char b[10])
- {
- int t = 0;
- cout<<"SCADERE\n\n";
- for(int i = 0; i <= 10; i++)
- {
- if(a[i] == '0' && b[i] == '0')
- {
- if(t == 1)
- {
- if(imprumuta(a, b, i) == true)
- {
- cout<<"1"; t = 0;
- }
- }
- else
- {
- cout<<"0"; t = 0;
- }
- }
- if(a[i] == '0' && b[i] == '1')
- {
- if(t == 1)
- {
- if(imprumuta(a, b, i) == true)
- {
- cout<<"0"; t = 0;
- }
- }
- else
- {
- cout<<"1"; t = 1;
- }
- }
- if(a[i] == '1' && b[i] == '0')
- {
- if(t == 1)
- {
- if(imprumuta(a, b, i) == true)
- {
- cout<<"0"; t = 0;
- }
- }
- else
- {
- cout<<"1"; t = 0;
- }
- }
- if(a[i] == '1' && b[i] == '1')
- {
- if(t == 1)
- {
- if(imprumuta(a, b, i) == true)
- {
- cout<<"1"; t = 1;
- }
- }
- else
- {
- cout<<"0"; t = 0;
- }
- }
- }
- }
- int main()
- {
- int op;
- char a[10],b[10];
- cout<<"Introduceti numerele: ";cin>>a>>b;
- cout<<"Alegeti operatia: \n1.Adunare\n2.Scadere\n\n";cin>>op;
- if(op == 1)
- binartoint_suma(a, b);
- else
- binartoint_scadere(a, b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement