C/C++高精算法

本文详细介绍了高精度算法中的加法、减法、乘法以及除法(包括除低精和除高精),通过字符串模拟的方式实现,展示了如何在编程中处理大数值计算
摘要由CSDN通过智能技术生成

高精

高精算法包含高精加法、高精减法、高精乘法、高精除低精和高精除高精,采用字符串模拟的思想

高精加法

#include<bits/stdc++.h>
using namespace std;
//compare比较函数:相等返回0,大于返回1,小于返回-1
int compare(string str1,string str2)
{
    if(str1.length()>str2.length()) return 1;
    else if(str1.length()<str2.length())  return -1;
    else return str1.compare(str2);
}
//只能是两个正数相加
string add(string str1,string str2)//高精度加法
{
    string str;
    int len1=str1.length();
    int len2=str2.length();
    //前面补0,弄成长度相同,string的便捷操作
    if(len1<len2)
    {
        for(int i=1;i<=len2-len1;i++)
           str1="0"+str1;
    }
    else
    {
        for(int i=1;i<=len1-len2;i++)
           str2="0"+str2;
    }
    len1=str1.length();
    int cf=0;
    int temp;
    for(int i=len1-1;i>=0;i--)
    {
        temp=str1[i]-'0'+str2[i]-'0'+cf;
        cf=temp/10;
        temp%=10;
        str=char(temp+'0')+str;
    }
    if(cf!=0)  str=char(cf+'0')+str;//添加首位
    return str;
}

高精减法

#include<bits/stdc++.h>
using namespace std;
//只能是两个正数相减,而且要大减小,如果是负数需要处理成加的形式,小减大改变位置变大减小最后加负号
string sub(string str1,string str2)
{
    string str;
    int tmp=str1.length()-str2.length();
    int cf=0;//判断借不借位
    for(int i=str2.length()-1;i>=0;i--)
    {
        if(str1[tmp+i]<str2[i]+cf)
        {
            str=char(str1[tmp+i]-str2[i]-cf+'0'+10)+str;
            cf=1;
        }
        else
        {
            str=char(str1[tmp+i]-str2[i]-cf+'0')+str;
            cf=0;
        }
    }
    for(int i=tmp-1;i>=0;i--)//看上部分是不是还欠着位
    {
        if(str1[i]-cf>='0')
        {
            str=char(str1[i]-cf)+str;
            cf=0;
        }
        else
        {
            str=char(str1[i]-cf+10)+str;
            cf=1;
        }
    }
    str.erase(0,str.find_first_not_of('0'));//去除结果中多余的前导0
    return str;
}


高精乘法

#include<bits/stdc++.h>
using namespace std;
//只能是两个正数相乘
string add(string str1,string str2);//高精度加法
string mul(string str1,string str2)
{
    string str;
    int len1=str1.length();
    int len2=str2.length();
    string tempstr;
    for(int i=len2-1;i>=0;i--)
    {
        tempstr="";
        int temp=str2[i]-'0';
        int t=0;
        int cf=0;
        if(temp!=0)
        {
            for(int j=1;j<=len2-1-i;j++)
              tempstr+="0";
            for(int j=len1-1;j>=0;j--)
            {
                t=(temp*(str1[j]-'0')+cf)%10;
                cf=(temp*(str1[j]-'0')+cf)/10;
                tempstr=char(t+'0')+tempstr;//加的操作保证了不会多位
            }
            if(cf!=0) tempstr=char(cf+'0')+tempstr;
        }
        str=add(str,tempstr);
    }
    str.erase(0,str.find_first_not_of('0'));
    return str;
}

高精除低精

#include<bits/stdc++.h>
using namespace std;
#define foo(i,a,b) for(int i=a;i<b;i++)
#define fio(i,a,b) for(int i=a;i>b;i--)

int main()
{
	string muti1;
	char muti2[20];
	cin>>muti1>>muti2;
	int mu1[10000],mu3[10000],len2=strlen(muti2);
	long mu2=atol(muti2),yvshu=0;
	memset(mu1,0,sizeof(mu1));
	memset(mu3,0,sizeof(mu3));
	foo(i,0,muti1.length())  mu1[i]=muti1[muti1.length()-i-1]-'0';//反方向取muti1
	fio(i,muti1.length()-1,-1)//一步一步取下一位添加到yvshu中做除法,模拟除法
	{
		yvshu*=10;
		yvshu+=mu1[i];
		if(yvshu>=1)
		{
			mu3[i]+=yvshu/mu2;
			yvshu-=yvshu/mu2*mu2;
		}
	}
	int markedi=10003;
	fio(i,9999,-1)
	{
		if(mu3[i]!=0)
		{
			markedi=i;//找到第一位i
			break;
		}
	}
	if(markedi==10003)
	{
		cout<<"0";
	}
	else
	{
		fio(i,markedi,-1) cout<<mu3[i];
	}
	return 0;
}

高精除高精

//两个正数相除,商为quotient,余数为residue
int compare(string str1,string str2);//比较哪个数位数多
string sub(string str1,string str2);//高精度减法
string mul(string str1,string str2);//高精度乘法
void div(string str1,string str2,string &quotient,string &residue)
{
    quotient=residue="";//清空
    if(str2=="0")//判断除数是否为0
    {
        quotient=residue="ERROR";
        return;
    }
    if(str1=="0")//判断被除数是否为0
    {
        quotient=residue="0";
        return;
    }
    int res=compare(str1,str2);
    if(res<0)
    {
        quotient="0";
        residue=str1;
        return;
    }
    else if(res==0)
    {
        quotient="1";
        residue="0";
        return;
    }
    else
    {
        int len1=str1.length();
        int len2=str2.length();
        string tempstr;
        tempstr.append(str1,0,len2-1);
        for(int i=len2-1;i<len1;i++)
        {
            tempstr=tempstr+str1[i];//一位一位加
            tempstr.erase(0,tempstr.find_first_not_of('0'));//去前置0,减法运算中出现的
            if(tempstr.empty())
              tempstr="0";
            for(char ch='9';ch>='0';ch--)//试商
            {
                string str,tmp;
                str=str+ch;
                tmp=mul(str2,str);
                if(compare(tmp,tempstr)<=0)//试商成功
                {
                    quotient=quotient+ch;
                    tempstr=sub(tempstr,tmp);
                    break;
                }
            }
        }
        residue=tempstr;
    }
    quotient.erase(0,quotient.find_first_not_of('0'));
    if(quotient.empty()) quotient="0";
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++实现车床椭圆插补高精算法的示例代码: ```cpp #include <iostream> #include <cmath> using namespace std; const double PI = 3.14159265358979323846; // 计算椭圆上等分点的坐标,支持高精度计算 void CalculateEllipsePoints(double cx, double cy, double a, double b, double angle, int n, double* x, double* y) { double theta = 2 * PI / n; // 等分角度 double cosAngle = cos(angle); double sinAngle = sin(angle); for (int i = 0; i < n; i++) { double alpha = i * theta; double cosAlpha = cos(alpha); double sinAlpha = sin(alpha); double px = a * cosAlpha * cosAngle - b * sinAlpha * sinAngle + cx; double py = a * cosAlpha * sinAngle + b * sinAlpha * cosAngle + cy; x[i] = round(px); // 四舍五入保留整数部分 y[i] = round(py); } } int main() { double cx = 100; // 椭圆中心点坐标 double cy = 100; double a = 50; // 长轴和短轴长度 double b = 30; double angle = PI / 4; // 旋转角度 int n = 36; // 等分点个数 double* x = new double[n]; // 存储等分点的坐标 double* y = new double[n]; CalculateEllipsePoints(cx, cy, a, b, angle, n, x, y); // 输出等分点的坐标 for (int i = 0; i < n; i++) { cout << "x[" << i << "] = " << x[i] << ", y[" << i << "] = " << y[i] << endl; } delete[] x; delete[] y; return 0; } ``` 该示例代码在计算椭圆上等分点的坐标时,采用了高精度计算的方式,即通过四舍五入将计算结果保留整数部分,从而提高计算精度。具体实现中,与普通算法的不同之处在于,将计算得到的浮点数坐标值四舍五入后,存储在x和y数组中。这样可以保证在计算机的有限精度下,计算结果能够尽可能地接近实际值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值