吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 784|回复: 9
收起左侧

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

[复制链接]
wangarc 发表于 2024-9-12 21:05
本帖最后由 wangarc 于 2024-9-14 16:42 编辑

两个十进制字符,相加得到十进制字符的结果和进位。好久没写C了,都快忘了,欢迎大家来批评。
比如:字符 '6' + 字符 '8' + 进位 '1' = 进位 '1' 和 结果 '5'。

[C] 纯文本查看 复制代码
int dec_char_add(char in_carry, char c1, char c2, char * out_carry, char * result);
/*	dec_char_add() return 1 if no error, otherwise 0
 *
 *						c1			['0'-'9']					'6'
 *		+				c2			['0'-'9']			+		'8'	
 *		+				in_carry	['0'-'1']			+		'1'
 *	_________________________________________		________________
 *		=	out_carry	result							=	'1'	'5'
 *			['0'-'1']	['0'-'9']
 */

int dec_char_add(char in_carry, char c1, char c2, char * out_carry, 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_carry = '0'; *result = '0'; break;
						case '1': *out_carry = '0'; *result = '1'; break;
						case '2': *out_carry = '0'; *result = '2'; break;
						case '3': *out_carry = '0'; *result = '3'; break;
						case '4': *out_carry = '0'; *result = '4'; break;
						case '5': *out_carry = '0'; *result = '5'; break;
						case '6': *out_carry = '0'; *result = '6'; break;
						case '7': *out_carry = '0'; *result = '7'; break;
						case '8': *out_carry = '0'; *result = '8'; break;
						case '9': *out_carry = '0'; *result = '9'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '1':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '1'; break;
						case '1': *out_carry = '0'; *result = '2'; break;
						case '2': *out_carry = '0'; *result = '3'; break;
						case '3': *out_carry = '0'; *result = '4'; break;
						case '4': *out_carry = '0'; *result = '5'; break;
						case '5': *out_carry = '0'; *result = '6'; break;
						case '6': *out_carry = '0'; *result = '7'; break;
						case '7': *out_carry = '0'; *result = '8'; break;
						case '8': *out_carry = '0'; *result = '9'; break;
						case '9': *out_carry = '1'; *result = '0'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '2':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '2'; break;
						case '1': *out_carry = '0'; *result = '3'; break;
						case '2': *out_carry = '0'; *result = '4'; break;
						case '3': *out_carry = '0'; *result = '5'; break;
						case '4': *out_carry = '0'; *result = '6'; break;
						case '5': *out_carry = '0'; *result = '7'; break;
						case '6': *out_carry = '0'; *result = '8'; break;
						case '7': *out_carry = '0'; *result = '9'; break;
						case '8': *out_carry = '1'; *result = '0'; break;
						case '9': *out_carry = '1'; *result = '1'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '3':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '3'; break;
						case '1': *out_carry = '0'; *result = '4'; break;
						case '2': *out_carry = '0'; *result = '5'; break;
						case '3': *out_carry = '0'; *result = '6'; break;
						case '4': *out_carry = '0'; *result = '7'; break;
						case '5': *out_carry = '0'; *result = '8'; break;
						case '6': *out_carry = '0'; *result = '9'; break;
						case '7': *out_carry = '1'; *result = '0'; break;
						case '8': *out_carry = '1'; *result = '1'; break;
						case '9': *out_carry = '1'; *result = '2'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '4':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '4'; break;
						case '1': *out_carry = '0'; *result = '5'; break;
						case '2': *out_carry = '0'; *result = '6'; break;
						case '3': *out_carry = '0'; *result = '7'; break;
						case '4': *out_carry = '0'; *result = '8'; break;
						case '5': *out_carry = '0'; *result = '9'; break;
						case '6': *out_carry = '1'; *result = '0'; break;
						case '7': *out_carry = '1'; *result = '1'; break;
						case '8': *out_carry = '1'; *result = '2'; break;
						case '9': *out_carry = '1'; *result = '3'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '5':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '5'; break;
						case '1': *out_carry = '0'; *result = '6'; break;
						case '2': *out_carry = '0'; *result = '7'; break;
						case '3': *out_carry = '0'; *result = '8'; break;
						case '4': *out_carry = '0'; *result = '9'; break;
						case '5': *out_carry = '1'; *result = '0'; break;
						case '6': *out_carry = '1'; *result = '1'; break;
						case '7': *out_carry = '1'; *result = '2'; break;
						case '8': *out_carry = '1'; *result = '3'; break;
						case '9': *out_carry = '1'; *result = '4'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '6':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '6'; break;
						case '1': *out_carry = '0'; *result = '7'; break;
						case '2': *out_carry = '0'; *result = '8'; break;
						case '3': *out_carry = '0'; *result = '9'; break;
						case '4': *out_carry = '1'; *result = '0'; break;
						case '5': *out_carry = '1'; *result = '1'; break;
						case '6': *out_carry = '1'; *result = '2'; break;
						case '7': *out_carry = '1'; *result = '3'; break;
						case '8': *out_carry = '1'; *result = '4'; break;
						case '9': *out_carry = '1'; *result = '5'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '7':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '7'; break;
						case '1': *out_carry = '0'; *result = '8'; break;
						case '2': *out_carry = '0'; *result = '9'; break;
						case '3': *out_carry = '1'; *result = '0'; break;
						case '4': *out_carry = '1'; *result = '1'; break;
						case '5': *out_carry = '1'; *result = '2'; break;
						case '6': *out_carry = '1'; *result = '3'; break;
						case '7': *out_carry = '1'; *result = '4'; break;
						case '8': *out_carry = '1'; *result = '5'; break;
						case '9': *out_carry = '1'; *result = '6'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '8':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '8'; break;
						case '1': *out_carry = '0'; *result = '9'; break;
						case '2': *out_carry = '1'; *result = '0'; break;
						case '3': *out_carry = '1'; *result = '1'; break;
						case '4': *out_carry = '1'; *result = '2'; break;
						case '5': *out_carry = '1'; *result = '3'; break;
						case '6': *out_carry = '1'; *result = '4'; break;
						case '7': *out_carry = '1'; *result = '5'; break;
						case '8': *out_carry = '1'; *result = '6'; break;
						case '9': *out_carry = '1'; *result = '7'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		case '9':	switch (c2)
					{
						case '0': *out_carry = '0'; *result = '9'; break;
						case '1': *out_carry = '1'; *result = '0'; break;
						case '2': *out_carry = '1'; *result = '1'; break;
						case '3': *out_carry = '1'; *result = '2'; break;
						case '4': *out_carry = '1'; *result = '3'; break;
						case '5': *out_carry = '1'; *result = '4'; break;
						case '6': *out_carry = '1'; *result = '5'; break;
						case '7': *out_carry = '1'; *result = '6'; break;
						case '8': *out_carry = '1'; *result = '7'; break;
						case '9': *out_carry = '1'; *result = '8'; break;
						default: ret_val = 0; //Error: c2 out of range
					}
					break;
		default:	ret_val = 0; //Error: c1 out of range
	}
	switch (in_carry)	//go through "switch case" to get answer without calculate.
	{
		case '0':	break;
		case '1':	switch (*result)
					{
						case '0': *result = '1'; break;
						case '1': *result = '2'; break;
						case '2': *result = '3'; break;
						case '3': *result = '4'; break;
						case '4': *result = '5'; break;
						case '5': *result = '6'; break;
						case '6': *result = '7'; break;
						case '7': *result = '8'; break;
						case '8': *result = '9'; break;
						case '9': *out_carry = '1'; *result = '0'; break;
					}
					break;
		default:	ret_val = 0; //Error: in_carry out of range
	}
	if (ret_val != 1)
	{
		*out_carry = '0';
		*result = '0';
	}
	return ret_val;
}

免费评分

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

查看全部评分

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

JARK006 发表于 2024-9-12 21:38
int dec_char_add(char in_carry, char c1, char c2, char* out_carry, char* result) {
    if (c1 < '0' || '9' < c1) {
        *out_carry = '0';
        *result = '0';
        return 1;
    }

    if (c2 < '0' || '9' < c2) {
        *out_carry = '0';
        *result = '0';
        return 2;
    }

    if (in_carry < '0' || '9' < in_carry) {
        *out_carry = '0';
        *result = '0';
        return 3;
    }

    int sum = (in_carry - '0') + (c1 - '0') + (c2 - '0');
    *out_carry = (sum / 10) + '0';
    *result = (sum % 10) + '0';

    return 0;
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
wangarc + 1 + 1 我很赞同!

查看全部评分

DEATHTOUCH 发表于 2024-9-12 22:11
看看leetcode第2题,两数相加。和你这个还挺有关系的呢。
 楼主| wangarc 发表于 2024-9-12 22:33
JARK006 发表于 2024-9-12 21:38
[md]```c
int dec_char_add(char in_carry, char c1, char c2, char* out_carry, char* result) {
    if ...

这样有个前提啊,需要0-9的编码是连续的。不过,还没见过不连续的。
 楼主| wangarc 发表于 2024-9-12 22:34
DEATHTOUCH 发表于 2024-9-12 22:11
看看leetcode第2题,两数相加。和你这个还挺有关系的呢。

俺没刷过leetcode,太高级了。
JARK006 发表于 2024-9-13 01:03
wangarc 发表于 2024-9-12 22:33
这样有个前提啊,需要0-9的编码是连续的。不过,还没见过不连续的。

你去查一下ASCII码表就明白了
 楼主| wangarc 发表于 2024-9-13 07:09
JARK006 发表于 2024-9-13 01:03
你去查一下ASCII码表就明白了

嗯嗯,明白,ASCII是连续的。
ice2flash 发表于 2024-9-13 10:32
现在看代码,如同看天书
侃遍天下无二人 发表于 2024-9-13 11:03
你写的怎么这么复杂
 楼主| wangarc 发表于 2024-9-13 12:31

突发奇想,字符拼凑。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-24 14:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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