大数相乘(分治法实现时间复杂度为O(n^1.59))

/*
 * Author: dengzhaoqun
 * Date:    2011/04/11
 */
 
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX_LENGTH 1000
 
//------------define the data struct--------------------
typedef struct
{
char *cptr;
int len;
}CharNum;
//------------the end of defining data struct----------
 
//------the methods' declaration------
void DelHeadZero(CharNum *Result);
CharNum *DoMinus(CharNum *A,CharNum *B,int *flag);
int GetMax(int x,int y);
int GetMin(int x,int y);
CharNum *GetNum(char ch);
void Init(char *ch,int len);
CharNum *Merge(CharNum *AC,CharNum *BD,CharNum *ABDC,int k,int flagABDC);
void MergeAdd(CharNum *des,CharNum *source,int k);
void MergeMinus(CharNum *des,CharNum *source,int k);
CharNum *Multiple(CharNum *X,CharNum *Y);
void ShowCharNum(CharNum *Result);
void ToChars(int mul,CharNum *Result);
void TopDown(CharNum *chN);
char *ToStr(CharNum *chN);
//----end of method declaration-----
 
//-------------the main method--------------
int main()
{
CharNum *X;
CharNum *Y;
CharNum *Result;
 
//init
    X=NULL;
Y=NULL;
Result=NULL;
 
//get X,Y
X=GetNum('X');
Y=GetNum('Y');
 
Result=Multiple(X,Y);
ShowCharNum(Result);
 
free(X);
free(Y);
free(Result);
 
getchar();
 
return 0;
}
//------------------the end of main method------------------
 
 
//-----------begin the method of TopDown---------------
void TopDown(CharNum *chN)
{
int i;
int j;
int k;
char *ch;
 
j=chN->len-1;
while(chN->cptr[j]!='#')
{
j--;
}
 
ch=(char *)malloc(j*sizeof(char));
 
k=j-1;
i=0;
for(i,k;i<j;k--,i++)
{
ch[i]=chN->cptr[k];
}
i=0;
for(i;i<j;i++)
{
chN->cptr[i]=ch[i];
}
 
free(ch);
}
//-----------end of method TopDown--------------
 
//-----------implement the method of getNum-------------
CharNum *GetNum(char ch)
{
 
CharNum *chN;
chN=(CharNum *)malloc(sizeof(CharNum));
chN->cptr=NULL;
chN->len=0;
 
chN->cptr=(char *)malloc(MAX_LENGTH*sizeof(char));
Init(chN->cptr,MAX_LENGTH);
chN->len=MAX_LENGTH;
printf("Input the value of %c(length<=)",ch);
printf("%d"
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值