首页 > 其他分享 >高精度模板

高精度模板

时间:2023-05-27 09:45:05浏览次数:41  
标签:return 高精度 int else Bigint operator inline 模板

xiayicheng 的高精模板,可自取

介绍

各变量作用

变量名 作用
\(len\) 存储数字长度
\(symbol\) 存储数字符号,\(1\)为负,\(0\)为正
\(s\) 倒序存储数字

功能\(^*\)

  • 变量赋值 :\(\texttt{int,char,Bigint}\)
  • 比较大小 :\(\texttt{Bigint}\)
  • 加减法 :\(\texttt{Bigint}\)
  • 乘法 :\(\texttt{Bigint}\)
  • 乘方 :\(\texttt{int}\)
  • 输入输出
    $\small{{}^*以上内容\color{red}均已重载运算符} $

代码

点击查看代码
#include<bits/stdc++.h>
#define Open(i) freopen(i".in", "r", stdin), freopen(i".out", "w", stdout);
#define Quick ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define FOR(i, l, r) for (int i = (l); i < (r); i++)
#define ROF(i, l, r) for (int i = (l) - 1; i >= (r); i--)
#ifndef ONLINE_JUDGE
	#include "algo/debug.h"
#else
	#define debug(...) 42
	#define debug2(...) 42
#endif
#define MAXN 10010
using namespace std;
struct Bigint
{
	int len, s[MAXN], symbol;
	Bigint()
	{
		memset(s, 0, sizeof(s));
		len = 1;
		symbol = 0;
	}
	inline Bigint operator = (const char*num)
	{
		Bigint();
		if (num[0] == '-')
		{
			symbol = 1;
			len = strlen(num) - 1;
			for (int i = 1; i <= len; ++i)
				s[i - 1] = num[len - i + 1] - '0';
		}
		else
		{
			symbol = 0;
			len = strlen(num);
			for (int i = 0; i < len; ++i)
				s[i] = num[len - i - 1] - '0';
		}
		return *this;
	}
	inline Bigint operator = (const int num)
	{
		char a[MAXN];
		if (num < 0)
		{
			sprintf(a, "%d", 0 - num);
			*this = a;
			symbol = 1;
		}
		else
		{
			sprintf(a, "%d", num);
			*this = a;
			symbol = 0;
		}
		return *this;
	}
	inline Bigint (const int num)
	{
		*this = num;
	}
	inline Bigint (const char * num)
	{
		*this = num;
	}
	inline Bigint operator = (const Bigint &a)
	{
		symbol = a.symbol;
		len = a.len;
		FOR(i, 0, len)
		s[i] = a.s[i];
		return *this;
	}
	inline int unsigned_cmp(const Bigint &a)
	{
		if (len > a.len)
			return 1;
		else if (len < a.len)
			return -1;
		else
		{
			//			cout<<1;
			for (int i = len - 1; i >= 0; i--)
			{
				if (s[i] > a.s[i])
					return 1;
				else if (s[i] < a.s[i])
					return -1;
			}
		}
		return 0;
	}
	inline int cmp (const Bigint &a)
	{
		if (a.symbol != symbol)
		{
			if (a.symbol == 1)
				return 1;
			else
				return -1;
		}
		else
		{
			if (symbol == 0)
				return unsigned_cmp(a);
			else
				return 0 - unsigned_cmp(a);
		}
	}
	inline bool operator > (const Bigint &a)
	{
		int tmp = cmp(a);
		if (tmp == 1)
			return true;
		else
			return false;
	}
	inline bool operator == (const Bigint &a)
	{
		int tmp = cmp(a);
		if (tmp == 0)
			return true;
		else
			return false;
	}
	inline bool operator < (const Bigint &a)
	{
		int tmp = cmp(a);
		if (tmp == -1)
			return true;
		else
			return false;
	}
	inline bool operator >= (const Bigint &a)
	{
		int tmp = cmp(a);
		if (tmp == 1 or tmp == 0)
			return true;
		else
			return false;
	}
	inline bool operator <= (const Bigint &a)
	{
		int tmp = cmp(a);
		if (tmp == -1 or tmp == 0)
			return true;
		else
			return false;
	}
	inline Bigint unsigned_plus (const Bigint &a)
	{
		Bigint c;
		c.len = max(len, a.len) + 1;
		for (int i = 0, x = 0; i < c.len; ++i)
		{
			c.s[i] = s[i] + a.s[i] + x;
			x = c.s[i] / 10;
			c.s[i] = c.s[i] % 10;
		}
		if (c.s[c.len - 1] == 0)
			--c.len;
		return c;
	}
	inline Bigint unsigned_minus (const Bigint &a)
	{
		Bigint c;
		c.len = max(len, a.len) + 1;
		for (int i = 0, x = 0; i < c.len; ++i)
		{
			c.s[i] = s[i] - a.s[i] - x;
			if (c.s[i] < 0)
				c.s[i] += 10, x = 1;
			else
				x = 0;
		}
		for (int i = c.len - 1; i >= 0; i--)
		{
			//			cout<<len<<" ";
			if (c.s[i] == 0)
				--c.len;
			else
				break;
		}
		return c;
	}
	inline Bigint operator + (Bigint &a)
	{
		if (symbol == a.symbol)
		{
			if (symbol == 0)
			{
				Bigint c = unsigned_plus(a);
				c.symbol = 0;
				return c;
			}
			else
			{
				Bigint c = unsigned_plus(a);
				c.symbol = 1;
				return c;
			}
		}
		else
		{
			if (symbol == 0)
			{
				if (unsigned_cmp(a) != -1)
				{
					Bigint c = unsigned_minus(a);
					c.symbol = 0;
					return c;
				}
				else
				{
					Bigint c = *this;
					c = a.unsigned_minus(c);
					c.symbol = 1;
					return c;
				}
			}
			else
			{
				if (unsigned_cmp(a) != -1)
				{
					Bigint c = unsigned_minus(a);
					c.symbol = 1;
					return c;
				}
				else
				{
					Bigint c = *this;
					c = a.unsigned_minus(c);
					c.symbol = 0;
					return c;
				}
			}
		}
	}
	inline Bigint operator - (Bigint &a)
	{
		if (symbol == a.symbol)
		{
			if (symbol == 0)
			{
				//				cout<<cmp(a);
				if (cmp(a) != -1)
				{
					Bigint tmp = unsigned_minus(a);
					tmp.symbol = 0;
					return tmp;
				}
				else
				{
					Bigint tmp = *this;
					tmp = a.unsigned_minus(tmp);
					tmp.symbol = 1;
					return tmp;
				}
			}
			else
			{
				if (unsigned_cmp(a) != -1)
				{
					Bigint tmp = unsigned_minus(a);
					tmp.symbol = 1;
					return tmp;
				}
				else
				{
					Bigint tmp = *this;
					tmp = a.unsigned_minus(tmp);
					tmp.symbol = 0;
					return tmp;
				}
			}
		}
		else
		{
			if (symbol == 0)
			{
				Bigint tmp = unsigned_plus(a);
				tmp.symbol = 0;
				return tmp;
			}
			else
			{
				Bigint tmp = unsigned_plus(a);
				tmp.symbol = 1;
				return tmp;
			}
		}
	}
	inline Bigint operator * (const Bigint &x)
	{
		Bigint c;
		c.len = len + x.len;
		for (int i = 0; i < len; ++i)
			for (int j = 0; j < x.len; ++j)
			{
				c.s[i + j] += s[i] * x.s[j];
				c.s[i + j + 1] += c.s[i + j] / 10;
				c.s[i + j] %= 10;
			}
		for (int i = c.len - 1; i >= 0; i--)
		{
			if (c.s[i] == 0)
				--c.len;
			else
				break;
		}
		c.symbol = (symbol != x.symbol);
		return c;
	}
	inline Bigint operator ^ (int x)
	{
		Bigint c = *this, ans = *this;
		for (; x != 1; x >>= 1)
		{
			ans = ans * c;
			if (x % 2 == 1)
				ans = ans * c;
			c = c * c;
		}
		return ans;
	}
	inline void operator += (Bigint &a)
	{
		*this = *this + a;
	}
	inline void operator -= (Bigint &a)
	{
		*this = *this - a;
	}
	inline void operator *= (const Bigint &a)
	{
		*this = *this * a;
	}
	inline void operator ^= (int &a)
	{
		*this = *this ^ a;
	}
};
ostream& operator<<(ostream &out, const Bigint& x)
{
	if (x.symbol == 1)
		printf("-");
	else if (x.len == 0)
		printf("0");
	for (int i = x.len - 1; i >= 0; --i)
		printf("%d", x.s[i]);
	return out;
}
istream& operator>>(istream &in, Bigint &x)
{
	char num[MAXN];
	in >> num;
	x = num;
	return in;
}

标签:return,高精度,int,else,Bigint,operator,inline,模板
From: https://www.cnblogs.com/xiayicheng/p/17436292.html

相关文章

  • 所有背包问题模板
    01背包问题:无优化for(inti=1;i<=n;i++){for(intc=0;c<=m;c++){f[i][c]=f[i-1][c];if(c>=w[i])f[i][c]=max(f[i][c],f[i-1][c-w[i]]+v[i]);}}一维数组优化:for(inti=1;i<=n;i++){for(intc=m;c>=0;c--){......
  • 【模板】01背包问题
    一个在旅途中的长者有一个最多能用\(M\)公斤的背包,现在有\(n\)件物品,它们的重量分别是\(W1,W2,...,Wn\),它们的价值分别为\(C1,C2,...,Cn\).求旅行者能获得最大总价值。输入第1行:两个整数,\(M\)(背包容量,\(M\le200\))和\(n\)(物品数量,\(n\le30\));第\(2\)至\(n+1\)行:每行两个整数\(......
  • 高精度加法(含代码)
    高精度加法例1例如:1111111111111+9,列成竖式,1111111111111+9---------------------------先算个位,1+9=10,满10,向十位进1。1111111111111+9......
  • 23-05-26 刷题-【中缀表达式求值的模板】
    basiccalculator系列题目:(可以作为模板题,记住)224.基本计算器-力扣(LeetCode)[hard]想法:中缀表达式求值。数据结构中栈的应用中缀转后缀。后缀能去掉括号。a+(b+c)*d==》abc+d*+后缀表达式求值:abc+d*+要考虑表达式的优先级,怎么处理括号。括号的优先级,不知......
  • 利用函数模板解决双倍功能 利用类模板解决绝对值功能 vector应用测试
    请使用模板参数设计实现双倍功能函数,函数功能要求实现返回值为输入参数的两倍,函数参数应能适应整型、浮点型、双精度型等各种类型,返回值类型与参数一样。裁判测试程序样例: #include<iostream>usingnamespacestd;/*请在这里填写答案*/intmain(void){charc='\0';......
  • 手机App模板开发的优势和弊端有哪些?
    手机App模板开发是自移动App开发行业产生以来,比较受欢迎、较简单的App制作方式,也是很多App开发公司提供给客户的服务,但凡事都有两面性,App模板制作手机客户端同时也具备一定的弊端,下面来看看手机App模板开发的优势和弊端。 手机App模板开发的优点App模板开发就是已经开发好的一套系......
  • Flask009_模板的使用
    渲染模板index.html1<!DOCTYPEhtml>2<htmllang="en">3<head>4<metacharset="UTF-8">5<title>首页</title>6</head>7<body>8<h1>这是首页</h1>9</body&g......
  • 【.NetCore】结合MasaDcc实现动态配置小程序消息模板并进行推送消息
    仅适用于更换小程序模板(属于换汤不换药)。可实现多环境对应不同的小程序模板一.配置文件格式"MiniProgramConfig":{"Token":"r8Z6weJVCb0","EncodingAESKey":"MhemkNp9DZXqe24A","AppId":"wxff9df85f87","App......
  • laytpl( Layui 的一款轻量 JavaScript 模板引擎)
    laytpl 是Layui的一款轻量JavaScript模板引擎,在字符解析上有着比较出色的表现。laytpl是一款颠覆性的JavaScript模板引擎文档说明一、模版语法输出一个普通字段,不转义html:{{d.field}}输出一个普通字段,并转义html:{{=d.field}}JavaScript脚本:{{#JavaScriptstate......
  • Problem C: 数量的类模板
    HomeWebBoardProblemSetStandingStatusStatisticsProblemC:数量的类模板TimeLimit:1Sec  MemoryLimit:128MBSubmit:1173  Solved:812[Submit][Status][WebBoard]Description定义一个类模板Data,用于包装C++中的基本数据类型int和double。它......