OKlibrary  0.2.1.6
PdExtend.cpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 3.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 
00019 #include <iostream>
00020 #include <vector>
00021 #include <cstdlib>
00022 
00023 #include <boost/lexical_cast.hpp>
00024 
00025 namespace {
00026 
00027 #  ifndef INT_TYPE_VDW
00028 #    define INT_TYPE_VDW int
00029 #  endif
00030 
00031   typedef INT_TYPE_VDW int_type;
00032 
00033   enum {
00034     errcode_parameter = 1,
00035     errcode_parameter_values = 2,
00036     errcode_neg_parameter = 3,
00037     errcode_null_literal = 4,
00038     errcode_too_big_var = 5
00039   };
00040         
00041   const std::string program = "PdExtend";
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 << "Exactly one argument is needed, the number N of "
00050       "vertices.\n";
00051     return(errcode_parameter);
00052   }
00053 
00054   try {
00055 
00056     const int_type N = boost::lexical_cast<int_type>(argv[1]);
00057     if (N < 0) {
00058       std::cerr << err << "The parameter must be non-negative.\n";
00059       return(errcode_neg_parameter);
00060     }
00061     const int_type middle = (N+1)/2;
00062 
00063     typedef std::vector<int_type> v_type;
00064     v_type solution;
00065 
00066     // reading of palindromic solution:
00067     for (int_type x; std::cin >> x; solution.push_back(x)) {
00068       if (x == 0) {
00069         std::cerr << err << "Illegal literal 0.\n";
00070         return(errcode_null_literal);
00071       }
00072       if (std::abs(x) > middle) {
00073         std::cerr << err << "Variable too big: " << x << ".\n";
00074         return(errcode_too_big_var);
00075       }
00076     }
00077 
00078     // output of extended solution:
00079     {typedef v_type::const_iterator iterator;
00080      const iterator end = solution.end();
00081      for (iterator i = solution.begin(); i != end; ++i)
00082        std::cout << *i << " ";
00083     }
00084     {typedef v_type::const_reverse_iterator iterator;
00085      const iterator end = solution.rend();
00086      for (iterator i = solution.rbegin(); i != end; ++i)
00087        std::cout << ((*i > 0) ? N-*i+1 : -(N-(-*i)+1)) << " ";
00088     }
00089     std::cout << std::endl;
00090   }
00091   catch (boost::bad_lexical_cast&) {
00092     std::cerr << err << "The parameter must be an integer\n"
00093       "in the range given by INT_TYPE_VDW (default = int).\n";
00094     return(errcode_parameter_values);
00095   }
00096 
00097 }
00098