设计一个程序实现两个任意长的整数求和运算。 基本要求 利用双向循环链表实现长整数的存储,每个结点含一个整型变量。任何整型变量的范围是 -(21*-1)~(21*-1)。 输入和输出形式:每4位- -组, 组间用逗号隔开。 测试数据 (1)0和0,应输出“0”。 (2) -2345,6789,-7654,3211, 应输出“-10000,0000”。 (3) 9999.9999,,000000000, 应输出9999,000,0001”。 录 (4) 1,001,000,-1,001,0001, 应输出“0”。 (5) 1,00,0001, -1 ,00,0000,应输出“1”。 实现提示 (1)每个结点中可以存放的最大整数为219-1=32767,这样才能保证两数相加不会溢出。但若按 32 768进制数存放,在+进制数与32768进制数之间的转换十分不方便,故可以在每个结点中仅存放十 进制数的4位,即不超过9 999的非负整数,整个链表视为万进制数。 (2)可以利用头结点数据域的符号代表长整数的符号。用其绝对值表示元素结点数目。相加过程 中不要破坏两个操作数链表。两操作数的头指针存于指针数组中是简化程序结构的一一种方法。 不能给 长整数位数规定上限。用c语言做
时间: 2023-06-14 17:04:26 浏览: 205
以下是基于双向循环链表实现任意长整数求和运算的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DIGITS 4 // 每个结点存放的最大位数
#define BASE 10000 // 进制数,即10的4次方
// 长整数结点
typedef struct IntegerNode {
int value; // 存放值
struct IntegerNode* prev; // 指向上一个结点的指针
struct IntegerNode* next; // 指向下一个结点的指针
} IntegerNode;
// 长整数
typedef struct Integer {
IntegerNode* head; // 指向头结点的指针
int sign; // 符号,1表示正数,-1表示负数
int length; // 结点数目,即数值的位数
} Integer;
// 初始化长整数
void init_integer(Integer* integer) {
integer->head = (IntegerNode*)malloc(sizeof(IntegerNode));
integer->head->prev = integer->head->next = integer->head;
integer->sign = 1;
integer->length = 0;
}
// 销毁长整数
void destroy_integer(Integer* integer) {
IntegerNode* node = integer->head->next;
while (node != integer->head) {
IntegerNode* next_node = node->next;
free(node);
node = next_node;
}
free(integer->head);
integer->head = NULL;
integer->sign = 1;
integer->length = 0;
}
// 在长整数的末尾添加一个结点
void append_node(Integer* integer, int value) {
IntegerNode* node = (IntegerNode*)malloc(sizeof(IntegerNode));
node->value = value;
node->prev = integer->head->prev;
node->next = integer->head;
node->prev->next = node;
node->next->prev = node;
integer->length++;
}
// 从字符串中初始化长整数
void init_integer_from_string(Integer* integer, const char* str) {
init_integer(integer);
int length = strlen(str);
int start = 0;
if (str[0] == '-') {
integer->sign = -1;
start = 1;
}
int i;
for (i = start; i < length; i += MAX_DIGITS) {
int end = i + MAX_DIGITS;
if (end > length) {
end = length;
}
int value = 0;
int j;
for (j = i; j < end; j++) {
value = value * 10 + str[j] - '0';
}
append_node(integer, value);
}
while (integer->head->prev != integer->head && integer->head->prev->value == 0) {
IntegerNode* node = integer->head->prev;
node->prev->next = integer->head;
integer->head->prev = node->prev;
free(node);
integer->length--;
}
if (integer->length == 0) {
integer->sign = 1;
}
}
// 将长整数转换为字符串
void integer_to_string(const Integer* integer, char* str) {
if (integer->length == 0) {
strcpy(str, "0");
return;
}
if (integer->sign == -1) {
*str++ = '-';
}
IntegerNode* node = integer->head->prev;
while (node != integer->head) {
int value = node->value;
sprintf(str, "%04d", value);
str += 4;
node = node->prev;
}
*str = '\0';
}
// 比较两个长整数的大小,返回1表示大于,0表示等于,-1表示小于
int compare_integer(const Integer* integer1, const Integer* integer2) {
if (integer1->sign > integer2->sign) {
return 1;
}
if (integer1->sign < integer2->sign) {
return -1;
}
IntegerNode* node1 = integer1->head->prev;
IntegerNode* node2 = integer2->head->prev;
while (node1 != integer1->head && node2 != integer2->head) {
if (node1->value > node2->value) {
return integer1->sign;
}
if (node1->value < node2->value) {
return -integer1->sign;
}
node1 = node1->prev;
node2 = node2->prev;
}
if (node1 == integer1->head && node2 == integer2->head) {
return 0;
}
if (node1 == integer1->head) {
return -integer1->sign;
}
return integer1->sign;
}
// 将长整数加上一个整数
void add_integer(Integer* integer, int value) {
if (value == 0) {
return;
}
if (integer->length == 0) {
append_node(integer, value);
return;
}
if (integer->sign == -1) {
value = -value;
}
IntegerNode* node = integer->head->prev;
int carry = 0;
while (value != 0 || carry != 0) {
if (node == integer->head) {
append_node(integer, 0);
node = integer->head->prev;
}
int sum = node->value + value % BASE + carry;
node->value = sum % BASE;
carry = sum / BASE;
value /= BASE;
node = node->prev;
}
while (integer->head->prev != integer->head && integer->head->prev->value == 0) {
IntegerNode* node = integer->head->prev;
node->prev->next = integer->head;
integer->head->prev = node->prev;
free(node);
integer->length--;
}
if (integer->length == 0) {
integer->sign = 1;
}
}
// 将长整数加上另一个长整数
void add_integer_integer(Integer* integer1, const Integer* integer2) {
if (integer1->sign == integer2->sign) {
IntegerNode* node1 = integer1->head->prev;
IntegerNode* node2 = integer2->head->prev;
int carry = 0;
while (node1 != integer1->head && node2 != integer2->head) {
int sum = node1->value + node2->value + carry;
node1->value = sum % BASE;
carry = sum / BASE;
node1 = node1->prev;
node2 = node2->prev;
}
while (node2 != integer2->head) {
int sum = node2->value + carry;
append_node(integer1, sum % BASE);
carry = sum / BASE;
node2 = node2->prev;
}
if (carry != 0) {
append_node(integer1, carry);
}
} else if (integer1->sign == 1) {
Integer integer3;
init_integer(&integer3);
integer3.sign = -1;
IntegerNode* node1 = integer1->head->prev;
IntegerNode* node2 = integer2->head->prev;
int borrow = 0;
while (node1 != integer1->head && node2 != integer2->head) {
int diff = node1->value - node2->value - borrow;
if (diff < 0) {
diff += BASE;
borrow = 1;
} else {
borrow = 0;
}
append_node(&integer3, diff);
node1 = node1->prev;
node2 = node2->prev;
}
while (node2 != integer2->head) {
int diff = -node2->value - borrow;
if (diff < 0) {
diff += BASE;
borrow = 1;
} else {
borrow = 0;
}
append_node(&integer3, diff);
node2 = node2->prev;
}
while (node1 != integer1->head) {
int diff = node1->value - borrow;
if (diff < 0) {
diff += BASE;
borrow = 1;
} else {
borrow = 0;
}
append_node(&integer3, diff);
node1 = node1->prev;
}
while (integer3.head->prev != integer3.head && integer3.head->prev->value == 0) {
IntegerNode* node = integer3.head->prev;
node->prev->next = integer3.head;
integer3.head->prev = node->prev;
free(node);
integer3.length--;
}
if (integer3.length == 0) {
integer3.sign = 1;
}
destroy_integer(integer1);
*integer1 = integer3;
} else {
Integer integer3;
init_integer(&integer3);
integer3.sign = 1;
IntegerNode* node1 = integer1->head->prev;
IntegerNode* node2 = integer2->head->prev;
int borrow = 0;
while (node1 != integer1->head && node2 != integer2->head) {
int diff = node2->value - node1->value - borrow;
if (diff < 0) {
diff += BASE;
borrow = 1;
} else {
borrow = 0;
}
append_node(&integer3, diff);
node1 = node1->prev;
node2 = node2->prev;
}
while (node1 != integer1->head) {
int diff = -node1->value - borrow;
if (diff < 0) {
diff += BASE;
borrow = 1;
} else {
borrow = 0;
}
append_node(&integer3, diff);
node1 = node1->prev;
}
while (node2 != integer2->head) {
int diff = node2->value - borrow;
if (diff < 0) {
diff += BASE;
borrow = 1;
} else {
borrow = 0;
}
append_node(&integer3, diff);
node2 = node2->prev;
}
while (integer3.head->prev != integer3.head && integer3.head->prev->value == 0) {
IntegerNode* node = integer3.head->prev;
node->prev->next = integer3.head;
integer3.head->prev = node->prev;
free(node);
integer3.length--;
}
if (integer3.length == 0) {
integer3.sign = 1;
}
destroy_integer(integer1);
*integer1 = integer3;
}
}
int main() {
char str1[100], str2[100];
printf("请输入两个长整数(每4位一组,组间用逗号隔开):\n");
scanf("%s %s", str1, str2);
Integer integer1, integer2;
init_integer_from_string(&integer1, str1);
init_integer_from_string(&integer2, str2);
add_integer_integer(&integer1, &integer2);
char result[100];
integer_to_string(&integer1, result);
printf("两数之和为:%s\n", result);
destroy_integer(&integer1);
destroy_integer(&integer2);
return 0;
}
```
程序的核心是`add_integer_integer`函数,它实现了两个长整数相加的功能。当两个长整数符号相同时,它们的绝对值相加即可得到结果,同时需要考虑进位的情况。当两个长整数符号不同时,可以将它们都转换为正数,然后求它们的差,最后根据符号的不同来确定结果的符号。在求差的过程中,需要考虑借位的情况。
阅读全文