gd6179 2018-07-06 03:48 采纳率: 100%
浏览 1174
已采纳

c++针对二进制补码算术中整数的算术运算问题,请大神指点

c++ 二进制问题请教

以下程序针对二进制补码算术中整数的算术运算问题,要求先确定一个bit pattern size ,然后第一个函数要把输入的两个整数转化为二进制输出,第二个函数把两个二进制数相加输出,第三个函数再把结果转化回十进制。
只使用c++字符串数据类型,不使用任何数组变量。不改变主程序也不更改函数名,实现缺失函数,使程序运行正确。可编写额外的帮助函数,从函数中调用但不能对程序做修改。
需要使用c++字符串数据类型来表示这两个二进制补码的表示形式,不能使用任何数组变量,可使用字符串变量作为一个整体或使用索引访问字符串中变量中的字符。
程序示例运行如下
输入位模式大小的正整数:5
输入整数A:9
输入整数B:-14
十进制 9 + -14 = -5
二进制补码9 是 01001
二进制补码-14 是 10010
01001和10010的二进制和是11011。
在二进制补码计算中,9+ -14=-5。

#include iostream>
#include cmath>
#include string>

using namespace std;

int main()
{
//Read in the bit pattern size
int L;
do {
cout << "Enter positive integer for the bit pattern size ";
cin >> L;
}while (L <= 0);

    //Read in two integers a and b 
    int a, b; 
    cout << "Enter an integer a "; 
    cin >> a; 
    cout << "Enter an integer b "; 
    cin >> b; 

    //Calculate the decimal arithmetic sum of a and b and print the result int 
    c1 = a + b; 
    cout << "In decimal " << a << " + " << b << " is " << c1 << endl; 

    //Compute the two's complement representations of a and b 
    //Each integer must be represented in L-bits pattern 
    //Also these two's complement representations must be returned as string data types 
    string A = decimalToTwocomplementString(a, L); 
    string B = decimalToTwocomplementString(b, L); 

    //Print the two's complement representations of a and b 
    cout << "The two's complement of " << a << " is\t " << A << endl; 
    cout << "The two's complement of " << b << " is\t " << B << endl; 

    //Compute the binary sum of the two's complement representations of a and b 
    //The result must be returned as L-bit pattern string data type 
    string C = TwoComplementStringAddition(A, B);

    //Print the two's complement representation binary sum 
    cout << "The binary sum of " << A << " and " << B << " is " << C << endl; 

    //Convert the two's complement representation binary sum to decimal and print 
    int c2 = TwoComplementStringToDecimal(C); 
    cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl; 

    //Print some concluding results 
    if (c1 == c2) cout << c1 << " is equal to " << c2 << ". Good Job!" << endl; 
    else

{
cout << c1 << " is not equal to " << c2 << endl;
cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl;
}
system("Pause");
return 0;
}

原程序拷屏如下:
图片说明
图片说明

  • 写回答

6条回答

  • 繁宵微梦 2018-07-06 14:01
    关注
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    
    string decimalToTwocomplementString(int num, int length) {
        string binary;
        int positive_num = abs(num);
        while (positive_num != 0) {
            if (num >= 0){
                binary.insert(0,to_string(positive_num & 1));
                positive_num >>= 1;
            } else {
                binary.insert(0,to_string(!(positive_num & 1)));
                positive_num >>=1;
            }
        }
        if (num < 0) {
            for (reverse_iterator it = binary.rbegin(); it != binary.rend(); it++) {
                if (*it == '1') {
                    *it = '0';
                } else {
                    *it = '1';
                    break;
                }
            }
        }
        while (binary.length() < length) {
            if (num >= 0) {
                binary.insert(0, "0");
            } else {
                binary.insert(0, "1");
            }
        }
        return binary;
    }
    
    string TwoComplementStringAddition (const string& a, const string& b) {
        int carry = 0;
        string result;
        for (reverse_iterator ita = a.rbegin(), itb = b.rbegin(); ita != a.rend(); ita++, itb++) {
            int num_a = *ita - '0';
            int num_b = *itb - '0';
            result.insert(0,to_string(num_a ^ num_b ^ carry));
            if (num_a == 1 && num_b == 1) {
                carry = 1;
            } else if (num_a == 0 && num_b == 0) {
                carry = 0;
            }
        }
        return result;
    }
    
    int TwoComplementStringToDecimal(string binary) {
        int result = 0;
        string::iterator it = binary.begin();
        bool negative = *it == '1';
        if (negative) {
            for (; it != binary.end(); it++) {
                *it = *it == '1' ? '0' : '1';
            }
            for (reverse_iterator iterator = binary.rbegin(); iterator != binary.rend(); iterator++) {
                if (*iterator == '1') {
                    *iterator = '0';
                } else {
                    *iterator = '1';
                    break;
                }
            }
        }
        for (it = binary.begin(); it != binary.end(); it++) {
            result += (*it - '0') * pow(2,distance(it,binary.end()) - 1);
        }
        return negative ? -result : result;
    }
    
    int main()
    {
    //Read in the bit pattern size
        int L;
        do {
            cout << "Enter positive integer for the bit pattern size ";
            cin >> L;
        }while (L <= 0);
    
        //Read in two integers a and b
        int a, b;
        cout << "Enter an integer a ";
        cin >> a;
        cout << "Enter an integer b ";
        cin >> b;
    
        //Calculate the decimal arithmetic sum of a and b and print the result int
        int c1 = a + b;
        cout << "In decimal " << a << " + " << b << " is " << c1 << endl;
    
        //Compute the two's complement representations of a and b
        //Each integer must be represented in L-bits pattern
        //Also these two's complement representations must be returned as string data types
        string A = decimalToTwocomplementString(a, L);
        string B = decimalToTwocomplementString(b, L);
    
        //Print the two's complement representations of a and b
        cout << "The two's complement of " << a << " is\t " << A << endl;
        cout << "The two's complement of " << b << " is\t " << B << endl;
    
        //Compute the binary sum of the two's complement representations of a and b
        //The result must be returned as L-bit pattern string data type
        string C = TwoComplementStringAddition(A, B);
    
        //Print the two's complement representation binary sum
        cout << "The binary sum of " << A << " and " << B << " is " << C << endl;
    
        //Convert the two's complement representation binary sum to decimal and print
        int c2 = TwoComplementStringToDecimal(C);
        cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;
    
        //Print some concluding results
        if (c1 == c2) cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
        else
    
        {
            cout << c1 << " is not equal to " << c2 << endl;
            cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl;
        }
        return 0;
    }
    
    

    这个是整个程序的代码,实现了那三个函数,其他的都没有改动。
    虽然我实现的方法不算是很聪明,但是应该还是很好理解的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形