yukicoder 222 引き算と足し算

2015/06/20 (Sat) yukicoder 構文解析

問題

問題文

方針

書かれているとおりに,文字列を演算子で $2$ つに分け,演算子と逆の計算を行う. 正規表現 is 便利.

実装

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    string s;
    cin >> s;
    int op = isdigit(s[0]) ? 0 : 1;
    while(isdigit(s[op])) op++;
    string a = s.substr(0,op);
    string b = s.substr(op+1);
    if(s[op]=='+') cout << atoi(a.c_str()) - atoi(b.c_str()) << endl;
    if(s[op]=='-') cout << atoi(a.c_str()) + atoi(b.c_str()) << endl;
}