高精加模板求助
  • 板块学术版
  • 楼主Gcc_Gdb_7_8_1
  • 当前回复1
  • 已保存回复2
  • 发布时间2024/8/31 13:31
  • 上次更新2024/8/31 13:40:55
查看原帖
高精加模板求助
1058410
Gcc_Gdb_7_8_1楼主2024/8/31 13:31

无聊写的高精度头文件(只写了加法),结果老是 CE, 一直看不懂

Code:

/*
	Name: bigint.hpp
	Author: Gcc_Gdb_7_8_1
	Date: 29/08/24 22:13
	Description: This file is an about big integer. It only can usage in C++.
*/

#ifndef __BIGINT_H
#define __BIGINT_H 1

#ifndef ONLINE_JUDGE
#pragma GCC system_header
#endif

#ifndef __cplusplus
#error It only can usage in C++!
#endif

#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>
#include <cstring>

class Biginteger
{
	private:
		std::vector<char> num;
		int op; // 1 or -1
	public:
		Biginteger() {}
		Biginteger(int num) {
            while (num) {
				this -> num.push_back(num % 10);
				num /= 10;
			}
			std::reverse(this -> num.begin(), this -> num.end());
		}
		Biginteger(long long num) {
            while (num) {
				this -> num.push_back(num % 10LL);
				num /= 10LL;
			}
			std::reverse(this -> num.begin(), this -> num.end());
		}
		Biginteger(unsigned long long num) {
            while (num) {
				this -> num.push_back(num % 10ULL);
				num /= 10ULL;
			}
			std::reverse(this -> num.begin(), this -> num.end());
		}
		Biginteger(std::string str) {
			for (size_t i = str.size() - 1; i >= 0; ++i) {
				num.push_back(str[i] ^ 48);
			}
		}
		Biginteger(const char* str) {
			for (int i = strlen(str); i >= 0; ++i) {
				num.push_back(str[i] ^ 48);
			}
		}
		Biginteger(const Biginteger &rhs) {
			num.resize(0);
			for (auto i: rhs.num) {
				num.push_back(i);
			}
		}
		void read(void) {
			op = 1;
			char ch = getchar();
			while (ch < '0' || ch > '9') {
				if (ch == '-') {
					op = -1;
				}
				ch = getchar();
			}
			while (ch >= '0' && ch <= '0') {
				num.push_back(ch ^ 48);
				ch = getchar();
			}
			std::reverse(num.begin(), num.end());
		}
		void write(void) {
			if (op == -1) {
				putchar('-');
			}
			for (size_t i = num.size() - 1; i >= 0; --i) {
				putchar(num[i] ^ 48);
			}
		}
		const Biginteger operator+(const Biginteger &rhs) const {
			Biginteger res;
			char add = 0;
			int num_old_size = num.size();
			int rhs_num_old_size = rhs.num.size();
			num.resize(std::max(num.size(), rhs.num.size()) + 1);
			rhs.num.resize(std::max(num.size(), rhs.num.size()) + 1);
			for (int i = 0; i < std::max(num.size(), rhs.num.size()) + 1; ++i) {
				res.num.push_back(num[i] + rhs.num[i] + add);
				add = res.num[i] % 10;
				res.num[i] /= 10;
			}
			while (res.num.size() > 1 && !res.num.back()) {
				res.num.pop_back();
			}
			num.resize(num_old_size);
			rhs.num.resize(rhs_num_old_size);
			res.op = op * rhs.op;
			return res;
		}
		
};

typedef Biginteger bigint;

#endif

CE 内容:

99 55 C:\Users\42175\OneDrive\Desktop\bigint.hpp [Error] passing 'const std::vector<char>' as 'this' argument of 'void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp, _Alloc>::size_type = long long unsigned int]' discards qualifiers [-fpermissive]

2024/8/31 13:31
加载中...