[C] 纯文本查看 复制代码
#include <stdio.h>
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_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 is_dec_str(char * str, int * p_or_n, int * i_qty, int * d_qty, int * dot_pos);
/* is_dec_str() return 1 if Yes, otherwise 0
* Check str if a valid decimal number or not. Return 1 if Yes, or 0 if No.
* str: the string to be checked.
* p_or_n: indicate the number is positive (1) or negative (-1) (after check).
* i_qty: integeral part char qty (length)
* d_qty: decimal part char qty (length)
* dot_pos: return position of '.', (or '\0' if no '.'), of the str
* eg: if p_or_n = -1, i_qty = 5, d_qty = 2, dot_pos = 6 str may look like -nnnnn.nn
*/
int add_ip_dp(char * str1, int dot_pos1, int i_qty1, int d_qty1,
char * str2, int dot_pos2, int i_qty2, int d_qty2,
char * str3, int str3_array_size, int * offset);
/* add_ip_dp() return 1 if no error, otherwise 0
* add integral part and decimal part (str3 = str1 + str2)
* dot_pos: index of str where '.' located for decimal number
* or '\0' located for integral number.
* i_qty: integral number quantity (integral part length)
* d_qty: decimal number quantity (decimal part length)
* str3: array to store result
* str3_array_size: array size
* offset: result will start from here (this function write result from tail to head)
*/
int sub_ip_dp(char * str1, int dot_pos1, int i_qty1, int d_qty1,
char * str2, int dot_pos2, int i_qty2, int d_qty2,
char * str3, int str3_array_size, int * offset);
/* sub_ip_dp() return 1 if no error, -1 if sub error (str1 < str2), 0 if other errors.
* sub integral part and decimal part (str3 = str1 - str2)
* dot_pos: index of str where '.' located for decimal number
* or '\0' located for integral number.
* i_qty: integral number quantity (integral part length)
* d_qty: decimal number quantity (decimal part length)
* str3: array to store result
* str3_array_size: array size
* offset: result will start from here (this function write result from tail to head)
*/
int dec_str_add(char * str1, char * str2, char * str3, int str3_array_size, int * offset);
/* dec_str_add() return 1 if no error, otherwise 0
* add decimal strings (str3 = str1 + str2)
* str3: array to store result
* str3_array_size: array size
* offset: result will start from here (this function write result from tail to head)
* better to have big size for str3,
* at least +2 more than total length of str1 and str2 is suggested.
*/
int dec_str_sub(char * str1, char * str2, char * str3, int str3_array_size, int * offset);
/* dec_str_sub() return 1 if no error, otherwise 0
* sub decimal strings (str3 = str1 - str2)
* str3: array to store result
* str3_array_size: array size
* offset: result will start from here (this function write result from tail to head)
* better to have big size for str3,
* at least +2 more than total length of str1 and str2 is suggested.
*/
int main(void) //testing
{
char * s1 = "0";
char * s2 = "99999999999999999999";
char * s3 = ".00000000000000000001";
char * s4 = "1.00";
char * s5 = "3.14159";
char * s6 = "0003.14159000";
char * s7 = "9999999999.9999999999";
char str[128];
int i, r;
r = dec_str_add(s1, s2, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s1, s2, str + i);
r = dec_str_sub(s1, s2, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s - %s = %s\n", r, i, s1, s2, str + i);
r = dec_str_add(s2, s3, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s2, s3, str + i);
r = dec_str_sub(s2, s3, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s - %s = %s\n", r, i, s2, s3, str + i);
r = dec_str_add(s3, s4, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s3, s4, str + i);
r = dec_str_sub(s3, s4, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s - %s = %s\n", r, i, s3, s4, str + i);
r = dec_str_add(s4, s5, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s4, s5, str + i);
r = dec_str_sub(s4, s5, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s - %s = %s\n", r, i, s4, s5, str + i);
r = dec_str_add(s5, s6, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s5, s6, str + i);
r = dec_str_sub(s5, s6, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s - %s = %s\n", r, i, s5, s6, str + i);
r = dec_str_add(s6, s7, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s + %s = %s\n", r, i, s6, s7, str + i);
r = dec_str_sub(s6, s7, str, 128, &i);
printf("\nreturn %d, offset = %d,\n%s - %s = %s\n", r, i, s6, s7, str + i);
return 0;
}
int dec_str_sub(char * str1, char * str2, char * str3, int str3_array_size, int * offset)
{
int pn1, pn2; //indicate positive or negative for str1 and str2.
int iq1, iq2; //store integral part quantity for str1 and str2.
int dq1, dq2; //store decimal part quantity for str1 and str2.
int dp1, dp2; //store '.' position for str1 and str2.
int rt1, rt2, rt3; //temp return value;
rt1 = is_dec_str(str1, &pn1, &iq1, &dq1, &dp1);
if (rt1 == 0)
return 0; //Error: str1 is not a number.
rt2 = is_dec_str(str2, &pn2, &iq2, &dq2, &dp2);
if (rt2 == 0)
return 0; //Error: str2 is not a number.
if (pn1 == 1 && pn2 == -1) //str1 is positive, str2 is negative
{
rt3 = add_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
}
else if (pn1 == 1 && pn2 == 1) //str1 is positive, str2 is positive
{
rt3 = sub_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
if (rt3 == -1)
{ //switch str1 and str2, try again.
rt3 = sub_ip_dp(str2, dp2, iq2, dq2, str1, dp1, iq1, dq1,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed again finally.
if (--(*offset) >= 0)
str3[*offset] = '-'; //put on a '-' for str3
else
return 0; //Error: not enough space in str3
}
}
else if (pn1 == -1 && pn2 == -1) //str1 is negative, str2 is negative
{
rt3 = sub_ip_dp(str2, dp2, iq2, dq2, str1, dp1, iq1, dq1,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
if (rt3 == -1)
{ //switch str1 and str2, try again.
rt3 = sub_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed again finally.
if (--(*offset) >= 0)
str3[*offset] = '-'; //put on a '-' for str3
else
return 0; //Error: not enough space in str3
}
}
else if (pn1 == -1 && pn2 == 1) //str1 is negative, str2 is positive
{
rt3 = add_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
if (str3[*offset] == '0' && str3[*offset+1] == '\0')
; //do nothing, it's 0, don't put on '-', happened while (-0) - (0)
else
{
if (--(*offset) >= 0)
str3[*offset] = '-'; //put on a '-' for str3
else
return 0; //Error: not enough space in str3
}
}
else
return 0; //Error: something wrong, not possible.
return 1; //Done.
}
int dec_str_add(char * str1, char * str2, char * str3, int str3_array_size, int * offset)
{
int pn1, pn2; //indicate positive or negative for str1 and str2.
int iq1, iq2; //store integral part quantity for str1 and str2.
int dq1, dq2; //store decimal part quantity for str1 and str2.
int dp1, dp2; //store '.' position for str1 and str2.
int rt1, rt2, rt3; //temp return value;
rt1 = is_dec_str(str1, &pn1, &iq1, &dq1, &dp1);
if (rt1 == 0)
return 0; //Error: str1 is not a number.
rt2 = is_dec_str(str2, &pn2, &iq2, &dq2, &dp2);
if (rt2 == 0)
return 0; //Error: str2 is not a number.
if (pn1 == 1 && pn2 == 1) //str1 is positive, str2 is positive
{
rt3 = add_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
}
else if (pn1 == 1 && pn2 == -1) //str1 is positive, str2 is negative
{
rt3 = sub_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
if (rt3 == -1)
{ //switch str1 and str2, try again.
rt3 = sub_ip_dp(str2, dp2, iq2, dq2, str1, dp1, iq1, dq1,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed again finally.
if (--(*offset) >= 0)
str3[*offset] = '-'; //put on a '-' for str3
else
return 0; //Error: not enough space in str3
}
}
else if (pn1 == -1 && pn2 == 1) //str1 is negative, str2 is positive
{
rt3 = sub_ip_dp(str2, dp2, iq2, dq2, str1, dp1, iq1, dq1,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
if (rt3 == -1)
{ //switch str1 and str2, try again.
rt3 = sub_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed again finally.
if (--(*offset) >= 0)
str3[*offset] = '-'; //put on a '-' for str3
else
return 0; //Error: not enough space in str3
}
}
else if (pn1 == -1 && pn2 == -1) //str1 is negative, str2 is negative
{
rt3 = add_ip_dp(str1, dp1, iq1, dq1, str2, dp2, iq2, dq2,
str3, str3_array_size, offset);
if (rt3 == 0)
return 0; //Error: failed, maybe, not enough space in str3
if (str3[*offset] == '0' && str3[*offset+1] == '\0')
; //do nothing, it's 0, don't put on '-', happened while (-0) + (-0)
else
{
if (--(*offset) >= 0)
str3[*offset] = '-'; //put on a '-' for str3
else
return 0; //Error: not enough space in str3
}
}
else
return 0; //Error: something wrong, not possible.
return 1; //Done.
}
int sub_ip_dp(char * str1, int dot_pos1, int i_qty1, int d_qty1,
char * str2, int dot_pos2, int i_qty2, int d_qty2,
char * str3, int str3_array_size, int * offset)
{
//return 1 if no error, -1 if sub error (str1 < str2), 0 if other errors.
char * p1 = str1 + dot_pos1; // p1 is a pointer to '.' or '\0' of str1
char * p2 = str2 + dot_pos2; // p2 is a pointer to '.' or '\0' of str2
int i = str3_array_size; // i is an index of str3, copy to *offset finally
char borrow = '0'; // borrow is a borrow
char c1, c2, c3; // c1, c2 and c3 are temp chars
int k, j; // k and j are temp index
// set '\0' at the end of str3
if (--i >= 0)
str3[i] = '\0';
else
return 0; //Error: insufficient space in str3
// sub decimal part: from tail to head
k = (d_qty1 > d_qty2) ? d_qty1 : d_qty2;
while (k > 0)
{
c1 = (k > d_qty1) ? '0' : p1[k];
c2 = (k > d_qty2) ? '0' : p2[k];
if (0 == dec_char_sub(borrow, c1, c2, &borrow, &c3))
return 0; //Error: return error from dec_char_add()
if (--i >= 0)
str3[i] = c3;
else
return 0; //Error: insufficient space in str3
if (str3[i+1] == '\0' && str3[i] == '0')
i++; //ignore '0' in the end of decimal
--k;
}
// put a '.' in the string
if (str3[i] != '\0')
{
if (--i >= 0)
str3[i] = '.';
else
return 0; //Error: insufficient space in str3
}
// sub integral part: from tail to head
k = 1;
j = (i_qty1 > i_qty2) ? i_qty1 : i_qty2;
while (k <= j)
{
c1 = (k > i_qty1) ? '0' : p1[-k];
c2 = (k > i_qty2) ? '0' : p2[-k];
if (0 == dec_char_sub(borrow, c1, c2, &borrow, &c3))
return 0; //Error: return error from dec_char_add()
if (--i >= 0)
str3[i] = c3;
else
return 0; //Error: insufficient space in str3
++k;
}
// consider borrow after last dec_char_sub()
if (borrow == '1')
return -1; //Error: str1 is smaller than str2, it's not allowed
//you should switch str1 and str2
//try again: - (str2 - str1)
// ignore all begining '0' in the string
while (str3[i] == '0')
++i;
// add a '0' if start with a '.'
if (str3[i] == '.')
{
if (--i >= 0)
str3[i] = '0';
else
return 0; //Error: insufficient space in str3
}
// add a '0' if empty string
if (str3[i] == '\0')
{
if (--i >= 0)
str3[i] = '0';
else
return 0; //Error: insufficient space in str3
}
// set *offset for str3
*offset = i;
return 1; //Done
}
int add_ip_dp(char * str1, int dot_pos1, int i_qty1, int d_qty1,
char * str2, int dot_pos2, int i_qty2, int d_qty2,
char * str3, int str3_array_size, int * offset)
{
//return 1 = no error, 0 = fail.
char * p1 = str1 + dot_pos1; // p1 is a pointer to '.' or '\0' of str1
char * p2 = str2 + dot_pos2; // p2 is a pointer to '.' or '\0' of str2
int i = str3_array_size; // i is an index of str3, copy to *offset finally
char carry = '0'; // carry is a carry
char c1, c2, c3; // c1, c2 and c3 are temp chars
int k, j; // k and j are temp index
// set '\0' at the end of str3
if (--i >= 0)
str3[i] = '\0';
else
return 0; //Error: insufficient space in str3
// add decimal part: from tail to head
k = (d_qty1 > d_qty2) ? d_qty1 : d_qty2;
while (k > 0)
{
c1 = (k > d_qty1) ? '0' : p1[k];
c2 = (k > d_qty2) ? '0' : p2[k];
if (0 == dec_char_add(carry, c1, c2, &carry, &c3))
return 0; //Error: return error from dec_char_add()
if (--i >= 0)
str3[i] = c3;
else
return 0; //Error: insufficient space in str3
if (str3[i+1] == '\0' && str3[i] == '0')
i++; //ignore '0' in the end of decimal
--k;
}
// put a '.' in the string
if (str3[i] != '\0')
{
if (--i >= 0)
str3[i] = '.';
else
return 0; //Error: insufficient space in str3
}
// add integral part: from tail to head
k = 1;
j = (i_qty1 > i_qty2) ? i_qty1 : i_qty2;
while (k <= j)
{
c1 = (k > i_qty1) ? '0' : p1[-k];
c2 = (k > i_qty2) ? '0' : p2[-k];
if (0 == dec_char_add(carry, c1, c2, &carry, &c3))
return 0; //Error: return error from dec_char_add()
if (--i >= 0)
str3[i] = c3;
else
return 0; //Error: insufficient space in str3
++k;
}
// consider carry after last dec_char_add()
if (carry == '1')
{
if (--i >= 0)
str3[i] = '1';
else
return 0; //Error: insufficient space in str3
}
// ignore all begining '0' in the string
while (str3[i] == '0')
++i;
// add a '0' if start with a '.'
if (str3[i] == '.')
{
if (--i >= 0)
str3[i] = '0';
else
return 0; //Error: insufficient space in str3
}
// add a '0' if empty string
if (str3[i] == '\0')
{
if (--i >= 0)
str3[i] = '0';
else
return 0; //Error: insufficient space in str3
}
// set *offset for str3
*offset = i;
return 1; //Done
}
int is_dec_str(char * str, int * p_or_n, int * i_qty, int * d_qty, int * dot_pos)
{
int ret_val = 1; //return value, 1 = Yes, 0 = No.
int dot_qty = 0; //'.' qty found in the str
int i = 0; //i is an index of str.
switch (str[i])
{
case '-': *p_or_n = -1; i++; break; //negative number and bypass the '-'
case '+': i++; //bypass the '+'
default: *p_or_n = 1; //positive number
}
*i_qty = 0;
*d_qty = 0;
*dot_pos = -1; //initialized as a not possible value.
while (str[i] != '\0' && ret_val)
{
switch (str[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': dot_qty ? ++(*d_qty) : ++(*i_qty);
break;
case '.': if (++dot_qty > 1)
ret_val = 0; //Error: too many '.'
*dot_pos = i;
break;
default: ret_val = 0; //Error: unexpected char found
}
i++;
}
if (*i_qty == 0 && *d_qty == 0)
ret_val = 0; //Error: no valid number found
if (*dot_pos == -1) //there is no '.' in the str, str is an integer
*dot_pos = i; //dot_pos set at '\0'(str end) for "no dot" integer
if (ret_val != 1)
{
*p_or_n = 0;
*i_qty = 0;
*d_qty = 0;
*dot_pos = 0;
}
return ret_val;
}
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;
}
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;
}