Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519891
  • 博文数量: 118
  • 博客积分: 3995
  • 博客等级: 中校
  • 技术积分: 1276
  • 用 户 组: 普通用户
  • 注册时间: 2005-11-15 12:15
文章分类

全部博文(118)

文章存档

2014年(1)

2013年(1)

2010年(6)

2009年(27)

2008年(10)

2007年(33)

2006年(38)

2005年(2)

我的朋友

分类:

2009-11-18 15:35:22

大数乘法(二)稍作修改便可支持浮点数乘法,浮点数需要记录小数点的位置,这里使用带头节点的链表来表示浮点数,头节点存放浮点数的位置

原来的大整数乘以一个小整数继续使用,需要修改下面的函数

/*
 * multiply of two float number
 * */

node *multiply(node *h1, node *h2)
{
    node *p;
    node *head;
    node *q;

    /* init list head */
    if ((head=(node *)malloc(sizeof(node))) == NULL) exit(1);
    head->val = h1->val + h2->val;
    head->next = NULL;
    q = NULL;

     /* skip head of h1,h2 */
    for (p=h2->next,h1=h1->next; p != NULL; p=p->next) {
        if (p->val > 0)   /* 这个设计可能使后面计算小数点位置为负数 */
            mul(&q, h1, p->val);
        if (head->next == NULL) /* 记录 */
            head->next = q;
        q = q->next;
    }
    return head;
}


逆转,包括小数点的计算

/*
 * reverse a list
 * */

node * reverse(node *h)
{
    node *p,*q,*tmp;
    int n;

    /* 去掉小数点后面的零(如果考虑精度就不去掉) */
    while (h->next != NULL && h->next->val == 0) {
        p = h->next;
        h->next = h->next->next;
        h->val --; /* point --*/
        free(p);
    }

    /* 逆转列表 */
    n = 0;
    for (p=h->next,q=NULL; p != NULL; p=tmp) {
        tmp = p->next;
        p->next = q;
        q = p;
        n ++;
    }
    h->val = n - h->val;  /* 小数点调整 */
    h->next = q;

    /* 去掉小数点前,整数部分开始的零 */
    while (h->val > 0 && h->next != NULL && h->next->val == 0) {
        p = h->next;
        h->next = h->next->next;
        h->val --; /* point --*/
        free(p);
    }
    return h;
}


其他函数

void print_val(node *h)
{
    node *p;
    int n;
    int i;
    n = h->val;
    if (n<=0) {   /* 小数点到最近一个非零小数位中间有多个0,浮点数小于1*/
        printf("0.");
        while(n++<0)
            printf("0");
    }

    /* 打印 */
    n = h->val;
    for (p=h->next; p!=NULL; p=p->next) {
        printf("%d", p->val);
        if (--n == 0 && p->next != NULL) /* 浮点数大于1 */
            printf(".");
    }
    printf("\n");
}

/* 初始化一个浮点数

 * 可以在这里添加处理无用0,减少后面的计算

 */
node *init(char *str)
{
    node *h,*p;
    int flag,point;

    if ((h=(node *)malloc(sizeof(node))) == NULL) exit(1);
    h->next = NULL;
    flag = 0;
    point = 0;
    while (*str) {
        if (*str == '.')
            flag = 1;
        else {
            if ((p=(node *)malloc(sizeof(node))) == NULL) exit(1);
            p->next = h->next;
            h->next = p;
            p->val = *str - '0';/* base above 10 need further process */
            if (flag) point ++;
        }
        str ++;
    }
    h->val = point;

    return h;
}



测试

int main()
{
    char *str1 = "0.12345678";
    char *str2 = "0.000012345678";
    node *h1,*h2;
    node *h;

    h1 = init(str1);
    h2 = init(str2);
    h = multiply(h1, h2);
    h = reverse(h);
    print_val(h);
    return 0;
}
阅读(2126) | 评论(0) | 转发(0) |
0

上一篇:大数乘法(二)

下一篇:使用树状数组 求和

给主人留下些什么吧!~~