高精度、矩阵计算器

博客提及了bigint.h文件、bigint.cpp文件以及main函数,这些通常是C++程序开发中的关键元素,bigint相关文件可能用于处理大整数,main函数是程序入口。

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

bigint.h文件:

#include<map>
#include<vector>
#include<cstdio>
#include<stack>
#include<iomanip>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int BASE_W = 8;
const long long BASE = 100000000;

class bigint
{
private:
	vector<long long>a;
	int sign;
public:
	bigint();
	bigint(const long long& b);
	bigint(const bigint& b);

	friend bool operator<(const bigint& a, const bigint& other);
	friend bool operator<=(const bigint& a, const bigint& b);
	friend bool operator>(const bigint& a, const bigint& b);
	friend bool operator>=(const bigint& a, const bigint& b);
	friend bool operator==(const bigint& a, const bigint& b);
	friend bool operator<(const bigint& a, const long long& b);
	friend bool operator<=(const bigint& a, const long long& b);
	friend bool operator>(const bigint& a, const long long& b);
	friend bool operator>=(const bigint& a, const long long& b);
	friend bool operator==(const bigint& a, const long long& b);
	friend bool operator!=(const bigint& a, const long long& b);
	friend bool operator!=(const bigint& a, const bigint& b);

	friend bigint operator+(const bigint& a, const bigint& b);
	friend bigint operator-(const bigint& a, const bigint& b);
	friend bigint operator*(const bigint& a, const long long& b);
	friend bigint operator*(const long long& b, const bigint& a);
	friend bigint operator*(const bigint& a, const bigint& b);
	friend bigint operator/(const bigint& a, const long long& b);
	friend bigint operator/(const bigint& a, const bigint& b);
	friend bigint operator%(const bigint& a, const bigint& b);
	friend long long operator%(const bigint& a, const long long& b);
	friend bigint abs(const bigint& a);
	
	//++i;
	bigint& operator++()
	{
		*this+=1;
		return *this;
	}
	bigint& operator--()
	{
		*this -= 1;
		return *this;
	}
	//i++
	const bigint operator++(int)
	{
		bigint tmp = *this;
		*this+=1;
		return bigint(tmp);
	}
	const bigint operator--(int)
	{
		bigint tmp = *this;
		*this -= 1;
		return bigint(tmp);
	}

	bigint& operator+=(const bigint& other);
	bigint& operator+=(const long long& other);
	bigint& operator-=(const bigint& other);
	bigint& operator-=(const long long& other);
	bigint& operator*=(const bigint& other);
	bigint& operator*=(const long long& other);
	bigint& operator/=(const bigint& other);
	bigint& operator/=(const long long& other);
	bigint& operator=(long long b);
	bigint& operator=(const bigint& other);

	friend istream& operator>>(istream& stream, bigint& v);
	friend ostream& operator<<(ostream& stream, const bigint& v);
};

class Matrix
{
public:
	vector<vector<bigint>> a;
	Matrix();
	Matrix(const vector<vector<bigint>>& b);
	Matrix(const Matrix& b);
	friend Matrix operator + (const Matrix& a, const Matrix& b);
	friend Matrix operator + (const Matrix& a, const bigint& b);
	friend Matrix operator - (const Matrix& a, const Matrix& b);
	friend Matrix operator - (const Matrix& a, const bigint& b);
	friend Matrix operator * (const Matrix& a, const Matrix& b);
	friend Matrix operator * (const Matrix& a, const bigint& b);

	Matrix& operator +=(const Matrix& other);
	Matrix& operator -=(const Matrix& other);
	Matrix& operator *= (const Matrix & other);
	Matrix& operator +=(const bigint& other);
	Matrix& operator -=(const bigint& other);
	Matrix& operator *= (const bigint& other);
	
	Matrix inverse();//输出逆矩阵
	Matrix ladder();//化为行阶梯
	bigint value();//输出行列式的值

	void get();
	void show();
	friend void dfs(int n, const Matrix& a, bigint& ans, vector<int>& ss, map<int, int>& book, int num);
	friend Matrix qpow(Matrix a, bigint b);
};

bigint qpow(bigint a, bigint b);
void ops(stack<bigint>& s1, stack<char>& op);
bigint Compute(string s);

bigint.cpp文件

#include"bigint.h"
#include<map>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

bigint::bigint() :sign(1) {}
bigint::bigint(const bigint& b)
{
	a.clear();
	a = b.a;
	sign = b.sign;
}
bigint::bigint(const long long& b)
{
	a.clear();
	if (b >= 0) sign = 1;
	long long p = abs(b);
	a.push_back(p);
	long long t = 0;
	for (int i = 0; i < a.size(); i++)
	{
		a[i] = a[i] + t;
		t = a[i] / BASE;
		a[i] %= BASE;
	}
	while (t)
	{
		a.push_back(t % BASE);
		t /= BASE;
	}
}
bigint& bigint::operator=(long long b)
{
	a.clear();
	if (b >= 0) sign = 1;
	else sign = 0;
	long long p = abs(b);
	a.push_back(p);
	long long t = 0;
	for (int i = 0; i < a.size(); i++)
	{
		a[i] = a[i] + t;
		t = a[i] / BASE;
		a[i] %= BASE;
	}
	while (t)
	{
		a.push_back(t % BASE);
		t /= BASE;
	}
	return *this;
}
bigint& bigint::operator=(const bigint& other)
{
	a = other.a;
	sign = other.sign;
	return *this;
}

bigint abs(const bigint& a)
{
	bigint ans = a;
	ans.sign = 1;
	return ans;
}
bool operator<=(const bigint& a, const bigint& b)
{
	if (a == b) return 1;
	if (a < b) return 1;
	return 0;
}
bool operator>(const bigint& a, const bigint& b)
{
	if (a == b) return 0;
	if (a.sign > b.sign) return 1;
	if (a.sign < b.sign) return 0;
	if (a.sign)
	{
		if (a.a.size() > b.a.size()) return 1;
		else if (a.a.size() < b.a.size()) return 0;
		for (int i = a.a.size() - 1; i >= 0; i--)
		{
			if (a.a[i] > b.a[i]) return 1;
			else if (a.a[i] < b.a[i]) return 0;
		}
	}
	else
	{
		if (a.a.size() < b.a.size()) return 1;
		else if (a.a.size() > b.a.size()) return 0;
		for (int i = a.a.size() - 1; i >= 0; i--)
		{
			if (a.a[i] < b.a[i]) return 1;
			else if (a.a[i] > b.a[i]) return 0;
		}
	}
}
bool operator>=(const bigint& a, const bigint& b)
{
	if (a == b) return 1;
	if (a > b) return 1;
	return 0;
}
bool operator<(const bigint& a, const bigint& b)
{
	if (a >= b) return 0;
	else return 1;
}
bool operator==(const bigint& a, const bigint& b)
{
	if (a.sign != b.sign) return 0;
	if (a.a.size() != b.a.size()) return 0;
	for (int i = 0; i < a.a.size(); i++)
	{
		if (a.a[i] != b.a[i]) return 0;
	}
	return 1;
}
bool operator<(const bigint& a, const long long& b)
{
	bigint c = b;
	return a < c;
}
bool operator<=(const bigint& a, const long long& b)
{
	bigint c = b;
	return a <= c;
}
bool operator>(const bigint& a, const long long& b)
{
	bigint c = b;
	return a > c;
}
bool operator>=(const bigint& a, const long long& b)
{
	bigint c = b;
	return a >= c;
}
bool operator==(const bigint& a, const long long& b)
{
	bigint c = b;
	return a == c;
}
bool operator!=(const bigint& a, const long long& b)
{
	if (a == b) return 0;
	else return 1;
}
bool operator!=(const bigint& a, const bigint& b)
{
	if (a == b) return 0;
	else return 1;
}

bigint operator+(const bigint& a, const bigint& b)
{
	bigint ans = a;
	ans += b;
	return ans;
}
bigint operator-(const bigint& a, const bigint& b)
{
	bigint ans = a;
	bigint c = b;
	if (c.sign) c.sign = 0;
	else c.sign = 1;
	ans += c;
	return ans;
}
bigint operator*(const bigint& a, const long long& b)
{
	bigint c = a;
	if (b == 0)
	{
		c.a.clear();
		c.a.push_back(0);
		return c;
	}
	long long e = b;
	if (e < 0)
	{
		if (a > 0) c.sign = 0;
		else c.sign = 1;
		e = -1 * e;
	}
	else c.sign = 1;
	long long t = 0;
	for (int i = 0; i < c.a.size(); i++)
	{
		c.a[i] = c.a[i] * e + t;
		t = c.a[i] / BASE;
		c.a[i] %= BASE;
	}
	while (t)
	{
		c.a.push_back(t % BASE);
		t /= BASE;
	}
	return c;
}
bigint operator*(const long long& b, const bigint& a)
{
	bigint c = a;
	if (b == 0||a==0)
	{
		c = 0;
		return c;
	}
	long long t = 0;
	int sign = 1;
	if (b < 0)
	{
		c.sign = !c.sign;
		sign = -1;
	}
	for (int i = 0; i < c.a.size(); i++)
	{
		c.a[i] = c.a[i] * b * sign + t;
		t = c.a[i] / BASE;
		c.a[i] %= BASE;
	}
	while (t)
	{
		c.a.push_back(t % BASE);
		t /= BASE;
	}
	return c;
}
bigint operator*(const bigint& a, const bigint& b)
{
	bigint ans;
	if (a == 0 || b == 0)
	{
		ans = 0;
		return ans;
	}
	int sign = 1;
	if (!a.sign) sign *= -1;
	if (!b.sign) sign *= -1;
	for (int i = 0; i < b.a.size(); i++)
	{
		bigint c = a * b.a[i];
		for (int j = 0; j < i; j++)
		{
			c = c * BASE;
		}
		ans += c;
	}
	if (sign == 1) ans.sign = 1;
	else ans.sign = 0;
	return ans;
}
bigint operator/(const bigint& a, const long long& b)
{
	int flag = 1;
	if (b < 0) flag *= -1;
	if (!a.sign) flag *= -1;
	bigint c = a;
	if (flag == 1) c.sign = 1;
	else c.sign = 0;
	long long copyb = abs(b);
	long long r = 0;
	for (int i = c.a.size() - 1; i >= 0; i--)
	{
		r = r * BASE + c.a[i];
		if (r < copyb)
		{
			c.a[i] = 0;
		}
		else
		{
			c.a[i] = r / copyb;
			r %= copyb;
		}
	}
	while (c.a.size() > 1 && c.a[c.a.size() - 1] == 0) c.a.pop_back();
	return c;
}
bigint operator/(const bigint& a, const bigint& b)
{
	bigint ans = 0;
	int sign = 1;
	if (!a.sign) sign *= -1;
	if (!b.sign) sign *= -1;
	if (sign == 1) ans.sign = 1;
	else ans.sign = 0;
	if (ans.sign)
	{
		if (a.sign && a < b) return ans;
		if (!a.sign && a > b) return ans;
	}
	bigint r = 0;
	bool flag = 0;
	for (int i = a.a.size() - 1; i >= 0; i--)
	{
		r = r * BASE + a.a[i];
		if (r < b)
		{
			if (!flag) continue;
			else
			{
				ans.a.push_back(0);
			}
		}
		else
		{
			if (!flag) ans.a.clear();
			flag = 1;
			long long ll = 0, rr = BASE;
			while (ll < rr)
			{
				long long mid = ll + rr + 1 >> 1;
				if (b * mid > r) rr = mid - 1;
				else ll = mid;
			}
			ans.a.push_back(ll);
			r = r - ll * b;
		}
	}
	reverse(ans.a.begin(), ans.a.end());
	return ans;
}
bigint operator%(const bigint& a, const bigint& b)
{
	if (abs(a) < abs(b)) return a;
	bigint ans = 0;
	int sign = 1;
	if (!a.sign) sign *= -1;
	if (!b.sign) sign *= -1;
	if (sign == 1) ans.sign = 1;
	else ans.sign = 0;
	if (ans.sign)
	{
		if (a.sign && a < b) return ans;
		if (!a.sign && a > b) return ans;
	}
	bigint r = 0;
	bool flag = 0;

	for (int i = a.a.size() - 1; i >= 0; i--)
	{
		r = r * BASE + a.a[i];
		if (r < b)
		{
			if (!flag) continue;
			else
			{
				ans.a.push_back(0);
			}
		}
		else
		{
			if (!flag) ans.a.clear();
			flag = 1;
			long long ll = 0, rr = BASE;
			while (ll < rr)
			{
				long long mid = ll + rr + 1 >> 1;
				if (b * mid > r) rr = mid - 1;
				else ll = mid;
			}
			ans.a.push_back(ll);
			r = r - ll * b;
		}
	}
	reverse(ans.a.begin(), ans.a.end());
	r.sign = ans.sign;
	return r;
}
long long operator%(const bigint& a, const long long& b)
{
	int flag = 1;
	if (b < 0) flag *= -1;
	if (!a.sign) flag *= -1;
	bigint c = a;
	if (flag == 1) c.sign = 1;
	else c.sign = 0;
	long long copyb = abs(b);
	long long r = 0;
	for (int i = c.a.size() - 1; i >= 0; i--)
	{
		r = r * BASE + c.a[i];
		if (r < copyb)
		{
			c.a[i] = 0;
		}
		else
		{
			c.a[i] = r / copyb;
			r %= copyb;
		}
	}
	while (c.a.size() > 1 && c.a[c.a.size() - 1] == 0) c.a.pop_back();
	if (flag == -1) r *= -1;
	return r;
}

bigint& bigint::operator+=(const bigint& other)
{
	if (sign == other.sign)
	{
		for (int i = 0; i < max(other.a.size(), a.size()); i++)
		{
			if (i >= other.a.size()) break;
			else if (i >= a.size()) a.push_back(other.a[i]);
			else a[i] += other.a[i];
		}
		long long t = 0;
		for (int i = 0; i < a.size(); i++)
		{
			a[i] += t;
			t = a[i] / BASE;
			a[i] %= BASE;
		}
		while (t)
		{
			a.push_back(t % BASE);
			t /= BASE;
		}
	}
	else
	{
		bigint c;
		if (sign > other.sign)
		{
			c = other;
			c.sign = 1;
			if (*this >= c)
			{
				sign = 1;
				for (int i = 0; i < c.a.size(); i++)
				{
					if (a[i] < c.a[i])
					{
						a[i] += BASE;
						a[i + 1]--;
					}
					a[i] -= c.a[i];
				}
				for (int i = c.a.size(); i < a.size(); i++)
				{
					if (a[i] < 0)
					{
						a[i] += BASE;
						a[i + 1]--;
					}
					else break;
				}
				while (a.size() > 1 && !a.back()) a.pop_back();
				return *this;
			}
			else
			{
				c.sign = 0;
				for (int i = 0; i < a.size(); i++)
				{
					if (c.a[i] < a[i])
					{
						c.a[i] += BASE;
						c.a[i + 1]--;
					}
					c.a[i] -= a[i];
				}
				for (int i = c.a.size(); i < a.size(); i++)
				{
					if (a[i] < 0)
					{
						a[i] += BASE;
						a[i + 1]--;
					}
					else break;
				}
				while (c.a.size() > 1 && !c.a.back()) c.a.pop_back();
				*this = c;
				return *this;
			}
		}
		else
		{
			c = other;
			sign = 1;
			if (*this <= c)
			{
				sign = 1;
				for (int i = 0; i < a.size(); i++)
				{
					if (c.a[i] < a[i])
					{
						c.a[i] += BASE;
						c.a[i + 1]--;
					}
					c.a[i] -= a[i];
				}
				for (int i = c.a.size(); i < a.size(); i++)
				{
					if (a[i] < 0)
					{
						a[i] += BASE;
						a[i + 1]--;
					}
					else break;
				}
				while (c.a.size() > 1 && !c.a.back()) c.a.pop_back();
				*this = c;
				return *this;
			}
			else
			{
				sign = 0;
				for (int i = 0; i < c.a.size(); i++)
				{
					if (a[i] < c.a[i])
					{
						a[i] += BASE;
						a[i + 1]--;
					}
					a[i] -= c.a[i];
				}
				for (int i = c.a.size(); i < a.size(); i++)
				{
					if (a[i] < 0)
					{
						a[i] += BASE;
						a[i + 1]--;
					}
					else break;
				}
				while (a.size() > 1 && !a.back()) a.pop_back();
				return *this;
			}
		}
	}
}
bigint& bigint::operator-=(const bigint& other)
{
	*this = *this - other;
	return *this;
}
bigint& bigint::operator*=(const bigint& other)
{
	*this = *this * other;
	return *this;
}
bigint& bigint::operator+=(const long long& other)
{
	bigint b = other;
	*this += b;
	return *this;
}
bigint& bigint::operator-=(const long long& other)
{
	bigint b = other;
	*this -= b;
	return *this;
}
bigint& bigint::operator*=(const long long& other)
{
	bigint b = other;
	*this *= b;
	return *this;
}
bigint& bigint::operator/=(const long long& other)
{
	long long c = other;
	*this = *this / c;
	return *this;
}
bigint& bigint::operator/=(const bigint& other)
{
	*this = *this / other;
	return *this;
}

istream& operator>>(istream& stream, bigint& v)
{
	v.a.clear();
	v.sign = 1;
	string s;
	stream >> s;
	int t = 0;
	if (s[0] == '-')
	{
		v.sign = 0;
		t++;
	}
	for (int i = s.size() - 1; i >= t; i -= BASE_W)
	{
		long long sum = 0;
		for (int j = BASE_W - 1; j >= 0; j--)
		{
			if (i - j < t) continue;
			sum = sum * 10 + s[i - j] - '0';
		}
		v.a.push_back(sum);
	}
	return stream;
}
ostream& operator<<(ostream& stream, const bigint& v)
{
		if (!v.sign)
			stream << '-';
	stream << (v.a.empty() ? 0 : v.a.back());
	for (int i = (int)v.a.size() - 2; i >= 0; --i)
		stream << setw(BASE_W) << setfill('0') << v.a[i];
	return stream;
}

Matrix::Matrix(){}
Matrix::Matrix(const Matrix& b) :a(b.a) {}
Matrix::Matrix(const vector<vector<bigint>>& b) 
{
	for (int i = 0; i < b.size(); i++)
	{
		for (int j = 0; j < b[i].size(); j++)
		{
			this->a[i][j] = b[i][j];
		}
	}
}

Matrix operator + (const Matrix& a, const Matrix& b)
{
	Matrix ans;
	for (int i = 0; i < a.a.size() || i < b.a.size(); i++)
	{
		vector<bigint> s;
		for (int j = 0;i<a.a.size()&&j < a.a[i].size() || i<b.a.size()&& j< b.a[i].size(); j++)
		{
			s.push_back(0);
			if (i < a.a.size() && j < a.a[i].size()) s[j] += a.a[i][j];
			if (i < b.a.size() && j < b.a[i].size()) s[j] += b.a[i][j];
		}
		ans.a.push_back(s);
	}
	for (int i = 0; i < ans.a.size(); i++)
	{
		while (ans.a[i].size() < ans.a.size()) ans.a[i].push_back(0);
	}
	return ans;
}
Matrix operator + (const Matrix& a, const bigint& b)
{
	Matrix ans;
	for (int i = 0; i < a.a.size(); i++)
	{
		vector<bigint>s;
		for (int j = 0; j < a.a[i].size(); j++)
		{
			s.push_back(a.a[i][j] + b);
		}
		ans.a.push_back(s);
	}
	return ans;
}
Matrix operator - (const Matrix& a, const Matrix& b)
{
	Matrix ans;
	for (int i = 0; i < a.a.size() || i < b.a.size(); i++)
	{
		vector<bigint> s;
		for (int j = 0; i < a.a.size() && j < a.a[i].size() || i < b.a.size() && j < b.a[i].size(); j++)
		{
			s.push_back(0);
			if (i < a.a.size() && j < a.a[i].size()) s[j] += a.a[i][j];
			if (i < b.a.size() && j < b.a[i].size()) s[j] -= b.a[i][j];
		}
		ans.a.push_back(s);
	}
	for (int i = 0; i < ans.a.size(); i++)
	{
		while (ans.a[i].size() < ans.a.size()) ans.a[i].push_back(0);
	}
	return ans;
}
Matrix operator - (const Matrix& a, const bigint& b)
{
	Matrix ans;
	for (int i = 0; i < a.a.size(); i++)
	{
		vector<bigint> s;
		for (int j = 0; j < a.a[i].size(); j++)
		{
			s.push_back(a.a[i][j] - b);
		}
		ans.a.push_back(s);
	}
	return ans;
}
Matrix operator * (const Matrix& a, const Matrix& b)
{
	Matrix ans, copya = a, copyb = b;
	if (a.a[0].size() < b.a.size())
	{
		for (int i = 0; i < a.a.size(); i++)
		{
			int t = b.a.size() - a.a[0].size();
			while (t--)
			{
				copya.a[i].push_back(0);
			}
		}
	}
	else if (a.a[0].size() > b.a.size())
	{
		vector<bigint>s;
		for (int i = 0; i < b.a[0].size(); i++) s.push_back(0);
		int t = a.a[0].size() - b.a.size();
		while (t--) copyb.a.push_back(s);
	}
	for (int i = 0; i < copya.a.size(); i++)
	{
		vector<bigint>s;
		for (int j = 0; j < copyb.a[0].size(); j++)
		{
			s.push_back(0);
		}
		ans.a.push_back(s);
	}
	for (int i = 0; i < copya.a.size(); i++)//行
	{
		for (int j = 0; j < copyb.a[0].size(); j++)//列
		{
			for (int k = 0; k < copyb.a.size(); k++)
			{
				ans.a[i][j] += copya.a[i][k] * copyb.a[k][j];
			}
		}
	}
	return ans;
}
Matrix operator * (const Matrix& a, const bigint& b)
{
	Matrix ans;
	for (int i = 0; i < a.a.size(); i++)
	{
		vector<bigint>s;
		for (int j = 0; j < a.a[i].size(); j++)
		{
			s.push_back(a.a[i][j] * b);
		}
		ans.a.push_back(s);
	}
	return ans;
}

Matrix& Matrix::operator +=(const Matrix& other)
{
	*this = *this + other;
	return *this;
}
Matrix& Matrix::operator -=(const Matrix& other)
{
	*this = *this - other;
	return *this;
}
Matrix& Matrix::operator *= (const Matrix& other)
{
	*this = *this * other;
	return *this;
}
Matrix& Matrix::operator +=(const bigint& other)
{
	*this = *this + other;
	return *this;
}
Matrix& Matrix::operator -=(const bigint& other)
{
	*this = *this - other;
	return *this;
}
Matrix& Matrix::operator *= (const bigint& other)
{
	*this = *this * other;
	return *this;
}


Matrix Matrix::inverse() //输出伴随矩阵
{
	Matrix ans = *this;
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < a.size(); j++)
		{
			Matrix t;
			for (int m = 0; m < a.size(); m++)
			{
				if (m == i) continue;
				vector<bigint> s;
				for (int n = 0; n < a.size(); n++)
				{
					if (n != j) s.push_back(a[m][n]);
				}
				t.a.push_back(s);
			}
			if ((i + j) & 1) ans.a[j][i] = -1 * t.value();
			else ans.a[j][i] = t.value();

		}
	}
	return ans;
}

Matrix Matrix::ladder()//化为行阶梯
{
	bigint gcd(bigint a, bigint b);
	Matrix ans = *this;
	for (int i = 0; i < ans.a[0].size()&&i<ans.a.size(); i++)
	{
		bigint g = ans.a[i][i];
		for (int j = i+1; j < ans.a.size(); j++)
		{
			bigint t = gcd(g, ans.a[j][i]);
			if (t != 0) g = g * ans.a[j][i] / t;
		}

		for (int j = i; j < ans.a.size(); j++)
		{
			bigint p = 1;
			if(ans.a[j][i]!=0) p = g / ans.a[j][i];
			for (int k = 0; k < ans.a[j].size(); k++)
			{
				ans.a[j][k] *= p;
			}
		}
		for (int j = i+1; j < ans.a.size()&&a[i][i]==0; j++)
		{
			if (ans.a[j][i] != 0)
			{
				vector<bigint>temp = ans.a[j];
				ans.a[j] = ans.a[i];
				ans.a[i] = temp;
				break;
			}
		}
		for (int j = i+1; j < ans.a.size(); j++)
		{
			if (ans.a[j][i] != 0)
			{
				for (int k = 0; k < ans.a[j].size(); k++) ans.a[j][k] -= ans.a[i][k];
			}
		}

		for (int j = i; j < ans.a.size(); j++)
		{
			g = 0;
			for (int k = 0; k < ans.a[j].size(); k++)
			{
				g = gcd(g, ans.a[j][k]);
			}

			for (int k = 0; k < ans.a[j].size(); k++)
			{
				if (g != 0) ans.a[j][k] /= g;
			}
		}
	}
	for (int i = 0; i < ans.a.size(); i++)
	{
		if (ans.a[i][0] < 0)
		{
			for (int j = 0; j < ans.a[i].size(); j++)
			{
				ans.a[i][j] *= -1;
			}
		}
	}
	return ans;
}

bigint Matrix::value() //输出行列式的值
{
	void dfs(int n, const Matrix& a,bigint& ans,vector<int>& ss,map<int,int>& book,int num);
	Matrix a = *this;
	int n = a.a.size();
	vector<int>ss;
	map<int, int>book;
	bigint ans=0;
	dfs(n, a, ans, ss, book,0);
	return ans;
}
void Matrix::get()
{
	cout << "请输入矩阵的行数和列数" << endl;
	int m, n;
	cin >> m >> n;
	cout << "请输入矩阵:" << endl;
	for (int i = 0; i < m; i++)
	{
		vector<bigint> s;
		for (int j = 0; j < n; j++)
		{
			bigint t;
			cin >> t;
			s.push_back(t);
		}
		a.push_back(s);
	}
}
void Matrix::show()
{
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < a[i].size(); j++)
		{
			if (j != a[i].size() - 1) cout << a[i][j] << setw(10) << setfill(' ');
			else cout << a[i][j] << endl;
		}
	}
}

bigint gcd(bigint a, bigint b)
{
	if (a < 0) a = -1*a;
	if (b < 0) b = -1*b;
	if (a == 0) return b;
	if (b == 0) return a;
	return gcd(b, a % b);
}
bigint qpow(bigint a, bigint b)
{
	bigint ans = 1;
	if (b == 0) return ans;
	if (a == 0) return 0;
	while (b > 0)
	{
		if (b % 2) ans *= a;
		b /= 2;
		a *= a;
	}
	return ans;
}
Matrix qpow(Matrix a,bigint b)
{
	Matrix ans = a;
	if (b == 0)
	{
		for (int i = 0; i < ans.a.size(); i++)
		{
			for (int j = 0; j < ans.a[i].size(); j++)
			{
				if (ans.a[i][j] > 0) ans.a[i][j] = 1;
			}
		}
		return ans;
	}
	b--;
	while (b > 0)
	{
		if (b % 2) ans *= a;
		b /= 2;
		a *= a;
	}
	return ans;
}
void ops(stack<bigint>& s1, stack<char>& op)
{
	char c = op.top(); op.pop();
	if (c == '!')
	{
		bigint b = s1.top(); s1.pop();
		bigint ans = 1;
		for (int i = 1; i <= b; i++) ans *= i;
		s1.push(ans);
		return;
	}
	bigint b = s1.top(); s1.pop();
	bigint a = s1.top(); s1.pop();
	if (c == '+')s1.push(a + b);
	else if (c == '-')s1.push(a - b);
	else if (c == '*')s1.push(a * b);
	else if (c == '/')s1.push(a / b);
	else
	{
		bigint t = qpow(a, b);
		s1.push(t);
	}
}
int nx(vector<int>& ss)
{
	int sum = 0;
	for (int i = 0; i < ss.size(); i++)
	{
		for (int j = 0; j < i; j++)
		{
			if (ss[i] < ss[j]) sum++;
		}
	}
	if (sum % 2) return -1;
	else return 1;
}
void dfs(int n, const Matrix& a, bigint& ans, vector<int>& ss, map<int, int>& book, int num)
{
	if (num == n)
	{
		bigint sum = 1;
		for (int i = 0; i < ss.size(); i++)
		{
			sum*=a.a[i][ss[i]];
		}
		int flag = nx(ss);
		ans += flag * sum;
		return;
	}
	for (int i = 0; i < n; i++)
	{
		if (!book[i])
		{
			book[i] = 1;
			ss.push_back(i);
			dfs(n, a, ans, ss, book, num + 1);
			ss.pop_back();
			book[i] = 0;
		}
	}
}
bigint Compute(string s)
{
	stack<bigint> s1;
	stack<char> op;
	int n = s.size();
	for (int i = 0; i < n; i++) op.push('(');
	s.push_back(')');
	n = s.size();
	for (int i = 0; i < n; i++)
	{
		char c = s[i];
		if (c >= '0' && c <= '9')
		{
			bigint t = 0;
			int j = i;
			while (s[j] >= '0' && s[j] <= '9')
			{
				t = t * 10 + s[j] - '0';
				j++;
			}
			i = j - 1;
			s1.push(t);
		}
		else
		{
			if (c == '-')
			{
				if (i && s[i - 1] != ')' && !(s[i - 1] >= '0' && s[i - 1] <= '9') || i == 0)
				{
					bigint t = 0;
					int j = i + 1;
					while (s[j] >= '0' && s[j] <= '9')
					{
						t = t * 10 + s[j] - '0';
						j++;
					}
					i = j - 1;
					s1.push(-1ll * t);
				}
				else
				{
					while (op.size() && op.top() != '(')ops(s1, op);
					op.push(c);
				}
			}
			else if (c == '+')
			{
				while (op.size() && op.top() != '(')ops(s1, op);
				op.push(c);
			}
			else if (c == '*' || c == '/')
			{
				while (op.size() && op.top() != '(' && op.top() == '*' || op.top() == '/' || op.top() == '^')ops(s1, op);
				op.push(c);
			}
			else if (c == '^')
			{
				while (op.size() && op.top() == '^')ops(s1, op);
				op.push(c);
			}
			else if (c == ')')
			{
				while (op.size() && op.top() != '(')ops(s1, op);
				op.pop();
			}
			else if (c == '(') op.push(c);
			else if (c == '!')
			{
				op.push(c);
				ops(s1, op);
			}
		}
	}
	return s1.top();
}

main函数:

#include<stack>
#include<cstdlib>
#include<string>
#include<iostream>
#include"bigint.h"

using namespace std;

bigint qpow(bigint a, bigint b);
void ops(stack<bigint>& s1, stack<char>& op);
bigint Compute(string s);
Matrix qpow(Matrix a, bigint b);
void show();
void show_1();
void show_11(const bigint& a);
void show_2();
void show_21();
void show_22();
void show_23();
void show_24();
void show_25();

int main()
{
	int n;
	while (1)
	{
		system("cls");
		show();
		cin >> n;
		if (n == 1)
		{
			while (1)
			{
				system("cls");
				show_1();
				getchar();
				char pos;
				pos = getchar();
				if (pos == '0') break;
			}

		}
		else if (n == 2)
		{
			system("cls");
			show_2();
			int n2;
			cin >> n2;
			if (n2 == 1)
			{
				while (1)
				{
					system("cls");
					show_21();
					getchar();
					char pos;
					pos = getchar();
					if (pos == '0') break;
				}
			}
			else if (n2 == 2)
			{
				while (1)
				{
					system("cls");
					show_22();
					getchar();
					char pos;
					pos = getchar();
					if (pos == '0') break;
				}
			}
			else if (n2 == 3)
			{
				while (1)
				{
					system("cls");
					show_23();
					getchar();
					char pos;
					pos = getchar();
					if (pos == '0') break;
				}
			}
			else if (n2 == 4)
			{
				while (1)
				{
					system("cls");
					show_24();
					getchar();
					char pos;
					pos = getchar();
					if (pos == '0') break;
				}
			}
			else if (n2 == 5)
			{
				while (1)
				{
					system("cls");
					show_25();
					getchar();
					char pos;
					pos = getchar();
					if (pos == '0') break;
				}
			}
		}
		else if (n == 3)
		{
			system("cls");
			break;
		}
	}
	return 0;
}

void show()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl;
	cout << endl;
	cout << endl;
	cout << "1、普通计算器" << endl;
	cout << "2、矩阵计算" << endl;
	cout << "3、退出" << endl;
}
void show_1()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl;
	cout << endl << endl;
	cout << "支持加、减、乘、除、幂、阶乘运算" << endl;
	cout << "例如:2^31+5!+(2*3+5)/2" << endl << endl;
	cout << "请输入表达式:" << endl;
	string s;
	cin >> s;
	bigint ans=Compute(s);
	cout << "结果为:" << ans << endl << endl;
	cout << "按0返回,按其他键重新输入" << endl;
}
void show_11(const bigint& a)
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl;
	cout << endl << endl;
	cout << "结果为:" << a << endl << endl;
	cout << "按0返回,按其他键重新输入" << endl;
}
void show_2()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl << endl << endl;
	cout << "1、矩阵乘法" << endl;
	cout << "2、幂运算" << endl;
	cout << "3、矩阵转化成行阶梯" << endl;
	cout << "4、计算伴随矩阵、行列式的值(方阵)" << endl;
	cout << "5、查询斐波那契第n项" << endl;
	cout << "6、返回上一级" << endl;
}
void show_21()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl << endl << endl;
	cout << "请输入矩阵个数" << endl;
	int n;
	cin >> n;
	Matrix ans;
	for (int i = 1; i <= n; i++)
	{
		cout << "第" << i << "个矩阵" << endl;
		Matrix a;
		a.get();
		if (i == 1) ans = a;
		else ans *= a;
		cout << endl;
	}
	cout << endl;
	cout << "结果为:" << endl;
	ans.show();
	cout << endl;
	cout << "按0返回,按其他键继续" << endl;
}
void show_22()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl << endl << endl;
	Matrix a,ans;
	a.get();
	cout << "请输入幂数" << endl;
	bigint b;
	cin >> b;
	ans = qpow(a, b);
	cout << "结果为:"<< endl;
	ans.show();
	cout << endl;
	cout << "按0返回,按其他键重新输入" << endl;
}
void show_23()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl << endl << endl;
	Matrix a,b;
	a.get();
	b = a.ladder();
	cout << "行阶梯矩阵为:" << endl;
	b.show();
	cout << endl;
	cout << "按0返回,按其他键重新输入" << endl;
}
void show_24()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl << endl << endl;
	Matrix a;
	a.get();
	cout << "伴随矩阵为:" << endl;
	Matrix ans = a.inverse();
	ans.show();
	cout << endl;
	cout << "行列式的值为:" << endl;
	cout << a.value() << endl << endl;
	cout << endl;
	cout << "按0返回,按其他键重新输入" << endl;
}
void show_25()
{
	cout << "**********高精度计算器************" << endl;
	cout << "制作人:软件185褚润发" << endl << endl << endl;
	cout << "输入n的数值" << endl;
	bigint n;
	cin >> n;
	Matrix s;
	vector<bigint>a;
	a.push_back(1);
	a.push_back(1);
	s.a.push_back(a);
	a.pop_back();
	a.push_back(0);
	s.a.push_back(a);
	Matrix ans = qpow(s, n);
	cout << ans.a[1][0] << endl;
	cout << endl;
	cout << "按0返回,按其他键继续" << endl;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值