#include<stdio.h>
#include<string.h>
int a[10086];
int b[10086];
int c[10086];
char s1[10086];
char s2[10086];
//additon
void High_precision_addition(char *n,char *m)
{
int l1=strlen(n);
int l2=strlen(m);
int len;
if(l1>l2)
len=l1;
else
len=l2;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=l1-1;i>=0;i--)
a[l1-i-1]=n[i]-'0';
for(int i=l2-1;i>=0;i--)
b[l2-i-1]=m[i]-'0';
for(int i=0;i<len;i++)
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
if(a[len]!=0) len++;
for(int i=len-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
//difference
void High_precision_subtraction(char *n,char *m)
{
int l1=strlen(n);
int l2=strlen(m);
int flag=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
if (l1<l2||(strcmp(n,m)<0&&l1==l2) )
{
flag = 1;
for (int i=l2-1;i>=0;i--)
a[l2-i-1]=m[i]-'0';
for (int i=l1-1;i>=0;i--)
b[l1-i-1]=n[i]-'0';
}
else
{
for (int i=l1-1;i>=0;i--)
a[l1-i-1]=n[i] - '0';
for (int i=l2-1;i>=0;i--)
b[l2-i-1]=m[i]-'0';
}
int len;
if (l1>l2)
len=l1;
else
len=l2;
for (int i=0;i<len;i++)
{
a[i]=a[i]-b[i];
if(a[i]<0)
{
a[i+1]-=1;
a[i]+=10;
}
}
while (a[len-1]==0&&len>1) len--;
if (flag==1) printf("-");
for (int i=len-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
//multiply
void High_precision_multiplication(char *n,char *m)
{
int l1=strlen(n),l2=strlen(m);
int i,j;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
for(i=0,j=l1-1;i<l1;i++,j--)
a[i]=n[j]-'0';
for(i=0,j=l2-1;i<l2;i++,j--)
b[i]=m[j]-'0';
for (i=0;i<l1;i++)
for (j=0;j<l2;j++)
c[i+j]+=a[i]*b[j];
for (i=0;i<l1+l2;i++)
{
if (c[i]>=10)
{
c[i+1]+=c[i]/10;
c[i]%=10;
}
}
for (j=10085;j>0;j--)
{
if (c[j]!=0)
break;
}
for (i=j;i>=0;i--)
{
printf("%d",c[i]);
}
printf("\n");
}
//main
int main(void)
{
char Arithmetic_symbol;
while(1)
{
printf("Enter two numbers and the Arithmetic symbol which you wanna make it opreation:\n");
printf("Tips:Arithmetic symbol you can enter:+,-,*\n");
scanf("%s%s",s1,s2); getchar(); scanf("%c",&Arithmetic_symbol);
if(Arithmetic_symbol=='+') High_precision_addition(s1,s2);
else if(Arithmetic_symbol=='-') High_precision_subtraction(s1,s2);
else if(Arithmetic_symbol=='*') High_precision_multiplication(s1,s2);
else printf("Please enter the correct value!\n");
}
return 0;
}
C语言 加法 减法 乘法 的高精度模版
猜你喜欢
转载自blog.csdn.net/LynlinBoy/article/details/81148787
今日推荐
周排行