日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

大数计算

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大数计算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

大數計算:

? ? ? ?由于編程語言提供的基本數值數據類型表示的數值范圍有限,不能滿足較大規模的高精度數值計算,因此需要利用其他方法實現高精度數值的計算,于是產生了大數運算。

?

大數計算簡析:

? ? ? ?大數計算實現的理論是,首先提取輸入值賦予指定String字符串。

? ? ? ?通過String.charAt(index)提取每一位的值,賦予int數組.

? ? ? ?然后求相乘每一位的值和進位。

? ? ? ?直到最后每一位都求出來。

?

代碼實現:

? ? ? ?

  1 import java.awt.*;
  2 import java.awt.event.ActionEvent;
  3 import java.awt.event.ActionListener;
  4 import javax.swing.*; 
  5 public class DashuCf extends JFrame {
  6     
  7     
  8     JLabel l1=new JLabel("FirstNum ");
  9     JLabel l2=new JLabel("SecondNum");
 10     JLabel l3=new JLabel("Result   ");
 11     JTextField FirstNum = new JTextField(20); 
 12     JTextField SecondNum = new JTextField(20); 
 13     JTextField Result = new JTextField(20); 
 14     JTextField Check = new JTextField(20); 
 15     JButton CountButton = new JButton("Count");     
 16     public static void dS(JTextField a,JTextField b,JTextField c,JTextField d){
 17         
 18         d.setText("");
 19         String fc=a.getText();
 20         String sc=b.getText();
 21         int[] f,s;
 22         f = new int[fc.length()];
 23         s = new int[sc.length()];    
 24         for(int i=0; i<fc.length();i++){
 25                if((int)fc.charAt(i)>=48&&(int)fc.charAt(i)<58)
 26                  f[fc.length()-1-i]=(int)fc.charAt(i)-48;
 27                else
 28                {
 29                    d.setText("The number of FirstNum is invalide!!!");
 30                    return;
 31                }
 32         }
 33        for(int i=0; i<sc.length();i++){
 34             
 35            if((int)sc.charAt(i)>=48&&(int)sc.charAt(i)<58)
 36              s[sc.length()-1-i]=(int)sc.charAt(i)-48;
 37            else
 38            {
 39                d.setText("The number of SecondNum is invalide!!!");
 40                return;
 41            }
 42                
 43         }       
 44        int max=s.length+f.length;
 45        int key=0;
 46        int[] biaoji,out;
 47        biaoji = new int[max+3];
 48        out = new int[max+3];
 49        
 50        for(int i=0;i<max;i++){
 51            biaoji[i]=0;
 52            out[i]=0;
 53        }
 54        
 55        for(int i=0;i<max;i++){
 56            
 57            key=biaoji[i]; 
 58            for(int m=0;m<=i;m++){
 59                
 60                if((m<f.length)&&((i-m)>=0)&&((i-m)<s.length))
 61                key=f[m]*s[i-m]+key;
 62               
 63            }
 64            out[i]=key%10;
 65           biaoji[i+1]=((key-out[i])/10)%10+biaoji[i+1];
 66           biaoji[i+2]=((key-out[i]-out[i+1]*10)/100)%10+biaoji[i+2];
 67           biaoji[i+3]=((key-out[i]-out[i+1]*10-out[i+2]*100)/1000)%10+biaoji[i+3];
 68            
 69        }
 70        String result="";
 71        for(int i=max-1;i>=0;i--){
 72            
 73            if(!(i==max-1&&out[i]==0))
 74            {  
 75                result=result+out[i];}
 76        }
 77        c.setText(result);
 78     }
 79     public DashuCf()  
 80     {                     
 81         CountButton.addActionListener(new CountAction());
 82         Check.setForeground(new Color(108, 2, 10));
 83         setLayout(new GridLayout(0,2,10,5));
 84         setTitle("大數乘法"); 
 85         getContentPane().add(l1);
 86         getContentPane().add(FirstNum);
 87         getContentPane().add(l2);
 88         getContentPane().add(SecondNum);
 89         getContentPane().add(l3);
 90         getContentPane().add(Result);
 91         getContentPane().add(Check);
 92         getContentPane().add(CountButton);      
 93         setSize(600, 200);  
 94         setVisible(true);  
 95     } 
 96 class CountAction implements ActionListener {
 97         
 98         public void actionPerformed(ActionEvent e) {
 99             dS(FirstNum,SecondNum,Result,Check);
100         }
101     }
102 
103 public static void main(String argv[]){
104     new DashuCf();
105 }
106 
107 }

?

效果演示:

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?輸入正確的數值

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 輸入非法數值時

?

轉載于:https://www.cnblogs.com/udld/p/4155579.html

總結

以上是生活随笔為你收集整理的大数计算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。