版本比较
DDBSWDJX_ljr_159357
个人介绍
增加 191 行删除 99 行
7 行未改动891011121314121513141516172122181920242122232425262728293031323334353736373839404143444546424344454647484951525051525356575854555657586159606364656667616263646566676970686970717273747576777873748182757677787980878182838491929385868788899091929394959697989910098991001011021031011021031041051061071081091101111121131141151161171181191051201211221231241251261271281291301311321331091341351361371111121131381391401411421431441451461471481491501511521531541551561571581591601611151161171181191201621631641221651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941291301951961971981992002012021361372032042052062071422082092102112122132141482152162172182192201501511521532212222231552242252261581591601611622272282292302312322332342352362372382392401662412422432442451711722462471742482492501762512522532542552561811821831841851862572582592602612622632641881892652662671911922682692702711941951962722732742752762772782792802812822002832842852862872882892052902912922932942952962972982992113003013022142153033043053062173073083092203103113123132233143153162262273173183193203213223237 行未改动#include <algorithm>#include <cctype>#include <ctime>
int _abplus_marker = 0;
namespace Lognamespace Log {{ enum Level { DEBUG, INFO, WARN, ERROR }; std::string levelStr(Level lv) { switch(lv){ case DEBUG: return "DEBUG"; case INFO: return "INFO"; case WARN: return "WARN"; case INFO: return "INFO"; case WARN: return "WARN"; case ERROR: return "ERROR"; default: return "UNKNOWN"; default: return "UNKNOWN"; } }
class Logger { private: bool enable{true}; Level minLv{DEBUG}; public: void setEnable(bool b){ enable = b; } void setMinLevel(Level l){ minLv = l; }
void log(Level lv, const std::string& msg) { if(!enable || lv < minLv) return; if (!enable || lv < minLv) return; std::time_t t = std::time(nullptr); char buf[48]; std::strftime(buf, sizeof(buf), "%H:%M:%S", std::localtime(&t)); std::cout << "[" << buf << "][" << levelStr(lv) << "] " << msg << "\n"; } void debug(const std::string& s){ log(DEBUG,s); } void info(const std::string& s){ log(INFO,s); } void warn(const std::string& s){ log(WARN,s); } void error(const std::string& s){ log(ERROR,s); } void debug(const std::string& s){ log(DEBUG, s); } void info(const std::string& s) { log(INFO, s); } void warn(const std::string& s) { log(WARN, s); } void error(const std::string& s){ log(ERROR, s); } }; Logger glog;}
namespace StrUtil{namespace StrUtil { std::string trim(const std::string& s) { size_t l = 0, r = s.size(); while(l < r && std::isspace((unsigned char)s[l])) l++; while(r > l && std::isspace((unsigned char)s[r-1])) r--; return s.substr(l, r-l); while (l < r && std::isspace(static_cast<unsigned char>(s[l]))) l++; while (r > l && std::isspace(static_cast<unsigned char>(s[r-1]))) r--; return s.substr(l, r - l); }
bool isNumber(const std::string& s) bool isInteger(const std::string& s) { if(s.empty()) return false; size_t i=0; if(s[0]=='-'||s[0]=='+'){ if(s.size()==1) return false; i=1; if (s.empty()) return false; size_t i = 0; if (s[0] == '-' || s[0] == '+') { if (s.size() == 1) return false; i = 1; } for(;i<s.size();i++) if(!std::isdigit((unsigned char)s[i])) return false; for (; i < s.size(); ++i) if (!std::isdigit(static_cast<unsigned char>(s[i]))) return false; return true; }
long long toLL(const std::string& s) { if(!isNumber(s)) throw std::invalid_argument("not number"); return std::stoll(s); }}
namespace BigNum{namespace BigNum { struct BigInt { std::vector<int> dig; bool neg{false};
BigInt(){ dig.push_back(0); } BigInt() { dig.push_back(0); } BigInt(long long v) { neg = v < 0; unsigned long long n = neg ? (unsigned long long)(-v) : (unsigned long long)v; if(n==0){ dig.push_back(0); return; } while(n>0){ dig.push_back(n%10); n/=10; } unsigned long long n = neg ? static_cast<unsigned long long>(-v) : static_cast<unsigned long long>(v); if (n == 0) { dig.push_back(0); return; } while (n > 0) { dig.push_back(static_cast<int>(n % 10)); n /= 10; } } BigInt(const std::string& s) { std::string t = StrUtil::trim(s); size_t p=0; if(t[0]=='-'){ neg=true; p=1; } else if(t[0]=='+') p=1; for(int i=(int)t.size()-1;i>=(int)p;i--) dig.push_back(t[i]-'0'); while(dig.size()>1 && dig.back()==0) dig.pop_back(); if(dig.size()==1&&dig[0]==0) neg=false; size_t p = 0; if (t[0] == '-') { neg = true; p = 1; } else if (t[0] == '+') { p = 1; } for (int i = static_cast<int>(t.size()) - 1; i >= static_cast<int>(p); --i) dig.push_back(t[i] - '0'); while (dig.size() > 1 && dig.back() == 0) dig.pop_back(); if (dig.size() == 1 && dig[0] == 0) neg = false; }
// 比较绝对值:|this| < |o| BigInt operator+(const BigInt& o) const bool absLess(const BigInt& o) const { if (dig.size() != o.dig.size()) return dig.size() < o.dig.size(); for (int i = static_cast<int>(dig.size()) - 1; i >= 0; --i) if (dig[i] != o.dig[i]) return dig[i] < o.dig[i]; return false; }
BigInt absAdd(const BigInt& o) const { BigInt res; res.dig.clear(); if(neg == o.neg) int carry = 0; size_t i = 0; while (i < dig.size() || i < o.dig.size() || carry) { int carry=0; size_t i=0; while(i<dig.size()||i<o.dig.size()||carry) int a = (i < dig.size()) ? dig[i] : 0; int b = (i < o.dig.size()) ? o.dig[i] : 0; int sum = a + b + carry; res.dig.push_back(sum % 10); carry = sum / 10; ++i; } res.neg = false; return res; }
BigInt absSub(const BigInt& o) const { // 前提:|this| >= |o| BigInt res; res.dig.clear(); int borrow = 0; for (size_t i = 0; i < dig.size(); ++i) { int a = dig[i] - borrow; int b = (i < o.dig.size()) ? o.dig[i] : 0; borrow = 0; if (a < b) { int a = i<dig.size() ? dig[i]:0; int b = i<o.dig.size() ? o.dig[i]:0; int sum = a+b+carry; res.dig.push_back(sum%10); carry = sum/10; i++; a += 10; borrow = 1; } res.neg = neg; res.dig.push_back(a - b); } while (res.dig.size() > 1 && res.dig.back() == 0) res.dig.pop_back(); res.neg = false; return res; }
BigInt operator+(const BigInt& o) const { if (neg == o.neg) { BigInt r = absAdd(o); r.neg = neg; return r; } if (absLess(o)) { BigInt r = o.absSub(*this); r.neg = o.neg; return r; } BigInt r = absSub(o); r.neg = neg; return r; }
std::string toString() const { std::string s; if(neg) s+="-"; for(int i=(int)dig.size()-1;i>=0;i--) s += (char)('0'+dig[i]); if (neg) s += "-"; for (int i = static_cast<int>(dig.size()) - 1; i >= 0; --i) s += static_cast<char>('0' + dig[i]); return s; } };}
namespace Parser{namespace Parser { using namespace Log; struct Result { bool ok{false}; long long a{0},b{0}; BigNum::BigInt a, b; };
Result parse(const std::string& line) { Result r; std::string s = StrUtil::trim(line); if(s.empty()){ glog.warn("empty line"); return r; } if (s.empty()) { glog.warn("empty line"); return r; } std::stringstream ss(s); std::string sa,sb; ss>>sa>>sb; if(!(ss&&ss.eof())){ glog.error("need two numbers"); return r; } if(!StrUtil::isNumber(sa)||!StrUtil::isNumber(sb)) std::string sa, sb; if (!(ss >> sa >> sb)) { glog.error("invalid numeric token"); glog.error("need two tokens"); return r; } try{ r.a = StrUtil::toLL(sa); r.b = StrUtil::toLL(sb); }catch(...){ glog.error("convert failed"); std::string rem; if (ss >> rem) { glog.error("extra token: " + rem); return r; } if (!StrUtil::isInteger(sa) || !StrUtil::isInteger(sb)) { glog.error("invalid numeric token"); return r; } r.a = BigNum::BigInt(sa); r.b = BigNum::BigInt(sb); r.ok = true; glog.debug("parsed a="+std::to_string(r.a)+" b="+std::to_string(r.b)); glog.debug("parsed a=" + r.a.toString() + " b=" + r.b.toString()); return r; }}
namespace Test{namespace Test { using namespace Log; struct Case{ struct Case { std::string name; long long a,b,exp; std::string a, b, exp; };
int runTests() { std::vector<Case> cs = { {"1+2",1,2,3}, {"0+0",0,0,0}, {"-5+3",-5,3,-2}, {"100+200",100,200,300}, {"-10+-20",-10,-20,-30}, {"999+1",999,1,1000} {"1+2", "1", "2", "3"}, {"0+0", "0", "0", "0"}, {"-5+3", "-5", "3", "-2"}, {"100+200", "100", "200", "300"}, {"-10+-20", "-10", "-20", "-30"}, {"999+1", "999", "1", "1000"}, {"big", "9999999999999999999999999999", "2", "10000000000000000000000000001"} }; int pass=0,fail=0; for(auto& c : cs) int pass = 0, fail = 0; for (auto& c : cs) { long long got = c.a + c.b; if(got == c.exp) BigNum::BigInt va(c.a), vb(c.b); BigNum::BigInt got = va + vb; if (got.toString() == c.exp) { glog.info("[PASS] "+c.name); pass++; }else{ glog.error("[FAIL] "+c.name+" exp="+std::to_string(c.exp)+" got="+std::to_string(got)); glog.info("[PASS] " + c.name); pass++; } else { glog.error("[FAIL] " + c.name + " exp=" + c.exp + " got=" + got.toString()); fail++; } } glog.info("test total:"+std::to_string(cs.size())+" pass:"+std::to_string(pass)+" fail:"+std::to_string(fail)); glog.info("test total:" + std::to_string(cs.size()) + " pass:" + std::to_string(pass) + " fail:" + std::to_string(fail)); return fail; }}
int main(int argc,char** argv)int main(int argc, char** argv){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr);
using namespace Log; using namespace Parser;
glog.setMinLevel(WARN); bool doTest = false; for(int i=1;i<argc;i++) for (int i = 1; i < argc; ++i) { std::string arg(argv[i]); if(arg == "--test") doTest = true; if(arg == "--quiet") glog.setEnable(false); if (arg == "--test") doTest = true; if (arg == "--quiet") glog.setEnable(false); }
if(doTest) if (doTest) { int f = Test::runTests(); if(f>0) return 1; if (f > 0) return 1; }
std::string line; while(std::getline(std::cin,line)) while (std::getline(std::cin, line)) { auto res = parse(line); if(!res.ok) continue; std::cout << (res.a + res.b) << "\n"; if (!res.ok) continue; std::cout << (res.a + res.b).toString() << "\n"; } return 0;}
```