- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 1024
- #define OFFSET 3
- //check str
- int check_num(char *str,int len);
- //adpter the str with the same len set '0'
- void adpter(char *str1,int len1,char *str2,int len2);
- //half adder to add the same bit digit,and out put sum and carry out
- void haf_adder(char added,char adder,char *sum,char *caout);
- //compute carry
- void carry(char cain1,char cain2,char *caout);
- //compute the same bit digit,and output the sum and the carry
- void full_adder(char added,char adder,char cain,char *sum,char *caout);
- //the terminal add function
- char* adder(char *added,int len1,char *adder,int len2);
- int main(int argc, char **argv)
- {
- char added[MAX]={0};
- char adder1[MAX]={0};
- memset(added,'\\0',MAX);
- memset(adder1,'\\0',MAX);
- printf("added:");
- scanf("%s",added);
- printf("adder:");
- scanf("%s",adder1);
- char *p = adder(added,strlen(added),adder1,strlen(adder1));
- printf("sum :%s \\n",p);
- return 0;
- }
- /*
- * str: the string input
- * len: the length of str
- * return: 0 all of the char of str is a digit
- * -1 str contains at least one char is not digit
- */
- int check_num(char *str,int len)
- {
- int i=0;
- for (i; i<len; i++)
- if (str[i]<'0' || str[i]>'9')
- return -1;
- return 0;
- }
- /*
- * str1:the string of added
- * len1:the length of str1
- * str2:the string of adder
- * len2:the length of str2
- */
- void adpter(char *str1,int len1,char *str2,int len2)
- {
- if (len1 == len2)
- return ;
- else
- {
- int offset = 0;
- if (len1 < len2)
- {
- offset = len2 -len1;
- int i = len2-1;
- for (i; i>=offset; i--)
- str1[i] = str1[i - offset];
- for (i = 0; i<offset; i++)
- str1[i] = '0';
- }
- else
- {
- offset = len1 - len2;
- int i = len1-1;
- for (i; i>=offset; i--)
- str2[i] = str2[i - offset];
- for (i = 0; i<offset; i++)
- str2[i] = '0';
- }
- }
- }
- /*
- * added:a bit was add
- * adder:a bit whit the same position
- * sum: the result of adder + added
- * caout:output output carry 0 or 1
- */
- void haf_adder(char added,char adder,char *sum,char *caout)
- {
- int sum1;
- sum1 = (added-48) + (adder-48);
- if (sum1>=10)//has carry
- {
- *caout = '1';
- *sum = sum1%10 + 48;
- }
- else//no carry
- {
- *caout = '0';
- *sum = sum1 + 48;
- }
- }
- /*
- * added: a bit was add
- * adder: a bit whit the same position
- * cain : input carry of the previous compute
- * sum : the result of adder + added
- * caout: output output carry 0 or 1
- */
- void full_adder(char added,char adder,char cain,char *sum,char *caout)
- {
- char sum1;
- char caout1;
- char caout2;
- //the first half_adder
- haf_adder(added,adder,&sum1,&caout1);
- //the second half_adder
- haf_adder(sum1,cain,sum,&caout2);
- //check carry
- carry(caout1,caout2,caout);
- }
- /*cain1: the first half_adder output carry
- *cain2: the second half_adder output carry
- * caout: output the carry of a full_adder
- */
- void carry(char cain1,char cain2,char *caout)
- {
- *caout = (cain1-48) + (cain2-48) + 48;
- }
- /*
- * adder:the string of number was about to add
- * added:the string of number to add
- */
- char* adder(char *added,int len1,char *adder,int len2)
- {
- int longest = (len1>=len2?len1:len2);
- char *aded = (char*)malloc(longest);
- int i=0;
- for (i; i<len1; i++)
- aded[i] = added[i];
- char *ader = (char*)malloc(longest);
- for (i=0; i<len1; i++)
- ader[i] = adder[i];
- adpter(aded,len1,ader,len2);
- int len = longest + 2;
- char *result = (char*)malloc(len);
- memset(result,0,len);
- char sum = '0';
- char caout= '0';
- char cain = '0';
- i = len - 2 - 1;
- for (i; i>=0; i--)
- {
- full_adder(aded[i],ader[i],cain,&sum,&caout);
- cain = caout;
- result[i] = sum;
- }
- //deal with the last carry
- if (caout == '1')
- {
- i = longest;
- for (i; i>0; i--)
- result[i] = result[i - 1];
- result[0] = caout;
- }
- free(ader);
- free(aded);
- return result;
- }
- //该片段来自于http://www.codesnippet.cn/detail/130520133295.html
来源: http://www.codesnippet.cn/detail/130520133295.html