C语言大数相加实现

主要思路:
由于大数不可存入int数组中,所以将数字转为字符输入char数组中,将两个数组数字从个位开始,模拟正常数字加法,需要进位1时设置进位标志,两数相加大于10时进位,见具体代码。

#include <string.h>
#include <stdio.h>

int main ()
{
	char a[100], b[100], c[101];   //创建用于接收两个大数的字符数组以及接收相加后结果的数组,两个100位的大数相加,最多101位
	int i, j, count = 0, alen, blen, carry = 0, temp; 
	//i,j为循环标志, count为接收结果的数组的长度, temp为数字每一位相加之后的结果, carry为进位标志,若为进位即为1否则为0 
	gets(a);
	gets(b);
	
	alen = strlen(a);
	blen = strlen(b);
	
	for (i = alen-1, j = blen-1; i>=0 && j>=0; i--,j--)  //将两个数字从个位逐加,即从数组的最高位加起 
	{
		temp = a[i] - '0' + b[j] - '0' + carry;  //模拟每一位相加的过程,将字符转换为数字字符,carry为进位标志 
		if (temp >= 10){
			temp = temp - 10;
			carry = 1;	//每一位相加后大于10即表示有进位 
		}else{
			carry = 0;
		}
		
		c[count++] = temp + '0';	//相加后的结果暂时转为字符进行处理 
	}
	
	//当逐位相加,某个数组有剩余时
	while (i >= 0){
		temp  = a[i] - '0' + carry;		//此时仍需要加carry的原因是有可能在最后一位相加后需要进位
		if (temp >= 10){
			temp = temp - 10;
			carry = 1;
		}else {
			carry = 0;
		}
		
		c[count++] = temp + '0';
		i--;
	} 
	
	while (j >= 0){
		temp = b[j] - '0' + carry;
		if (temp >= 10){
			temp = temp - 10;
			carry = 1;
		}else {
			carry = 0;
		}
		
		c[count++] = temp + '0';
		j--;
	}
	
	if (carry){	//若最后还有进位,则使最高位为1 
		c[count] = 1 + '0';
	}else {
		count--;	//由于前一步是count++,count已经自加,由于没有进位,则使count-- 
	}
	
	while (count >= 0){
		printf ("%c",c[count--]);
	} 
	
	return 0;
 } 
  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是C语言链表实现大数相加的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node { int val; struct Node *next; } Node; Node *createNode(int val) { Node *node = (Node*)malloc(sizeof(Node)); node->val = val; node->next = NULL; return node; } void insertNode(Node **head, int val) { Node *node = createNode(val); node->next = *head; *head = node; } void printList(Node *head) { while (head) { printf("%d", head->val); head = head->next; } printf("\n"); } Node *addTwoNumbers(Node *l1, Node *l2) { Node *dummyHead = createNode(0); Node *cur = dummyHead; int carry = 0; while (l1 || l2 || carry) { int sum = carry; if (l1) { sum += l1->val; l1 = l1->next; } if (l2) { sum += l2->val; l2 = l2->next; } carry = sum / 10; cur->next = createNode(sum % 10); cur = cur->next; } return dummyHead->next; } int main() { char str1[100], str2[100]; scanf("%s%s", str1, str2); int len1 = strlen(str1), len2 = strlen(str2); Node *l1 = NULL, *l2 = NULL; for (int i = len1 - 1; i >= 0; i--) { insertNode(&l1, str1[i] - '0'); } for (int i = len2 - 1; i >= 0; i--) { insertNode(&l2, str2[i] - '0'); } Node *res = addTwoNumbers(l1, l2); printList(res); return 0; } ``` 该程序首先读入两个字符串表示的大数,然后将其转换为链表形式。接着使用链表实现的加法将两个链表相加,最后输出相加结果的链表形式。 该程序使用了链表的基本操作,如创建节点、插入节点、遍历节点等。其中 `addTwoNumbers` 函数实现了链表的加法,每次取出两个链表的对应结点以及进值,计算其和并构造新的节点,最后连接到结果链表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

waxuuuu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值