浮点数的生成过程(规格化和IEEE754标准)C语言实现

在上一篇文章的基础上增加了对浮点数的处理。如下图
在这里插入图片描述
本代码在code::blocks 17.12中运行正常

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 64
typedef struct rational_number{
    char charnum[MAXSIZE];//输入数用char表示
    long long integer;//输入数的整数部分
    double decimal;//输入数的小数部分(浮点数)
    double approximate;//输入的数的近似浮点数
    int floatlock;
}Rational;

typedef struct all_kinds_of_code{
    char* truecode;//原码
    char* reversecode;//反码
    char* modecode;//补码
    char* parallelcode;//移码
}Allcode;

typedef struct float_number{
    char* code;
    int bit;//总位数
    int j;//阶码位
}Floatn;



char* bin_plus_bin(char* A, char* B);
Allcode* bin_to_codes(int num,int bit);
Floatn* build_float_number(double s,int j,int bit,int jbit);

//有理数扫描
Rational* scan_rational(){
    printf("请输入有理数,若需要强制按浮点数形式处理,请在数字前加f:\n");
    Rational* num=(Rational*)malloc(sizeof(Rational));
    char c;
    int flag=0;//触发器,整数部分=0,小数部分=1
    int i=0;//全局char指针
    int weight=10;//表示小数部分的权重
    int negtive=1;//表示正负数
    num->floatlock=0;
    //初始化
    num->integer=0;
    num->decimal=0;
    num->approximate=0;

    while((c=getchar())!='\n'){
        if(c=='f'){
            num->floatlock=1;
            continue;
        }
        num->charnum[i++]=c;
        if(c=='-'){
            negtive=-1;
            continue;
        }
        if(c=='.'){
            flag=1;
            continue;
        }
        if(flag==0){
            num->integer=(num->integer)*10+c-'0';
            num->approximate=(num->approximate)*10+c-'0';
        }else{
            num->decimal+=(float)(c-'0')/weight;
            num->approximate+=(float)(c-'0')/weight;
            weight*=10;
        }
    }
    num->charnum[i]='\0';
    num->integer*=negtive;
    num->approximate*=negtive;
    num->decimal*=negtive;
    return num;
}

//给Allcode格式每个码统一输入某个值
void send_to_Allcode(char c,int digit, Allcode* codes){
    codes->truecode[digit]=c;
    codes->reversecode[digit]=c;
    codes->modecode[digit]=c;
    codes->parallelcode[digit]=c;
}

//对Allcode格式输出
void print_Allcode(Allcode* codes){
    printf("原码:%s  |",codes->truecode);
    printf("反码:%s  |",codes->reversecode);
    printf("补码:%s  |",codes->modecode);
    printf("移码:%s  \n",codes->parallelcode);
}

//对二进制字符串某位置反
void reverse_digit(char* code, int digit){
    if(code[digit]=='0'){
        code[digit]='1';
    }else if(code[digit]=='1'){
        code[digit]='0';
    }
}

//十进制 (真)小数分别输出其补码、反码 、移码
Allcode* dec_to_codes(double num,int bit){
    Allcode* codes=(Allcode*)malloc(sizeof(Allcode));
    codes->truecode=(char*)malloc((bit+1)*sizeof(char));
    codes->reversecode=(char*)malloc((bit+1)*sizeof(char));
    codes->modecode=(char*)malloc((bit+1)*sizeof(char));
    codes->parallelcode=(char*)malloc((bit+1)*sizeof(char));//最后一位:'\0'

    //最前面0 1 位及最后一位处理
    if(num>=1){
        printf("需要真小数\n");
        return -1;
    }else if(num<0){
        send_to_Allcode('1',0,codes);
        num*=-1;
    }else{
        send_to_Allcode('0',0,codes);
    }
    send_to_Allcode('\0',bit,codes);

    //生成原码(也是后面所有码的基础)
    int i=1;
    while(i<bit){
        num*=2;
        if(num>=1){
            send_to_Allcode('1',i++,codes);
            num-=1;
        }else{
            send_to_Allcode('0',i++,codes);
        }
    }

    //生成反码(也是补码和移码的基础)
    if(codes->truecode[0]=='1'){
        for(int i=1;i<bit;i++){
            reverse_digit(codes->reversecode,i);
            reverse_digit(codes->modecode,i);
            reverse_digit(codes->parallelcode,i);
        }
    }

    //生成补码(也是移码的基础)
    char* one="1";
    if(codes->truecode[0]=='1'){
        codes->modecode=bin_plus_bin(codes->modecode,one);
        codes->parallelcode=bin_plus_bin(codes->modecode,one);
    }

    //生成移码
    if(codes->truecode[0]=='0'){
        codes->parallelcode[0]='1';
    }else{
        codes->parallelcode[0]='0';
    }

    return codes;
}

//十进制 整数分别输出其补码、反码 、移码
Allcode* bin_to_codes(int num,int bit){
    Allcode* codes=(Allcode*)malloc(sizeof(Allcode));
    codes->truecode=(char*)malloc((bit+1)*sizeof(char));
    codes->reversecode=(char*)malloc((bit+1)*sizeof(char));
    codes->modecode=(char*)malloc((bit+1)*sizeof(char));
    codes->parallelcode=(char*)malloc((bit+1)*sizeof(char));//最后一位:'\0'

    //最前面0位及最后一位处理
    if(num>=0){
        send_to_Allcode('0',0,codes);
    }else{
        send_to_Allcode('1',0,codes);
        num*=-1;
    }
    send_to_Allcode('\0',bit,codes);


    //生成原码(也是后面所有码的基础)
    for(int i=bit-1;i>0;i--){
        send_to_Allcode(num%2+'0',i,codes);
        num/=2;
    }

    //生成反码(也是补码和移码的基础)
    if(codes->truecode[0]=='1'){
        for(int i=1;i<bit;i++){
            reverse_digit(codes->reversecode,i);
            reverse_digit(codes->modecode,i);
            reverse_digit(codes->parallelcode,i);
        }
    }

    //生成补码(也是移码的基础)
    char* one="1";
    if(codes->truecode[0]=='1'){
        codes->modecode=bin_plus_bin(codes->modecode,one);
        codes->parallelcode=bin_plus_bin(codes->parallelcode,one);
    }


    //生成移码
    if(codes->truecode[0]=='0'){
        codes->parallelcode[0]='1';
    }else{
        codes->parallelcode[0]='0';
    }

    return codes;
}

//二进制加法(不适用整数与小数混加)
char* bin_plus_bin(char* A, char* B){
    int len=strlen(A);
    if(len<strlen(B)){
        len=strlen(B);
    }
    char* res=malloc((len+1)*sizeof(char));
    res[len]='\0';
    int flag=0;
    int ri=len-1;
    int ai=strlen(A)-1;
    int bi=strlen(B)-1;
    do{
        if(A[ai]=='.'){
            res[ri--]='.';
            ai--;
            continue;
        }
        if(B[bi]=='.'){
            res[ri--]='.';
            bi--;
            continue;
        }
        if(ai==-1){
            if(flag==1){
                if(B[bi]=='1'){
                    res[ri--]='0';
                    bi--;
                }else{
                    res[ri--]='1';
                    bi--;
                    flag=0;
                }
            }else{
                res[ri--]=B[bi--];
            }
            continue;
        }
        if(bi==-1){
            if(flag==1){
                if(A[ai]=='1'){
                    res[ri--]='0';
                    ai--;
                }else{
                    res[ri--]='1';
                    ai--;
                    flag=0;
                }
            }else{
                res[ri--]=A[ai--];
            }
            continue;
        }
        if(A[ai]!=B[bi]){//一个是1一个是0
            if(flag==0){
                res[ri--]='1';
                ai--;
                bi--;
                continue;
            }else{
                res[ri--]='0';
                ai--;
                bi--;
                continue;
            }
        }
        if(A[ai]==B[bi]){
            if(A[ai]=='0'){
                if(flag==0){
                    res[ri--]='0';
                    ai--;
                    bi--;
                    continue;
                }else{
                    res[ri--]='1';
                    flag=0;
                    ai--;
                    bi--;
                    continue;
                }
            }else if(A[ai]=='1'){
                if(flag==0){
                    res[ri--]='0';
                    flag=1;
                    ai--;
                    bi--;
                    continue;
                }else{
                    res[ri--]='1';
                    ai--;
                    bi--;
                    continue;
                }
            }
        }
    }while(ri>-1);
    return res;
}

//对Floatn格式输出
void print_Floatnum(Floatn* flo){
    printf("\n%s\n",flo->code);
    printf("  ");
    for(int i=1;i<flo->j;i++){
        printf("^");
    }
    for(int i=1;i<(flo->bit)-(flo->j);i++){
        printf("*");
    }
    printf("\n");
}

//构造规格化阶码和尾数并构造浮点数
Floatn* construst_float_number(double num,int bit,int jbit){

    int j=0;
    int j_2=0;
    int weight=1;
    Floatn* floatcode;
    if(num<0){
        weight=-1;
        num*=-1;
    }

    double num_2=num;
    while(num>=1&&num!=0){
        num/=2;
        j++;
    }
    while(num<0.5&&num!=0){
        num*=2;
        j--;
    }
    printf("\n规格化表示:=%lf*2^%d\n",weight*num,j);
    floatcode=build_float_number(weight*num,j,bit,jbit);
    while(num_2>=2&&num_2!=0){
        num_2/=2;
        j_2++;
    }
    while(num_2<1&&num_2!=0){
        num_2*=2;
        j_2--;
    }
    printf("\nIEEE 754:=%lf*2^%d\n",weight*num_2,j_2);
    num_2-=1;
    floatcode=build_float_number(weight*num_2,j_2,bit,jbit);

    return floatcode;
}

//构造浮点数
Floatn* build_float_number(double s,int j,int bit,int jbit){
    //j:阶码 jbit:阶码位数 s:尾数 bit 总位数
    Floatn* floatcode=malloc(sizeof(Floatn));
    floatcode->code=malloc((bit+1)*sizeof(char));
    floatcode->j=jbit;
    floatcode->bit=bit;
    char* jchar=bin_to_codes(j,jbit)->parallelcode;//移码
    char* schar=dec_to_codes(s,bit-jbit)->modecode;//补码
    printf("尾码的补码:%s\n",schar);
    printf("阶码的移码:%s\n",jchar);
    int i=0;
    int si=0;
    int ji=0;
    floatcode->code[i++]=schar[si++];
    while(jchar[ji]!='\0'){
        floatcode->code[i++]=jchar[ji++];
    }
    while(schar[si]!='\0'){
        floatcode->code[i++]=schar[si++];
    }
    floatcode->code[bit]='\0';
    return floatcode;
}

int main()
{
    Rational* num;
    int bit=32;//总位数(浮点数和定点数)
    int jbit=8;//浮点数的阶码位数(包括符号位)
    while(1){
        num=scan_rational();
        if(num->floatlock==1){
            //按浮点数处理
            printf("> 输入浮点数:%s\n",num->charnum);
            print_Floatnum(construst_float_number(num->integer+num->decimal,bit,jbit));
            printf("-------\n");
        }else if(num->decimal==0){
            //按整形处理
            printf("> 输入定点整数:%d\n",num->integer);
            print_Allcode(bin_to_codes(num->integer,bit));
            printf("> 输入定点小数:%d\n",-1*num->integer);
            print_Allcode(bin_to_codes(-1*num->integer,bit));
            printf("-------\n");
        }else if(num->integer==0){
            //按小数型处理
            printf("> 输入定点小数:%f\n",num->decimal);
            print_Allcode(dec_to_codes(num->decimal,bit));
            printf("> 输入定点小数:%f\n",-1*num->decimal);
            print_Allcode(dec_to_codes(-1*num->decimal,bit));
            printf("-------\n");
        }else{
            //按浮点数处理
            printf("> 输入浮点数:%s\n",num->charnum);
            print_Floatnum(construst_float_number(num->integer+num->decimal,bit,jbit));
            printf("-------\n");
        }
    }

}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值