动态链表存贮数据长整数、高精度运算c语言代码

时间: 2023-10-10 11:11:15 浏览: 60
以下是动态链表存储数据长整数、高精度运算的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 判断链表是否为空 int isListEmpty(struct ListNode *head) { return head == NULL; } // 在链表尾部插入节点 void insertNode(struct ListNode **head, int val) { struct ListNode *newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val = val; newNode->next = NULL; if (*head == NULL) { *head = newNode; return; } struct ListNode *cur = *head; while (cur->next != NULL) { cur = cur->next; } cur->next = newNode; } // 从字符串转换为链表 struct ListNode* listFromString(char *str) { int len = strlen(str); struct ListNode *head = NULL; for (int i = len - 1; i >= 0; i--) { int val = str[i] - '0'; insertNode(&head, val); } return head; } // 从链表转换为字符串 char* stringFromList(struct ListNode *head) { char *str = (char*)malloc(sizeof(char) * 10000); int index = 0; while (head != NULL) { str[index++] = head->val + '0'; head = head->next; } str[index] = '\0'; return str; } // 高精度加法 struct ListNode* add(struct ListNode* l1, struct ListNode* l2) { struct ListNode *result = NULL; int carry = 0; while (l1 != NULL || l2 != NULL || carry != 0) { int sum = carry; if (l1 != NULL) { sum += l1->val; l1 = l1->next; } if (l2 != NULL) { sum += l2->val; l2 = l2->next; } carry = sum / 10; sum %= 10; insertNode(&result, sum); } return result; } // 高精度减法 struct ListNode* subtract(struct ListNode* l1, struct ListNode* l2) { struct ListNode *result = NULL; int borrow = 0; while (l1 != NULL || l2 != NULL) { int sub = borrow; if (l1 != NULL) { sub += l1->val; l1 = l1->next; } if (l2 != NULL) { sub -= l2->val; l2 = l2->next; } if (sub < 0) { sub += 10; borrow = -1; } else { borrow = 0; } insertNode(&result, sub); } while (!isListEmpty(result) && result->val == 0) { struct ListNode *temp = result; result = result->next; free(temp); } return result; } // 高精度乘法 struct ListNode* multiply(struct ListNode* l1, struct ListNode* l2) { struct ListNode *result = NULL; struct ListNode *cur1 = l1; while (cur1 != NULL) { struct ListNode *cur2 = l2; struct ListNode *temp = NULL; int carry = 0; while (cur2 != NULL || carry != 0) { int mul = carry; if (cur2 != NULL) { mul += cur1->val * cur2->val; cur2 = cur2->next; } carry = mul / 10; mul %= 10; insertNode(&temp, mul); } int zeros = 0; while (zeros--) { insertNode(&temp, 0); } result = add(result, temp); cur1 = cur1->next; } return result; } // 高精度除法 struct ListNode* divide(struct ListNode* l1, struct ListNode* l2) { struct ListNode *result = NULL; struct ListNode *cur1 = l1; while (cur1 != NULL) { struct ListNode *temp = NULL; int quotient = 0; while (1) { temp = multiply(l2, listFromString("0")); for (int i = 0; i < quotient; i++) { insertNode(&temp, 0); } if (cur1->val < stringFromList(temp)[0] - '0') { break; } quotient++; } result = multiply(result, listFromString("10")); insertNode(&result, quotient); cur1 = cur1->next; } return result; } int main() { char *str1 = "123456789"; char *str2 = "987654321"; struct ListNode *l1 = listFromString(str1); struct ListNode *l2 = listFromString(str2); struct ListNode *sum = add(l1, l2); struct ListNode *diff = subtract(l1, l2); struct ListNode *product = multiply(l1, l2); struct ListNode *quotient = divide(l1, l2); printf("Sum: %s\n", stringFromList(sum)); printf("Difference: %s\n", stringFromList(diff)); printf("Product: %s\n", stringFromList(product)); printf("Quotient: %s\n", stringFromList(quotient)); return 0; } ```

相关推荐

最新推荐

recommend-type

C语言:一元多项式加减法运算(链表 附答案).docx

C语言链表的入门题,里面提供了两种思路供参考,用链表来实现一元多项式的加减法,并按照一定规律输出。也是练习链表和排序算法的一道小实验,初学链表的小伙伴可以参考参考噢
recommend-type

C语言数据结构实现链表逆序并输出

主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下
recommend-type

C语言链表题目(附答案).docx

大一初学C语言时的期末作业,涉及到链表的建立和功能的实现,涉及指针、函数、动态结构建立等方面的知识,初学者可以参考参考尝试尝试哟!!!
recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

C语言基于循环链表解决约瑟夫环问题的方法示例

主要介绍了C语言基于循环链表解决约瑟夫环问题的方法,简单描述了约瑟夫环问题并结合实例形式分析了C语言使用循环链表解决约瑟夫环问题的具体操作技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。