Complement a multiplication

C implementation booth algorithm, fixed-point decimal complement a multiplication

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define N 10
char strA[N] = {1};
char strpB[N] = {0};
char strnB[N] = {0};
char strC[N] = {0};

char negate(char s){
    if(s == '1')
        return '0';
    else if(s == '0')
        return '1';
    else
        return s;
}

void transformOne(char n[N]){
    size_t len = strlen(n);
    char m[N] = {0};
    
    for(int i=1;i<len-1;i++){
        strpB[i] = n[i];
        strnB[i] = negate(n[i]);
    }
    strpB[len-1] = strnB[len-1] = '1';    //As will last. 1 
    
    IF (n-[ 0 ] == ' + ' ) { 
        strpB [ 0 ] = ' 0 ' ; 
        strnB [ 0 ] = ' . 1 ' ; 
    } 
    the else { 
        strpB [ 0 ] = ' . 1 ' ; 
        strnB [ 0 ] = ' 0 ' ;
         for ( int I = . 1 ; I <len; I ++ ) { 
            m [I] =  strpB [I];
            strpB [I]= strnB[i];
            strnB[i] = m[i];
        }
    }
    strA[0] = strA[1] = '0';
    strA[2] = '.';
    for(int i=3;i<len;i++)
        strA[i] = '0';
}

void transformTwo(char n[N]){
    size_t len = strlen(n);
    if(n[0 ] == ' - ' ) { 
        Strc [ 0 ] = Strc [len 2 ] = ' . 1 ' ;     // last one is necessary. 1 
        for ( int I = . 1 ; I <len 2 ; I ++ ) { 
            Strc [I] = o negate (n-[I + . 1 ]); 
        } 
    } 
    the else    // number of n-complement code equals itself 
        for ( int I = 0 ; I <len . 1 ; I ++ ) 
            Strc [I] = n-[I + . 1 ]; 
} 

void oneComplementMultiplication(){
 
    char sign[2] = {0};
    char carry[2] = {'0','0'};
    
    size_t lenA = strlen(strA);
    size_t lenC = strlen(strC);
    
    size_t step = lenC-2;
    strC[lenC] = '0';
    
    //拿去字符'.'
    for(int i=2;i<N;i++)
        strA[i] = strA[i+1];
    for(int i=2;i<N;i++){
        strpB[i] = strpB[i+1];
        strnB[i] = strnB[i+1];
    }
    for(int i=1;i<N;i++)
        strC[i] = strC[i+1];
    lenA--;

    for(int i=0;i<step+1;i++){
        sign[0] = strC[lenC-1];
        sign[1] = Strc [lenC- 2 ]; 
        
        with Carry [ 0 ] = with Carry [ . 1 ] = ' 0 ' ;
         for ( Long I = lenA- . 1 ; I> = 0 ; i-- ) { 
            with Carry [ 0 ] = with Carry [ . 1 ];
             IF (Sign [ 0 ] <Sign [ . 1 ]) 
                strA [I] = ( char ) (strnB [I] strA + [I] - 48 );     // converting the digital sum - character result into 
            the else  IF (Sign [ 0 ]> Sign [ . 1])
                strA[i] = (char)(strpB[i]+strA[i]-48);
            else
                break;
            if((strA[i]+carry[0]-48) <= '1'){
                strA[i] = (char)(strA[i]+carry[0]-48);
                carry[1] = '0';
            }
            else if ((strA[i] == '1') && (carry[0] == '1')){
                strA[i] = '0';
                carry[1] = '1';
            }
            else if((strA[i] == '2') && (carry[0] == '0')){
                strA[i] = '0';
                carry[1] = '1';
            
                strA [i]{Else
            }= '1';
                carry[1] = '1';
            }
        }

        if(i < step){
            for(long i=lenC-1;i>0;i--)
                strC[i] = strC[i-1];
            strC[0] = strA[lenA-1];
            for(long i=lenA-1;i>0;i--)
                strA[i] = strA[i-1];
            strA[0] = strA[1];
        }
    }
    for(long i=lenA;i<lenA+step;i++)
        strA[i] = strC[i-lenA];
}

int judgment(char n[],long m){
    if((n[0]=='+') || (n[0]=='-'))
        if(n[1] == '0')
            if(n[2] == '.')
                if(n[m-1] == '1'){
                    for(int i=3;i<m-1;i++)
                        if((n[i] != '0') && (n[i] != '1'))
                            return 1;
                                return 0;}
                                    return 1;}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    //char X[N] = "-0.1101";
    //char Y[N] = "-0.1011";
    char X[N] = {0};
    char Y[N] = {0};
    size_t lenX = 0;
    size_t lenY = 0;
    int boolX = 0;
    int boolY = 0;
    
    do{
        printf("input:");
        scanf("%s%s",X,Y);
        
        lenX = strlen(X);
        lenY = strlen(Y);
        
        boolX = judgment(X,lenX);
        boolY = judgment(Y,lenY);
        
    }while(boolX | boolY);  //全为0退出循环
    
    transformOne(X);
    transformTwo(Y);
    oneComplementMultiplication();
    strA[1] = '.';
    
    for(int i=0;i<lenX+3;i++){
        printf("%c",strA[i]);
    }
    printf("\n");
       
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/bingw/p/11800475.html