加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数加减法

发布时间:2020-12-14 03:28:09 所属栏目:大数据 来源:网络整理
导读:在计算机中,由于处理器位宽限制,只能处理有限精度的十进制整数加减法,比如在32位宽处理器计算机中,参与运算的操作数和结果必须在-231~231-1之间。如果需要进行更大范围的十进制整数加法,需要使用特殊的方式实现,比如使用字符串保存操作数和结果,采取

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

要求编程实现上述高精度的十进制加法。
要求实现方法:
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]!=''; ++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; /*减法位数不会增加,外加一个''和负号'-'*/
	char *result = (char *)malloc(sizeof(char)*lenRes);
	
	result[--lenRes] = '';
	
	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("%sn",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("0n");
	}
	else if (cmp > 0) /*negative大*/
	{
		bigMinusSmall(negative,positive,'-');
	}
	else if (cmp < 0) /*positive大*/
	{
		bigMinusSmall(positive,negative,'+');
	}
}

void add(char *strA,char *strB,tmp;
	int carry = 0;
	int lenA = strlen(strA);
	int lenB = strlen(strB);
	int lenRes = (lenA > lenB) ? lenA+3: lenB+3; /*结果最多产生一位进位,外加一个''和符号位*/
	char *result = (char *)malloc(sizeof(char)*lenRes);
	
	result[--lenRes] = '';
	
	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("%sn",result+lenRes);
	}
	else
	{
		printf("%sn",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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读