Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/pierre/calcu_binaire/addition.c

(Deskargatu)
#include <stdio.h>  
#include <string.h>  
    
   
    
    
   int main ()  
    
    {  
        size_t length;  
        char tab1[80],tab2[80];   
        int d,e,tab3[80],tab4[80];  
        int i,a=0,b=0;  
      
          
        printf("\nPremier nombre binaire\n");  
        gets(tab1);  
        length = strlen(tab1);  
        d=length;  
      
        printf("\nSecond nombre binaire\n");  
        gets(tab2);  
        length = strlen(tab2);  
        e=length;  
      
        if (d>=e) {  
            for (i=d-1; i>=d-e; i--) {tab4[i]=tab2[i-(d-e)]-48;};  
            for (i=d-e-1; i>=0; i--) {tab4[i]=0;};  
            for (i=d-1; i>=0; i--) {tab3[i]=tab4[i]+tab1[i]-48+a;  
                if (tab3[i]==0) {tab3[i]=0;a=0;};                           
                if (tab3[i]==1) {tab3[i]=1;a=0;};  
                if (tab3[i]==2) {tab3[i]=0;a=1;};  
                 if (tab3[i]==3) {tab3[i]=1;a=1;};  
           };  
            printf("\nLa somme des 2 nombres est :\n");  
           if (a==1) {printf("1");};                           
            for(i=0; i<=d-1; i++) printf("%d", tab3[i]);  
        };   
        
        if (d<e) {  
            for (i=e-1; i>=e-d; i--) {tab4[i]=tab1[i-(e-d)]-48;};  
            for (i=e-d-1; i>=0; i--) {tab4[i]=0;};  
            for (i=e-1; i>=0; i--) {tab3[i]=tab4[i]+tab2[i]-48+b;  
                if (tab3[i]==0) {tab3[i]=0;b=0;};                           
                if (tab3[i]==1) {tab3[i]=1;b=0;};  
                if (tab3[i]==2) {tab3[i]=0;b=1;};  
                if (tab3[i]==3) {tab3[i]=1;b=1;};  
            };  
            printf("\nLa somme des 2 nombres est :\n");  
            if (b==1) {printf("1");};                           
            for(i=0; i<=e-1; i++) printf("%d", tab3[i]);             
        };   
      
      
   }