9.9版,223行
2025-12-09 18:35:12
发布于:福建
1阅读
0回复
0点赞
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <memory>
#include <algorithm>
#include <functional>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cmath>
class CalculationException {
private:
std::string msg;
public:
CalculationException(const std::string& m) : msg(m) {}
const char* what() const { return msg.c_str(); }
};
class Number {
private:
long long value;
bool valid;
void validate() {
valid = (value >= 0 && value <= 1000000000);
}
public:
Number(long long v = 0) : value(v) { validate(); }
long long getValue() const {
if (!valid) throw CalculationException("无效数值");
return value;
}
void setValue(long long v) {
value = v;
validate();
}
bool isValid() const { return valid; }
std::string toString() const {
std::ostringstream oss;
oss << value;
return oss.str();
}
static Number fromString(const std::string& str) {
std::istringstream iss(str);
long long val;
if (!(iss >> val)) {
throw CalculationException("无法解析数字: " + str);
}
return Number(val);
}
};
class ComputeStrategy {
public:
virtual ~ComputeStrategy() {}
virtual Number compute(const Number& a, const Number& b) = 0;
virtual std::string getName() const = 0;
};
class DirectStrategy : public ComputeStrategy {
public:
Number compute(const Number& a, const Number& b) override {
return Number(a.getValue() + b.getValue());
}
std::string getName() const override {
return "直接加法策略";
}
};
class BitwiseStrategy : public ComputeStrategy {
private:
long long bitwiseAdd(long long a, long long b) const {
while (b != 0) {
long long carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
public:
Number compute(const Number& a, const Number& b) override {
return Number(bitwiseAdd(a.getValue(), b.getValue()));
}
std::string getName() const override {
return "位运算加法策略";
}
};
class RecursiveStrategy : public ComputeStrategy {
private:
long long recursiveAdd(long long a, long long b) const {
if (b == 0) return a;
return recursiveAdd(a ^ b, (a & b) << 1);
}
public:
Number compute(const Number& a, const Number& b) override {
return Number(recursiveAdd(a.getValue(), b.getValue()));
}
std::string getName() const override {
return "递归加法策略";
}
};
class InputParser {
private:
static bool isAllDigits(const std::string& s) {
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
}
public:
static std::pair<Number, Number> parse(const std::string& input) {
std::vector<std::string> tokens;
std::istringstream iss(input);
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
if (tokens.size() != 2) {
throw CalculationException("需要恰好两个数字");
}
if (!isAllDigits(tokens[0]) || !isAllDigits(tokens[1])) {
throw CalculationException("输入必须是非负整数");
}
Number a = Number::fromString(tokens[0]);
Number b = Number::fromString(tokens[1]);
if (!a.isValid() || !b.isValid()) {
throw CalculationException("数字超出范围 [0, 1000000000]");
}
return std::make_pair(a, b);
}
};
class Calculator {
private:
std::vector<std::unique_ptr<ComputeStrategy>> strategies;
std::vector<std::string> history;
public:
Calculator() {
strategies.push_back(std::unique_ptr<ComputeStrategy>(new DirectStrategy()));
strategies.push_back(std::unique_ptr<ComputeStrategy>(new BitwiseStrategy()));
strategies.push_back(std::unique_ptr<ComputeStrategy>(new RecursiveStrategy()));
}
Number calculate(const Number& a, const Number& b) {
if (!a.isValid() || !b.isValid()) {
throw CalculationException("无效的操作数");
}
Number result = strategies[0]->compute(a, b);
for (size_t i = 1; i < strategies.size(); ++i) {
Number altResult = strategies[i]->compute(a, b);
if (altResult.getValue() != result.getValue()) {
if (result.getValue() - a.getValue() != b.getValue()) {
throw CalculationException("计算验证失败");
}
}
}
std::ostringstream oss;
oss << a.getValue() << " + " << b.getValue()
<< " = " << result.getValue()
<< " (策略: " << strategies[0]->getName() << ")";
history.push_back(oss.str());
return result;
}
void printHistory() const {
std::cout << "=== 计算历史 ===" << std::endl;
for (const auto& entry : history) {
std::cout << entry << std::endl;
}
}
};
class ResultValidator {
public:
static bool validate(const Number& a, const Number& b, const Number& result) {
bool method1 = (result.getValue() == a.getValue() + b.getValue());
bool method2 = (result.getValue() - a.getValue() == b.getValue());
bool method3 = (result.getValue() - b.getValue() == a.getValue());
bool method4 = result.isValid();
return method1 && method2 && method3 && method4;
}
};
class AdditionApplication {
private:
Calculator calculator;
int totalCalculations;
public:
AdditionApplication() : totalCalculations(0) {}
long long process(const std::string& input) {
try {
auto [a, b] = InputParser::parse(input);
Number result = calculator.calculate(a, b);
if (!ResultValidator::validate(a, b, result)) {
throw CalculationException("结果验证失败");
}
totalCalculations++;
return result.getValue();
} catch (const CalculationException& e) {
std::istringstream iss(input);
long long a, b;
if (iss >> a >> b) {
if (a < 0 || a > 1000000000 || b < 0 || b > 1000000000) {
throw CalculationException("数字超出范围");
}
return a + b;
}
throw;
}
}
int getTotalCalculations() const {
return totalCalculations;
}
};
int main() {
std::string input;
std::getline(std::cin, input);
try {
AdditionApplication app;
long long result = app.process(input);
std::cout << result << std::endl;
return 0;
} catch (const std::exception& e) {
std::istringstream iss(input);
long long a, b;
if (iss >> a >> b) {
if (a >= 0 && a <= 1000000000 && b >= 0 && b <= 1000000000) {
std::cout << (a + b) << std::endl;
return 0;
}
}
std::cerr << "错误: 无法处理输入" << std::endl;
return 1;
}
}
这里空空如也







有帮助,赞一个