UNB/ CS/ David Bremner/ teaching/ old/ cs6375/ files/ readwrite.c
// (c) David Bremner <bremner@unb.ca> 2009
// Distributed under the WTFPL <http://sam.zoy.org/wtfpl/>
// Party like this is C99 
#include <glpk.h>
#include <stdio.h>
int main(int argc, char **argv){

  glp_prob *lp=glp_create_prob();
  // possibly replace GLP_MPS_DECK with GLP_MPS_FILE
  if (glp_read_mps(lp, GLP_MPS_DECK, NULL,argv[1])==0){

    int rows=glp_get_num_rows(lp);
    int cols=glp_get_num_cols(lp);
    int index[cols];
    double coeff[cols];

    for (int j=1; j<=cols; j++)
      printf ("C[%d]=%g\n",j,glp_get_obj_coef(lp, j));

    for (int i=1; i<=rows; i++){
      int non_zeros=glp_get_mat_row(lp,i,index,coeff);
      printf("x[%d]=",i);

      for (int j=1; j<=non_zeros; j++){
	printf("%c%gx[%d]",coeff[j]<0 ? '+' : '-', coeff[j],index[j]);
      }
      puts("");
    }

    for (int i=1; i<=rows; i++){
            switch (glp_get_row_type(lp,i)){
      case GLP_FR:
	break;
      case GLP_LO:
	printf("x[%d]>=%g\n",i,glp_get_row_lb(lp,i));
	
	break;
      case GLP_UP:
	printf("x[%d]<=%g\n",i,glp_get_row_ub(lp,i));
	break;
      case GLP_DB:
	printf("%g <= x[%d] <= %g\n", glp_get_row_lb(lp,i),i, glp_get_row_ub(lp,i));
	break;
      case GLP_FX:
	printf("x[%d]=%g\n",i,glp_get_row_lb(lp,i));
	break;
      }
    }
    
  }

  
}