NewInt5

本文介绍了一个自定义的大整数类NewInt,该类实现了不受计算机字长限制的整数运算,包括加减乘除及赋值操作,并通过示例展示了如何使用这个类进行大整数的运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

勤劳的董花花又来贴代码了= =、

这一次实现了加减乘除和赋值还有<<功能

然后贴代码啦

NewInt.h

#include "iostream.h"
#include <string>
class NewInt{
private:
	int *array;
	int length;
public:
	NewInt();
	NewInt(const char*s,int len);
	~NewInt(){
		/*if (array!=NULL)
		{
			delete[]array;
		}*/
	}
	void clear(){
		if (array!=NULL)
		{
			delete []array;
		}
	}
	void changeToLonger(int len);
	void addToLast(int len);
	NewInt operator +(NewInt &ni);
	NewInt operator -(NewInt &ni);
	NewInt operator *(NewInt &ni);
	NewInt operator /(NewInt &ni);
	NewInt& operator =(const NewInt &ni);
	friend ostream & operator<<(ostream&out,NewInt &ni);
	void setLength(int len);
	bool compare(int s1[],int s2[],int len);
	void quFan();//取反,起名无能表示压力很大
};

NewInt.cpp

//4.定义一个不受计算机字长限制的整数类INT,要求INT与INT以及INT与C++基本数据类型int之间能进行+、-、×、÷和=运算,
//并且能通过cout输出INT类型的值。
#include "iostream.h"
#include "NewInt.h"

NewInt::NewInt(){
	array =NULL;
	length = 0;
}
NewInt::NewInt(const char* s,int len){
	array = new int[len+1];
	if (s[0]=='-')
	{
		array[0] = -1;
		for(int i = 1;i<=len;i++){
			array[i] = s[i]-'0';
		}
	}else {
		array[0] = 0;
		for (int i = 1;i<=len;i++)
		{
			array[i] = s[i-1]-'0';
		}
	}
	length = len+1;
	
}

void NewInt::setLength(int len){
	array = new int[len];
	length = len;
}
//比较绝对值大小。这两个数字长度相同
bool NewInt::compare(int s1[],int s2[],int len){
	int flag = 0;
	for (int i = 1;i<len;i++)
	{
		if (s1[i]-s2[1]<0)
		{
			flag = 1;
		}
	}
	if (flag ==0)
	{
		return true;
	}else{
		return false;
	}
}
void NewInt::changeToLonger(int len){
    int *temp = array;
	if (array[0]==0)
	{
		array = new int[len];
		for (int i = len-length+1;i<len;i++)
		{
			array[i]=temp[i-(len-length)];
		}
		for (int j = 1;j<=len-length;j++)
		{
			array[j] = 0;
		}
		array[0]=0;
		length = len;
	}else if (array[0]==-1)
	{
		array = new int[len];
		array[0]=-1;
		for (int i = len-length+1;i<len;i++)
		{
			array[i]=temp[i-(len-length)];
		}
		for (int j = 1;j<=len-length;j++)
		{
			array[j] = 0;
		}
		length = len;
	}
	
}

void NewInt::quFan(){
	if (array[0]==0)
	{
		array[0]=-1;
	}else{
		array[0]=0;
	}
}

void NewInt::addToLast(int len){
	int *temp = array;
	array = new int[length+len];
	
	for (int i = length;i<length+len;i++)
	{
		array[i]=0;
	}
	for (int j = 0;j<length;j++)
	{
		array[j] = temp[j];
	}
	length = len+length;
}

NewInt NewInt::operator +(NewInt &ni){
	NewInt temp;
	if (length>=ni.length)
	{
		ni.changeToLonger(length);
		temp.setLength(length);
	}else{
		changeToLonger(ni.length);
		temp.setLength(ni.length);
	}
	if (array[0]==0&&ni.array[0]==0)
	{
		for (int i1 = 0;i1<temp.length;i1++)
		{
			temp.array[i1]=0;
		}
		for(int i = temp.length -1;i>=1;i--){
			if (temp.array[i]+array[i]+ni.array[i]<=9)
			{
				temp.array[i]+= array[i]+ni.array[i];
			}else {
				temp.array[i] += array[i]+ni.array[i]-10;
				temp.array[i-1]+=1;
			}
		}
		if (temp.array[0]>0)
		{
			temp.length++;
			int *tempArray = temp.array;
			temp.array = new int[temp.length];
			for (int i = temp.length;i>0;i--)
			{
				temp.array[i] = tempArray[i-1];
			}
			temp.array[0] = 0;
			
		}
	}else if (array[0]==0&&ni.array[0]==-1)//正数加上负数,你妹妹啊
	{
		for (int i1 =0;i1<temp.length;i1++)
		{
			temp.array[i1]=0;
		}
		if (compare(array,ni.array,temp.length))
		{
			for (int i = temp.length-1;i>=1;i--)
			{
				if (temp.array[i]+array[i]-ni.array[i]>=0)
				{
					temp.array[i]+=array[i]-ni.array[i];
				}else{
					temp.array[i]+=array[i]-ni.array[i]+10;
					temp.array[i-1]-=1;
				}
			}
		}else {
			temp.array[0] = -1;
			for (int i = temp.length-1;i>=1;i--)
			{
				if (temp.array[i]-array[i]+ni.array[i]>=0)
				{
					temp.array[i]+=ni.array[i]-array[i];
				}else{
					temp.array[i]+=ni.array[i]-array[i]+10;
					temp.array[i-1]-=1;
				}
			}
		}
	}else if (array[0]==-1&&ni.array[0]==0)//负数加正数,好玩吗吗
	{
		for (int i1 =0;i1<temp.length;i1++)
		{
			temp.array[i1]=0;
		}
		if (compare(array,ni.array,temp.length))
		{
			temp.array[0]=-1;
			for (int i = temp.length-1;i>=1;i--)
			{
				if (temp.array[i]+array[i]-ni.array[i]>=0)
				{
					temp.array[i]+=array[i]-ni.array[i];
				}else{
					temp.array[i]+=array[i]-ni.array[i]+10;
					temp.array[i-1]-=1;
				}
			}
		}else {
			for (int i = temp.length-1;i>=1;i--)
			{
				if (temp.array[i]-array[i]+ni.array[i]>=0)
				{
					temp.array[i]+=ni.array[i]-array[i];
				}else{
					temp.array[i]+=ni.array[i]-array[i]+10;
					temp.array[i-1]-=1;
				}
			}
		}

	}else {
		temp.array[0]=-1;
		for (int i1 =1;i1<temp.length;i1++)
		{
			temp.array[i1] = 0;
		}
		for(int i = length -1;i>=1;i--){
			if (temp.array[i]+array[i]+ni.array[i]<=9)
			{
				temp.array[i]+= array[i]+ni.array[i];
			}else {
				temp.array[i] += array[i]+ni.array[i]-10;
				temp.array[i-1]+=1;
			}
		}
		if (temp.array[0]>=0)
		{
			temp.length++;
			int *tempArray = temp.array;
			temp.array = new int[temp.length];
			for (int i = temp.length;i>0;i--)
			{
				temp.array[i] = tempArray[i-1];
			}
			temp.array[1]+=1;
			temp.array[0] = -1;
			
		}
	}
	return temp;
	
}

NewInt NewInt::operator -(NewInt &ni){
	NewInt temp;
	NewInt middleValue;
	middleValue.setLength(ni.length);
	for (int i = 0;i<ni.length;i++)
	{
		middleValue.array[i]=ni.array[i];
	}
	middleValue.quFan();
	temp=*this + middleValue;
	return temp;
}

NewInt NewInt::operator *( NewInt &ni){
	//先初始化temp使得temp与this相同长度,但是均为0
	NewInt temp;
	temp.setLength(length);
	for (int i =0;i<temp.length;i++)
	{
		temp.array[i]=0;
	}


	if ((array[0]==0&&ni.array[0]==0)||(array[0]==-1&&ni.array[0]==-1))//正数乘以正数
	{
		for (int i = 1;i<ni.length;i++)
		{
			int j = ni.array[i];

			NewInt middleValue1 ;
			middleValue1.setLength(length);
			for (int z = 0;z<length;z++)
			{
				middleValue1.array[z] = array[z];
			}

			NewInt middleValue2 ;
			middleValue2.setLength(length);
			for (int z1 = 0;z1<length;z1++)
			{
				middleValue2.array[z1] = 0;
			}

			while (j!=0)
			{
				middleValue2=middleValue2+middleValue1;
				j--;
			}
			middleValue2.addToLast(ni.length-i-1);
			temp =temp+middleValue2;
		}
		temp.array[0]=0;
	}else if ((array[0]==0&&ni.array[0]==-1)||(array[0]==-1&&ni.array[0]==0))
	{
		for (int i = 1;i<ni.length;i++)
		{
			int j = ni.array[i];
			
			NewInt middleValue1 ;
			middleValue1.setLength(length);
			for (int z = 0;z<length;z++)
			{
				middleValue1.array[z] = array[z];
			}
			
			NewInt middleValue2 ;
			middleValue2.setLength(length);
			for (int z1 = 0;z1<length;z1++)
			{
				middleValue2.array[z1] = 0;
			}
			
			while (j!=0)
			{
				middleValue2=middleValue2+middleValue1;
				j--;
			}
			middleValue2.addToLast(ni.length-i-1);
			temp =temp+middleValue2;
		}
		temp.array[0]=-1;
	}
	return temp;
}

NewInt NewInt::operator /(NewInt &ni){
	NewInt temp;
	temp.setLength(2);
	temp.array[0]=0;
	temp.array[1]=0;
	
	NewInt middleValue;
	middleValue.setLength(2);
	middleValue.array[0]=0;
	middleValue.array[1]=1;

	NewInt middleValue1 ;
	middleValue1.setLength(length);
	for (int z = 0;z<length;z++)
	{
		middleValue1.array[z] = array[z];
	}

	if (array[0]==0&&ni.array[0]==0)
	{
		while (middleValue1.array[0]==0)
		{
			middleValue1 = middleValue1-ni;
			temp = temp+middleValue;
		}
		temp = temp -middleValue;
	}else if (array[0]==0&&ni.array[0]==-1)
	{
		NewInt middleValue2;
		middleValue2.setLength(ni.length);
		for (int i = 0;i<ni.length;i++)
		{
			middleValue2.array[i]=ni.array[i];
		}
	    middleValue2.quFan();

		while (middleValue1.array[0]==0)
		{
			middleValue1 = middleValue1-middleValue2;
			temp = temp+middleValue;
		}
		temp = temp -middleValue;
		temp.array[0]=-1;
	}else if (array[0]==-1&&ni.array[0]==0)
	{
		while (middleValue1.array[0]==-1)
		{
			middleValue1 = middleValue1+ni;
			temp = temp+middleValue;
		}
		temp = temp -middleValue;
		temp.array[0]=-1;
	}else{
		middleValue1.quFan();
		while (middleValue1.array[0]==0)
		{
			middleValue1 = middleValue1+ni;
			temp = temp+middleValue;
		}
		temp = temp -middleValue;
		temp.array[0]=0;
	}
	return temp;
}
//NewInt& operator =(const NewInt &ni);
NewInt& NewInt::operator =(const NewInt &ni){
	length =ni.length;
	int *temp = new int[ni.length];
	for (int i =0;i<ni.length;i++)
	{
		temp[i] =ni.array[i];
	}
	delete []array;
	array = temp;
	return *this;
}

ostream & operator<<(ostream&out,NewInt &ni){
	if (ni.array[0]==-1)
	{
		out<<"-";
	}
	for (int i = 1;i<ni.length;i++)
	{
		out<<ni.array[i];
	}
	return out;
}

main.cpp

#include "NewInt.h"
#include <cstring>
#include "iostream.h"
void main(){
	char s[]= {'-','2','0',NULL};
	char s1[] = {'-','5',NULL};
	NewInt new1(s,strlen(s)-1);
	NewInt new2(s1,strlen(s1)-1);
	cout<<new1<<endl;
	cout<<new2<<endl;
	new1.clear();
	new2.clear();
}

然后跟int直接的运算等等上恩。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值