大整数乘法计算

题外话:今天上午刚考完英语,糟糕透了。听力填空的时候居然写了个vecation,后来觉得看上去别捏,又改成了vocation。

后来翻字典,才知道是vacation。哎,这么基本的单词都忘了!!  下午去图书馆,就写了这个大整数计算的小程序。

 

 


普通的计算器只能计算16位数字,如附件中自带的计算器

截图:

 

 

 

 


 

自己写的计算程序

 

代码

  1 #include<iostream>
2 #include<memory>
3 #include<conio.h>
4 using namespace std;
5 int gn=0;//global variable that represents the size
6
7
8
9 int* multi(int* num1, int size1 ,int* num2, int size2)
10 {
11 int size=size1+size2;
12 gn=size-1;
13 int* ret=new int[size+1];
14 memset(ret,0,sizeof(int)*size);
15 int i=0,j=0,k;
16
17 /*----------------------------核心算法-----------------------------*/
18 for(i=0;i<size1;++i)
19 {
20 k=i;
21 for(j=0;j<size2;j++)
22 {
23
24 ret[k++]+=num1[i]*num2[j];
25 }
26 }
27 /*-------------------------------------------------------------------*/
28
29 /*--------------------------判断是否进位-----------------------------*/
30 for (j=size-1;j>0;--j)
31 {
32 if (ret[j]>=10)
33 {
34 ret[j-1]+=ret[j]/10;
35 ret[j]%=10;
36 }
37 }
38
39 /*-------单独判断ret[0]----------*/
40 if (ret[0]>=10)
41 {
42 for (i=size;i>0;--i)
43 ret[i]=ret[i-1];
44 ret[0]=ret[1]/10;
45 ret[1]%=10;
46 gn=size;
47
48
49 }
50 /*----------------------------------------------------------------------*/
51 return ret;
52
53
54
55 }
56
57 void main()
58 {
59 cout<<"by tiredoy"<<endl
60 <<"-------------------------------"<<endl<<endl;
61 int pa[2000],pb[2000];
62 int a=0,b=0,n=0;
63 char ch=getch();
64 /*------------------------------------宁肯麻烦,也不要goto--------------------------------------*/
65 int flag=1;
66 while(flag)
67 {
68 while(ch!=42)
69 {
70 while ( ch>=48&&ch<=57 )
71 {
72 pa[a]=ch-48;
73 cout<<pa[a];
74 ++a;
75 break;
76 }
77 ch=getch();
78 }
79 if (a!=0)
80 {
81 flag=0;
82
83 }
84 else {
85 ch=getch();
86 }
87 continue;
88
89 }
90 flag=1;
91 cout<<endl<<"*"<<endl;
92 while(flag)
93 {
94 while(!(ch==13||ch==61))
95 {
96 while ( ch>=48&&ch<=57 )
97 {
98 pb[b]=ch-48;
99 cout<<pb[b];
100 ++b;
101 break;
102 }
103 ch=getch();
104 }
105 if (b!=0)
106 {
107 flag=0;
108
109 }
110 else {
111 ch=getch();
112 }
113 continue;
114
115 }
116
117 int* ret=multi(pa,a,pb,b);
118
119 cout<<endl<<"="<<endl;
120 for(int i=0;i<gn;++i)
121 cout<<ret[i];
122 cout<<endl;
123
124
125
126
127
128
129 }

 

 程序&源码下载

posted @ 2012-01-09 22:29  tiredoy  阅读(1862)  评论(2编辑  收藏  举报