C语言高精度计算

高精度乘法

#include "pch.h"
#include <iostream>
#include <stdlib.h>

int main()
{
char n1[1500], n2[1500];
int a[1500] = {'\0'};
int b[1500] = {'\0'};
int c[3000] = {0};
int m, n, i, j, o;
printf("请输入被乘数:\n");
scanf_s("%s",&n1,128);
m = strlen(n1);
printf("请输入乘数:\n");
scanf_s("%s",&n2,128);
n = strlen(n2);
if(m>n){o = m;}else{o = n;}
//将char类型的元素转换成int类型
for (i = 0, j = m - 1; i < m; i++, j--) {
a[i] = n1[j] - '0';
}
//将char类型的元素转换成int类型
for (i = 0, j = n - 1; i < n; i++, j--) {
b[i] = n2[j] - '0';
}
//用数组a的每一位跟数组b进行乘法操作
for (i = 0; i < m;i++) {
for (j = 0; j < n; j++) {
c[i + j] = c[i+j]+(a[i] * b[j]);
}
}
//处理进位
for (i = 0; i < m + n; i++) { if (c[i] >= 10) {//若该位大于10则向前进该处元素的十位数
c[i + 1] = c[i+1] + c[i] / 10;//20=>2,10=>1
c[i] = c[i] % 10;//取个位数
}
}
//由于存储c时是逆序存储,所以要再次逆序输出
for (i = o-1; i >=0;i--) {
printf("%d",c[i]);
}
printf("\n");
system("pause");
}


高精度加法

#include "pch.h"
#include <iostream> 
#include <stdlib.h>

int main()
{
	char n1[1500], n2[1500];
	int a[1500] = {'\0'};
	int b[1500] = {'\0'};
	int c[3000] = {0};
	int m, n, i, j,o;
	printf("请输入被加数:\n");
	scanf_s("%s",&amp;amp;n1,128);
	m = strlen(n1);
	printf("请输入加数:\n");
	scanf_s("%s",&amp;amp;n2,128);
	n = strlen(n2);
	for (i = 0, j = m - 1; i &amp;lt; m; i++, j--) {
		a[i] = n1[j]-'0' ;
	}
	for (i = 0, j = n - 1; i &amp;lt; n; i++, j--) { b[i] = n2[j]-'0' ; } if (m &amp;gt; n) { o = m; }
	else { o = n; }
	for (i = 0; i &amp;lt; o;i++) {
			c[i] = c[i]+(a[i] + b[i]);
	}
	for (i = 0; i &amp;lt; o; i++) { if (c[i] &amp;gt;= 10) {
			c[i + 1] =c[i+1]+( c[i] / 10);
			c[i] = c[i] % 10;
		}
}
	for (i = o-1; i &amp;gt;=0;i--) {
		printf("%d",c[i]);
	}
	printf("\n");
	system("pause");
}


高精度减法

#include "pch.h"
#include <iostream> 
#include <stdlib.h>

int main()
{
	char n1[1500], n2[1500];
	int a[1500] = { '\0' };
	int b[1500] = { '\0' };
	int c[3000] = { 0 };
	int m, n, i, j, o,flag=0;
	printf("请输入被减数:\n");
	scanf_s("%s", &amp;amp;n1, 128);
	m = strlen(n1);
	printf("请输入减数:\n");
	scanf_s("%s", &amp;amp;n2, 128);
	n = strlen(n2);
	for (i = 0, j = m - 1; i &amp;lt; m; i++, j--) {
		a[i] = n1[j] - '0';
	}
	for (i = 0, j = n - 1; i &amp;lt; n; i++, j--) { b[i] = n2[j] - '0'; } if (m &amp;gt; n) { o = m; }
	else { o = n; }
	for (i = 0; i &amp;lt; o; i++) {
		c[i] = c[i] + (a[i] - b[i]);
	}
	for (i = 0; i &amp;lt; o; i++) {
		if (c[i] &amp;lt; 0 &amp;amp;&amp;amp; flag != 1) { flag = 1; }//记录正负号
		if (c[i] &amp;lt; 0 &amp;amp;&amp;amp; a[i] == 0) { c[i + 1] = c[i + 1] - 1; c[i] = 10 - b[i]; } if (b[i] == 0 &amp;amp;&amp;amp; c[i] &amp;gt; 0) {
			c[i] = 10 - a[i];
			if (c[i + 1] &amp;gt; 0) { c[i + 1] = c[i + 1] - 1; }
			else {
				c[i + 1] = c[i + 1] + 1;
			}
		}
		if (c[i] &amp;lt; 0) { c[i] = -1 * c[i]; } } if (flag == 1) { printf("-"); } for (i = o-1; i &amp;gt;=0;i--) {
		printf("%d",c[i]);
	}
	printf("\n");
	system("pause");
}

高精度除低精度

#include "pch.h"
#include <iostream> 
#include <stdlib.h>

int main()
{
	char n1[1500];
	int n2;
	double a[3000] = { '\0' }, b[3000] = { 0 }, c = 0;
	int m, n, i, j, o,flag=0;
	printf("请输入被除数:\n");
	scanf_s("%s", &amp;amp;amp;n1, 128);
	m = strlen(n1);
	printf("请输入除数:\n");
	scanf_s("%d", &amp;amp;amp;n2, 128);
	if (n2 == 0) { printf("除数不可为0\n"); system("pause"); exit(0); }
	for (i = 0, j = m - 1; i &amp;amp;lt; m; i++, j--) {
		a[i] = n1[j] - '0';
	}
	for (i = 0,j=0; i &amp;amp;lt; m;i++,j++) { b[i] = b[i] + (a[i] / n2); for (o=j; o&amp;amp;gt;0; o--) {
			b[i] = b[i] * 10;
		}
	}
	for (i = 0; i&amp;amp;lt;m; i++) {
		c = c + b[i];
	}
	printf("%lf",c);
	printf("\n");
	system("pause");
}

原理:使用被除数的每一位进行除法运算,并将最终结果sum起来

作者: 7gugu

I'm a phper!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据