• 大数相乘-高精度乘法


    一、算法简要描述

        给定两个数,相乘如何得到高精度的结果,给定的两个数,不确定是不是浮点数,即可能一个数带多位小数,另一个带小数,或者两个数都带多位小数,或都不带小数,针对这些情况,程序应该都要考虑,所谓的高精度其实就是看两个数的小数位有多少,那么其结果的小数位数应该为两数小数位数之和。

    二、算法思路

       针对上述描述,其实大部分思路首先想到的应该是用字符串来表示这两个数,带小数点和不带小数点最终都可转换成类似于两个大整数相乘的情况,在最后考虑把小数点放到结果的合适位置即可

    三、算法代码

    /*
    two float multipy, design a high-precision algorithm
    */
    
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    /*
    * 函数:实现两数相乘,并在合适位置插入小数点,posDot为两数小数位数之和
    */
    void big_multi(const char *str1,const char *str2,int posDot)
    {
        int i,j;
        int len1=strlen(str1);
        int len2=strlen(str2);
        int rlen=len1+len2;
        int *pResult = new int[rlen];  //分配一个数组存储计算结果,大小为两数的位数之和
        memset(pResult,0,rlen*sizeof(int));
        //逐位相乘并放在相应位置,pResult[0]存放最终的进位,所以计算时是pResult[i+j+1]
        for(i=0;i<len1;++i)
        {
            for(j=0;j<len2;++j)
                pResult[i+j+1]+=(str1[i]-'0')*(str2[j]-'0');
        }
        //判断逐位相乘的结果是否大于10,如果大于10要从后向前进位
        for(i=rlen-1;i>0;--i)
        {
            int t=pResult[i];
            if(t>9)
            {
                pResult[i-1] += t/10;
                pResult[i]=t%10;
            }    
        }
        //判断结果开头是否是以0开头的,如果是要去掉
        i=0;
        while(pResult[i]==0)
            i++;
        //小数点位置应为从后往前posDot位
        j=i;
        while(j<rlen)
        {
            
            if(j==rlen-posDot)
                cout<<".";
            cout<<pResult[j];
            j++;
        }
        cout<<endl;
        
    }
    int main()
    {
    
        string s1,s2;
        int pos1,pos2,posDot;
        while(cin>>s1>>s2)
        {
            if(s1=="quit")
                break;
            
            pos1=s1.find(".");
            pos2=s2.find(".");
            //计算两数最终相乘结果的小数位数,有3种情况如下
            //两数都有小数点的情况
            if(pos1!=string::npos && pos2!=string::npos)
                posDot = s1.length()+s2.length()-pos1-pos2-2;
            //其中一个有小数点的情况
            else if (pos2!=string::npos)
                posDot = s2.length()-pos2-1;
            //其中一个有小数点的情况
            else if (pos1!=string::npos)
                posDot = s1.length()-pos1-1;
            //在相乘之前,先把两数中的小数点去掉,用stl的remove_if函数
            string::iterator newEnd1=remove_if(s1.begin(),s1.end(),bind2nd(equal_to<char>(),'.'));
            string::iterator newEnd2=remove_if(s2.begin(),s2.end(),bind2nd(equal_to<char>(),'.'));
            //去年小数点后,还要把新字符串结尾和原字符串结尾之间的多余字符删除
            s1.erase(newEnd1,s1.end());
            s2.erase(newEnd2,s2.end());
            const char *str1=s1.c_str();
            const char *str2=s2.c_str();
            //cout<<str1<<" "<<str2<<" "<<posDot<<endl;
            big_multi(str1,str2,posDot);
        }
        
        return 0;
    }
  • 相关阅读:
    动态规划算法介绍——概念、意义及应用、例题
    两个大数相减
    删除apache的签名的shell脚本
    C++中智能指针的设计和使用
    eclipse save action不起作用
    [leetcode]51. N-QueensN皇后
    [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值
    [leetcode]88. Merge Sorted Array归并有序数组
    [leetcode]636. Exclusive Time of Functions函数独占时间
    [leetcode]257. Binary Tree Paths二叉树路径
  • 原文地址:https://www.cnblogs.com/ballwql/p/4937642.html
Copyright © 2020-2023  润新知