大数加减法

在计算机中,由于处理器位宽限制,只能处理有限精度的十进制整数加减法,比如在32位宽处理器计算机中,参与运算的操作数和结果必须在-231~231-1之间。如果需要进行更大范围的十进制整数加法,需要使用特殊的方式实现,比如使用字符串保存操作数和结果,采取逐位运算的方式。如下:
9876543210 + 1234567890 = ?   让字符串 num1="9876543210", 字符串 num2="1234567890", 结果保存在字符串 result = "11111111100"。
-9876543210 + -1234567890 = ? 让字符串 num1="-9876543210",字符串 num2="-1234567890",结果保存在字符串 result = "-11111111100"。

要求编程实现上述高精度的十进制加法。
要求实现方法:
public String add (String num1, String num2)
【输入】num1:字符串形式操作数1,如果操作数为负,则num1的前缀为符号位'-'
num2:字符串形式操作数2,如果操作数为负,则num2的前缀为符号位'-'
【返回】保存加法计算结果字符串,如果结果为负,则字符串的前缀为'-'
注:
(1)当输入为正数时,'+'不会出现在输入字符串中;当输入为负数时,'-'会出现在输入字符串中,且一定在输入字符串最左边位置;
(2)输入字符串所有位均代表有效数字,即不存在由'0'开始的输入字符串,比如"0012", "-0012"不会出现;
(3)要求输出字符串所有位均为有效数字,结果为正或0时'+'不出现在输出字符串,结果为负时输出字符串最左边位置为'-'。

代码如下:

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

#define N 1000

/*去除前导0*/
int removeHeadZero(char *result, int lenRes)
{
	int i;
	
	for (i=lenRes; result[i]!='\0'; ++i)
	{
		if (result[i] != '0')
		{
			break;
		}
	}
	
	return i;
}

void bigMinusSmall(char *big, char *small, char sign)
{
	int i, j, numBig, numSmall;
	int borrow = 0;
	int lenBig = strlen(big);
	int lenSmall = strlen(small);
	int lenRes = (lenBig > lenSmall) ? lenBig+2: lenSmall+2; /*减法位数不会增加,外加一个'\0'和负号'-'*/
	char *result = (char *)malloc(sizeof(char)*lenRes);
	
	result[--lenRes] = '\0';
	
	for (i=lenBig-1, j=lenSmall-1; j>=0; --i, --j)
	{	
		numBig = big[i] - '0';
		numSmall = small[j] - '0';
		
		if (numBig-borrow < numSmall)
		{
			result[--lenRes] = (10 + numBig - borrow - numSmall) + '0';
			borrow = 1;
		}
		else
		{
			result[--lenRes] = (numBig - borrow - numSmall) + '0';
			borrow = 0;	
		}
	}
	
	for (; i>=0; --i)
	{
		numBig = big[i] - '0';
		
		if (numBig - borrow < 0)
		{
			result[--lenRes] = (10 + numBig - borrow)+ '0';
			borrow = 1;
		}
		else	
		{
			result[--lenRes] = (numBig - borrow)+ '0';
			borrow = 0;
		}
	}
	
	lenRes = removeHeadZero(result, lenRes);
	
	if (sign == '-')
	{
		result[--lenRes] = '-';
	}

	printf("%s\n", result+lenRes);
	
	free(result);
	result = NULL;
}

void minus(char *negative, char *positive)
{
	int cmp;
	int lenNeg = strlen(negative);
	int lenPos = strlen(positive);
	
	if (lenNeg == lenPos)
	{
		cmp = strcmp(negative, positive);
	}
	else if (lenNeg > lenPos)
	{
		cmp = 1;
	}
	else
	{
		cmp = -1;
	}

	if (0 == cmp)
	{
		printf("0\n");
	}
	else if (cmp > 0) /*negative大*/
	{
		bigMinusSmall(negative, positive, '-');
	}
	else if (cmp < 0) /*positive大*/
	{
		bigMinusSmall(positive, negative, '+');
	}
}

void add(char *strA, char *strB, char sign)
{
	int i, j, tmp;
	int carry = 0;
	int lenA = strlen(strA);
	int lenB = strlen(strB);
	int lenRes = (lenA > lenB) ? lenA+3: lenB+3; /*结果最多产生一位进位,外加一个'\0'和符号位*/
	char *result = (char *)malloc(sizeof(char)*lenRes);
	
	result[--lenRes] = '\0';
	
	for (i=lenA-1, j=lenB-1; i>=0 && j>=0; --i,--j)
	{
		tmp = strA[i] - '0' + strB[j] - '0' + carry;
		carry = tmp / 10;
		result[--lenRes] = (tmp % 10) + '0';
	}
	
	for (; i>=0; --i)
	{
		tmp = strA[i] - '0' + carry;
		carry = tmp / 10;
		result[--lenRes] = (tmp % 10) + '0';
	}
	
	for (; j>=0; --j)
	{
		tmp = strB[j] - '0' + carry;
		carry = tmp / 10;
		result[--lenRes] = (tmp % 10) + '0';
	}
	
	if (carry != 0)
	{
		result[--lenRes] = carry + '0';
	}
	
	if (sign == '-')
	{
		result[--lenRes] = '-';
		printf("%s\n", result+lenRes);
	}
	else
	{
		printf("%s\n", result+lenRes);
	}

	free(result);
	result = NULL;
}

int main(void)
{
	char strA[N], strB[N];
	
	while (scanf("%s%s", strA, strB) != EOF)
	{
		if (strA[0]=='-' && strB[0]=='-') /*都是负数时*/
		{
			add(strA+1, strB+1, '-'); /*从1开始是为了跳过'-'*/
		}
		else if (strA[0] == '-')
		{
			minus(strA+1, strB); /*第一个参数是负数串*/
		}
		else if (strB[0] == '-')
		{
			minus(strB+1, strA);
		}
		else /*都是正数时*/
		{
			add(strA, strB, '+'); /*都是正数*/
		}
	}
	
	return 0;
}


 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值