小伙伴 ! 2023-10-29 18:18 采纳率: 100%
浏览 17
已结题

关于C语言的一个实验,有专家解答吗!

img


这是实验图片,难度太大了 在限求大老 鸡及叽及叽记鸡!!有偿有偿

  • 写回答

10条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-29 18:22
    关注
    #include "stdio.h"
    
    //定义二进制数的位数
    #define SIZE 5
    //定义二进制数中表示正负号的位置
    #define SIGN_POSITION 4
    //定义结果数字数组的长度
    #define NUM_LENGTH 9
    
    void inputBinary(int num[]) {
        int i = SIZE;
        char temp[SIZE] = {0};
        int j = 0;
        scanf("%s", temp);
        while (i) {
            if (temp[i - 1] == '+') {
                num[SIGN_POSITION] = 0;  // 正号
            }
            if (temp[i - 1] == '-') {
                num[SIGN_POSITION] = 1;  // 负号
            }
            if (temp[i - 1] == '1') {
                num[j++] = 1;  // 二进制数字1
            }
            if (temp[i - 1] == '0') {
                num[j++] = 0;  // 二进制数字0
            }
            i--;
        }
    }
    
    void outputBinary(int num[]) {
        int i = SIZE;
        while (i) {
            printf("%d", num[i - 1]);
            i--;
        }
    }
    
    void complement(int num[]) {
        int i;
        if (num[SIGN_POSITION] == 0) {
            return;  // 无需取反
        } else {
            i = SIGN_POSITION;
            while (i > 0) {
                if (num[i - 1] == 1) {
                    num[i - 1] = 0;
                } else {
                    num[i - 1] = 1;
                }
                i--;
            }
        }
    }
    
    void negate(int num[]) {
        if (num[SIGN_POSITION] == 0) {
            return;  // 正数不需要取相反数
        } else {
            int i = 0;
            while (num[i] && i < SIZE) {
                num[i++] = 0;
            }
            num[i] = 1;
        }
    }
    
    void multiplyBinary(int x[], int y[], int result[]) {
        int i, j, carry = 0;
        for (i = 0; i < SIGN_POSITION; i++) {
            result[i] = y[i];
        }
        for (i = 0; i < SIGN_POSITION; i++) {
            if (result[0] == 1) {
                for (j = 0; j < SIGN_POSITION; j++) {
                    carry = result[j + SIGN_POSITION] = x[j] + result[j + SIGN_POSITION];
                    if (carry == 2) {
                        result[j + SIGN_POSITION] = 0;
                        result[j + SIZE] = result[j + SIZE] + 1;
                    }
                    if (carry == 3) {
                        result[j + SIGN_POSITION] = 1;
                        result[j + SIZE] = result[j + SIZE] + 1;
                    }
                }
            }
            for (j = 0; j < 8; j++) {
                result[j] = result[j + 1];  // 右移
            }
            result[8] = 0;
        }
        if (x[SIGN_POSITION] != y[SIGN_POSITION]) {
            result[8] = 1;  // 结果为负数
        }
        i = NUM_LENGTH;
        while (i) {
            printf("%d", result[i - 1]);
            i--;
        }
    }
    
    void divideBinary(int x[], int y[]) {
        int i, j, carry, temp, flag = 0;
        int neg_y[SIZE + 1] = {0};
        int result[11] = {0};
        for (i = 0; i < SIZE; i++) {
            neg_y[i] = y[i];
        }
        complement(neg_y);
        negate(neg_y);
        neg_y[SIZE] = neg_y[SIGN_POSITION] = 1;
        for (i = 0; i < SIGN_POSITION; i++) {
            result[i + SIZE] = x[i];
        }
        for (i = 0; i < SIZE; i++) {
            for (j = 0; j < 6; j++) {
                carry = result[j + SIZE] = result[j + SIZE] + neg_y[j];
                if (carry == 2) {
                    result[j + SIZE] = 0;
                    result[j + SIZE + 1] = result[j + SIZE + 1] + 1;
                }
                if (carry == 3) {
                    result[j + SIZE] = 1;
                    result[j + SIZE + 1] = result[j + SIZE + 1] + 1;
                }
            }
            if (result[NUM_LENGTH] == 1) {
                result[0] = 0;
                flag++;
                for (j = 0; j < 4; j++) {
                    temp = result[j + SIZE] = result[j + SIZE] + y[j];
                    if (temp == 2) {
                        result[j + SIZE] = 0;
                        result[j + SIZE + 1] = result[j + SIZE + 1] + 1;
                    }
                    if (temp == 3) {
                        result[j + SIZE] = 1;
                        result[j + SIZE + 1] = result[j + SIZE + 1] + 1;
                    }
                }
            } else {
                result[0] = 1;
                flag++;
            }
            if (flag == SIZE) {
                break;
            }
            for (int p = 10; p > 0; p--) {
                result[p] = result[p - 1];
            }
            result[0] = 0;
        }
        if (x[SIGN_POSITION] != y[SIGN_POSITION]) {
            printf("Q=1.");
            for (i = 3; i >= 0; i--) {
                printf("%d", result[i]);
            }
            printf("    ");
        } else {
            printf("Q=0.");
            for (i = 3; i >= 0; i--) {
                printf("%d", result[i]);
            }
            printf("    ");
        }
        printf("R=%d.0000", x[SIGN_POSITION]);
        for (i = 3; i >= 0; i--) {
            printf("%d", result[i + SIZE]);
        }
        printf("\n");
    }
    
    int main() {
        int num_x[SIZE] = {0};
        int num_y[SIZE] = {0};
        int result[NUM_LENGTH] = {0};
        printf("请输入二进制定点整数x和y(以空格隔开,共%d位):", SIZE);
        inputBinary(num_x);
        inputBinary(num_y);
        printf("X*Y:");
        multiplyBinary(num_x, num_y, result);
        printf("\nX/Y:");
        divideBinary(num_x, num_y);
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(9条)

报告相同问题?

问题事件

  • 系统已结题 11月7日
  • 已采纳回答 10月30日
  • 赞助了问题酬金15元 10月29日
  • 创建了问题 10月29日

悬赏问题

  • ¥15 求解决为什么json数据完整,却解析失败
  • ¥15 打开Smart schedule运行examples/transformer-xl/scripts/run_enwik8_base_moe.sh 报错
  • ¥15 求一个C# sm4 加密解密的代码
  • ¥15 想做个网络加速器,怎么实现
  • ¥20 centos7下载mysql提示DNS解析异常
  • ¥20 vx转账功能对方不能领取
  • ¥15 vue2+codemirror 运行后页面光标太大了
  • ¥15 pdfjs库如何在前端实现打印、旋转等功能
  • ¥15 combobox数据绑定问题
  • ¥15 maven打包时,为什么有的依赖打不进去包