【大数四则运算】——线性表 [习题描述] 设计—个实现任意长的整数进行四则运算和幂次运算的演示程序。 [基本要求] 利用双向循环链表实现大数的存储,每个结点含一个整型变量。 [实现提示] 实现原理:任何一个数都可以表示成指数形式,如下所示: N=nEe (0=<n 的绝对值<=1,e 为 10 的指数幂) 例如 100 可以表示成 1E2,1001 可以表示成 1.01E3
时间: 2024-03-09 18:47:48 浏览: 119
大数的四则运算(附代码)
这是一道关于大数四则运算的题目,需要使用双向循环链表来存储大数,每个节点包含一个整型变量,实现指数形式的表示法。
具体实现思路如下:
1. 定义一个结构体表示链表的节点,包含一个整型变量和两个指针分别指向前驱节点和后继节点。
2. 定义一个函数来创建链表,该函数使用一个字符串表示大数,将其转化为链表的形式存储。
3. 定义四个函数分别实现大数的加、减、乘、除四种运算,这四个函数的实现都需要对链表进行操作。
4. 定义一个函数实现大数的幂次运算,该函数使用快速幂算法实现,也需要对链表进行操作。
5. 定义一个函数实现链表的输出,将链表转换为指数形式输出。
具体实现过程中需要注意链表的初始化、插入、删除等操作,以及处理进位和借位的情况。同时,为了方便计算,可以将大数转化为以10的指数幂为底的指数形式,这样可以方便地进行数位的对齐和运算。
以下是一个可能的C语言实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BASE 10000
#define MAX_DIGITS 100
typedef struct Node {
int val;
struct Node *prev, *next;
} Node;
typedef struct {
Node *head, *tail;
int len;
} BigInt;
Node *new_node(int val) {
Node *node = (Node *)malloc(sizeof(Node));
node->val = val;
node->prev = node->next = NULL;
return node;
}
BigInt *new_bigint() {
BigInt *bigint = (BigInt *)malloc(sizeof(BigInt));
bigint->head = bigint->tail = NULL;
bigint->len = 0;
return bigint;
}
void insert_front(BigInt *bigint, int val) {
Node *node = new_node(val);
if (bigint->head == NULL) {
bigint->head = bigint->tail = node;
} else {
node->next = bigint->head;
bigint->head->prev = node;
bigint->head = node;
}
bigint->len++;
}
void insert_back(BigInt *bigint, int val) {
Node *node = new_node(val);
if (bigint->tail == NULL) {
bigint->head = bigint->tail = node;
} else {
node->prev = bigint->tail;
bigint->tail->next = node;
bigint->tail = node;
}
bigint->len++;
}
void remove_front(BigInt *bigint) {
if (bigint->head == NULL) return;
Node *node = bigint->head;
bigint->head = node->next;
if (bigint->head != NULL) bigint->head->prev = NULL;
else bigint->tail = NULL;
bigint->len--;
free(node);
}
void remove_back(BigInt *bigint) {
if (bigint->tail == NULL) return;
Node *node = bigint->tail;
bigint->tail = node->prev;
if (bigint->tail != NULL) bigint->tail->next = NULL;
else bigint->head = NULL;
bigint->len--;
free(node);
}
void clear(BigInt *bigint) {
while (bigint->len > 0) remove_front(bigint);
}
BigInt *from_str(char *str) {
BigInt *bigint = new_bigint();
int len = strlen(str);
int i, j, n;
for (i = len - 1; i >= 0; i -= 4) {
int val = 0;
for (j = 0, n = 1; j < 4 && i - j >= 0; j++, n *= 10) {
val += (str[i - j] - '0') * n;
}
insert_front(bigint, val);
}
while (bigint->len > 1 && bigint->head->val == 0) remove_front(bigint);
return bigint;
}
char *to_str(BigInt *bigint) {
char *str = (char *)malloc(sizeof(char) * (bigint->len * 4 + 1));
int i = 0;
Node *node = bigint->head;
while (node != NULL) {
if (node != bigint->head) {
sprintf(str + i, "%04d", node->val);
i += 4;
} else {
sprintf(str + i, "%d", node->val);
i += (node->val == 0 ? 1 : (int)log10(node->val) + 1);
}
node = node->next;
}
str[i] = '\0';
return str;
}
int compare(BigInt *a, BigInt *b) {
if (a->len != b->len) return (a->len > b->len ? 1 : -1);
Node *a_node = a->head, *b_node = b->head;
while (a_node != NULL && b_node != NULL) {
if (a_node->val != b_node->val) return (a_node->val > b_node->val ? 1 : -1);
a_node = a_node->next;
b_node = b_node->next;
}
return 0;
}
void add(BigInt *a, BigInt *b, BigInt *result) {
int carry = 0;
Node *a_node = a->tail, *b_node = b->tail;
while (a_node != NULL || b_node != NULL || carry != 0) {
int sum = carry;
if (a_node != NULL) sum += a_node->val;
if (b_node != NULL) sum += b_node->val;
insert_front(result, sum % BASE);
carry = sum / BASE;
if (a_node != NULL) a_node = a_node->prev;
if (b_node != NULL) b_node = b_node->prev;
}
}
void subtract(BigInt *a, BigInt *b, BigInt *result) {
int borrow = 0;
Node *a_node = a->tail, *b_node = b->tail;
while (a_node != NULL || b_node != NULL || borrow != 0) {
int diff = borrow;
if (a_node != NULL) diff += a_node->val;
if (b_node != NULL) diff -= b_node->val;
if (diff < 0) {
diff += BASE;
borrow = -1;
} else {
borrow = 0;
}
insert_front(result, diff);
if (a_node != NULL) a_node = a_node->prev;
if (b_node != NULL) b_node = b_node->prev;
}
while (result->len > 1 && result->head->val == 0) remove_front(result);
}
void multiply(BigInt *a, BigInt *b, BigInt *result) {
clear(result);
int i, j;
for (i = 0; i < a->len; i++) {
int carry = 0;
BigInt *temp = new_bigint();
for (j = 0; j < i; j++) insert_back(temp, 0);
Node *b_node = b->tail;
while (b_node != NULL || carry != 0) {
int prod = carry;
if (b_node != NULL) prod += b_node->val * a->head->val;
insert_front(temp, prod % BASE);
carry = prod / BASE;
if (b_node != NULL) b_node = b_node->prev;
}
add(result, temp, result);
clear(temp);
}
}
void divide(BigInt *a, BigInt *b, BigInt *quo, BigInt *rem) {
clear(quo);
clear(rem);
BigInt *dividend = new_bigint();
Node *node = a->head;
while (node != NULL) {
insert_back(dividend, node->val);
node = node->next;
int i;
for (i = 0; i < BASE; i++) {
multiply(b, rem, rem);
add(rem, new_bigint(), rem);
add(rem, new_node(i), rem);
if (compare(rem, b) >= 0) break;
}
insert_front(quo, i);
subtract(rem, b, rem);
}
while (quo->len > 1 && quo->head->val == 0) remove_front(quo);
while (rem->len > 1 && rem->head->val == 0) remove_front(rem);
free(dividend);
}
void pow_helper(BigInt *a, int n, BigInt *result) {
if (n == 0) {
insert_front(result, 1);
return;
}
pow_helper(a, n / 2, result);
multiply(result, result, result);
if (n % 2 == 1) {
BigInt *temp = new_bigint();
copy(a, temp);
multiply(result, temp, result);
clear(temp);
}
}
void pow(BigInt *a, int n, BigInt *result) {
clear(result);
pow_helper(a, n, result);
}
int main() {
BigInt *a = from_str("123456789012345678901234567890");
BigInt *b = from_str("987654321098765432109876543210");
BigInt *c = new_bigint();
BigInt *d = new_bigint();
BigInt *e = new_bigint();
add(a, b, c);
subtract(a, b, d);
multiply(a, b, e);
char *a_str = to_str(a);
char *b_str = to_str(b);
char *c_str = to_str(c);
char *d_str = to_str(d);
char *e_str = to_str(e);
printf("%s + %s = %s\n", a_str, b_str, c_str);
printf("%s - %s = %s\n", a_str, b_str, d_str);
printf("%s * %s = %s\n", a_str, b_str, e_str);
pow(a, 3, c);
char *c_pow_str = to_str(c);
printf("%s ^ 3 = %s\n", a_str, c_pow_str);
free(a_str);
free(b_str);
free(c_str);
free(d_str);
free(e_str);
free(c_pow_str);
clear(a);
clear(b);
clear(c);
clear(d);
clear(e);
free(a);
free(b);
free(c);
free(d);
free(e);
return 0;
}
```
阅读全文