大整数乘法竖式模拟 c语言,大整数乘法 - 桃花源 - OSCHINA - 中文开源技术交流社区...
#include#include#includechar result[200];void mulStr(char *str, char *one, char* two){char *str1 = one;char *str2 = two;if(!strcmp(str1,"0")||!strcmp(str2,"0")){strcpy(str,"0");return;}bool sinal = tr
#include
#include
#include
char result[200];
void mulStr(char *str, char *one, char* two)
{
char *str1 = one;
char *str2 = two;
if(!strcmp(str1,"0")||!strcmp(str2,"0"))
{
strcpy(str,"0");
return;
}
bool sinal = true;
int len1 = strlen(str1);
int len2 = strlen(str2);
if(*str1=='-')
{
len1--;
str1++;
sinal = !sinal;
}
if(*str2=='-')
{
len2--;
str2++;
sinal = !sinal;
}
int i,j;
int carry=0, out=0,addition;
char *result = str;
memset(result,'0',len1+len2+1);
for(i=len1-1;i>=0;i--)
{
for(j=len2-1;j>=0;j--)
{
out = (*(str1+i)-'0')*(*(str2+j)-'0')+carry+*(str+len1+len2-i-j-2)-'0';
*(str+len1+len2-i-j-2) = out%10 + '0';
carry = out/10;
}
*(str+len1-i+len2-1) = carry +'0';
carry = 0;
}
char *pend = result + len1 + len2 -1;
if(*pend=='0')
{
if(!sinal)
{
*pend = '-';
*(pend+1) = 0;
pend++;
}
else
{
*pend = 0;
pend--;
}
}
else
{
if(!sinal)
{
*(pend+1) = '-';
*(pend+2) = 0;
pend++;
}
else
{
*(pend+1)=0;
}
}
char ch;
while(result
{
ch = *pend;
*pend = *result;
*result = ch;
result++;
pend--;
}
return ;
}
int main()
{
char str1[100];
char str2[100];
char result[200];
while(~scanf("%s %s", str1,str2))
{
mulStr(result,str1,str2);
printf("%s\n",result);
}
return 0;
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)