• <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>

            兩個數相乘,小數點后位數沒有限制,請寫一個高精度算法【轉】

            算法提示:

                      輸入 string a, string b; 計算string c=a*b; 返回 c;

            1,    紀錄小數點在a,b中的位置l1,l2, 則需要小數點后移動位置數為l=length(a)+length(b)-l1-l2-2;

            2,    去掉a,b中的小數點,(a,b小數點后移,使a,b變為整數)

            3,    計算c=a*b; (同整數的大數相乘算法)

            4,    輸出c,(注意在輸出倒數第l個數時,輸出一個小數點。若是輸出的數少于l個,就補0)

            du51(郁郁思揚)的答案:


            變為整數求就行了.輸入的時候記一下,小數點位置..輸出再做點文章就行了.
            下面的是大整數的運算.
            #include<iostream>
            using namespace std;
            #define MAX 10000
            struct Node{
               int data;
               Node *next;
            };
            void output(Node *head)
            {
               if(!head->next&&!head->data)return;
               output(head->next);
               cout<<head->data;
            }
            void Mul(char *a,char *b,int pos)        
            {
               char *ap=a,*bp=b;
               Node *head=0;
               head=new Node;head->data=0,head->next=0;   //頭
               Node *p,*q=head,*p1;
               int temp=0,temp1,bbit;
               while(*bp)                //若乘數不為空 ,繼續.
               {
                   p=q->next;p1=q;
                   bbit=*bp-48;          //把當前位轉為整型
                   while(*ap||temp)            //若被乘數不空,繼續
                   {
                       if(!p)            //若要操作的結點為空,申請之
                       {
                           p=new Node;
                           p->data=0;
                           p->next=0;
                           p1->next=p;
                       }
                       if(*ap==0)temp1=temp;
                       else { temp1=(p1->data)+(*ap-48)*bbit+temp;ap++; }
                       p1->data=temp1%10;    //留當前位
                       temp=temp1/10;    //進位以int的形式留下.
                       p1=p;p=p->next;                 //被乘數到下一位
                   }
                   ap=a;bp++;q=q->next;                //q進下一位
               }
               p=head;
               output(p);                   //顯示
               cout<<endl;
               while(head)                 //釋放空間
               {
                       p=head->next;
                       delete head;
                       head=p;
               }
            }
            int main()
            {
               cout<<"請輸入兩個數"<<endl;
               char test1[MAX],test2[MAX];
               cin.getline(test1,MAX,'\n');
               cin.getline(test2,MAX,'\n');
               Mul(strrev(test1),strrev(test2));
               system("PAUSE");
               return 0;
            }
            上面大整數已經寫了.你加幾個東西就行了.
            #include<iostream>
            using namespace std;
            #define MAX 10000
            struct Node{
               int data;
               Node *next;
            };
            void output(Node *head,int pos)
            {
               if(!head->next&&!head->data)return;
               output(head->next,pos-1);
               cout<<head->data;
               if(!pos)cout<<".";
            }
            void Mul(char *a,char *b,int pos)        
            {
               char *ap=a,*bp=b;
               Node *head=0;
               head=new Node;head->data=0,head->next=0;   //頭
               Node *p,*q=head,*p1;
               int temp=0,temp1,bbit;
               while(*bp)                //若乘數不為空 ,繼續.
               {
                   p=q->next;p1=q;
                   bbit=*bp-48;          //把當前位轉為整型
                   while(*ap||temp)            //若被乘數不空,繼續
                   {
                       if(!p)            //若要操作的結點為空,申請之
                       {
                           p=new Node;
                           p->data=0;
                           p->next=0;
                           p1->next=p;
                       }
                       if(*ap==0)temp1=temp;
                       else { temp1=(p1->data)+(*ap-48)*bbit+temp;ap++; }
                       p1->data=temp1%10;    //留當前位
                       temp=temp1/10;    //進位以int的形式留下.
                       p1=p;p=p->next;                 //被乘數到下一位
                   }
                   ap=a;bp++;q=q->next;                //q進下一位
               }
               p=head;
               output(p,pos);                   //顯示
               cout<<endl;
               while(head)                 //釋放空間
               {
                       p=head->next;
                       delete head;
                       head=p;
               }
            }
            int main()
            {
               cout<<"請輸入兩個數"<<endl;
               char test1[MAX],test2[MAX],*p;
               int pos=0;
               cin.getline(test1,MAX,'\n');
               cin.getline(test2,MAX,'\n');
               if(p=strchr(test1,'.'))
               {
                   pos+=strlen(test1)-(p-test1)-1;
                   do
                   {
                       p++;
                       *(p-1)=*p;
                   }while(*p);
               }       
               if(p=strchr(test2,'.'))
               {
                   pos+=strlen(test2)-(p-test2)-1;
                   do
                   {
                       p++;
                       *(p-1)=*p;
                   }while(*p);
               }   
               Mul(strrev(test1),strrev(test2),pos);
               system("PAUSE");
               return 0;
            }

             

            本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/hanlin1985/archive/2008/10/10/3048605.aspx

            posted on 2011-06-07 10:25 Hsssssss 閱讀(7695) 評論(0)  編輯 收藏 引用 所屬分類: C++代碼

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章分類

            文章檔案

            收藏夾

            搜索

            最新評論

            久久综合给合久久狠狠狠97色 | 7777精品久久久大香线蕉| 久久91精品综合国产首页| 2021国产成人精品久久| 三级片免费观看久久| 久久影院综合精品| 国产精品美女久久久网AV| 美女久久久久久| 精品国产乱码久久久久久郑州公司| 精品一区二区久久久久久久网站| 免费一级做a爰片久久毛片潮| 国产精品美女久久久m| 久久精品国产黑森林| 亚洲中文精品久久久久久不卡| 久久无码av三级| 亚洲AV日韩精品久久久久久| 国产精品成人99久久久久| 香蕉久久夜色精品升级完成| 久久e热在这里只有国产中文精品99| 青青草原综合久久大伊人| 97精品国产97久久久久久免费| 日产精品久久久久久久性色| 久久久久久A亚洲欧洲AV冫| 国产成年无码久久久久毛片| 国产精品99久久久精品无码| 久久国产香蕉视频| 99精品国产在热久久| 久久久久久久久66精品片| 国产成人无码精品久久久久免费| 日日躁夜夜躁狠狠久久AV| 色综合久久夜色精品国产| 国产午夜精品理论片久久 | 99久久精品国产毛片| 老色鬼久久亚洲AV综合| 伊人久久大香线蕉综合影院首页 | 亚洲国产一成久久精品国产成人综合 | 伊人久久大香线蕉精品| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 狠狠精品干练久久久无码中文字幕 | 亚洲中文字幕无码一久久区| 一97日本道伊人久久综合影院|