weitom1982

向各位技术前辈学习,学习再学习.
posts - 296, comments - 49, trackbacks - 0, articles - 0
  IT博客网 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

大数运算全部源代码

Posted on 2006-03-20 23:26 高山流水 阅读(559) 评论(4)  编辑 收藏 引用 所属分类: 计算机基础知识
这个程序应该是我在2004年写的
当时还只是一个读化工专业的大四学生
基本上对什么编程规范、什么算法,什么性能优化没什么了解
只是简单地用字符串模拟实现了任意大整数的四则运算
现在看来当然很幼稚了
不过为保持原来面目,一直没改,留做纪念。
供有需要的朋友参考
敬请指教




  1//long integer operation , inclue addition,substracttion,multiplicaton
  2//and division
  3#include <stdio.h>
  4#include <conio.h>
  5#include <ctype.h>
  6#include <string.h>
  7#define MAX 1000
  8#define MARK  ('0'-1)
  9
 10//The Prototypes of the functions
 11char* Initialiaze(char [],int);
 12int  Read(char [],char [],char * );
 13int  Print(char [],char [],char [],char []);
 14int  Calculate(char[],char[],char[],char []);
 15char* Addition(char[],char[]);
 16char* Substraction(char[],char[]);
 17char* Multiplication(char[],char[]);
 18char* Division(char[],char[]);
 19char Div_per_bit(char [],int , char []);
 20int Sub_per_bit(char [],char [],int *);
 21int Copy(char [],int ,char []);
 22int Compare(char [],char []);
 23int  Data_Process(char []);
 24int  Value(char);
 25int  Check(char []);
 26int  Compare(char [],char []);
 27int Judge(int);
 28int Convert(char [],char [],int);
 29
 30//The main function,calling  Read , Calculate  and Print function
 31int  main(void)
 32{    char a[MAX],b[MAX],c[2*MAX],action[MAX],ch='\t';
 33     while   (ch!=EOF)
 34     {
 35       clrscr();
 36       Read(a,b,action);
 37       if (Calculate(a,b,c,action))
 38         Print(a,b,c,action);
 39       ch=getchar();
 40     }

 41     return 0;
 42}

 43//The check function check if the input number is legal
 44int  Check(char s[])
 45{    int i;
 46     if (strlen(s)==0)
 47         return 0;
 48     if (strlen(s)>=MAX)
 49         return 0;
 50
 51     for (i=0; s[i]; i++)
 52       if (!isdigit(s[i]))
 53      return 0;
 54     return 1;
 55}

 56//The Iniatilize function ,initialize the result number before calculation
 57char* Initialize(char s[],int length)
 58{    int i;
 59     for (i=0; i<length; i++)
 60       s[i]=MARK;
 61     return s;
 62
 63}

 64//The Read function,Read the operands and the operation
 65int  Read(char a[],char b[],char action[])
 66{
 67     printf("************This Program calculats two long integer action!************\n");
 68     printf("Input long integer A,max length is %d:\n",MAX);
 69     gets(a);
 70     while (!Check(a))
 71     {   printf("Input error , Please input a correct value :\n");
 72     gets(a);
 73     }

 74     printf("Iuput the operation over a & b \n");
 75     printf("addition is '+' ,substraction is '-',multiplication is '*',division is '/'\n");
 76     printf("                            Warning:\n");
 77     printf("If you do the division,A must bigger than B ,and B must not equal to zero !\n");
 78     gets(action);
 79     while ((Compare(action,"+")!=0&& (Compare(action,"-")!=0&& (Compare(action,"*")!=0&& (Compare(action,"/")!=0))
 80     {   printf("Input error , Please input a correct action :\n");
 81         gets(action);
 82     }

 83     printf("Input long integer b,max length is %d:\n",MAX);
 84     gets(b);
 85     while (!Check(b))
 86     {   printf("Input error , Please input a correct value :\n");
 87     gets(b);
 88     }

 89     return 1;
 90}

 91//The Calculate function,calling Addition,Substraction,Multiplication or Division function  in accordance with the action
 92int  Calculate(char a[],char b[],char c[],char action[])
 93{
 94    if (Compare(action,"+")==0)
 95    {     strcpy(c,Addition(a,b));
 96         return 1;
 97    }

 98    if (Compare(action,"-")==0)
 99    {      if  ((Substraction(a,b))!=NULL)
100              strcpy(c,Substraction(a,b));
101          else
102          {      strcpy(c,"-");
103              strcat(c,Substraction(b,a));
104          }

105          return 1;
106    }

107    if (Compare(action,"*")==0)
108    {    strcpy(c,Multiplication(a,b));
109         return 1;
110    }

111
112    if (Compare(action,"/")==0)
113    {     if ((Division(a,b))!=NULL)
114            strcpy(c,Division(a,b));
115     else
116     {   printf("Press Ctrl-Z to end, Press Enter to recalculate!\n");
117         return 0;
118     }

119         return 1;
120     }

121
122}

123//The Print function , print the result
124int  Print(char a[],char b[],char c[],char action[])
125{    printf("The result of \n%s \n%s \n%s \nis :\n",a,action,b);
126     puts(c);
127     printf("Press Ctrl-Z to end , Press Enter  to recalculate..\n");
128     return 1;
129}

130//The Addition function , add two operands and return the result
131char* Addition(char a[],char b[])
132{    char c[2*MAX],d[2*MAX];
133     int i,j,k,a_length,b_length;
134     Initialize(c,2*MAX);
135     a_length=strlen(a);
136     b_length=strlen(b);
137     for (i=a_length-1,j=b_length-1,k=2*MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
138     {  if ( i>=0 && j>=0 )
139      c[k]=Value(a[i])+Value(b[j])+'0';
140    else
141      if (i>=0 && j<0 )
142         c[k]=Value(a[i])+'0';
143      else
144        if ( i<0 && j>=0 )
145           c[k]=Value(b[j])+'0';
146     }

147     Data_Process(c);
148     Convert(c,d,2*MAX);
149     return d;
150}

151//The Substraction function , substract one operand from another operand , and return the result
152char* Substraction(char a[],char b[])
153{    char c[2*MAX],d[2*MAX];
154     int i,j,k,a_length,b_length,sub_result,symbol,flag[2*MAX]={0};
155     Initialize(c,2*MAX);
156     a_length=strlen(a);
157     b_length=strlen(b);
158     if (strcmp(a,b)==0)
159    return ("0");
160     for (i=a_length-1,j=b_length-1,k=2*MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
161     {   sub_result=a[i]-b[j];
162     symbol=Judge(sub_result);
163     if (i>=1 && j>=0)
164     {      if (flag[k]==0)
165        {   if  (a[i]>=b[j])
166               c[k]=sub_result+'0';
167            else
168            {       c[k]=sub_result+10+'0';
169               flag[k-1]=1;
170            }

171        }

172        else
173        {   if (a[i]-b[j]>=1)
174              c[k]=sub_result-1+'0';
175            else
176            {     c[k]=sub_result+9+'0';
177              flag[k-1]=1;
178            }

179        }

180     }

181     else
182     if  (i==0 && j<0)
183        {
184             if (flag[k]==0)
185              c[k]=a[i];
186             else
187             {   if (a[i]==1)
188                ;
189             else
190                 c[k]=a[i]-1;
191             }

192        }

193     else
194     {  if ((i==0&& (j==0))
195           {   if (flag[k]==0)
196               {  switch (symbol)
197              {  case 0:   ;
198                       break;
199                 case 1:   c[k]=sub_result+'0';
200                       break;
201                 case -1:  return NULL;
202                       break;
203               }

204               }

205               else
206               {   switch (Judge(sub_result-1))
207               {  case 0:   ;
208                        break;
209                  case 1:   c[k]=sub_result-1+'0';
210                        break;
211                  case -1:  return NULL;
212                        break;
213               }

214               }

215           }

216         else
217         if ((i<0&& (j>=0))
218            return NULL;
219         else
220         if ((i>0&& (j<0))
221         {  if (flag[k]==0)
222              c[k]=a[i];
223            else
224            {   if (a[i]>'0')
225               c[k]=a[i]-1;
226            else
227            {  c[k]='9';
228               flag[k-1]=1;
229            }

230            }

231         }

232     }

233     }

234     Convert(c,d,2*MAX);
235     return d;
236}

237//The Multiplication function,multipy two operands and return the result
238char* Multiplication(char a[],char b[])
239{    char c[2*MAX],d[2*MAX];
240     int i,j,k,p,a_length,b_length;
241     Initialize(c,2*MAX);
242     a_length=strlen(a);
243     b_length=strlen(b);