65,199
社区成员




这是我的源代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector> // 包含对应的头文件来使用 std::vector
class BigInteger {
public:
BigInteger() : number("0") {}
BigInteger(std::string num) : number(removeLeadingZeros(num)) {}
BigInteger(const BigInteger& other) : number(other.number) {}
// 重载运算符
BigInteger operator+(const BigInteger& other) const;
BigInteger operator-(const BigInteger& other) const;
BigInteger operator*(const BigInteger& other) const;
BigInteger operator/(const BigInteger& other) const;
// 输入输出重载
friend std::ostream& operator<<(std::ostream& out, const BigInteger& bigInt);
friend std::istream& operator>>(std::istream& in, BigInteger& bigInt);
private:
std::string number; // 用于存储大整数的字符串
// 辅助函数
static std::string addStrings(const std::string& a, const std::string& b);
static std::string subtractStrings(const std::string& a, const std::string& b);
static std::string multiplyStrings(const std::string& a, const std::string& b);
static std::string divideStrings(const std::string& a, const std::string& b);
static bool isSmaller(const std::string& a, const std::string& b);
static std::string removeLeadingZeros(const std::string& s);
};
std::string BigInteger::addStrings(const std::string& a, const std::string& b) {
std::string result;
int carry = 0;
int i = a.length() - 1, j = b.length() - 1;
while (i >= 0 || j >= 0 || carry) {
int sum = carry;
if (i >= 0) sum += a[i--] - '0';
if (j >= 0) sum += b[j--] - '0';
carry = sum / 10;
result.push_back(sum % 10 + '0');
}
reverse(result.begin(), result.end());
return result;
}
std::string BigInteger::subtractStrings(const std::string& a, const std::string& b) {
// 确保 a 总是大于或等于 b
if (isSmaller(a, b)) {
return "-" + subtractStrings(b, a);
}
std::string result;
int n1 = a.length(), n2 = b.length();
std::reverse(a.begin(), a.end());
std::reverse(b.begin(), b.end());
int carry = 0;
for (int i = 0; i < n2; i++) {
int sub = ((a[i] - '0') - (b[i] - '0') - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else {
carry = 0;
}
result.push_back(sub + '0');
}
for (int i = n2; i < n1; i++) {
int sub = ((a[i] - '0') - carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else {
carry = 0;
}
result.push_back(sub + '0');
}
std::reverse(result.begin(), result.end());
return removeLeadingZeros(result);
}
std::string BigInteger::multiplyStrings(const std::string& a, const std::string& b) {
int n1 = a.size();
int n2 = b.size();
if (n1 == 0 || n2 == 0) {
return "0";
}
std::vector<int> result(n1 + n2, 0);
int i_n1 = 0;
int i_n2 = 0;
for (int i = n1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = a[i] - '0';
i_n2 = 0;
for (int j = n2 - 1; j >= 0; j--) {
int n2 = b[j] - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.size() - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
std::string s = "";
while (i >= 0) {
s += std::to_string(result[i--]);
}
return s;
}
std::string BigInteger::divideStrings(const std::string& numerator, const std::string& denominator) {
// 首先处理分母为0的情况
if (denominator == "0") return "NaN"; // 不是数
int lenNumerator = numerator.length();
int lenDenominator = denominator.length();
// 如果分子小于分母,则结果为0
if (isSmaller(numerator, denominator)) return "0E0";
std::string result;
std::string temp = numerator.substr(0, lenDenominator - 1);
int resIndex = 0;
while (lenDenominator + resIndex <= lenNumerator) {
temp += numerator[lenDenominator - 1 + resIndex];
temp = removeLeadingZeros(temp);
// 用于存储临时商的计数器
int count = 0;
while (!isSmaller(temp, denominator)) {
temp = subtractStrings(temp, denominator);
count++;
}
result += std::to_string(count);
resIndex++;
}
// 移除结果中的前导零
result = removeLeadingZeros(result);
// 如果结果为空,返回0
if (result.empty()) return "0E0";
// 格式化为科学计数法
if (result.length() == 1) {
return result + "E0";
}
else {
return result[0] + std::string(".") + result.substr(1) + "E" + std::to_string(result.length() - 1);
}
}
bool BigInteger::isSmaller(const std::string& a, const std::string& b) {
int n1 = a.length(), n2 = b.length();
if (n1 < n2) return true;
if (n2 < n1) return false;
for (int i = 0; i < n1; i++) {
if (a[i] < b[i]) return true;
else if (a[i] > b[i]) return false;
}
return false; // 相等的情况
}
std::string BigInteger::removeLeadingZeros(const std::string& s) {
std::string::size_type i = 0;
while (i < s.length() - 1 && s[i] == '0') {
i++;
}
return s.substr(i);
}
BigInteger BigInteger::operator+(const BigInteger& other) const {
return BigInteger(addStrings(this->number, other.number));
}
BigInteger BigInteger::operator-(const BigInteger& other) const {
return BigInteger(subtractStrings(this->number, other.number));
}
BigInteger BigInteger::operator*(const BigInteger& other) const {
return BigInteger(multiplyStrings(this->number, other.number));
}
BigInteger BigInteger::operator/(const BigInteger& other) const {
return BigInteger(divideStrings(this->number, other.number));
}
std::ostream& operator<<(std::ostream& out, const BigInteger& bigInt) {
out << bigInt.number;
return out;
}
std::istream& operator>>(std::istream& in, BigInteger& bigInt) {
in >> bigInt.number;
bigInt.number = BigInteger::removeLeadingZeros(bigInt.number);
return in;
}
int main() {
BigInteger num1, num2;
std::cin >> num1 >> num2;
std::cout << "Sum: " << num1 + num2 << std::endl;
std::cout << "Difference: " << num1 - num2 << std::endl;
std::cout << "Product: " << num1 * num2 << std::endl;
std::cout << "Division: " << num1 / num2 << std::endl;
return 0;
}
这是报错:
文本信息:严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C3892 “_Left”: 不能给常量赋值 Unsigned_large_integer_calculator C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\utility 101
这是相关代码:
再次感谢!!!
std::string BigInteger::subtractStrings(const std::string& a, const std::string& b) {
....
std::reverse(a.begin(), a.end());
std::reverse(b.begin(), b.end());
这种问题就通过注释代码块然后看编译能不能成功来判断问题
仅供参考:
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j<n;j++) {
scanf("%s%s",a1,a2);
L=strlen(a1);
for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';
L=strlen(a2);
for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';
for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];
for (i=0;i<MAXLEN;i++) {
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}
printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);
z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");
printf("\n");
}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110