高精度计算器
2025-06-07 19:37:28
发布于:江苏
质量非常高的高精度计算器
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
char c;
long long n;
char a[10005], b[10005];
void init(int a[], char s[]) {
a[0] = strlen(s);
for(int i = 1; i <= a[0]; i++) {
a[i] = s[a[0] - i] - '0';
}
}
void print(int a[]) {
int i;
if(a[0] == 0) {
cout << 0 << endl; return;
}
for(i = a[0]; i > 0; i--) cout << a[i];
return;
}
int compare(int a[], int b[]) {
int i;
if(a[0] > b[0]) return 1;
if(a[0] < b[0]) return -1;
for(i = a[0]; i > 0; i--) {
if(a[i] > b[i]) return 1;
if(a[i] < b[i]) return -1;
}
return 0;
}
void high_technique_minus(int a[], int b[]) {
int flag, i;
flag = compare(a, b);
if(flag == 0) {
a[0] = 0; return;
}
if(flag == 1) {
for(i = 1; i <= a[0]; i++) {
if(a[i] < b[i]) {
a[i + 1]--; a[i] += 10;
}
a[i] -= b[i];
}
while(a[0] > 1 && a[a[0]] == 0) a[0]--;
return;
}
}
void number_copy(int p[], int q[], int delta) {
for(int i = 1; i <= p[0]; i++) q[i + delta - 1] = p[i];
q[0] = p[0] + delta - 1;
}
void high_technique_divite(int a[], int b[], int c[]) {
int i, tmp[1005];
c[0] = a[0] - b[0] + 1;
for(i = c[0]; i > 0; i--) {
memset(tmp, 0, sizeof(tmp));
number_copy(b, tmp, i);
while(compare(a, tmp) >= 0) {
c[i]++;
high_technique_minus(a, tmp);
}
}
while(c[0] > 1 && c[c[0]] == 0) c[0]--;
return;
}
void high_involved_c5_11_including_jia(char a[], char b[]) {
int x = 0, lena = 0, lenb = 0, lenc = 0;
int a1[10005], b1[10005], c[10005];
memset(a1, 0, sizeof(a1)); memset(b1, 0, sizeof(b1)); memset(c, 0, sizeof(c));
lena = strlen(a);
lenb = strlen(b);
for(int i = 1; i <= lena; i++) {
a1[i] = a[lena - i] - '0';
}
for(int i = 1; i <= lenb; i++) {
b1[i] = b[lenb - i] - '0';
}
lenc = 1;
while(lenc <= lena || lenc <= lenb) {
c[lenc] = a1[lenc] + b1[lenc] + x;
x = c[lenc] / 10;
c[lenc] %= 10;
lenc++;
}
c[lenc] = x;
while(c[lenc] == 0 && lenc > 1) lenc--;
for(int i = lenc; i >= 1; i--) cout << c[i];
cout << endl;
}
void high_involved_c5_11_including_jian(char a[], char b[]) {
int lena = 0, lenb = 0, lenc = 0, i = 0;
char n[10005];
int a1[10005], b1[10005], c[10005];
memset(a1, 0, sizeof(a1)); memset(b1, 0, sizeof(b1)); memset(c, 0, sizeof(c));
if(strlen(a) < strlen(b) || (strlen(a) == strlen(b) && strcmp(a, b) < 0)) {
strcpy(n, a);
strcpy(a, b);
strcpy(b, n);
cout << '-';
}
lena = strlen(a); lenb = strlen(b);
for(i = 0; i <= lena - 1; i++) {
a1[lena - i] = int(a[i] - '0');
}
for(i = 0; i <= lenb - 1; i++) {
b1[lenb - i] = int(b[i] - '0');
}
i = 1;
while(i <= lena || i <= lenb) {
if(a1[i] < b1[i]) {
a1[i] += 10;
a1[i + 1]--;
}
c[i] = a1[i] - b1[i];
i++;
}
lenc = i;
while(c[lenc] == 0 && lenc > 1) lenc--;
for(i = lenc; i >= 1; i--) {
cout << c[i];
}
cout << endl;
}
void high_involved_c5_11_including_chen(char a1[], char b1[]) {
int lena = 0, lenb = 0, lenc = 0, x = 0;
int a[10005], b[10005], c[10005];
memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c));
lena = strlen(a1); lenb = strlen(b1);
for(int i = 0; i <= lena - 1; i++) {
a[lena - i] = a1[i] - '0';
}
for(int i = 0; i <= lenb - 1; i++) {
b[lenb - i] = b1[i] - '0';
}
for(int i = 1; i <= lena; i++) {
x = 0;
for(int j = 1; j <= lenb; j++) {
c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];
x = c[i + j - 1] / 10;
c[i + j - 1] %= 10;
}
c[i + lenb] = x;
}
lenc = lena + lenb;
while(c[lenc] == 0 && lenc > 1) lenc--;
for(int i = lenc; i >= 1; i--) {
cout << c[i];
}
cout << endl;
}
void high_involved_c5_11_including_chu(char a1[], char b1[]) {
int a[10005]; memset(a, 0, sizeof(a));
int b[10005]; memset(b, 0, sizeof(b));
int c[10005]; memset(c, 0, sizeof(c));
init(a, a1); init(b, b1);
high_technique_divite(a, b, c);
print(c);
cout << ' ';
print(a);
}
int main() {
cout << "欢迎使用高精度计算器!" << endl;
cout << "请问您一共要计算多少道题:";
cin >> n;
cout << endl;
cout << "好的,计算马上开始!" << endl;
cout << "除法计算的是商和余数,中间会用空格隔开" << endl;
cout << "注意数与符号之间要有空格,并且只能输入两个数哦!" << endl << endl;
system("pause");
system("cls");
for(int i = 1; i <= n; i++) {
while(true) {
cout << "第" << i << '/' << n << "题" << endl << endl;
cin >> a >> c >> b;
if((c == '+') + (c == '-') + (c == '*') + (c == '/') == 0 || c == '/' && b[0] == '0' && b[1] == '\0') {
cout << "输入错误,重输!" << endl << endl;
system("pause");
system("cls");
}
else {
break;
}
}
cout << a << ' ' << c << ' ' << b << " = ";
if((strlen(a) < strlen(b) || (strlen(a) == strlen(b) && strcmp(a, b) < 0)) && c == '/') {
cout << 0 << ' ' << a << endl;
system("pause"); system("cls");
continue;
}
if(c == '+') high_involved_c5_11_including_jia(a, b);
if(c == '-') high_involved_c5_11_including_jian(a, b);
if(c == '*') high_involved_c5_11_including_chen(a, b);
if(c == '/') high_involved_c5_11_including_chu(a, b);
cout << endl;
system("pause");
system("cls");
}
cout << "计算结束了,欢迎您下次再来,再见!" << endl;
return 0;
}
都到这儿了,点个赞呗~
这里空空如也
有帮助,赞一个