本帖最后由 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;
}
|