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

📄 divide.h

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




struct head *PlusZeros(struct head *headnode, int num)
{
	struct node *nodepointer = new struct node; 
	int dig;
	int i;
	char a[9] = "";
	string buffer = "";
	string result = "";
	string substring ="";

	//digit to stream
	nodepointer = FirstLeft(headnode);
	while( nodepointer!= NULL)
	{
		ltoa(nodepointer->data, a, 10);
		buffer.assign(a);
		dig = Digit(nodepointer->data);
		for( ; dig<8; dig++)
		{
			buffer.insert(0,"0");
		}
		result.insert(0,buffer);
			
		nodepointer = NextLeft(nodepointer);
	}

	//add 0s to the end
	for(int i=0; i<num; i++)
	{
		result.append("0");
	}
	
	//string to digit
	MakeNull(headnode);
	int j = result.length();

	for( ;j>8 ;j=j-8)
	{
		substring = result.substr(j-8,8);
		const char *b = substring.c_str();
		nodepointer = new struct node;
		nodepointer->data = strtol(b, NULL, 10);
		InsertLeft(headnode,nodepointer);
	}

	substring = result.substr(0,j);
	const char *c = substring.c_str();
	nodepointer = new struct node;
	nodepointer->data = strtol(c, NULL, 10);
	InsertLeft(headnode,nodepointer);
	ClearZ(headnode);

	SetNoofDigits(headnode);
	SetNoofNode(headnode);

	return headnode;
}





struct head *SubOneZero(struct head *headnode)
{
	struct node *nodepointer = new struct node; 	
	int dig;
	int i;
	char a[9] = "";
	string buffer = "";
	string result = "";
	string substring ="";

	//digit to stream
	nodepointer = FirstLeft(headnode);
	while( nodepointer!= NULL)
	{
		ltoa(nodepointer->data, a, 10);
		buffer.assign(a);
		dig = Digit(nodepointer->data);
		for( ; dig<8; dig++)
		{
			buffer.insert(0,"0");
		}
		result.insert(0,buffer);
			
		nodepointer = NextLeft(nodepointer);
	}


	//subtract 0

	result.erase(result.length()-1,1);
	

	//string to digit
	MakeNull(headnode);
	int j = result.length();
	for( ;j>8 ;j=j-8)
	{
		substring = result.substr(j-8,8);
		const char *b = substring.c_str();
		nodepointer = new struct node;
		nodepointer->data = strtol(b, NULL, 10);
		InsertLeft(headnode,nodepointer);
	}

	substring = result.substr(0,j);
	const char *c = substring.c_str();
	nodepointer = new struct node;
	nodepointer->data = strtol(c, NULL, 10);
	InsertLeft(headnode,nodepointer);
	ClearZ(headnode);

	SetNoofDigits(headnode);
	SetNoofNode(headnode);

	return headnode;
}




struct head *Divide(struct head *oprnd1, struct head *oprnd2)
{
	int subnum;
	bool o1, o2;
	char a[2]= "";
	string div = "";
	struct head *oprnd3 = new struct head;
	struct head *result  = new struct head;
	struct head *buffer = new struct head;
	
	
	o1 = Sign(oprnd1);
	o2 = Sign(oprnd2);
	if(o1 == o2)
	{
		Assign(result, true);
	}
	else
	{
		Assign(result, false);
	}
	
	Assign(oprnd1, true);
	Assign(oprnd2, true);
	
	subnum = oprnd1->noofdigit - oprnd2->noofdigit;

	if(subnum>0)
	{

		oprnd3 = oprnd2;
		oprnd3 = PlusZeros(oprnd2, subnum);
		for(int i=subnum; i>=0; i--)
		{

			int j=0;
			
			
			for(; Sign(oprnd1) == true; j++)
			{
	
				oprnd1 = Subtract(oprnd1, oprnd3);

			
			}
			
			
			MakeNull(buffer);
			buffer = Add(oprnd1,oprnd3);
			MakeNull(oprnd1);
			oprnd1 = buffer;
			
			ltoa(j-1, a, 10);
			div.append(a);
			oprnd3 = SubOneZero(oprnd3);        

		}
		CreatandStore(result, div);	
	}
	else if(subnum<0)
	{
		CreatandStore(result,0);
	}
	else
	{
		int m;
		for(m=0; Sign(oprnd1) == true; m++)
		{
			oprnd1 = Subtract(oprnd1, oprnd2);
		}

		ltoa(m-1, a, 10);
		div.append(a);
		CreatandStore(result, div);
		
	}
	Assign(oprnd1, o1);
	Assign(oprnd2, o2);
	return result;

}

⌨️ 快捷键说明

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