• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            /*
              Name: 高精度運算
              Copyright:始發(fā)于goal00001111的專欄;允許自由轉(zhuǎn)載,但必須注明作者和出處
              Author: goal00001111
              Date: 01-12-08 15:04
              Description:
            高精度運算:加減乘除,乘方,階乘
            */

            #include<iostream>
            #include<string>

            using namespace std;

            void Reverse(string & str);
            void AddInt(string & c, string a, string b);
            void SubInt(string & c, string a, string b);
            void MulInt(string & c, string a, string b);
            void JieCHInt(string & c, string b);
            void DivInt(string & c, string a, string b);
            void PowInt(string & c, string a, string b);

            int main()
            {
                string a, b, c;
                cin >> a >> b;
               
                AddInt(c, a, b);
                cout << a << " + ";
                cout << b << " = " << endl;
                cout << c << endl;
               
                SubInt(c, a, b);
                cout << a << " - ";
                cout << b << " = " << endl;
                cout << c << endl;

                MulInt(c, a, b);
                cout << a << " * ";
                cout << b << " = " << endl;
                cout << c << endl;

            //   
            //    cin >> b;
            //    JieCHInt(c, b);
            //    cout << b << " ! = " << endl;
            //    cout << c << endl;
            //   
                DivInt(c, a, b);
                cout << a << " / ";
                cout << b << " = " << endl;
                cout << c << endl;
               
            //    PowInt(c, a, b);
            //    cout << a << " ^ ";
            //    cout << b << " = " << endl;
            //    cout << c << endl;
               
                system("pause");
                return 0;
            }

            void Reverse(string & str)
            {
                int left, right;
                left = 0; right = str.size()-1;
                while (left < right)
                {
                    char ch = str[left];
                    str[left] = str[right];
                    str[right] = ch;
                    left++; right--;
                }
            }

            void AddInt(string & c, string a, string b)//模仿遞增向量的合并方法
            {
                c.resize(0);
                Reverse(a);
                Reverse(b);
               
                //逆序計算a+b,則從低位開始計算
                int i, carry;
                i = carry = 0;
                while (i < a.size() && i < b.size())
                {
                    c    += (a[i]-'0' + b[i]-'0' + carry) % 10 + '0';
                    carry = (a[i]-'0' + b[i]-'0' + carry) / 10;
                    i++;
                }
                while (i < a.size())
                {
                    c    += (a[i]-'0' + carry) % 10 + '0';
                    carry = (a[i]-'0' + carry) / 10;
                    i++;
                }
                while (i < b.size())
                {
                    c    += (b[i]-'0' + carry) % 10 + '0';
                    carry = (b[i]-'0' + carry) / 10;
                    i++;
                }
                while (carry > 0)//計算進位部分
                {
                    c     += carry % 10 + '0';
                    carry /= 10;
                }
                i = c.size() - 1;
                while (c[i] == '0')//消除多余的高位0
                {
                    i--;
                }
                c = c.substr(0, i+1);
                Reverse(c);
            }

            void SubInt(string & c, string a, string b)//模仿遞增向量的合并方法
            {
                c.resize(0);
               
                if (a == b)
                {
                    c += '0';
                    return ;
                }
               
                bool flag = false;
                if (a.size() < b.size() || (a.size() == b.size() && a < b))//交換,并得到一個負號
                {
                    flag = true;
                    string temp = a;
                    a = b;
                    b = temp;
                }
               
                Reverse(a);
                Reverse(b); ;
                int i = 0;
                while (i < b.size())
                {
                    if (a[i] >= b[i])
                         c += a[i] - b[i] + '0';
                    else
                    {
                        a[i+1] -= 1;
                        c      += a[i] + 10 - b[i] + '0';
                    }  
                    i++;
                }
                while (i < a.size())
                {
                    if (a[i] < '0')
                    {
                        a[i+1] -= 1;
                        a[i] += 10;
                    }
                    c += a[i];
                    i++;
                }
                i = c.size() - 1;
                while (c[i] == '0')//消除多余的高位0
                {
                    i--;
                }
                c = c.substr(0, i+1);
                if (flag)
                    c += '-';
                Reverse(c);
            }

            void MulInt(string & c, string a, string b)
            {
                c.resize(0);
                if (a == "0" || b == "0")
                {
                    c += '0';
                    return ;
                }
               
                Reverse(a);
                Reverse(b);
                string ta, tb, tc;
                int carry = 0;
                for (int i=0; i<b.size(); i++)
                {
                    tc.resize(0);
                    for (int j=0; j<i; j++)//先在臨時和tc的低位補足0
                        tc += '0';
                   
                    carry = 0;
                    for (int j=0; j<a.size(); j++)
                    {
                        tc   += ((a[j]-'0') * (b[i]-'0') + carry) % 10 + '0';
                        carry = ((a[j]-'0') * (b[i]-'0') + carry) / 10;
                    }
                    while (carry > 0)//計算進位部分
                    {
                        tc    += carry % 10 + '0';
                        carry /= 10;
                    }
                    //累加到c中
                    ta = c;
                    Reverse(ta);
                    Reverse(tc);
                    AddInt(c, ta, tc);
                    Reverse(c);
                    //消除多余的高位0
                    int pos = c.size() - 1;
                    while (c[pos] == '0')
                    {
                        pos--;
                    }
                    c = c.substr(0, pos+1);
                }
               
                Reverse(c);
            }

            void JieCHInt(string & c, string b)
            {
                string tb = "2";
                c = "1";
                while (tb.size() < b.size() || (tb.size() == b.size() && tb <= b))
                {
                    MulInt(c, c, tb);
                    AddInt(tb, tb, "1");
                }
            }

            void DivInt(string & c, string a, string b)
            {
                c.resize(0);
                if (a == "0" || b == "0")
                {
                    c += '0';
                    return ;
                }
               
                string copyA = a;//存儲a的值,之后a的值會變化
                while (a == b || a.size() > b.size() || (a.size() == b.size() && a > b))//直到余數(shù)小于除數(shù)
                {
                    for (int n=1; n<=a.size(); n++)
                    {
                        string ta = a.substr(0, n);//提取不小于除數(shù)的部分被除數(shù)
                        if (ta == "0") //是0直接跳過
                        {
                            c += '0';
                            a = a.substr(n, a.size()-n);
                            break;
                        }
                        if (ta == b)//相等,商為1,被除數(shù)去掉前n位
                        {
                            c += '1';
                            a = a.substr(n, a.size()-n);
                            break;
                        }
                        else if (ta.size() > b.size() || (ta.size() == b.size() && ta > b))//被除數(shù)不小于除數(shù)
                        {
                            char i = 0;//記錄商
                            string tb = b;
                            while (ta.size() > tb.size() || (ta.size() == tb.size() && ta > tb))//用多次減法實現(xiàn)除法運算
                            {
                                AddInt(tb, tb, b);
                                i++;
                            }

                            if (ta == tb)//整除
                            {
                                c += i + '1';
                                a = a.substr(n, a.size()-n);
                                break;
                            }
                            else//余數(shù)不為0
                            {
                                c += i + '0';
                                int pos = ta.size(); //記錄上一次被除數(shù)的增添位置
                                SubInt(tb, tb, b);//再減回去,使tb < ta
                                SubInt(ta, ta, tb);//獲取余數(shù)
                                n = ta.size();//下一次增添被除數(shù)位置
                                ta += a.substr(pos, a.size()-pos);//得到新的被除數(shù)
                                a = ta;
                            }
                        }
                        else//被除數(shù)小于除數(shù),商為0
                        {
                            c += '0';
                        }
                    }
                }
               
                while (copyA.size() > c.size())//補足低位的0
                    c += '0';
               
                //消除多余的高位0
                int pos = 0;
                while (pos < c.size() && c[pos] == '0')
                {
                    pos++;
                }
                c = c.substr(pos, c.size()-pos);
               
                if (c.size() == 0)//商為0
                    c = "0";
            }

            void PowInt(string & c, string a, string b)
            {
                c.resize(0);
                if (a == "0")
                {
                    c = "0";
                    return ;
                }
               if (b == "0")
               {
                    c = "1";
                    return ;  
               }
               if (b == "1")
               {
                    c = a;
                    return ;  
               }
              
               string tb;
               DivInt(tb, b, "2");
               PowInt(c, a, tb);
               MulInt(c, c, c);
              
               if ((b[b.size()-1]-'0')%2 == 1)
                   MulInt(c, c, a);
            }


            Posted on 2008-12-01 15:09 夢想飛揚 閱讀(572) 評論(3)  編輯 收藏 引用

            Feedback

            # re: 高精度運算  回復(fù)  更多評論   

            2008-12-12 21:33 by 本拉瘸
            拜讀,不過我想寫成類更好一點.

            # re: 高精度運算  回復(fù)  更多評論   

            2008-12-13 14:00 by 本拉瘸
            不支持負數(shù)的運算是一個瑕疵.也就是多點flag和a.at(0),b.at(0).

            # re: 高精度運算  回復(fù)  更多評論   

            2008-12-13 18:37 by 本拉瘸
            高精度乘除法有問題.
            比如 99 20
            久久精品国产男包| 99精品久久精品一区二区| 国产精品久久久久影院嫩草| 久久久久国产一区二区 | 免费观看久久精彩视频| 色诱久久av| 热re99久久精品国产99热| 日韩精品无码久久一区二区三| 久久午夜福利无码1000合集| 国产精品99久久久久久宅男| 久久精品亚洲一区二区三区浴池 | 欧美亚洲国产精品久久蜜芽| 久久亚洲精品无码播放| 久久本道久久综合伊人| 久久精品一区二区影院| 久久综合精品国产二区无码| 日日躁夜夜躁狠狠久久AV| 久久久久这里只有精品| 久久免费美女视频| 久久se精品一区精品二区| 久久久精品午夜免费不卡| 久久精品无码一区二区WWW| 国产免费福利体检区久久| av国内精品久久久久影院| 久久se精品一区二区| 欧美喷潮久久久XXXXx| 伊人情人综合成人久久网小说| 国产精品亚洲综合久久| 国内精品久久久久久久久电影网 | 日本欧美久久久久免费播放网 | 亚洲午夜久久久久久久久久| 亚洲欧洲精品成人久久曰影片 | 国内精品伊人久久久久AV影院| 亚洲欧洲精品成人久久曰影片| 九九久久精品国产| 国产精品日韩深夜福利久久| 国产精品一区二区久久精品| AV色综合久久天堂AV色综合在| 久久A级毛片免费观看| 久久99毛片免费观看不卡| 好久久免费视频高清|