博客首页 注册 建议与交流 排行榜 加入友情链接         宝宝相册的专门空间
推荐 投诉 搜索: 帮助

苦丁茶

tomcent.cublog.cn
两个大整数相乘
使用MFC
class CMyDlg : public CDialog
{
public:
 CString m_first;
 CString m_second;
 CString m_result;
 
 。。。。。。。。。。。

 virtual void OnOK();
 virtual void OnCancel();
 afx_msg void Onreset();
 };
 
。。。。。。。。。。。。。。
 
void CMyDlg::OnOK() //确定执行
{
 // TODO: Add extra validation here
 UpdateData(true);
 CString  re,first,second;
 int record=0;
    first=m_first;  //保存初始值
    second=m_second;//保存初始值
    if(m_first.GetLength()%2!=0)//判断第一个数是否为偶数
 {     
           m_first=m_first+"0";  //不是就补零使它为偶数
     record++;             //记录所补的零的个数
 }
 if(m_second.GetLength()%2!=0)//判断第二个数是否为偶数
 {
           m_second=m_second+"0";  //不是就补零使它为偶数
     record++;               //记录所补的零的个数
 }
 //calculate(m_first,m_second);
    re=calculate(m_first,m_second); //计算 m_first  m_second
 m_result=re.Mid(0,re.GetLength()-record);//减去所补的零
   
    //m_result=atoi(re);
 m_first=first;  //还原初始值
    m_second=second;//还原初始值
    UpdateData(false);
 
}   
void CMyDlg::OnCancel() //退出
{
 // TODO: Add extra cleanup here
 
 CDialog::OnCancel();
}
CString CMyDlg::calculate(CString x,CString y)//计算
{
 int j,i;
 CString  result;//记录结果
    CString x1,x2,y1,y2;
 long int A,B,C,D;
 int n=0;
   if(x.GetLength()==y.GetLength())//如果x 与y 的位数相等
   {
       if(x.GetLength()==1)    //位数为一位时
    {
     result.Format("%d",atoi(x)*atoi(y));  //格式华
           return  result;  
        
    }
    else
    {
           n=x.GetLength()/2;  //分两段
     x2=x.Mid(0,n);
     x1=x.Mid(x.GetLength()-n,n);
           y2=y.Mid(0,n);
     y1=y.Mid(y.GetLength()-n,n);
           A=atol(calculate(x1,y1));
     B=atol(calculate(x2,y2));
    
     C=(atol(x1)-atol(x2))*(atol(y2)-atol(y1));
           D=C+A+B;//相当于{(x0-x1)*(y1-y0)+x0*y0+x1*y1}
     for(i=0;i<n;i++)
     {
      D=D*10;
      //B=B*10;
     
     }
     for(j=0;j<2*n;j++)
     {
       B=B*10;
   }
           result.Format("%d",A+D+B);
           return  result;
    
    }
     
      
   }
   else///如果为数不相等就 取其中 位数少的 进行计算 它的x1就为零
   {
        n=x.GetLength()>y.GetLength()?y.GetLength():x.GetLength();
     x2=x.Mid(0,x.GetLength()-n);
     x1=x.Mid(x.GetLength()-n,n);
           y2=y.Mid(0,y.GetLength()-n);
     y1=y.Mid(y.GetLength()-n,n);
           A=atol(calculate(x1,y1));
     B=0;
     C=(atol(x1)-atol(x2))*(atol(y2)-atol(y1));
           D=C+A+B;
     for(int i=0;i<n;i++)
     {
      D=D*10;
     }
           result.Format("%d",A+D+B);
           return  result;
      
     
   }
  
}
void CMyDlg::Onreset() //重设
{
 // TODO: Add your control notification handler code here
 m_second="";
 m_first="";
 m_result="";
 UpdateData(false);
}

发表于: 2006-11-14,修改于: 2006-11-14 12:37,已浏览557次,有评论0条 推荐 投诉

给我留言
版权所有 ChinaUnix.net 页面生成时间:1.91124