Skip to content

Instantly share code, notes, and snippets.

@samehkamaleldin
Last active August 29, 2015 14:20
Show Gist options
  • Save samehkamaleldin/20247d834bb9d3e9420b to your computer and use it in GitHub Desktop.
Save samehkamaleldin/20247d834bb9d3e9420b to your computer and use it in GitHub Desktop.
Function that reads a matrix from stdin
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
// -----------------------------------------------------------------------------
// used functions prototype declaration
// -----------------------------------------------------------------------------
vector<string> split(const char*, char);
bool is_float( string);
char* str2charp(string);
void omit_error();
float** read_matrix();
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// program's main function
// -----------------------------------------------------------------------------
int main(int argc, char** argv)
{
float** first_matrix = read_matrix();
return 0;
}
// -----------------------------------------------------------------------------
// FUNCTIONS DEFENITION SECTION
// -----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// function that reads a matrix from user
//----------------------------------------------------------------------------
float** read_matrix(){
//----------------------------------------------------------------------------
// read a line of input
//----------------------------------------------------------------------------
std::string line;
std::getline(cin, line);
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// convert from string to const char*
//----------------------------------------------------------------------------
char * line_c = str2charp(line);
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// split to tokents
//----------------------------------------------------------------------------
vector<string> tokens = split(line_c,';');
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// make sure that the first element is an opening of a matrix '['
//----------------------------------------------------------------------------
string first_token = tokens.front();
if( first_token.length() > 1 and first_token.at(0) == '[')
{
// remove the char '[' and save it back to tokens
first_token.erase(0,1);
tokens.front() = first_token;
}
else
{
omit_error();
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// make sure that the last element is a closer of a matrix ']'
//----------------------------------------------------------------------------
string last_token = tokens.back();
if( last_token.length() > 1 and last_token.at(last_token.length()-1) == ']' )
{
// remove the char '[' and save it back to tokens
last_token.erase(last_token.length()-1,1);
tokens.back() = last_token;
}
else
{
omit_error();
}
//----------------------------------------------------------------------------
// define rows count as M
int M = tokens.size();
vector<vector<float>> vector_matrix(M);
for(int tidx = 0; tidx < M; tidx ++)
{
string tk = tokens.at(tidx);
char* tk_cp = str2charp(tk);
vector<string> row_str = split(tk_cp, ' ');
for(int ridx = 0; ridx < row_str.size(); ridx ++)
{
string val_str = row_str.at(ridx);
// check if the value is float
if(is_float(val_str))
{
float val = atof(val_str.c_str());
vector_matrix.at(tidx).push_back(val);
}
else
{
omit_error();
}
}
}
//----------------------------------------------------------------------------
// generate the two dimentional array from the vector
// set columns count to be N
int N = vector_matrix.at(0).size();
// construct two dim array of float using malloc
float** matrix;
matrix = (float**) malloc(M * sizeof(float*));
for(int m = 0; m < M; m++){
matrix[m] = (float*) malloc(N * sizeof(float));
}
for(int mi = 0; mi < M; mi++){
vector<float> vrow = vector_matrix.at(mi);
// error if row size don't match matrix row sizes
if(vrow.size() != N)
{
omit_error();
}
for(int ni = 0; ni < N; ni ++){
matrix[mi][ni] = vrow.at(ni);
}
}
return matrix;
}
//----------------------------------------------------------------------------
// split a string with a specified character
//----------------------------------------------------------------------------
vector<string> split(const char *str, char c)
{
vector<string> result;
do
{
const char *begin = str;
while(*str != c && *str)
str++;
result.push_back(string(begin, str));
} while (0 != *str++);
return result;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// check if a string is float or not
//----------------------------------------------------------------------------
bool is_float( string str ) {
std::istringstream iss(str);
float f;
// noskipws considers leading whitespace invalid
iss >> noskipws >> f;
// Check the entire string was consumed and
// if either failbit or badbit is set
return iss.eof() && !iss.fail();
}
//----------------------------------------------------------------------------
// convert a string to c char pointer
//----------------------------------------------------------------------------
char* str2charp(string str)
{
char* char_pointer = new char[str.size() + 1];
std::copy(str.begin(), str.end(), char_pointer);
char_pointer[str.size()] = '\0';
return char_pointer;
}
//----------------------------------------------------------------------------
void omit_error()
{
cout << "ERROR";
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment