高精度加法
/*
* @Author : 剑枫
* @Date : 2020-09-05 08:50:26
* @LastEditors : 剑枫
* @LastEditTime : 2020-09-05 20:42:49
* @FilePath : \visual c++基本算法\高精度.cpp
*/
#include <iostream>
#include <cstring>
using namespace std;
struct HugeInt{
int len;
int num[100001];
};
HugeInt a, b, w; //w为结果
char c[100001], d[100001];
void Scan_HugeInt() { //读入两个大整数
cin >> c;
cin >> d;
a.len = strlen(c); //strlen求串长
b.len = strlen(d);
for(int i=0; i<a.len; i++) a.num[a.len - i] = c[i] - '0'; //逆序存储
for(int i=0; i<b.len; i++) b.num[b.len - i] = d[i] - '0';
}
void Plus() {
w.len = max(a.len, b.len); //num每一位是0,长度取max不影响加法
for(int i=1; i<=w.len; i++) {
w.num[i] += a.num[i] + b.num[i];
w.num[i+1] += w.num[i] / 10; //处理进位
w.num[i] %= 10; //处理当前位 保证<10
}
if(w.num[w.len + 1] != 0) w.len ++; //加法最多有可能会多出一位
}
int main() {
Scan_HugeInt();
Plus();
for(int i=w.len; i>=1; i--) cout << w.num[i]; //倒序存储 倒序输出
cout << endl;
return 0;
}
有理数减法
/*
* @Author : 剑枫
* @Date : 2020-09-06 19:42:27
* @LastEditors : 剑枫
* @LastEditTime : 2020-09-06 23:58:46
* @FilePath : \visual c++基本算法\jianhuagjd.cpp
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
char x[100010],y[100010];
int a[10010],b[10010],c[10010],l,k,p,j=0;
cin>>x;
cin>>y;
if(strlen(x)<strlen(y)||strlen(x)==strlen(y)&&strcmp(x,y)<0)
{
j=-1;
swap(x,y);
}
l=strlen(x);
k=strlen(y);
for(int i=0;i<l;i++)
{
a[l-i]=x[i]-'0';
}
for(int i=0;i<k;i++)
{
b[k-i]=y[i]-'0';
}
for(int i=1;i<=l;i++)
{
if(a[i]<b[i])
{
a[i+1]--;
a[i]+=10;
}
c[i]+=a[i]-b[i];
}
while(c[l]==0&&l!=1) l--;
if(j<0) cout<<'-';
for(int i=l;i>0;i--)
{
cout<<c[i];
}
cout<<endl;
return 0;
}
乘法
#include <iostream>
#include <cstring>
using namespace std;
struct HugeInt {
int len;
int num[100001];
};
HugeInt a, b, w;
char c[10001], d[10001];
void Scan_HugeInt() { //读入两个大整数
cin >> c;
cin >> d;
a.len = strlen(c);
b.len = strlen(d);
for(int i=0; i<a.len; i++) a.num[a.len - i] = c[i] - '0';
for(int i=0; i<b.len; i++) b.num[b.len - i] = d[i] - '0';
}
void Multiply() {
int x; //处理每次进位的变量
for(int i=1; i<=a.len; i++) { //a的第i位
x = 0;
for(int j=1; j<=b.len; j++) { //b的第j位
w.num[i+j-1] += a.num[i] * b.num[j] + x; //用 +=:结果与上次乘的结果相加
x = w.num[i+j-1] / 10;
w.num[i+j-1] %= 10; //进位处理
}
w.num[i+b.len] = x; //多出的最高位
}
w.len = a.len + b.len;
while(w.num[w.len] == 0 && (w.len != 1)) w.len --; //多余的0
}
int main() {
Scan_HugeInt();
Multiply();
for(int i=w.len; i>=1; i--) cout << w.num[i];
cout << endl;
return 0;
}
除法
#include <bits/stdc++.h>
using namespace std;
char string1[100],string2[100];
int number1[100],number2[100],ans[10000];
void multiply(){
int len1=strlen(string1);
int len2=strlen(string2);
memset(number2,0,sizeof(number1));
memset(number1,0,sizeof(number1));
memset(ans,0,sizeof(number1));
for (int i=0;i<len1;i++)
number1[len1-i-1]=string1[i]-'0';
for(int i=0;i<len2;i++)
number2[len2-i-1]=string2[i]-'0';
for (int i=0;i<len1;i++){
for (int j=0;j<len2;j++){
ans[i+j]+=number1[i]*number2[j];
}
}
int l=len1+len2-1;
for (int i=0;i<l;i++){
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
if (ans[l]>0) l++;
while (ans[l-1]>=10){
ans[l]=ans[l-1]/10;
ans[l-1]%=10;
l++;
}
while (ans[l-1]==0&&l>1) l--;
for (int i=l-1;i>=0;i--)
cout<<ans[i];
cout<<endl;
}
int main() {
cin>>string1>>string2;
multiply();
return 0;
}