首页 > 其他分享 >数据类型

数据类型

时间:2024-08-15 15:09:48浏览次数:7  
标签:digits const 数据类型 num dw operator return

数据类型

大数类

struct Bigint 
{
	int sign; string digits;
	/*====================*/
	Bigint(void) {}
	Bigint(string b) { (*this) = b; }
	Bigint(int b) { (*this) = to_string(b); }
	/*====================*/
	int size(void) 
	{
		return digits.size();
	}
	Bigint inverseSign(void) 
	{ 
		sign *= -1; return (*this);
	}
	Bigint normalize(int newSign) 
	{ 
		for (int i = digits.size() - 1; i > 0 && digits[i] == '0'; i--)
		{
			digits.erase(digits.begin() + i);
		}
		sign = (digits.size() == 1 && digits[0] == '0') ? 1 : newSign; return (*this);
	}
	/*====================*/
	void operator = (string b) 
	{ 
		digits = b[0] == '-' ? b.substr(1) : b;
		reverse(digits.begin(), digits.end());
		this->normalize(b[0] == '-' ? -1 : 1);
	}
	/*====================*/
	bool operator < (const Bigint& b) const 
	{ 
		if (sign != b.sign) return sign < b.sign;
		if (digits.size() != b.digits.size())
			return sign == 1 ? digits.size() < b.digits.size() : digits.size() > b.digits.size();
		for (int i = digits.size() - 1; i >= 0; i--) if (digits[i] != b.digits[i])
			return sign == 1 ? digits[i] < b.digits[i] : digits[i] > b.digits[i];
		return false;
	}
	bool operator == (const Bigint& b) const 
	{
		return digits == b.digits && sign == b.sign;
	}
	/*====================*/
	Bigint operator + (Bigint b) 
	{ 
		if (sign != b.sign) return (*this) - b.inverseSign();
		Bigint c;
		for (int i = 0, carry = 0; i < digits.size() || i < b.size() || carry; i++) {
			carry += (i < digits.size() ? digits[i] - 48 : 0) + (i < b.digits.size() ? b.digits[i] - 48 : 0);
			c.digits += (carry % 10 + 48);
			carry /= 10;
		}
		return c.normalize(sign);
	}
	Bigint operator - (Bigint b) 
	{ 
		if (sign != b.sign) return (*this) + b.inverseSign();
		int s = sign; sign = b.sign = 1;
		if ((*this) < b) return ((b - (*this)).inverseSign()).normalize(-s);
		Bigint c;
		for (int i = 0, borrow = 0; i < digits.size(); i++) {
			borrow = digits[i] - borrow - (i < b.size() ? b.digits[i] : 48);
			c.digits += borrow >= 0 ? borrow + 48 : borrow + 58;
			borrow = borrow >= 0 ? 0 : 1;
		}
		return c.normalize(s);
	}
	Bigint operator * (Bigint b) 
	{ 
		Bigint c("0");
		for (int i = 0, k = digits[i] - 48; i < digits.size(); i++, k = digits[i] - 48) {
			while (k--) c = c + b;
			b.digits.insert(b.digits.begin(), '0');
		}
		return c.normalize(sign * b.sign);
	}
	Bigint operator / (Bigint b) 
	{
		if (b.size() == 1 && b.digits[0] == '0') b.digits[0] /= (b.digits[0] - 48);
		Bigint c("0"), d;
		for (int j = 0; j < digits.size(); j++) d.digits += "0";
		int dSign = sign * b.sign; b.sign = 1;
		for (int i = digits.size() - 1; i >= 0; i--) {
			c.digits.insert(c.digits.begin(), '0');
			c = c + digits.substr(i, 1);
			while (!(c < b)) c = c - b, d.digits[i]++;
		}
		return d.normalize(dSign);
	}
	Bigint operator % (Bigint b) 
	{
		if (b.size() == 1 && b.digits[0] == '0') b.digits[0] /= (b.digits[0] - 48);
		Bigint c("0");
		b.sign = 1;
		for (int i = digits.size() - 1; i >= 0; i--) {
			c.digits.insert(c.digits.begin(), '0');
			c = c + digits.substr(i, 1);
			while (!(c < b)) c = c - b;
		}
		return c.normalize(sign);
	}
	/*====================*/
	friend ostream& operator<<(ostream& output, Bigint integer)
	{
		if (integer.sign == -1) output << "-";
		for (int i = integer.digits.size() - 1; i >= 0; i--)
		{
			output << integer.digits[i];
		}
		return output;
	}
	friend istream& operator>>(istream& input, Bigint& integer)
	{
		string str; input >> str; integer = str; return input;
	}
};

分数类

class Fraction
{
public:
	int up, dw;
private:
	int GCD(int a, int b)
	{
		return b == 0 ? a : GCD(b, a % b);
	}
	void Simplest(void)
	{
		int divisor = GCD(up, dw);
		if (divisor != 0)
		{
			up /= divisor, dw /= divisor;
			if (dw < 0)dw *= -1, up *= -1;
		}
	}
public:
	Fraction(const Fraction& temp)
	{
		up = temp.up, dw = temp.dw;
	}
	Fraction(int _up = 0, int _dw = 1)
	{
		up = _up, dw = _dw; Simplest();
	}
	/*====================*/
	double Val(void)
	{
		return double(up) / double(dw);
	}
	/*====================*/
	void operator+=(const Fraction& x)
	{
		up = up * x.dw + x.up * dw; dw = dw * x.dw; Simplest();
	}
	void operator-=(const Fraction& x)
	{
		up = up * x.dw - x.up * dw; dw = dw * x.dw; Simplest();
	}
	void operator*=(const Fraction& x)
	{
		up = up * x.up; dw = dw * x.dw; Simplest();
	}
	void operator/=(const Fraction& x)
	{
		up = up * x.dw; dw = dw * x.up; Simplest();
	}
	/*====================*/
	friend Fraction operator+(const Fraction& a, const Fraction& b)
	{
		return Fraction(a.up * b.dw + b.up * a.dw, a.dw * b.dw);
	}
	friend Fraction operator-(const Fraction& a, const Fraction& b)
	{
		return Fraction(a.up * b.dw - b.up * a.dw, a.dw * b.dw);
	}
	friend Fraction operator*(const Fraction& a, const Fraction& b)
	{
		return Fraction(a.up * b.up, a.dw * b.dw);
	}
	friend Fraction operator/(const Fraction& a, const Fraction& b)
	{
		return Fraction(a.up * b.dw, a.dw * b.up);
	}
	/*====================*/
	friend bool operator<(const Fraction& a, const Fraction& b)
	{
		return (a.up * b.dw) < (b.up * a.dw);
	}
	friend bool operator>(const Fraction& a, const Fraction& b)
	{
		return (a.up * b.dw) > (b.up * a.dw);
	}
	friend bool operator==(const Fraction& a, const Fraction& b)
	{
		return (a.up == b.up) && (a.dw == b.dw);
	}
	friend bool operator!=(const Fraction& a, const Fraction& b)
	{
		return !(a == b);
	}
	friend bool operator<=(const Fraction& a, const Fraction& b)
	{
		return !(a > b);
	}
	friend bool operator>=(const Fraction& a, const Fraction& b)
	{
		return !(a < b);
	}
};

模数类

template<int MOD = 998244353>
class Modulo
{
private:
	static int Pow(int a, int b)
	{
		int res = 1;
		while (b)
		{
			if (b & 1)
			{
				res = (1ll * res * a) % MOD;
			}
			b >>= 1, a = (1ll * a * a) % MOD;
		}
		return res;
	}
	static int Inv(int x)
	{
		return Pow(x, MOD - 2);
	}
public:
	int num;
	/*====================*/
	Modulo(int temp = 0)
	{
		num = temp % MOD;
	}
	Modulo(const Modulo& temp)
	{
		num = temp.num;
	}
	/*====================*/
	friend Modulo operator+(const Modulo& a, const Modulo& b)
	{
		return Modulo((a.num + b.num) >= MOD ? a.num + b.num - MOD : a.num + b.num);
	}
	friend Modulo operator-(const Modulo& a, const Modulo& b)
	{
		return Modulo((a.num - b.num + MOD) >= MOD ? a.num - b.num : a.num - b.num + MOD);
	}
	friend Modulo operator*(const Modulo& a, const Modulo& b)
	{
		return Modulo(a.num * b.num % MOD);
	}
	friend Modulo operator/(const Modulo& a, const Modulo& b)
	{
		return Modulo(a.num * Inv(b.num) % MOD);
	}
	/*====================*/
	friend bool operator< (const Modulo& a, const Modulo& b)
	{
		return a.num < b.num;
	}
	friend bool operator==(const Modulo& a, const Modulo& b)
	{
		return a.num == b.num;
	}
	friend bool operator> (const Modulo& a, const Modulo& b)
	{
		return a.num > b.num;
	}
	friend bool operator<=(const Modulo& a, const Modulo& b)
	{
		return a.num <= b.num;
	}
	friend bool operator!=(const Modulo& a, const Modulo& b)
	{
		return a.num != b.num;
	}
	friend bool operator>=(const Modulo& a, const Modulo& b)
	{
		return a.num >= b.num;
	}
	/*====================*/
	void operator+=(const Modulo& x)
	{
		num = num + x.num; if (num >= MOD)num -= MOD;
	}
	void operator-=(const Modulo& x)
	{
		num = num - x.num + MOD; if (num >= MOD)num -= MOD;
	}
	void operator*=(const Modulo& x)
	{
		num = num * x.num % MOD;
	}
	void operator/=(const Modulo& x)
	{
		num = num * Inv(x.num) % MOD;
	}
	/*====================*/
	friend ostream& operator<<(ostream& output, Modulo integer)
	{
		output << integer.num; return output;
	}
	friend istream& operator>>(istream& input, Modulo& integer)
	{
		int temp; input >> temp; integer = (temp % MOD + MOD) % MOD; return input;
	}
};

标签:digits,const,数据类型,num,dw,operator,return
From: https://www.cnblogs.com/ProtectEMmm/p/18360928

相关文章

  • 仓颉编程语言:整数类型(基础数据类型)
    整数类型分为有符号(signed)整数类型和无符号(unsigned)整数类型。有符号整数类型包括Int8、Int16、Int32、Int64和IntNative,分别用于表示编码长度为8-bit、16-bit、32-bit、64-bit和平台相关大小的有符号整数值的类型。无符号整数类型包括UInt8、UInt16、UInt32、UInt64......
  • 仓颉编程语言:布尔类型(基础数据类型)
    布尔类型使用Bool表示,用来表示逻辑中的真和假。布尔类型字面量布尔类型只有两个字面量:true和false。下面的例子展示了布尔字面量的使用:leta:Bool=trueletb:Bool=false布尔类型支持的操作布尔类型支持的操作符包括:逻辑操作符(逻辑非!,逻辑与&&,逻辑或||)、部......
  • 仓颉编程语言:字符串类型(基础数据类型)
    字符串类型使用String表示,用于表达文本数据,由一串Unicode字符组合而成。字符串字面量字符串字面量分为三类:单行字符串字面量,多行字符串字面量,多行原始字符串字面量。单行字符串字面量的内容定义在一对单引号或一对双引号之内,引号中的内容可以是任意数量的(除了非转义的双......
  • 数据类型
    Java基础语法中的数据类型是编程中非常重要的一个概念,它决定了变量能够存储什么类型的数据以及这些数据在内存中的表示方式。Java的数据类型可以分为两大类:基本数据类型(PrimitiveTypes)和引用数据类型(ReferenceTypes)。基本数据类型基本数据类型是Java中不可变的数据类型,它们直......
  • C程序设计(安徽专升本3.2基本数据类型)
    一、数据类型的分类 在本章节我们之讲解基础的数据类型,因为后续的数据类型将会单独对此讲解,常考的为基本数据类型,数组,函数,指针这几种类型!其它类型作为了解,认识即可二、整型类型此处对整数类型的讲解排除字符型和布尔型,它们单独拉出讲解,且我不喜欢废话讲解,我直接列表加代码......
  • 基本数据类型之间的转换
    自动类型转换(隐式转换)自动类型转换发生在从低级类型向高级类型转换时,不需要进行任何显式操作。Java中的基本数据类型按照精度从低到高的顺序是:byte、short、char(在运算中视为int)、int、long、float、double。转换规则如下:精度或可表示范围小的类型自动转换成精度或可表示范围大......
  • C语言---数据类型和变量
    1.数据类型介绍  C语⾔提供了丰富的数据类型来描述⽣活中的各种数据。使⽤整型类型来描述整数,使⽤字符类型来描述字符,使⽤浮点型类型来描述⼩数。所谓“类型”,就是相似的数据所拥有的共同特征,编译器只有知道了数据的类型,才知道怎么操作。2.内置类型1.字符型char  ......
  • MySQL数据库——数据库的数据类型(一)
    四、数据类型1.数据类型分类分类数据类型说明数值类型BIT(M)位类型。指定位数,默认值1,范围1-64TINYINT[UNSIGNED]带符号的范围-128127,无符号范围0255.默认有符号BOOL使用0和1表示真和假SMALLINT[UNSIGNED]带符号是-2^15次方到2^15-1,无符号是2^16-1IN......
  • 数据类型
    数据类型强类型语言:要求变量的使用严格符合规定,所有变量必须先定义后才能使用。弱类型语言:要求变量的使用符合规定。JSJava的数据类型分为两大类基本类型数值类boolean类:true和false占一位引用类型类、接口、数组1B(byte、字节)=8bit(位)publicclassDemo3{pu......
  • 数据类型的转换
    目录导言一、隐式类型转换1.整型隐式类型转换2.浮点型隐式类型转换3.字符型隐式类型转换4.布尔型隐式类型转换二、显式类型转换1.整型显式类型转换2.引用数据类型显式类型转换父类和子类之间的转换接口和实现类之间的转换三、类型转换的注意事项1.数据溢出和精度丢失2.强......