概述
C语言求高精度加减乘
用数组来模拟加减乘除的运算,加法是关键
加法:
int main()
{
//创建两个数组,用来存放两个加数
char arr1[1000] = {"0"};
char arr2[1000] = { "0" };
int arr3[1001] = {0};
scanf("%s %s",arr1,arr2);
int ch1[1000] = {0};
int ch2[1000]={0};
int i = 0;
int len1 =strlen(arr1);
int len2 =strlen(arr2);
int len = len1 >len2 ? len1 : len2;
for (i = len1 - 1; i>= 0; i--)
{
ch1[i]= arr1[len1-i-1]-48;//使数组逆序排列,进行加法,因为加法从地位到高位相加
}
for (i = len2 - 1; i>= 0; i--)
{
ch2[i]= arr2[len2-i-1]-48;//同理
}
int arrlen = len+1;
for (i = 0; i <arrlen; i++)
{
//核心算法
arr3[i]+= ch1[i] + ch2[i];
arr3[i+ 1] = arr3[i] / 10;
arr3[i]%= 10;
}
while ((arr3[arrlen] == 0&& arrlen > 0))
arrlen--;
//printf("%d ", arrlen);
for (i = arrlen; i>= 0; i--)
printf("%d", arr3[i]);
return 0;
}
减法
//Swap函数用来交换两个数组内容
Swap(int* e1, int* e2)
{
while (*e2)
{
int tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
//compal用来比较两个数组的大小
int compal(char* e1, char* e2)
{
if (strlen(e1) >strlen(e2))
return 0;
else
{
if (strlen(e1) < strlen(e2))
return 1;
else if (strlen(e1) == strlen(e2))
{
if (strcmp(e1, e2) < 0)
return 1;
else if (strcmp(e1, e2) > 0)
return 0;
}
}
}
int main()
{
char arr1[1000] = { "0" };
char arr2[1000] = { "0" };
int arr3[1001] = { 0 };
scanf("%s %s", arr1,arr2);
if (compal(arr1,arr2))
{
Swap(arr1,arr2);
int ch1[1000] = { 0 };
int ch2[1000] = { 0 };
int i = 0;
int len1 =strlen(arr1);
int len2 =strlen(arr2);
int len = len1 >len2 ? len1 : len2;
for (i = len1 - 1; i>= 0; i--)
{
ch1[i]= arr1[len1 - i - 1] - 48;
}
for (i = len2 - 1; i>= 0; i--)
{
ch2[i]= arr2[len2 - i - 1] - 48;
}
int arrlen = len + 1;
for (i = 0; i <arrlen; i++)
{
if (ch1[i] <ch2[i])
{
ch1[i]+= 10;
ch1[i+ 1] -= 1;
}
arr3[i]= ch1[i] - ch2[i];
}
while ((arr3[arrlen] == 0&& arrlen > 0))
arrlen--;
//printf("%d ", arrlen);
printf("-");
for (i = arrlen; i>= 0; i--)
{
printf("%d", arr3[i]);
}
}
else
{
int ch1[1000] = { 0 };
int ch2[1000] = { 0 };
int i = 0;
int len1 = strlen(arr1);
int len2 =strlen(arr2);
int len = len1 >len2 ? len1 : len2;
for (i = len1 - 1; i>= 0; i--)
{
ch1[i]= arr1[len1 - i - 1] - 48;
}
for (i = len2 - 1; i>= 0; i--)
{
ch2[i]= arr2[len2 - i - 1] - 48;
}
int arrlen = len + 1;
for (i = 0; i <arrlen; i++)
{
if (ch1[i] <ch2[i])
{
ch1[i]+= 10;
ch1[i+ 1] -= 1;
}
arr3[i]= ch1[i] - ch2[i];
}
while ((arr3[arrlen] == 0&& arrlen > 0))
arrlen--;
//printf("%d ", arrlen);
for (i = arrlen; i>= 0; i--)
printf("%d", arr3[i]);
}
return 0;
}
//乘法
int main()
{
char arr1[1000] = { "0" };
char arr2[1000] = { "0" };
int arr3[1001] = {0};
scanf("%s %s",arr1,arr2);
int ch1[1000] = {0};
int ch2[1000]={0};
int i = 0;
int len1 =strlen(arr1);
int len2 =strlen(arr2);
int arrlen = len1 +len2;
//int sub[1000][1000] = {0};
int len = len1 >len2 ? len1 : len2;
for (i = len1 ; i >0; i--)
{
ch1[i]= arr1[len1-i]-48;
}
for (i = len2; i >0; i--)
{
ch2[i]= arr2[len2-i]-48;
}
int j = 0;
for ( i= 1; i <len1+1; i++)
{
for (j = 1; j <len2+1; j++)
{
//核心算法
arr3[i+ j -1] += ch1[i]*ch2[j];
arr3[i+ j] += arr3[i + j - 1] / 10;
arr3[i+ j - 1] %= 10;
}
}
while ((arr3[arrlen] == 0&& arrlen >1))
arrlen--;
//printf("%d ", arrlen);
for (i = arrlen; i>0; i--)
printf("%d", arr3[i]);
return 0;
}
最后
以上就是飘逸星月为你收集整理的加减乘的深度应用的全部内容,希望文章能够帮你解决加减乘的深度应用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复