NewInt5

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

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

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

然后贴代码啦

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直接的运算等等上恩。

<think>我们正在讨论Go语言中的类型定义语法。用户的问题集中在`typenew int`的含义和用法上。根据引用[1],我们知道`type`关键字用于定义新类型或类型别名。让我们分析一下`typenew int`的具体情况。 首先,`type newint`声明了一个名为`new`的新类型,其底层类型是`int`。这意味着: -`new`是一个全新的类型,与`int`类型不同-虽然底层表示相同,但`new`类型和`int`类型不能直接混用-需要显式类型转换才能在它们之间进行赋值示例代码: ```gotypenewint//定义new类型,底层为intfunc main() {vara new=10varb int=20//错误:类型不匹配// a= b//正确:需要显式转换a =new(b)//正确:反向转换b= int(a)}``` 这种类型定义的主要用途:1.**增加类型安全性**:避免不同含义的整数值混用(如索引和数量) 2. **添加方法**:可以为自定义类型绑定方法```gofunc(nnew)Double()new {returnn *2}```3.**提高代码可读性**:通过命名体现数据的语义(如`type Celsiusfloat64`)注意与类型别名的区别:- `type myInt =int`创建的是别名,与原始类型完全等价- `type newint`创建的是新类型,与原始类型不兼容引用[1]中提到:"定义新类型"正是这种用法[^1]。同时引用[2]中说明`new`是内置函数,但这里用作类型名不会冲突,因为Go根据上下文区分关键字和标识符[^2]。 在结构体定义中(引用[3][^3]和引用[4][^4])也使用相同语法,但`type newint`是基础类型定义。**常见错误**: ```govar xnewx =100//正确:字面量可隐式转换x =int(100)//错误!需要new(100)或new(int(100))``` **最佳实践**: -用于区分相同底层类型的不同概念-配合方法实现面向对象-避免过度使用导致类型转换混乱**相关问题**: §§相关问题§§1. Go语言中类型别名(type alias)和类型定义(type definition)有什么区别? 2.如何为自定义基础类型(如`type Celsiusfloat64`)添加方法? 3.在Go语言中,结构体类型定义与基础类型定义在方法绑定上有何异同?4.为什么Go语言不允许自定义类型和底层类型隐式转换?</think>### Go语言中 `type new int` 的含义与用法详解 #### 一、核心含义 `type new int` 在 Go 语言中表示**定义一个新类型**: 1. **`new`** 是新类型的名称(自定义标识符) 2. **`int`** 是底层类型(underlying type) 3. 本质:创建了一个与 `int` 拥有相同内存表示但**类型系统独立**的新类型[^1] ```go type new int // 定义新类型 new,底层类型为 int ``` #### 二、关键特性 1. **类型安全性** ```go var a int = 10 var b new = 20 // 编译错误!类型不匹配 // a = b // 必须显式转换 a = int(b) // 正确 ``` - `new` 和 `int` 在类型系统中被视为不同类型 - 需要显式类型转换才能交互(增强类型安全)[^1] 2. **方法绑定能力** 可为新类型添加专属方法: ```go func (n new) Double() new { return n * 2 } func main() { num := new(5) fmt.Println(num.Double()) // 输出 10 } ``` 3. **底层数据兼容性** - 内存布局与 `int` 完全相同 - 支持所有 `int` 的运算操作: ```go sum := new(3) + new(5) // 合法运算 ``` #### 三、与类型别名的区别 | 特性 | `type new int` (新类型) | `type alias = int` (类型别名) | |--------------|-------------------------------|--------------------------------| | 类型等同性 | ❌ 与 `int` 不同 | ✅ 与 `int` 完全相同 | | 方法绑定 | ✅ 可添加新方法 | ❌ 不能添加新方法 | | 类型转换 | 需要显式转换 | 无需转换 | | 使用场景 | 增强类型安全/添加行为 | 代码兼容/重构 | #### 四、典型应用场景 1. **语义化类型** ```go type UserID int // 用户ID type ProductID int // 商品ID func ProcessOrder(uid UserID, pid ProductID) { // 避免ID类型混淆 } ``` 2. **扩展基础类型功能** ```go type Celsius float64 func (c Celsius) ToFahrenheit() Fahrenheit { return Fahrenheit(c*9/5 + 32) } ``` 3. **接口实现隔离** ```go type Counter int func (c *Counter) Increment() { *c++ } // 此时int类型没有Increment方法 ``` #### 五、注意事项 1. **避免与内置函数混淆** `new` 是 Go 的内置函数(用于分配内存)[^2],但作为类型名使用时: ```go // 合法:编译器根据上下文区分 var a new = 10 // 类型使用 ptr := new(int) // 内置函数使用 ``` 2. **结构体中的使用** 可嵌套在结构体中[^3][^4]: ```go type User struct { ID new // 自定义类型字段 Name string } ``` 3. **零值行为** 与底层类型一致: ```go var x new fmt.Println(x) // 输出 0 (int的零值) ``` ### 总结 `type new int` 的核心价值在于**创建类型安全的抽象**: - ✅ 保留底层类型的计算特性 - ✅ 添加专属方法的能力 - ✅ 防止意外类型混淆 - ❌ 不适用于需要完全类型兼容的场景 > 通过这种机制,Go在保持简单性的同时实现了类型系统的灵活性[^1]。 --- ### 相关问题
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值