OKlibrary  0.2.1.6
SolutionShift.cpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 4.12.2010 (Swansea)
00002 /* Copyright 2010 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00021 #include <iostream>
00022 #include <vector>
00023 
00024 #include <boost/lexical_cast.hpp>
00025 
00026 namespace {
00027 
00028 #  ifndef INT_TYPE_SOL
00029 #    define INT_TYPE_SOL int
00030 #  endif
00031 
00032   typedef INT_TYPE_SOL int_type;
00033 
00034   enum {
00035     errcode_parameter = 1,
00036     errcode_parameter_values = 2,
00037     errcode_neg_parameter = 3,
00038     errcode_null_literal = 4
00039   };
00040         
00041   const std::string program = "SolutionShift";
00042   const std::string err = "ERROR[" + program + "]: ";
00043 
00044 }
00045 
00046 int main(const int argc, const char* const argv[]) {
00047 
00048   if (argc > 2) {
00049     std::cerr << err << "At most one argument is needed, the shift d "
00050       "(default is d=1).\n";
00051     return(errcode_parameter);
00052   }
00053 
00054   try {
00055 
00056     const int_type d = (argc == 1) ? 1 : boost::lexical_cast<int_type>(argv[1]);
00057     if (d < 0) {
00058       std::cerr << err << "The parameter must be non-negative.\n";
00059       return(errcode_neg_parameter);
00060     }
00061 
00062     typedef std::vector<int_type> v_type;
00063     v_type solution;
00064 
00065     // reading of solution:
00066     for (int_type x; std::cin >> x; solution.push_back(x))
00067       if (x == 0) {
00068         std::cerr << err << "Illegal literal 0.\n";
00069         return(errcode_null_literal);
00070       }
00071 
00072     // output of shifted solution:
00073     {typedef v_type::const_iterator iterator;
00074      const iterator end = solution.end();
00075      for (iterator i = solution.begin(); i != end; ++i)
00076        std::cout << ((*i > 0) ? *i+d : -((-*i)+d)) << " ";
00077     }
00078     std::cout << std::endl;
00079   }
00080   catch (boost::bad_lexical_cast&) {
00081     std::cerr << err << "The parameter must be an integer\n"
00082       "in the range given by INT_TYPE_SOL (default = int).\n";
00083     return(errcode_parameter_values);
00084   }
00085 
00086 }
00087