VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 2760|回复: 2

[求助] 各位老师好!请求编辑大整数的快速乘法除法vb程序

[复制链接]
发表于 2020-4-18 12:15:02 | 显示全部楼层 |阅读模式
求编辑大整数的快速乘法除法vb程序,谢谢您!
如下为网上搜索到的快速乘法的c语言程序,请老师看看能不能运行?能不能翻译成vb程序?
迭代型:(共119行,乱了,再整理一下)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <conio.h>
#define N 150010const double pi = 3.141592653;
char s1[N>>1], s2[N>>1];
double rea[N], ina[N], reb[N], inb[N];
int ans[N>>1];
void Swap(double *x, double *y)
{
    double t = *x;
    *x = *y;
    *y = t;
}
int Rev(int x, int len)
{
    int ans = 0;
    int i;
   for(i = 0;
i < len; i++)
{
       ans <<= 1;
        ans |= (x & 1);
        x >>= 1;
    }
    return ans;
}
void FFT(double *reA, double *inA, int n, bool flag)
{
   int s;
    double lgn = log((double)n) / log((double)2);
    int i;
    for(i = 0; i < n; i++)
{  
      int j = Rev(i, lgn);
        if(j > i)
{
            Swap(&reA[i], &reA[j]);
            Swap(&inA[i], &inA[j]);
        }
    }
   for(s = 1; s <= lgn; s++)
{   
     int m = (1<<s);
        double reWm = cos(2*pi/m), inWm = sin(2*pi/m);
        if(flag) inWm = -inWm;
        int k;
        for(k = 0; k < n; k += m)
{  
          double reW = 1.0, inW = 0.0;
            int j;
           for(j = 0; j < m / 2; j++)
{  
              int tag = k+j+m/2;
                double reT = reW * reA[tag] - inW * inA[tag];
                double inT = reW * inA[tag] + inW * reA[tag];
               double reU = reA[k+j], inU = inA[k+j];
                reA[k+j] = reU + reT;
                inA[k+j] = inU + inT;
                reA[tag] = reU - reT;
               inA[tag] = inU - inT;
               double rew_t = reW * reWm - inW * inWm;
                 double inw_t = reW * inWm + inW * reWm;
                reW = rew_t;
               inW = inw_t;
           }
        }
    }
    if(flag)
{
        for(i = 0;
i < n; i++)
{
            reA[i] /= n;
            inA[i] /= n;
        }
   }
}
int main()
{
#if 0
   freopen("in.txt","r",stdin);
#endif
   while(~scanf("%s%s", s1, s2))
{
       memset(ans, 0 , sizeof(ans));
        memset(rea, 0 , sizeof(rea));
        memset(ina, 0 , sizeof(ina));
        memset(reb, 0 , sizeof(reb));
        memset(inb, 0 , sizeof(inb));
        int i, lent, len = 1, len1, len2;
        len1 = strlen(s1);
        len2 = strlen(s2);
        lent = (len1 > len2 ? len1 : len2);
        while(len < lent) len <<= 1;
        len <<= 1;
        for(i = 0;
i < len; i++)
{
           if(i < len1) rea[i] = (double)s1[len1-i-1] - '0';
            if(i < len2) reb[i] = (double)s2[len2-i-1] - '0';
            ina[i] = inb[i] = 0.0;
        }
        FFT(rea, ina, len, 0);
        FFT(reb, inb, len, 0);
        for(i = 0; i < len; i++)
{
           double rec = rea[i] * reb[i] - ina[i] * inb[i];
            double inc = rea[i] * inb[i] + ina[i] * reb[i];
            rea[i] = rec; ina[i] = inc;
        }
        FFT(rea, ina, len, 1);
        for(i = 0; i < len; i++)
           ans[i] = (int)(rea[i] + 0.4);
        for(i = 0; i < len; i++)
{
           ans[i+1] += ans[i] / 10;
            ans[i] %= 10;
        }
        int len_ans = len1 + len2 + 2;
        while(ans[len_ans] == 0 && len_ans > 0)
len_ans--;
        for(i = len_ans; i >= 0; i--)
            printf("%d", ans[i]);
       printf("\n");
    }
    return 0;
}
 楼主| 发表于 2020-12-31 01:02:18 | 显示全部楼层
Private Sub Command1_Click()
Dim xr() As Double, a As String
a = Trim(Text1)
ReDim xr(0 To Len(a) - 1)
For i1 = 0 To Len(a) - 1
xr(i1) = Mid(a, i1 + 1, 1)
  Next
Dim l As Long, le As Long, le1 As Long, n As Long, r As Long, p As Long, q As Long, m As Byte
Dim wr As Double, w1 As Double, wlr As Double, wl1 As Double, tr As Double, t1 As Double
Dim pi As Double, t As Double
Dim xi()
n = Len(a) '求数组大小,其值必须是2的幂
m = 0
l = 2
pi = 3.14159265358979
Do
l = l + l
m = m + 1
Loop Until l > n
n = l / 2
ReDim xi(n - 1)

l = 1
Do
  le = 2 ^ l
  le1 = le / 2
  wr = 1
  wi = 0
  If l = 1 Then
  t = 0
  Else
  t = pi / le1
  End If
  w1r = Cos(t)
  w1i = -Sin(t)
  Print l
  r = 0
Do
  p = r
  Do
   q = p + le1
   
   tr = xr(q) * wr - xi(q) * wi
   ti = xr(q) * wi + xi(q) * wr
   
   xr(q) = xr(p) - tr
   xi(q) = xi(p) - ti
   xr(p) = xr(p) + tr
   xi(p) = xi(p) + ti
   Print p, q
   
   Print xr(p), xr(q)
   
   
   p = p + le
Loop Until p > n - 2

wr = wr * w1r - wi * w1i
wi = wr * w1i + wi * w1r
r = r + 1
Loop Until r > le1 - 1
l = l + 1
Loop Until l > m

For i = 0 To n - 1 '仅输出模
   xr(i) = Sqr(xr(i) ^ 2 + xi(i) ^ 2)
   Text2 = Text2 & "  " & xr(i) & "+" & xi(i) & "i"
   Next

End Sub

Private Sub Command2_Click()
Text1 = ""
Text2 = ""
End Sub

输入:Text1 =80607000,结果:
    21+0i  13.4143937430245+-3.50000000000001i  4.40338197130883+-3.19974746830584i  6.56830852046515+-1.17525253169417i  
7+0i  4.64263292831844+3.49999999999999i  3.3449607857897+3.19974746830584i  9.60982505463419+1.17525253169419i
与正确值比较:12.9+10.9i, 2+7i, 3.1-1.1i, 7, 3.1+1.1i, 2-7i, 12.9-10.9i, 21也是不对。

哪里错了?请求老师给与指导,谢谢!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-1 21:37:50 | 显示全部楼层
Private Sub Command1_Click()
Dim xr() As Double, a As String
a = Trim(Text1)
ReDim xr(0 To Len(a) - 1)
For i1 = 0 To Len(a) - 1
xr(i1) = Mid(a, i1 + 1, 1)
  Next
Dim l As Long, le As Long, le1 As Long, n As Long, r As Long, p As Long, q As Long, m As Byte
Dim wr As Double, w1 As Double, wlr As Double, wl1 As Double, tr As Double, t1 As Double
Dim pi As Double, t As Double
Dim xi()
n = Len(a) '求数组大小,其值必须是2的幂
m = 0
l = 2
pi = 3.14159265358979
Do
l = l + l
m = m + 1
Loop Until l > n
n = l / 2
ReDim xi(n - 1)

l = 1
Do
  le = 2 ^ l
  le1 = le / 2
  wr = 1
  wi = 0
  t = pi / le1
  w1r = Cos(t)
  w1i = -Sin(t)
  r = 0
Do
  p = r
  Do
   q = p + le1
   
   tr = xr(q) * wr - xi(q) * wi
   ti = xr(q) * wi + xi(q) * wr
   
   xr(q) = xr(p) - tr
   xi(q) = xi(p) - ti
   xr(p) = xr(p) + tr
   xi(p) = xi(p) + ti
   p = p + le
Loop Until p > n - 1

wr = wr * w1r - wi * w1i
wi = wr * w1i + wi * w1r
r = r + 1
Loop Until r > le1 - 1
l = l + 1
Loop Until l > m

For i = 0 To n - 1 '仅输出模
   
   Text2 = Text2 & "  " & xr(i) & "+" & xi(i) & "i"
   Next
End Sub

Private Sub Command2_Click()
Text1 = ""
Text2 = ""
End Sub

输入:Text1 =80607000,结果:
21+0i  12.9497474683058+-3.50000000000001i  3.02512626584709+-3.19974746830584i  6.46231060122937+-1.17525253169417i  
7+0i  3.05025253169417+3.49999999999999i  0.974873734152908+3.19974746830584i  9.53768939877061+1.17525253169419i
与正确值比较:12.9+10.9i, 2+7i, 3.1-1.1i, 7, 3.1+1.1i, 2-7i, 12.9-10.9i, 21也是不对。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-7-4 22:56

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表