如何把2个16位二进制数进行加减乘除

chimenghuanzhe 2016-08-18 08:11:46
两个很大很大的整数(有几百位),我要把它转化成二进制数,然后每八位为一组,进行加减乘除运算。用C语言也行,用汇编也行。或者C语言里面嵌着汇编也可以。

一开始我用一个char型的数组, char a[500],来存储,每个元素a[i]存储0——9的数字,可是这样一来一个char型,占一个字节,才仅仅表示十个数,太浪费空间了。所以,导师命令,要用二进制表示,每八位一组,进行加减乘除,八位和八位之间考虑进位。


如果有哪位大神能直接解决这个问题更好了。如果无法,解决两个16位的二进制数(即两个字节)的四则运算也可以。让我好有个参考。
...全文
686 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-08-19
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
#include <string>
using namespace std;
inline int compare(string str1,string str2) {//相等返回0,大于返回1,小于返回-1
         if (str1.size()>str2.size()) return 1; //长度长的整数大于长度小的整数
    else if (str1.size()<str2.size()) return -1;
    else                              return str1.compare(str2); //若长度相等,则头到尾按位比较
}
string SUB_INT(string str1,string str2);
string ADD_INT(string str1,string str2) {//高精度加法
    int sign=1; //sign 为符号位
    string str;
    if (str1[0]=='-') {
        if (str2[0]=='-') {
            sign=-1;
            str=ADD_INT(str1.erase(0,1),str2.erase(0,1));
        } else {
            str=SUB_INT(str2,str1.erase(0,1));
        }
    } else {
        if (str2[0]=='-') {
            str=SUB_INT(str1,str2.erase(0,1));
        } else { //把两个整数对齐,短整数前面加0补齐
            string::size_type L1,L2;
            int i;
            L1=str1.size();
            L2=str2.size();
            if (L1<L2) {
                for (i=1;i<=L2-L1;i++) str1="0"+str1;
            } else {
                for (i=1;i<=L1-L2;i++) str2="0"+str2;
            }
            int int1=0,int2=0; //int2 记录进位
            for (i=str1.size()-1;i>=0;i--) {
                int1=(int(str1[i])-'0'+int(str2[i])-'0'+int2)%10;
                int2=(int(str1[i])-'0'+int(str2[i])-'0'+int2)/10;
                str=char(int1+'0')+str;
            }
            if (int2!=0) str=char(int2+'0')+str;
        }
    }
    //运算后处理符号位
    if ((sign==-1)&&(str[0]!='0')) str="-"+str;
    return str;
}
string SUB_INT(string str1,string str2) {//高精度减法
    int sign=1; //sign 为符号位
    string str;
    int i,j;
    if (str2[0]=='-') {
        str=ADD_INT(str1,str2.erase(0,1));
    } else {
        int res=compare(str1,str2);
        if (res==0) return "0";
        if (res<0) {
            sign=-1;
            string temp =str1;
            str1=str2;
            str2=temp;
        }
        string::size_type tempint;
        tempint=str1.size()-str2.size();
        for (i=str2.size()-1;i>=0;i--) {
            if (str1[i+tempint]<str2[i]) {
                j=1;
                while (1) {//zhao4zhong1添加
                    if (str1[i+tempint-j]=='0') {
                        str1[i+tempint-j]='9';
                        j++;
                    } else {
                        str1[i+tempint-j]=char(int(str1[i+tempint-j])-1);
                        break;
                    }
                }
                str=char(str1[i+tempint]-str2[i]+':')+str;
            } else {
                str=char(str1[i+tempint]-str2[i]+'0')+str;
            }
        }
        for (i=tempint-1;i>=0;i--) str=str1[i]+str;
    }
    //去除结果中多余的前导0
    str.erase(0,str.find_first_not_of('0'));
    if (str.empty()) str="0";
    if ((sign==-1) && (str[0]!='0')) str ="-"+str;
    return str;
}
string MUL_INT(string str1,string str2) {//高精度乘法
    int sign=1; //sign 为符号位
    string str;
    if (str1[0]=='-') {
        sign*=-1;
        str1 =str1.erase(0,1);
    }
    if (str2[0]=='-') {
        sign*=-1;
        str2 =str2.erase(0,1);
    }
    int i,j;
    string::size_type L1,L2;
    L1=str1.size();
    L2=str2.size();
    for (i=L2-1;i>=0;i--) { //模拟手工乘法竖式
        string tempstr;
        int int1=0,int2=0,int3=int(str2[i])-'0';
        if (int3!=0) {
            for (j=1;j<=(int)(L2-1-i);j++) tempstr="0"+tempstr;
            for (j=L1-1;j>=0;j--) {
                int1=(int3*(int(str1[j])-'0')+int2)%10;
                int2=(int3*(int(str1[j])-'0')+int2)/10;
                tempstr=char(int1+'0')+tempstr;
            }
            if (int2!=0) tempstr=char(int2+'0')+tempstr;
        }
        str=ADD_INT(str,tempstr);
    }
    //去除结果中的前导0
    str.erase(0,str.find_first_not_of('0'));
    if (str.empty()) str="0";
    if ((sign==-1) && (str[0]!='0')) str="-"+str;
    return str;
}
string DIVIDE_INT(string str1,string str2,int flag) {//高精度除法。flag==1时,返回商; flag==0时,返回余数
    string quotient,residue; //定义商和余数
    int sign1=1,sign2=1;
    if (str2 == "0") {  //判断除数是否为0
        quotient= "ERROR!";
        residue = "ERROR!";
        if (flag==1) return quotient;
        else         return residue ;
    }
    if (str1=="0") { //判断被除数是否为0
        quotient="0";
        residue ="0";
    }
    if (str1[0]=='-') {
        str1   = str1.erase(0,1);
        sign1 *= -1;
        sign2  = -1;
    }
    if (str2[0]=='-') {
        str2   = str2.erase(0,1);
        sign1 *= -1;
    }
    int res=compare(str1,str2);
    if (res<0) {
        quotient="0";
        residue =str1;
    } else if (res == 0) {
        quotient="1";
        residue ="0";
    } else {
        string::size_type L1,L2;
        L1=str1.size();
        L2=str2.size();
        string tempstr;
        tempstr.append(str1,0,L2-1);
        for (int i=L2-1;i<L1;i++) { //模拟手工除法竖式
            tempstr=tempstr+str1[i];
            tempstr.erase(0,tempstr.find_first_not_of('0'));//zhao4zhong1添加
            if (tempstr.empty()) tempstr="0";//zhao4zhong1添加
            for (char ch='9';ch>='0';ch--) { //试商
                string str;
                str=str+ch;
                if (compare(MUL_INT(str2,str),tempstr)<=0) {
                    quotient=quotient+ch;
                    tempstr =SUB_INT(tempstr,MUL_INT(str2,str));
                    break;
                }
            }
        }
        residue=tempstr;
    }
    //去除结果中的前导0
    quotient.erase(0,quotient.find_first_not_of('0'));
    if (quotient.empty()) quotient="0";
    if ((sign1==-1)&&(quotient[0]!='0')) quotient="-"+quotient;
    if ((sign2==-1)&&(residue [0]!='0')) residue ="-"+residue ;
    if (flag==1) return quotient;
    else         return residue ;
}
string DIV_INT(string str1,string str2) {//高精度除法,返回商
    return DIVIDE_INT(str1,str2,1);
}
string MOD_INT(string str1,string str2) {//高精度除法,返回余数
    return DIVIDE_INT(str1,str2,0);
}
int main() {
    char ch;
    string s1,s2,res;

    while (cin>>s1>>ch>>s2) {
        switch (ch) {
            case '+':res=ADD_INT(s1,s2);break;
            case '-':res=SUB_INT(s1,s2);break;
            case '*':res=MUL_INT(s1,s2);break;
            case '/':res=DIV_INT(s1,s2);break;
            case '%':res=MOD_INT(s1,s2);break;
            default :                   break;
        }
        cout<<res<<endl;
    }
    return(0);
}
paschen 2016-08-19
  • 打赏
  • 举报
回复
有很多大数运算现有的库,楼主可以去搜索参考
老王爱上猫 2016-08-19
  • 打赏
  • 举报
回复
这个你先手动算下,然后再转换为代码呗,应该不难吧
kongl123 2016-08-19
  • 打赏
  • 举报
回复
google 大数运算
lm_whales 2016-08-19
  • 打赏
  • 举报
回复
引用 12 楼 zhao4zhong1 的回复:
[quote=引用 11 楼 lm_whales 的回复:] [quote=引用 10 楼 zhao4zhong1 的回复:] [quote=引用 8 楼 lm_whales 的回复:] [quote=引用 5 楼 chimenghuanzhe 的回复:] @zhao4zhong1 谢谢你的解答。但是,导师说必须用汇编语言解决。至少中间的加减乘除计算部分要用汇编来完成。所以我在发愁呢。尤其是如何把汇编语言的代码嵌入到C语言的程序中。
把它的程序,编译成 asm 文件就可以了[/quote] 这个办法比较暗黑。[/quote] ^_^ 他 写错字了,抱歉[/quote] 是和手滑点错鼠标、忘关摄像头、……、一个节奏吗?[/quote]嗯
赵4老师 2016-08-19
  • 打赏
  • 举报
回复
引用 11 楼 lm_whales 的回复:
[quote=引用 10 楼 zhao4zhong1 的回复:] [quote=引用 8 楼 lm_whales 的回复:] [quote=引用 5 楼 chimenghuanzhe 的回复:] @zhao4zhong1 谢谢你的解答。但是,导师说必须用汇编语言解决。至少中间的加减乘除计算部分要用汇编来完成。所以我在发愁呢。尤其是如何把汇编语言的代码嵌入到C语言的程序中。
把它的程序,编译成 asm 文件就可以了[/quote] 这个办法比较暗黑。[/quote] ^_^ 他 写错字了,抱歉[/quote] 是和手滑点错鼠标、忘关摄像头、……、一个节奏吗?
lm_whales 2016-08-19
  • 打赏
  • 举报
回复
引用 10 楼 zhao4zhong1 的回复:
[quote=引用 8 楼 lm_whales 的回复:] [quote=引用 5 楼 chimenghuanzhe 的回复:] @zhao4zhong1 谢谢你的解答。但是,导师说必须用汇编语言解决。至少中间的加减乘除计算部分要用汇编来完成。所以我在发愁呢。尤其是如何把汇编语言的代码嵌入到C语言的程序中。
把它的程序,编译成 asm 文件就可以了[/quote] 这个办法比较暗黑。[/quote] ^_^ 他 写错字了,抱歉
赵4老师 2016-08-19
  • 打赏
  • 举报
回复
引用 8 楼 lm_whales 的回复:
[quote=引用 5 楼 chimenghuanzhe 的回复:] @zhao4zhong1 谢谢你的解答。但是,导师说必须用汇编语言解决。至少中间的加减乘除计算部分要用汇编来完成。所以我在发愁呢。尤其是如何把汇编语言的代码嵌入到C语言的程序中。
把它的程序,编译成 asm 文件就可以了[/quote] 这个办法比较暗黑。
赵4老师 2016-08-19
  • 打赏
  • 举报
回复
你直接写C语言,然后在调试时按Alt+8打开汇编窗口,参考每句C对应的32位汇编将对应C语句翻译为嵌入汇编__asm指令?
lm_whales 2016-08-19
  • 打赏
  • 举报
回复
引用 5 楼 chimenghuanzhe 的回复:
@zhao4zhong1 谢谢你的解答。但是,导师说必须用汇编语言解决。至少中间的加减乘除计算部分要用汇编来完成。所以我在发愁呢。尤其是如何把汇编语言的代码嵌入到C语言的程序中。
把它的程序,编译成 asm 文件就可以了
chimenghuanzhe 2016-08-19
  • 打赏
  • 举报
回复
@zhao4zhong1 请问您能不能帮我看一下这个帖子。http://bbs.csdn.net/topics/392006453 谢谢谢谢!
赵4老师 2016-08-19
  • 打赏
  • 举报
回复
__asm Microsoft Specific __asm assembly-language-instruction __asm { assembly-language-instructions } If used without braces, the __asm keyword means that the rest of the line is an assembly-language statement. If used with braces, it means that each line between the braces is an assembly-language statement. For compatibility with previous versions, _asm is a synonym for __asm. Since the __asm keyword is a statement separator, you can put assembly instructions on the same line. For related information, see Assembler (Inline) Topics. Note Microsoft C++ does not support the AT&T C++ asm keyword. END Microsoft Specific Example // Example of the __asm keyword __asm // __asm block { mov eax, 01h int 10h } __asm mov eax, 01h // Separate __asm lines __asm int 10h // Multiple __asm statements on a line __asm mov eax, 01h __asm int 10h
chimenghuanzhe 2016-08-19
  • 打赏
  • 举报
回复
@zhao4zhong1 谢谢你的解答。但是,导师说必须用汇编语言解决。至少中间的加减乘除计算部分要用汇编来完成。所以我在发愁呢。尤其是如何把汇编语言的代码嵌入到C语言的程序中。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧