当前位置: 代码迷 >> C语言 >> 两个大数相加
  详细解决方案

两个大数相加

热度:645   发布时间:2007-12-17 21:51:23.0
两个大数相加
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#define MAX 400
void my_read(int *a) //对操作数进行处理(倒序排列数组,便于运算)
{
    int len,i,flag=0;
    char s[MAX];
aa:
    while(1)
    {
        scanf("%s",&s);
        len=strlen(s);
        if (s[0]=='-'){
            printf("输入错误:操作数不为负数");
            goto aa;
        }
        for(i=len;i>=1;i--)
        {
            if ((s[len-i]-'0')<0||(s[len-i]-'0')>9)
            {
                printf("\t\t\t你输入的第%d位不合法,按任意键重新输!!",len-i+1);
                getchar();
                goto aa;
            }
            else
                a[i]=s[len-i]-'0';
        }
        a[0]=len;
        break;
    }
    return;
}

void my_print(int *a) //输出时把数组倒过来就是答案
{
    int i;
    if (a[0]<0) printf("-");
    if (a[0]==0)
    {
        printf("0");
        return;
    }
    for(i=abs(a[0]);i>=1;i--)
        printf("%d",a[i]);
    printf("\n");
}

void format(int *a) //当两个字符相加时超过10时
{
    int p;
    for(p=1;p<a[0]||a[p]>=10;p++)
    {
        if (p>=a[0]) a[p+1]=0;
        a[p+1]+=a[p]/10;
        a[p]=a[p]%10;
    }
    if (p>=a[0]) a[0]=p;
    return;
}
void add(int *a,int *b,int *c) //加法的运算
{
    int len,i;
    if (a[0]<b[0]) len=a[0];
    else len=b[0];
    for(i=1;i<=len;i++)
        c[i]=a[i]+b[i];
    if (len<a[0])
    {
        for (;i<=a[0];i++)
            c[i]=a[i];
        c[0]=a[0];
    }
    else
    {
        for(;i<=b[0];i++)
        {
            c[i]=b[i];
        }
        c[0]=b[0];
    }
    format (c);
}



void format1(int *a) //减法 相减为负时
{
    int i;
    for (i=1;i<=a[0];i++)
        if (a[i]<0)
        {   
            a[i]+=10;
            a[i+1]-=1;
        }
}

void sub_1(int *a,int *b,int *c)  
{
    int i;
    for (i=1;i<=b[0];i++)
        c[i]=a[i]-b[i];
    if (a[0]==b[0]) goto loop;
    for (;i<=a[0];i++)
        c[i]=a[i];
loop:
    c[0]=a[0];
    format1(c);
    i=c[0];
    while(1)
        if (c[i]==0) i--;
        else break;
        c[0]=i;
        return;
}


void sub(int *a,int *b,int *c)
{
    int i,flag=0;
    for (i=1;i<=a[0];i++)
        if (a[i]!=b[i]) { flag=1; break; }
        if (flag==0) {
            c[0]=0;
            return;
        }
        flag=0;
        if (a[0]==b[0]) {
            i=a[0];
            while(i>0)
            {
                if (a[i]>b[i]){
                    sub_1(a,b,c);
                    break;
                }
                if (a[i]<b[i]){
                    flag=1;
                    sub_1(b,a,c);
                    break;
                }
                i--;
            }
        }
        if (a[0]>b[0])
            sub_1(a,b,c);
        else
            if (a[0]<b[0])
            {
                flag=1;
                sub_1(b,a,c);
            }
            if (flag)
                c[0]=-c[0];
            else
                c[0]=c[0];
}
void main()
{
    int a[MAX],b[MAX],c[MAX];
    char yn,eq;



strat:   printf("请输入表达式(操作数和操作符之间需空格):\n");
         my_read(a);
         scanf("%s",&yn);
         my_read(b);
         scanf("%s",&eq);
         if (yn=='+'&&eq=='=')
         {
             add(a,b,c);
             printf("\n\t\t\t");
             printf("A=");
             my_print(a);
             printf("\n\t\t\t");
             printf("B=");
             my_print(b);
             printf("\n\t\t\t");
             printf("C=A+B=");
             my_print(c);
             goto loop;
         }
         if (yn=='-'&&eq=='=')
         {
             sub(a,b,c);
             printf("A=");
             my_print(a);
             printf("\nB=");
             my_print(b);
             printf("\nC=A-B=");
             my_print(c);
             goto loop;
         }
         else{ printf("输入错误");
         goto loop;
         }
loop:
         while(1)
         {
             printf("\n\t\t\t继续计算吗?(y/n)");
             yn=getchar();
             if (yn=='y') goto strat;
             if (yn=='n') return;
         }
}   
这个输入的时候运算符左右需加空格(比如1 + 1 =),如果我不想加空格(1+1=),那要怎么办?谢谢啦
搜索更多相关的解决方案: 大数  相加  

----------------解决方案--------------------------------------------------------
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <fstream.h>*/
#define MAX 400


void my_print(int *a) /* 输出时把数组倒过来就是答案 */
{
    int i;
    if (a[0]<0) printf("-");
    if (a[0]==0)
    {
        printf("0");
        return;
    }
    for(i=abs(a[0]);i>=1;i--)
        printf("%d",a[i]);
    printf("\n");
}

void format(int *a) /* 当两个字符相加时超过10时 */
{
    int p;
    for(p=1;p<a[0]||a[p]>=10;p++)
    {
        if (p>=a[0]) a[p+1]=0;
        a[p+1]+=a[p]/10;
        a[p]=a[p]%10;
    }
    if (p>=a[0]) a[0]=p;
    return;
}
void add(int *a,int *b,int *c) /* 加法的运算 */
{
    int len,i;
    if (a[0]<b[0]) len=a[0];
    else len=b[0];
    for(i=1;i<=len;i++)
        c[i]=a[i]+b[i];
    if (len<a[0])
    {
        for (;i<=a[0];i++)
            c[i]=a[i];
        c[0]=a[0];
    }
    else
    {
        for(;i<=b[0];i++)
        {
            c[i]=b[i];
        }
        c[0]=b[0];
    }
    format (c);
}



void format1(int *a) /* 减法 相减为负时 */
{
    int i;
    for (i=1;i<=a[0];i++)
        if (a[i]<0)
        {   
            a[i]+=10;
            a[i+1]-=1;
        }
}

void sub_1(int *a,int *b,int *c)  
{
    int i;
    for (i=1;i<=b[0];i++)
        c[i]=a[i]-b[i];
    if (a[0]==b[0]) goto loop;
    for (;i<=a[0];i++)
        c[i]=a[i];
loop:
    c[0]=a[0];
    format1(c);
    i=c[0];
    while(1)
        if (c[i]==0) i--;
        else break;
        c[0]=i;
        return;
}


void sub(int *a,int *b,int *c)
{
    int i,flag=0;
    for (i=1;i<=a[0];i++)
        if (a[i]!=b[i]) { flag=1; break; }
        if (flag==0) {
            c[0]=0;
            return;
        }
        flag=0;
        if (a[0]==b[0]) {
            i=a[0];
            while(i>0)
            {
                if (a[i]>b[i]){
                    sub_1(a,b,c);
                    break;
                }
                if (a[i]<b[i]){
                    flag=1;
                    sub_1(b,a,c);
                    break;
                }
                i--;
            }
        }
        if (a[0]>b[0])
            sub_1(a,b,c);
        else
            if (a[0]<b[0])
            {
                flag=1;
                sub_1(b,a,c);
            }
            if (flag)
                c[0]=-c[0];
            else
                c[0]=c[0];
}
int main()
{
    int a[MAX],b[MAX],c[MAX],i,j,len;
    char yn,eq,s[2*MAX];



start:   

        printf("请输入表达式:\n");
         scanf("%s",s);
         len=strlen(s);
         for(i=0;i<len;i++)
            if(s[i]=='+'||s[i]=='-')
                j=i;

        for(i=0;i<j;i++)
             a[i+1]=s[j-i-1]-'0';
        a[0]=j;
        
         for(i=0;i<len-j-2;i++)
             b[i+1]=s[len-i-2]-'0';
         b[0]=len-j-2;
        
         yn=s[j];
         eq=s[len-1];

         if (yn=='+'&&eq=='=')
         {
             add(a,b,c);
             printf("\n\t\t\t");
             printf("A=");
             my_print(a);
             printf("\n\t\t\t");
             printf("B=");
             my_print(b);
             printf("\n\t\t\t");
             printf("C=A+B=");
             my_print(c);
             goto loop;
         }
         if (yn=='-'&&eq=='=')
         {
             sub(a,b,c);
             printf("A=");
             my_print(a);
             printf("\nB=");
             my_print(b);
             printf("\nC=A-B=");
             my_print(c);
             goto loop;
         }
         else{ printf("输入错误");
         goto loop;
         }
loop:
         while(1)
         {
             printf("\n\t\t\t继续计算吗?(y/n)");
             yn=getchar();
             if (yn=='y') goto start;
             if (yn=='n') return;
         }
        return 0;
}
本帖最近评分记录

丛丛芳芫满冬秋,淡淡一笑泯恩仇!
2007-12-18 01:07:15
许一民

来 自:江苏连云港
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-29
  得分:0 
对您的程序进行了简单的修改,基本上也算满足要求了。
----------------解决方案--------------------------------------------------------
#include <stdio.h>
int main()
{
    char a[100],b[100],c;
    int i=0,j=0,m,n,flag=0;
    while((c=getchar())!=' ')
    {
        a[i]=c;
        i++;
    }
    a[i]='\0';
    while((c=getchar())!='\n')
    {
        b[j]=c;
        j++;
    }
    b[j]='\0';
    i--;
    j--;
    if(i>=j)
    {
        for(;j>=0;j--)
        {
            m=a[i]-'0'+b[j]-'0'+flag;
            n=m%10;
            a[i]='0'+n;
            flag=m/10;
            i--;
        }
        for(;i>=0;i--)
        {
            if(!flag)
                break;
            m=a[i]-'0'+flag;
            n=m%10;
            a[i]='0'+n;
            flag=m/10;
        }
        if(flag)
            printf("%d",flag);
        puts(a);
    }
    else
    {
        for(;i>=0;i--)
        {
            m=a[i]-'0'+b[j]-'0'+flag;
            n=m%10;
            b[j]='0'+n;
            flag=m/10;
            j--;
        }
        for(;j>=0;j--)
        {
            if(!flag)
                break;
            m=b[j]-'0'+flag;
            n=m%10;
            b[j]='0'+n;
            flag=m/10;
        }
        if(flag)
            printf("%d",flag);
        puts(b);
    }
    getchar();
    return 0;
}
大数相加(不超过100位,需要更大的改点点就可以)if语句里两部分基本一样,可以用个函数写出来。这样程序更短点!
----------------解决方案--------------------------------------------------------
谢谢两位了 ,唉,学了一个学期了竟然连这都不会,请问 有什么好的方法?
----------------解决方案--------------------------------------------------------
不要急,慢慢来,多思考,多练习吧!
----------------解决方案--------------------------------------------------------
  相关解决方案