吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 693|回复: 2
收起左侧

[C&C++ 原创] 两个十进制字符相减得到十进制字符的结果和借位

[复制链接]
wangarc 发表于 2024-9-13 18:40
本帖最后由 wangarc 于 2024-9-14 16:46 编辑

1. 两个十进制字符相加得到十进制字符的结果和进位

上一次完成了相加,这一次改成相减。两个十进制字符,相减得到十进制字符的结果和借位。解释已写在注释里了。

[C] 纯文本查看 复制代码
int dec_char_sub(char in_borrow, char c1, char c2, char * out_borrow, char * result);
/*	dec_char_sub() return 1 if no error, otherwise 0
 *
 *			['0'-'1']
 *			out_borrow	c1			['0'-'9']				'1'	'6'				'0'	'6'	
 *		-				c2			['0'-'9']			-		'8'			-		'2'	
 *		-				in_borrow	['0'-'1']			-		'1'			-		'1'	
 *	_________________________________________		________________	________________
 *		=				result							=		'7'			=		'3'		
 *						['0'-'9']
 */

int dec_char_sub(char in_borrow, char c1, char c2, char * out_borrow, char * result)
{
	int ret_val = 1; //return value, 1 = no error, otherwise 0
	switch (c1)		//go through "switch case" to get answer without calculate.
	{
		case '0':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '0'; break;
						case '1': *out_borrow = '1'; *result = '9'; break;
						case '2': *out_borrow = '1'; *result = '8'; break;
						case '3': *out_borrow = '1'; *result = '7'; break;
						case '4': *out_borrow = '1'; *result = '6'; break;
						case '5': *out_borrow = '1'; *result = '5'; break;
						case '6': *out_borrow = '1'; *result = '4'; break;
						case '7': *out_borrow = '1'; *result = '3'; break;
						case '8': *out_borrow = '1'; *result = '2'; break;
						case '9': *out_borrow = '1'; *result = '1'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '1':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '1'; break;
						case '1': *out_borrow = '0'; *result = '0'; break;
						case '2': *out_borrow = '1'; *result = '9'; break;
						case '3': *out_borrow = '1'; *result = '8'; break;
						case '4': *out_borrow = '1'; *result = '7'; break;
						case '5': *out_borrow = '1'; *result = '6'; break;
						case '6': *out_borrow = '1'; *result = '5'; break;
						case '7': *out_borrow = '1'; *result = '4'; break;
						case '8': *out_borrow = '1'; *result = '3'; break;
						case '9': *out_borrow = '1'; *result = '2'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '2':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '2'; break;
						case '1': *out_borrow = '0'; *result = '1'; break;
						case '2': *out_borrow = '0'; *result = '0'; break;
						case '3': *out_borrow = '1'; *result = '9'; break;
						case '4': *out_borrow = '1'; *result = '8'; break;
						case '5': *out_borrow = '1'; *result = '7'; break;
						case '6': *out_borrow = '1'; *result = '6'; break;
						case '7': *out_borrow = '1'; *result = '5'; break;
						case '8': *out_borrow = '1'; *result = '4'; break;
						case '9': *out_borrow = '1'; *result = '3'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '3':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '3'; break;
						case '1': *out_borrow = '0'; *result = '2'; break;
						case '2': *out_borrow = '0'; *result = '1'; break;
						case '3': *out_borrow = '0'; *result = '0'; break;
						case '4': *out_borrow = '1'; *result = '9'; break;
						case '5': *out_borrow = '1'; *result = '8'; break;
						case '6': *out_borrow = '1'; *result = '7'; break;
						case '7': *out_borrow = '1'; *result = '6'; break;
						case '8': *out_borrow = '1'; *result = '5'; break;
						case '9': *out_borrow = '1'; *result = '4'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '4':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '4'; break;
						case '1': *out_borrow = '0'; *result = '3'; break;
						case '2': *out_borrow = '0'; *result = '2'; break;
						case '3': *out_borrow = '0'; *result = '1'; break;
						case '4': *out_borrow = '0'; *result = '0'; break;
						case '5': *out_borrow = '1'; *result = '9'; break;
						case '6': *out_borrow = '1'; *result = '8'; break;
						case '7': *out_borrow = '1'; *result = '7'; break;
						case '8': *out_borrow = '1'; *result = '6'; break;
						case '9': *out_borrow = '1'; *result = '5'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '5':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '5'; break;
						case '1': *out_borrow = '0'; *result = '4'; break;
						case '2': *out_borrow = '0'; *result = '3'; break;
						case '3': *out_borrow = '0'; *result = '2'; break;
						case '4': *out_borrow = '0'; *result = '1'; break;
						case '5': *out_borrow = '0'; *result = '0'; break;
						case '6': *out_borrow = '1'; *result = '9'; break;
						case '7': *out_borrow = '1'; *result = '8'; break;
						case '8': *out_borrow = '1'; *result = '7'; break;
						case '9': *out_borrow = '1'; *result = '6'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '6':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '6'; break;
						case '1': *out_borrow = '0'; *result = '5'; break;
						case '2': *out_borrow = '0'; *result = '4'; break;
						case '3': *out_borrow = '0'; *result = '3'; break;
						case '4': *out_borrow = '0'; *result = '2'; break;
						case '5': *out_borrow = '0'; *result = '1'; break;
						case '6': *out_borrow = '0'; *result = '0'; break;
						case '7': *out_borrow = '1'; *result = '9'; break;
						case '8': *out_borrow = '1'; *result = '8'; break;
						case '9': *out_borrow = '1'; *result = '7'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '7':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '7'; break;
						case '1': *out_borrow = '0'; *result = '6'; break;
						case '2': *out_borrow = '0'; *result = '5'; break;
						case '3': *out_borrow = '0'; *result = '4'; break;
						case '4': *out_borrow = '0'; *result = '3'; break;
						case '5': *out_borrow = '0'; *result = '2'; break;
						case '6': *out_borrow = '0'; *result = '1'; break;
						case '7': *out_borrow = '0'; *result = '0'; break;
						case '8': *out_borrow = '1'; *result = '9'; break;
						case '9': *out_borrow = '1'; *result = '8'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '8':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '8'; break;
						case '1': *out_borrow = '0'; *result = '7'; break;
						case '2': *out_borrow = '0'; *result = '6'; break;
						case '3': *out_borrow = '0'; *result = '5'; break;
						case '4': *out_borrow = '0'; *result = '4'; break;
						case '5': *out_borrow = '0'; *result = '3'; break;
						case '6': *out_borrow = '0'; *result = '2'; break;
						case '7': *out_borrow = '0'; *result = '1'; break;
						case '8': *out_borrow = '0'; *result = '0'; break;
						case '9': *out_borrow = '1'; *result = '9'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '9':	switch (c2)
					{
						case '0': *out_borrow = '0'; *result = '9'; break;
						case '1': *out_borrow = '0'; *result = '8'; break;
						case '2': *out_borrow = '0'; *result = '7'; break;
						case '3': *out_borrow = '0'; *result = '6'; break;
						case '4': *out_borrow = '0'; *result = '5'; break;
						case '5': *out_borrow = '0'; *result = '4'; break;
						case '6': *out_borrow = '0'; *result = '3'; break;
						case '7': *out_borrow = '0'; *result = '2'; break;
						case '8': *out_borrow = '0'; *result = '1'; break;
						case '9': *out_borrow = '0'; *result = '0'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		default:	ret_val = 1; //Error: c1 out of range
	}
	switch (in_borrow)	//go through "switch case" to get answer without calculate.
	{
		case '0':	break;
		case '1':	switch (*result)
					{
						case '0': *out_borrow = '1'; *result = '9'; break;
						case '1': *result = '0'; break;
						case '2': *result = '1'; break;
						case '3': *result = '2'; break;
						case '4': *result = '3'; break;
						case '5': *result = '4'; break;
						case '6': *result = '5'; break;
						case '7': *result = '6'; break;
						case '8': *result = '7'; break;
						case '9': *result = '8'; break;
					}
					break;
		default:	ret_val = 0; //Error: in_carry out of range
	}
	if (ret_val != 1)
	{
		*out_borrow = '0';
		*result = '0';
	}
	return ret_val;
}

免费评分

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

查看全部评分

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

爱飞的猫 发表于 2024-9-14 04:17
需要大数运算的话,可以参考对应大数操作库的源码(或 Java 的源码)。基于字符操作的效率低了。
 楼主| wangarc 发表于 2024-9-14 07:12
本帖最后由 wangarc 于 2024-9-14 07:17 编辑
爱飞的猫 发表于 2024-9-14 04:17
需要大数运算的话,可以参考对应大数操作库的源码(或 Java 的源码)。基于字符操作的效率低了。

BigDecimal
版主是懂我的,才写两段就被识破了。
我是那种给自行车装上方形轮子的人。
我没看过别人的源码,看过再写会被带偏。

就想自己理个思路,就当是摸鱼。

免费评分

参与人数 1吾爱币 +1 收起 理由
爱飞的猫 + 1 我很赞同!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-12-14 07:32

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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