两个十进制字符相加得到十进制字符的结果和进位
本帖最后由 wangarc 于 2024-9-14 16:42 编辑两个十进制字符,相加得到十进制字符的结果和进位。好久没写C了,都快忘了,欢迎大家来批评。
比如:字符 '6' + 字符 '8' + 进位 '1' = 进位 '1' 和 结果 '5'。
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;
}
```c
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;
}
``` 看看leetcode第2题,两数相加。和你这个还挺有关系的呢。 JARK006 发表于 2024-9-12 21:38
```c
int dec_char_add(char in_carry, char c1, char c2, char* out_carry, char* result) {
if ...
{:1_921:}这样有个前提啊,需要0-9的编码是连续的。不过,还没见过不连续的。:lol DEATHTOUCH 发表于 2024-9-12 22:11
看看leetcode第2题,两数相加。和你这个还挺有关系的呢。
俺没刷过leetcode,太高级了。 wangarc 发表于 2024-9-12 22:33
这样有个前提啊,需要0-9的编码是连续的。不过,还没见过不连续的。
你去查一下ASCII码表就明白了 JARK006 发表于 2024-9-13 01:03
你去查一下ASCII码表就明白了
嗯嗯,明白,ASCII是连续的。 现在看代码,如同看天书{:1_925:} 你写的怎么这么复杂 侃遍天下无二人 发表于 2024-9-13 11:03
你写的怎么这么复杂
突发奇想,字符拼凑。
页:
[1]