⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subtract.h

📁 实现了任意长度整数的加减乘除以及成n次方运算
💻 H
字号:
#include<stdio.h>
#include "ADT.h"


void BasicSub(struct head *oprnd1, struct head *oprnd2, struct head *result)
{
	struct node *n1, *n2, *rslt;
	long int subs, undrflwdgt = 0;


		
	n1 = FirstLeft(oprnd1);
	n2 = FirstLeft(oprnd2);

	while(n1 != NULL && n2 != NULL)
	{
		subs = n1->data-n2->data-undrflwdgt;
		undrflwdgt = 0;
		if(subs<0)
		{
			subs = subs + 100000000;
			undrflwdgt = 1;
		}
			
		rslt = new struct node;
		rslt->data = subs;
		InsertLeft(result, rslt);
		n1 = NextLeft(n1);
		n2 = NextLeft(n2);
	}

	while(n1 != NULL)
	{
		subs = n1->data-undrflwdgt;
		rslt = new struct node;
		rslt->data = subs;
		InsertLeft(result, rslt);			
		n1 = NextLeft(n1);
	}
		
	
}

void BigSub(struct head *oprnd1, struct head *oprnd2, struct head *result)
{

	if(Sign(oprnd1) == Sign(oprnd2))
	{
		
		BasicSub(oprnd1, oprnd2, result);
		Assign(result, Sign(oprnd1));

	}
	else
	{

		BasicAdd(oprnd1, oprnd2, result);
		Assign(result, Sign(oprnd1));
		
	}
		
	
}

void SmallSub(struct head *oprnd1, struct head *oprnd2, struct head *result)
{
	if(Sign(oprnd1) == Sign(oprnd2))
	{

		BasicSub(oprnd2, oprnd1, result);
		Assign(result, !Sign(oprnd2));
	}
	else
	{ 
		BasicAdd(oprnd1, oprnd2, result);
		if(!Sign(oprnd1))
		Assign(result, Sign(oprnd1));			
	}
	
}


struct head *Subtract(struct head *oprnd1, struct head *oprnd2)
{

	struct head *result = new struct head;
	if(oprnd1->noofdigit > oprnd2->noofdigit)
	{
		
		BigSub(oprnd1, oprnd2, result);
	}

	else if(oprnd1->noofdigit < oprnd2->noofdigit)
	{
		SmallSub(oprnd1, oprnd2, result);
	}

	else
	{	
		struct node *n1, *n2;
		long  sublet;
		n1 = oprnd1->right;
		n2 = oprnd2->right;
		sublet = n1->data - n2->data;

		while(sublet == 0 && n1->rlink != NULL)
		{
			n1 = n1->rlink;
			n2 = n2->rlink;
			sublet = n1->data - n2->data;
		}

		if(sublet > 0)
		{
			
			BigSub(oprnd1, oprnd2, result);
		}
		else if(sublet < 0)
		{
			
			SmallSub(oprnd1, oprnd2, result);
		}
		else
		{	
			if(Sign(oprnd1)==Sign(oprnd2))
			{
				n1 = new struct node;
				n1->data = 0;
				result->right = n1;
				result->left = n1;
				Assign(result, true);
			}
			else
			{
				Assign(oprnd2,!Sign(oprnd2));
				result = BasicAdd(oprnd1, oprnd2);
				Assign(result, Sign(oprnd1));
				Assign(oprnd2,!Sign(oprnd2));
			}
		}
	}

    ClearZ(result);
	SetNoofDigits(result);	
	SetNoofNode(result);

	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -