C 大整数加法计算实现

```
#include<string.h>
#include<stdio.h>
#include<stdlib.h>

#define N 100

void nizhi_str(char str[])
{
    int len;
    int i;
    char temp;
    len = strlen(str);
    for (i = 0; i < len / 2; i++)
    {
        temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}

void sum(char str1[], char str2[], char result[])
{
    //逆置
    nizhi_str(str1);
    nizhi_str(str2);

    int len1, len2;
    int i;
    int jinwei = 0;
    int temp;
    len1 = strlen(str1);
    len2 = strlen(str2);

    for (i = 0; i < len1 && i < len2; i++)
    {
        temp = (str1[i] - '0' + str2[i] - '0') % 10;
        result[i] = temp + jinwei + '0';
        if (str1[i] - '0' + str2[i] - '0' >= 10)
        {
            jinwei = 1;
        }
        else
        {
            jinwei = 0;
        }
    }


    if (i < len1)
    {
        for (; i < len1; i++)
        {
            temp = (str1[i] - '0') % 10;
            result[i] = temp + jinwei + '0';

            if (str1[i] - '0' >= 10)
            {
                jinwei = 1;
            }
            else
            {
                jinwei = 0;
            }

        }
    }

    if (i < len2)
    {
        for (; i < len2; i++)
        {
            temp = (str2[i] - '0') % 10;
            result[i] = temp + jinwei + '0';

            if (str2[i] - '0' >= 10)
            {
                jinwei = 1;
            }
            else
            {
                jinwei = 0;
            }

        }
    }
    if (jinwei == 1)
    {
        result[i++] = '1';  
    }
    result[i] = '\0';
    nizhi_str(result);
}

void main()
{
    char str1[100] = { 0 };
    char str2[100] = { 0 };
    char result[100];

    printf("input number1:\n");
    gets(str1);
    printf("input number2:\n");
    gets(str2);
    printf("number1 = %s\n", str1);
    printf("number2 = %s\n", str2);

    sum(str1, str2, result);
    printf("result  = %s\n", result);
    system("pause");
}

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值