长整数、高精度运算c语言代码
时间: 2023-09-16 22:15:01 浏览: 131
以下是一个高精度加法的C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
void reverse(char *s)
{
int len = strlen(s);
for (int i = 0; i < len / 2; i++) {
char temp = s[i];
s[i] = s[len - i - 1];
s[len - i - 1] = temp;
}
}
void add(char *a, char *b, char *c)
{
int len_a = strlen(a);
int len_b = strlen(b);
reverse(a);
reverse(b);
int carry = 0;
for (int i = 0; i < MAX_LEN; i++) {
int num_a = i < len_a ? a[i] - '0' : 0;
int num_b = i < len_b ? b[i] - '0' : 0;
int sum = num_a + num_b + carry;
carry = sum / 10;
c[i] = sum % 10 + '0';
}
reverse(c);
if (c[0] == '0') {
memmove(c, c + 1, strlen(c));
}
}
int main()
{
char a[MAX_LEN], b[MAX_LEN], c[MAX_LEN];
scanf("%s%s", a, b);
add(a, b, c);
printf("%s\n", c);
return 0;
}
```
该代码可以输入两个长整型数,计算它们的和并输出。
实际上,高精度运算可以通过数组来实现,对于加法、减法、乘法和除法等操作,都可以基于数组来实现。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)