吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 579|回复: 1
收起左侧

[C&C++ 原创] 大数加法之无符号加法

[复制链接]
wangarc 发表于 2024-9-18 18:55
本段测试程序是人肉直接输入参数。也可以用以下函数获得:
1. 判断字符串是否为一个有效的数

本段程序需要调用以下函数:
1. 两个十进制字符相加得到十进制字符的结果和进位

[C] 纯文本查看 复制代码

int add_ip_dp(char * str1, int dot_pos1, int i_qty1, int d_qty1,
			  char * str2, int dot_pos2, int i_qty2, int d_qty2,
			  char * str3, int str3_array_size, int * offset);
/*  add_ip_dp() return 1 if no error, otherwise 0
 *  add integral part and decimal part (str3 = str1 + str2)
 *  dot_pos: index of str where '.' located for decimal number
 *							or '\0' located for integral number.
 *	i_qty: integral number quantity (integral part length)
 *	d_qty: decimal number quantity (decimal part length)
 *	str3: array to store result
 *	str3_array_size: array size
 *	offset: result will start from here (this function write result from tail to head)
 */

int main(void) //testing
{
	char * s1 = "0";
	char * s2 = "99999999999999999999";
	char * s3 = ".00000000000000000001";
	char * s4 = "1.00";
	char * s5 = "3.14159";
	char * s6 = "0003.14159000";
	char * s7 = "9999999999.9999999999";
	char str[128];
	int i, r;
	r = add_ip_dp(s1, 1, 1, 0, s2, 20, 20, 0, str, 128, &i);
	printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s1, s2, str + i);
	r = add_ip_dp(s2, 20, 20, 0, s3, 0, 0, 20, str, 128, &i);
	printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s2, s3, str + i);
	r = add_ip_dp(s3, 0, 0, 20, s4, 1, 1, 2, str, 128, &i);
	printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s3, s4, str + i);
	r = add_ip_dp(s4, 1, 1, 2, s5, 1, 1, 5, str, 128, &i);
	printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s4, s5, str + i);
	r = add_ip_dp(s5, 1, 1, 5, s6, 4, 4, 8, str, 128, &i);
	printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s5, s6, str + i);
	r = add_ip_dp(s6, 4, 4, 8, s7, 10, 10, 10, str, 128, &i);
	printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s6, s7, str + i);
	return 0;
}

int add_ip_dp(char * str1, int dot_pos1, int i_qty1, int d_qty1,
			  char * str2, int dot_pos2, int i_qty2, int d_qty2,
			  char * str3, int str3_array_size, int * offset)
{
	//return 1 = no error, 0 = fail.
	char * p1 = str1 + dot_pos1;	// p1 is a pointer to '.' or '\0' of str1
	char * p2 = str2 + dot_pos2;	// p2 is a pointer to '.' or '\0' of str2
	int i = str3_array_size;		// i is an index of str3, copy to *offset finally
	char carry = '0';				// carry is a carry
	char c1, c2, c3;				// c1, c2 and c3 are temp chars
	int k, j;						// k and j are temp index
	
	// set '\0' at the end of str3
	if (--i >= 0)
		str3[i] = '\0';
	else
		return 0;	//Error: insufficient space in str3

	// add decimal part: from tail to head
	k = (d_qty1 > d_qty2) ? d_qty1 : d_qty2;
	while (k > 0)
	{
		c1 = (k > d_qty1) ? '0' : p1[k];
		c2 = (k > d_qty2) ? '0' : p2[k];
		if (0 == dec_char_add(carry, c1, c2, &carry, &c3))
			return 0;		//Error: return error from dec_char_add()
		
		if (--i >= 0)
			str3[i] = c3;
		else
			return 0;		//Error: insufficient space in str3
		
		if (str3[i+1] == '\0' && str3[i] == '0')
			i++;			//ignore '0' in the end of decimal
		
		--k;
	}
	
	// put a '.' in the string
	if (str3[i] != '\0')
	{
		if (--i >= 0)
			str3[i] = '.';
		else
			return 0;		//Error: insufficient space in str3
	}

	// add integral part: from tail to head
	k = 1;
	j = (i_qty1 > i_qty2) ? i_qty1 : i_qty2;
	while (k <= j)
	{
		c1 = (k > i_qty1) ? '0' : p1[-k];
		c2 = (k > i_qty2) ? '0' : p2[-k];
		if (0 == dec_char_add(carry, c1, c2, &carry, &c3))
			return 0;		//Error: return error from dec_char_add()
		
		if (--i >= 0)
			str3[i] = c3;
		else
			return 0;		//Error: insufficient space in str3
		
		++k;
	}

	// consider carry after last dec_char_add()
	if (carry == '1')
	{
		if (--i >= 0)
			str3[i] = '1';
		else
			return 0;		//Error: insufficient space in str3
	}

	// ignore all begining '0' in the string
	while (str3[i] == '0')
		++i;

	// add a '0' if start with a '.'
	if (str3[i] == '.')
	{
		if (--i >= 0)
			str3[i] = '0';
		else
			return 0;		//Error: insufficient space in str3
	}

	// add a '0' if empty string
	if (str3[i] == '\0')
	{
		if (--i >= 0)
			str3[i] = '0';
		else
			return 0;		//Error: insufficient space in str3
	}

	// set *offset for str3
	*offset = i;
	return 1;	//Done
}

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

jacklemona 发表于 2024-9-18 19:12
感谢分享~
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 12:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表