NewInt 2

终于完整实现了加法啦,而且当当当当,我改变了一下初始化,然后数字没有最大长度为100的限制啦,随便你多长多长= =、就是占内存罢了

然后上代码啦

NewInt.h

#include <iostream>
#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 show();
	void changeToLonger(int len);
	NewInt operator +(NewInt &ni);
	NewInt operator -(NewInt &ni);
	NewInt operator *(NewInt &ni);
	NewInt operator /(NewInt &ni);
	void setLength(int len);
	bool compare(int s1[],int s2[],int len);
};

NewInt.cpp

#include <iostream>
using namespace std;
#include "NewInt.h"
void NewInt::show(){
	if (array[0]==-1)
	{
		cout<<"-";
	}else{
	}
	for (int i = 1;i<length;i++)
	{
		cout<<array[i];
	}
	cout<<"  ";
}
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;
	}
	
}

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;
			}
		}
	}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){
	
}

main.cpp

#include "NewInt.h"
#include <string>
#include <iostream>
using namespace std;
void main(){
	char s[]= {'4','5','9',NULL};
	char s1[] = {'-','5','5','5',NULL};
	NewInt new1(s,3);
	NewInt new2(s1,3);
	new1.show();
	new2.show();
	NewInt new3;
	new3 = new1+new2;
	new3.show();
	new1.clear();
	new2.clear();
	new3.clear();
}

为什么要调用clear方法呢,因为要释放内存,那为毛毛不用析构函数啊,因为计算中的返回值是NewInt 型的,所以不知道有没有好一点的方法。

然后就是想起来之前C++老师讲的,如果结果可能在以后的程序中作为中间值存在的话,那么返回值不能用指针,而是返回一个对象。因为返回指针是存到栈中,然后作为中间

结果的话会被清空,然后就找不到啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值