当前位置:网站首页>[C language] high precision addition, subtraction, multiplication and division template
[C language] high precision addition, subtraction, multiplication and division template
2022-07-27 21:55:00 【Dream and wake up, happy and carefree】
Actually csdn There's a lot of , But I'm not used to it , Debugging this afternoon was very unpleasant , I wrote one myself , spare .
Conventional thinking , Reverse order conversion int, After calculation, turn back in reverse order char[]
And the fastest way ,bitset, Direct bit operation ( But I will not )
void add(const char str1[], const char str2[], char ans[])
{
#define _MAX_WID 500
// Use guide :str1+str2=ans
// The core of the idea : Turn it into int, Implement carry , And then it becomes char
// The built-in length can be modified , If memory bursts, use global
int a[_MAX_WID], b[_MAX_WID];//a,b For temporary storage int
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = (len1 > len2 ? len1 : len2) + 1;// Take the length as the maximum +1
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
// Enter in reverse order , Make sure the tail is aligned
//char turn int, No need to add \0
for (int i = len1 - 1; i >= 0; i--)
a[len1 - i - 1] = str1[i] - '0';
for (int i = len2 - 1; i >= 0; i--)
b[len2 - i - 1] = str2[i] - '0';
// Operation and carry
for (int i = 0; i < len; i++)
{
a[i] = a[i] + b[i];
if (a[i] > 9)
{
a[i + 1] += 1;
a[i] -= 10;
}
}
// Remove excess from the head 0( Because the reverse order is actually a Ending )
while (a[len - 1] == 0 && len > 1)
len--;
// In reverse order int Restore char, Add \0
ans[len] = '\0';
for (int i = len - 1; i >= 0; i--)
ans[len - 1 - i] = a[i] + '0';
}bool minus(const char str1[], const char str2[], char ans[])
{
// Use guide :str1-str2=ans, If it's positive , Just go back to true
// The core of the idea : Turn it into int, Implement carry , And then it becomes char
// The built-in length can be modified , If memory bursts, use global
#define _MAX_WID 100
int a[_MAX_WID], b[_MAX_WID];//a,b For temporary storage int
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = len1 > len2 ? len1 : len2;
bool ispositive = 1;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
// Compare the size , Enter in reverse order , Make sure the tail is aligned
//char turn int, No need to add \0.
if (len1 < len2 || ((len1 == len2 && strcmp(str1, str2) < 0)))// Small decrease : The number of digits is small or the code order of the same digit is small
{
ispositive = 0;
// The previous small ,str2->a
for (int i = len2 - 1; i >= 0; i--)
a[len2 - 1 - i] = str2[i] - '0';
for (int i = len1 - 1; i >= 0; i--)
b[len1 - i - 1] = str1[i] - '0';
}
else
{
// The previous big ,str1->a
for (int i = len1 - 1; i >= 0; i--)
a[len1 - i - 1] = str1[i] - '0';
for (int i = len2 - 1; i >= 0; i--)
b[len2 - i - 1] = str2[i] - '0';
}
// Operation and carry
for (int i = 0; i < len; i++)
{
a[i] = a[i] - b[i];
if (a[i] < 0)
{
a[i + 1] -= 1;
a[i] += 10;
}
}
// Remove excess from the head 0( Because the reverse order is actually a Ending )
while (a[len - 1] == 0 && len > 1)
len--;
// In reverse order int Restore char, Add \0
ans[len] = '\0';
for (int i = len - 1; i >= 0; i--)
ans[len - 1 - i] = a[i] + '0';
return ispositive;
}
void multiple(const char str1[], const char str2[], char ans[])
{
// Use guide :str1*str2=ans| Pay attention to the outside char[],ans yes str Twice the width
// The core of the idea : Turn it into int, Implement carry , And then it becomes char
// The built-in length can be modified , If memory bursts, use global .
#define WIDTH 100
int a[WIDTH], b[WIDTH], c[2 * WIDTH];
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = len1 + len2;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
//char turn int, In reverse order
for (int i = len1 - 1; i >= 0; i--)
a[len1 - 1 - i] = str1[i] - '0';
for (int i = len2 - 1; i >= 0; i--)
b[len2 - 1 - i] = str2[i] - '0';
// Multiplication :( although ij There is no difference in order , But I still distinguish )
//j Is the following number , For weight ,i It's the number above , Right every time j Traverse once
for (int j = 0; j < len2; j++)
for (int i = 0; i < len1; i++)
c[i + j] += a[i] * b[j];
// carry , The first len position (c[len-1]) Don't go in , This is the upper limit of digits
for (int i = 0; i < len - 1; i++)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
// Head off
while (c[len - 1] == 0 && len > 1)
len--;
//int turn char, Supplementary string tail
ans[len] = '\0';
for (int i = len - 1; i >= 0; i--)
ans[len - 1 - i] = c[i] + '0';
}int divide(char str[], int divisor, char ans[])
{
// Divide large numbers by decimals
// Use guide :str/divisor=ans return remain
// The core of the idea : Turn it into int, Implement carry , And then it becomes char
// The built-in length can be modified , If memory bursts, use global .
#define WIDTH 100
int a[WIDTH], b[WIDTH];
int remain = 0;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int len = strlen(str);
//char turn int
for (int i = 0; i < len; i++)
a[i] = str[i] - '0';
// Calculate the remainder
for (int i = 0; i < len; i++)
{
b[i] = (remain * 10 + a[i]) / divisor;
remain = (remain * 10 + a[i]) % divisor;
}
// Find the starting point , Store in ans, Add \0,
int start = 0;
while (b[start] == 0 && start < len - 1)
start++;
for (int i = start, j = 0; i < len; i++, j++)
ans[j] = b[i] + '0';
return remain;
}边栏推荐
- 如何实现一个好的知识管理系统?
- 对L1正则化和L2正则化的理解[通俗易懂]
- 看起来是线程池的BUG,但是我认为是源码设计不合理。
- Box model and element positioning
- Up to 7.5gbps! The world's first 5nm 5g baseband snapdragon X60 release: support the aggregation of all major bands!
- 对象在内存中存在形式&内存分配机制
- 数组扩容、排序、嵌套语句应用
- Mobilevit learning notes
- Microsoft store can't download apps, vs2019 can't download plug-ins solution
- 高并发遇到死锁了,如何搞?
猜你喜欢

@RequestParam注解的详细介绍

How can anyone ask how MySQL archives data?
Excalidraw:很好用的在线、免费「手绘」虚拟白板工具

JVM memory model interview summary

Station B collapsed. If we were the developer responsible for the repair that night

单核CPU, 1G内存,也能做JVM调优吗?

Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design

B站崩了,那晚负责修复的开发人员做了什么?

枚举Enum的简单使用

Idea connects to MySQL database and performs SQL query operations
随机推荐
聊聊 MySQL 事务二阶段提交
CBAM learning notes
Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
2021-11-05类变量和类方法的理解
day 1 - day 4
怎么还有人问 MySQL 是如何归档数据的呢?
Box model and element positioning
8000 word explanation of OBSA principle and application practice
After sorting (bubble sorting), learn to continuously update other sorting methods
Cocoapods reload
Software test interview question: please say who is the best person to complete these tests, and what is the test?
软件测试面试题:系统测试的策略有多少种?
Simple use of enum
B站崩了,如果我们是那晚负责修复的开发人员
2021-11-05 understanding of class variables and class methods
Can JVM tuning be done with single core CPU and 1G memory?
为什么要使用MQ消息中间件?这几个问题必须拿下
零钱通项目(两个版本)含思路详解
软件测试面试题:软件测试项目从什么时候开始?为什么?
Idea connects to MySQL database and performs SQL query operations