匿名用户
13级
2010-09-07 回答
一下程序可以相加128位数字字符.
#include
#include
#include
int getSize(const char *const arr); //get the length of array
void binaryHugeNumAdd( char *first, char *second, char *result);
const unsigned int SIZE=128; //控制数字长度
void printResult(char *result);
int main(void)
{
char *first=(char *)malloc(SIZE);
char *second=(char *)malloc(SIZE);
char *result=(char *)malloc(SIZE+1);
int i=0;
printf("输入第一个数:");
gets(first);
fflush(stdin);
printf("输入第二个数:");
gets(second);
fflush(stdin);
binaryHugeNumAdd(first,second,result);
printResult(result);
free(first);
free(second);
free(result);
return 0;
}
int getSize(const char *const arr)
{
unsigned int length=0;
for(;arr[length]!='\0';length++) ;
return length;
}
void binaryHugeNumAdd( char *first, char *second, char *resultPtr)
{
char *tempPtr=NULL;
char *result=NULL;
short int temp=0,carry=0; //进位
short int firstL=0,secondL=0,tempL=0;
short int fl=0,sl=0;
int i=0,j=0;
firstL=getSize(first);
secondL=getSize(second);
//printf("\nfirstL=%d \nsecond=%d\n",firstL,secondL);
//printf("\nfirst=%s \nsecond=%s\n",first,second);
if(firstL
{
tempL=secondL;
secondL=firstL;
firstL=tempL;
tempPtr=second;
second=first;
first=tempPtr;
}
fl=firstL; //greater
sl=secondL; //less
result=(char *)malloc(firstL+2);
for( i=0;i
result[i]='0';
result[firstL+1]='\0';
//for( i=0;result[i]!='\0';i++)
//printf("%2c",result[i]);
//printf("\nfirstL=%d \nsecondL=%d\n",firstL,secondL);
//printf("\nfirst=%s \nsecond=%s\n",first,second);
while(firstL-1>=0)
{
if(secondL-1>=0)
temp=(first[firstL-1]-'0')+(second[secondL-1]-'0')+carry;
else
temp=(first[firstL-1]-
追问:
printf("输入第一个数:");gets(first);fflush(stdin);printf("输入第二个数:");
为什么输入两个数?
我们学C语言很浅,能中文解释下是什么思路算的吗?