// demonstration of creating a matrix at run time, // with input from the presumably human user #include "SOME_DIRECTORY/mat_gen_dim.h" using namespace mat_gen_dim; #include #include using std::cin; using std::string; using std::getline; using std::istringstream; sign_int get_int (bool const nonneg = false) { static sign_int n; while (true) { if (nonneg) cout << "(unsigned integer) "; else cout << "(signed integer) "; string str1; getline (cin, str1); istringstream iss {str1}; if (iss >> n) { string str2; iss >> str2; if (str2.empty()) return n; } else if (iss.bad()) { cout << "\n something strange happened "; exit (-1); } else if (iss.fail()) { iss.clear(); } else { cout << "\n something strange happened "; exit (-2); } cout << " *** \"" << str1 << "\" is not an integer *** \n"; } } double get_double () { static double n; while (true) { cout << "(floating-point) "; string str1; getline (cin, str1); istringstream iss {str1}; if (iss >> n) { string str2; iss >> str2; if (str2.empty()) return n; } else if (iss.bad()) { cout << "\n something strange happened "; exit (-3); } else if (iss.fail()) { iss.clear(); } else { cout << "\n something strange happened "; exit (-4); } cout << " *** \"" << str1 << "\" is not a floating-point number *** \n"; } } mat_gen_dim::blt get_blt (sign_int const n) { cout << "\n blt #" << n << " lower limit ... "; sign_int const bot {get_int()}; while (true) { cout << " blt #" << n << " upper limit ... "; sign_int const top {get_int ()}; if (top >= bot) { mat_gen_dim::blt const blt (bot, top); cout << " blt #" << n << " is " << blt << "\n"; return blt; } cout << " *** upper limit cannot be less than lower "; cout << "limit, which is " << bot << " *** \n"; } } mat_gen_dim::form get_form () { static int dim; while (true) { cout << "\n get form ... how many dimensions? "; dim = get_int(true); if (dim >= 0) break; cout << " *** dimensionality cannot be less than zero *** \n"; } vector temp; for (sign_int n {0}; n < dim; ++n) temp.push_back(get_blt(n)); mat_gen_dim::form const form {temp}; cout << "\n form: " << form; return form; } mat_gen_dim::var_matrix get_matrix (mat_gen_dim::form const & frm) { mat_gen_dim::var_matrix mat {frm}; cout << "\n\n fill matrix with values ... \n"; sign_int const count {frm.count()}; for (sign_int n {0}; n < count; ++n) { mat_gen_dim::vec_si const v {frm.ylppa(n)}; cout << " component " << v << ": "; mat.set (n, get_double()); } return mat; } int main () { cout.setf (std::ios::fixed, std::ios::floatfield); cout.precision (4); mat_gen_dim::form const frm {get_form()}; mat_gen_dim::con_matrix const mat {get_matrix(frm)}; cout << "\n finished matrix:" << mat; return 0; }