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