P1001 A+B Problem
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <ctime>
int _abplus_marker = 0;
namespace 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 ERROR: return "ERROR";
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;
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); }
};
Logger glog;
}
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);
}
bool isNumber(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;
}
for(;i<s.size();i++)
if(!std::isdigit((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
{
struct BigInt
{
std::vector<int> dig;
bool neg{false};
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; }
}
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;
}
BigInt operator+(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 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 = neg;
}
return res;
}
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]);
return s;
}
};
}
namespace Parser
{
using namespace Log;
struct Result
{
bool ok{false};
long long a{0},b{0};
};
Result parse(const std::string& line)
{
Result r;
std::string s = StrUtil::trim(line);
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))
{
glog.error("invalid numeric token");
return r;
}
try{
r.a = StrUtil::toLL(sa);
r.b = StrUtil::toLL(sb);
}catch(...){
glog.error("convert failed");
return r;
}
r.ok = true;
glog.debug("parsed a="+std::to_string(r.a)+" b="+std::to_string(r.b));
return r;
}
}
namespace Test
{
using namespace Log;
struct Case{
std::string name;
long long 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}
};
int pass=0,fail=0;
for(auto& c : cs)
{
long long got = c.a + c.b;
if(got == 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));
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)
{
using namespace Log;
using namespace Parser;
glog.setMinLevel(WARN);
bool doTest = false;
for(int i=1;i<argc;i++)
{
std::string arg(argv[i]);
if(arg == "--test") doTest = true;
if(arg == "--quiet") glog.setEnable(false);
}
if(doTest)
{
int f = Test::runTests();
if(f>0) return 1;
}
std::string line;
while(std::getline(std::cin,line))
{
auto res = parse(line);
if(!res.ok) continue;
std::cout << (res.a + res.b) << "\n";
}
return 0;
}