当前位置:网站首页>[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;
}边栏推荐
- Software test interview questions: the steps to write test cases by drawing cause and effect diagrams are___ And transforming the cause and effect diagram into a state diagram in five steps. What are
- DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication
- Internal class (detailed explanation of four internal classes)
- 2019Q4内存厂商营收排名:三星下滑5%,仅SK海力士、美光维持增长
- Form of objects in memory & memory allocation mechanism
- C language - Introduction - grammar - pointer (12)
- Recursion / backtracking (Part 1)
- Log4j 漏洞仍普遍存在,并持续造成影响
- Software testing interview question: how many strategies are there for system testing?
- 最高7.5Gbps!全球首款5nm 5G基带骁龙X60发布:支持聚合全部主要频段!
猜你喜欢

8000字讲透OBSA原理与应用实践

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

How to realize a good knowledge management system?

B站崩了,如果我们是那晚负责修复的开发人员

Read Plato farm's eplato and the reason for its high premium

@Can component be used on the same class as @bean?

It seems to be a bug of thread pool, but I think the source code design is unreasonable.
![[2022 Niuke multi School Game 2] k-link with bracket sequence I](/img/95/9d6710bfb7b9282b4a06a5f61a1f08.png)
[2022 Niuke multi School Game 2] k-link with bracket sequence I

关系型数据库的设计思想,20张图给你看的明明白白

Lvs+kept highly available cluster
随机推荐
软件测试面试题:系统测试的策略有多少种?
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
2021-11-05 understand main method syntax, code block and final keyword
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
一口气学完 Redis 集群方案
@Autowired注解与@Resource注解的区别
V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
@Detailed introduction of requestparam annotation
Monitor the running of server jar and restart script
Analysis of STL source code
Under the epidemic, the mobile phone supply chain and offline channels are blocked! Sales plummeted and inventory was serious!
怎么还有人问 MySQL 是如何归档数据的呢?
高并发遇到死锁了,如何搞?
Qmodbus library is used, and it is written as ROS node publishing topic and program cmakelist
对象在内存中存在形式&内存分配机制
Shengyang technology officially launched the remote voiceprint health return visit service system!
Finish learning redis cluster solution at one go
@The difference between Autowired annotation and @resource annotation
声扬科技正式上线闻声远程声纹健康回访服务系统!