| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
ADSL如何秒变专线,公网IP盒子了解一下千里之行 始于足下
共有 666 人关注过本帖
标题:设计一个长整数运算求和问题
只看楼主 加入收藏
zhuang1101
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-11
结帖率:0
  已结贴   问题点数:20  回复次数:3   
设计一个长整数运算求和问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//mat97
//以下是双链表的节点结构,每个节点存储一个4位的数,比如1,0031,0056存入链表后就是1,31,56三个节,输出的时候再补0输出!
typedef struct node{
    int n;
    struct node *next;
    struct node *prev;
} node;
node *p;
char num1[1024],num2[1024];
int conv(char *a)
{
    int n=0,i;
    for(i=0;a[i];++i)
    {
        n*=10;
        n+=(a[i]-'0');
    }
    return n;
}
int main()
{
    char c[2];
    int i,f;
    node *q;
    p=(node*)malloc(sizeof(node));
    p->next=p->prev=0;
    q=p;
    num1[0]=num2[0]=',';
    printf("Enter num 1:\n");
    scanf("%s",num1+1);
    for(i=strlen(num1);i>=0;--i)
    {
        if(num1[i]==',')
        {
            num1[i]=0;
            q->next=(node*)malloc(sizeof(node));
            q->next->prev=q;
            q->next->next=0;
            q=q->next;
            q->n=conv(num1+i+1);
        }
    }
    q->next=p;
    p->prev=q;
    printf("Enter op:\n");
    scanf("%s",c);
    *c=*c=='+'?0:1;
    printf("Enter num 2:\n");
    scanf("%s",num2+1);
    q=p;f=0;
    if(!*c) //+
    {
        for(i=strlen(num2);i>=0;--i)
        {
            if(num2[i]==',')
            {
                num2[i]=0;
                if(q->next==p)
                {
                    q->next=(node*)malloc(sizeof(node));
                    q->next->next=p;
                    q->next->prev=q;
                    q->next->n=0;
                    p->prev=q->next;
                }
                q=q->next;
                q->n+=(conv(num2+i+1)+f);
                if(q->n<10000)
                    f=0;
                else
                {
                    f=1;
                    q->n-=10000;
                }
            }
        }
        if(f)
        {
            if(q->next==p)
            {
                q->next=(node*)malloc(sizeof(node));
                q->next->next=p;
                q->next->prev=q;
                q->next->n=1;
            }
            else
            {
                while(q->next!=p)
                {
                    q=q->next;
                    q->n+=1;
                    if(q->n<10000)
                    {
                        f=0;
                        break;
                    }
                    else
                    {
                        q->n=0;
                        f=1;
                    }
                }
                if(f)
                {
                    q->next=(node*)malloc(sizeof(node));
                    q->next->next=p;
                    q->next->prev=q;
                    q->next->n=1;
                }
            }
        }
        printf("%d,",p->prev->n);
        for(q=p->prev->prev;q!=p;q=q->prev)
            printf("%04d,",q->n);
    }
    else //-
    {
        for(i=strlen(num2);i>=0;--i)
        {
            if(num2[i]==',')
            {
                num2[i]=0;
                if(q->next==p)
                {
                    q->next=(node*)malloc(sizeof(node));
                    q->next->next=p;
                    q->next->prev=q;
                    q->next->n=0;
                    p->prev=q->next;
                }
                q=q->next;
                q->n-=(conv(num2+i+1)+f);
                if(q->n>=0)
                    f=0;
                else
                {
                    f=1;
                    q->n+=10000;
                }
            }
        }
        if(f)
        {
            if(q->next==p)
            {
                q->n-=10000;
            }
            else
            {
                while(q->next!=p)
                {
                    q=q->next;
                    q->n-=1;
                    if(q->n>=0)
                    {
                        f=0;
                        break;
                    }
                    else
                    {
                        q->n+=10000;
                        f=1;
                    }
                }
                if(f)
                {
                    q->n-=10000;
                }
            }
        }
        printf("%d,",p->prev->n);
        for(q=p->prev->prev;q!=p;q=q->prev)
            printf("%04d,",q->n);
    }
    return 0;
}
//这是在网上搜的源代码  有错误 但找不到原因  向大神求解
搜索更多相关主题的帖子: include return 
2013-12-12 13:08
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:166
帖 子:6794
专家分:42379
注 册:2010-12-16
  得分:7 
你能看懂这些算法吗?

我行我乐
我的博客:
http://blog.yuccn. net
2013-12-16 20:43
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
  得分:7 
看了看,有意思,不过不理解题意

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-12-18 10:36
菜鸟学习篇
Rank: 2
等 级:论坛游民
帖 子:8
专家分:39
注 册:2013-12-18
  得分:7 
你能说明哪里有错误吗?我刚才运行,编译,测试都正常输出 没有问题哦,你遇到了什么问题
2013-12-18 20:55







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022537 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved