当前位置:网站首页>[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;
}边栏推荐
- 2019Q4内存厂商营收排名:三星下滑5%,仅SK海力士、美光维持增长
- Log4j vulnerability is still widespread and continues to cause impact
- 自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
- Talk about MySQL transaction two-phase commit
- How to deal with high concurrency deadlock?
- What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
- MySQL data recovery process is based on binlog redolog undo
- Station B collapsed. What did the developer responsible for the repair do that night?
- Regular expression exercise
- Zibbix installation and deployment
猜你喜欢

Zibbix installation and deployment

怎么还有人问 MySQL 是如何归档数据的呢?

@RequestParam注解的详细介绍

How to deal with high concurrency deadlock?
![[day_4-review, basic concepts of objects and arrays - 1]](/img/57/750f12d5315b682d2aeec81f39dafb.png)
[day_4-review, basic concepts of objects and arrays - 1]

为什么服务端程序都需要先 listen 一下

聊聊 MySQL 事务二阶段提交

MySQL执行过程及执行顺序

LinkedList underlying source code

In addition to "adding machines", in fact, your micro service can be optimized like this
随机推荐
Technology Management - we must focus on the big and let go of the small
IDEA常用快捷键及设置方法
软件测试面试题:在windows下保存一个文本文件时会弹出保存对话框,如果为文件名建立测试用例,等价类应该怎样划分?
Box model and element positioning
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
关系型数据库的设计思想,20张图给你看的明明白白
看起来是线程池的BUG,但是我认为是源码设计不合理。
Analysis of STL source code
Unit-- read Excel
In addition to "adding machines", in fact, your micro service can be optimized like this
C language - Introduction - grammar - pointer (12)
软件测试面试题:请说出这些测试最好由那些人员完成,测试的是什么?
After sorting (bubble sorting), learn to continuously update other sorting methods
Station B collapsed. What did the developer responsible for the repair do that night?
Search, insert and delete of hash table
For 3nm and below processes, ASML new generation EUV lithography machine exposure
Simple use of enum
Monitor the running of server jar and restart script
Mobilevit learning notes
排序(冒泡排序)后面学习持续更新其它排序方法