Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Pablo Moreno Olalla
- Email address: darthbrevu@yahoo.es
- */
- #include "BigNumber.h"
- #include <list>
- #include <cstdio>
- #include <string>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- //These functions are going to be called just at one point in the code.
- //So there's no loss in inlining them, although they aren't very small.
- inline void tokenize(const string &str,list<string> &out,char chr=' ') {
- //This could alse have been achieved using objects from the sstream header, but this is safer.
- string::size_type pos=0;
- string::size_type pos2;
- size_t N=str.length();
- while (pos<N&&pos!=string::npos) {
- pos=str.find_first_not_of(chr,pos);
- if (pos==string::npos) break;
- pos2=str.find_first_of(chr,pos);
- out.push_back(str.substr(pos,pos2-pos));
- pos=pos2;
- }
- }
- inline void funString(const string &str) {
- //This function reads a line and adds the numbers contained there, and then shows the result.
- list<string> substrs;
- tokenize(str,substrs);
- Number res=0;
- bool anyNumber=false;
- for (list<string>::const_iterator it=substrs.begin();it!=substrs.end();++it) try {
- res+=Number(*it); //This may raise an exception if the string pointed by it does not contain a number.
- anyNumber=true;
- } catch (const runtime_error &) {} //Not a number? Well... let's just ignore it and continue adding ;).
- if (anyNumber) cout<<static_cast<string>(res)<<endl; //No numbers? Then let's just not show anything.
- }
- int main(int argc,char **argv) {
- //The first two lines are not strictly necessary, but make the code a little more handy.
- if (argc>=2) freopen(argv[1],"r",stdin);
- if (argc>=3) freopen(argv[2],"w",stdout);
- string tmp;
- do {
- getline(cin,tmp);
- funString(tmp);
- } while (!cin.eof());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement