Advertisement
nasserahmed96

matrix

May 9th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5.  
  6. #define GROW_BY 10
  7.  
  8.  
  9.  
  10. void convertStringToFloat(char *);
  11.  
  12. int main(int argc, char *arcv[]){
  13.     //Define a user input as a string, loop over that string and separate each row of it with ;
  14.     //so then the loop determines ';' it counts a new row and so on, determine the number
  15.     //of rows by the first row in the matrix
  16.     char  matrix[100][100];
  17.     char *str_p, *next_p, *tmp_p;
  18.     int ch, need, chars_read;
  19.     if(GROW_BY < 2){
  20.         fprintf(stderr, "Growth constant is too small\n");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.  
  24.     str_p = (char *)malloc(GROW_BY);
  25.  
  26.     if(str_p == NULL){
  27.         fprintf(stderr, "No initial store\n");
  28.         exit(EXIT_FAILURE);
  29.     }
  30.     next_p = str_p;
  31.     chars_read = 0;
  32.     while((ch = getchar()) != EOF){
  33.         int row = 0;
  34.         int col = 0;
  35.  
  36.         //parse the input characters from here, every ';' indicates a new row in the matrix
  37.         if(ch == ']'){
  38.             *next_p = 0;
  39.             //printf("%s\n", str_p);
  40.             convertStringToFloat(str_p);
  41.             free(str_p);
  42.             chars_read = 0;
  43.             str_p = (char *)malloc(GROW_BY);
  44.             if(str_p == NULL){
  45.                 fprintf(stderr, "No initial store \n");
  46.                 exit(EXIT_FAILURE);
  47.             }
  48.             next_p = str_p;
  49.             continue;
  50.         }
  51.         if(chars_read == (GROW_BY - 1)){
  52.             *next_p = 0;
  53.             need = next_p - str_p + 1;
  54.             tmp_p = (char *)malloc(need + GROW_BY);
  55.             if(tmp_p == NULL){
  56.                 fprintf(stderr, "No more store\n");
  57.                 exit(EXIT_FAILURE);
  58.             }
  59.             strcpy(tmp_p, str_p);
  60.             free(str_p);
  61.             str_p = tmp_p;
  62.             next_p = str_p + need - 1;
  63.             chars_read = 0;
  64.         }
  65.         *next_p++ = ch;
  66.         chars_read++;
  67.     }
  68.     if(str_p - next_p){
  69.         *next_p = 0;
  70.         fprintf(stderr, "Incomplete last line\n");
  71.         printf("%s\n", str_p);
  72.     }
  73.    
  74.     exit(EXIT_SUCCESS);
  75. }
  76.  
  77.  
  78. void convertStringToFloat(char *str){
  79.     //First split the line into an array
  80.     char ch;
  81.     ch = *str;
  82.     char *pch, *end_row;
  83.     pch = strtok_r(str, ";", &end_row);
  84.     while(pch != NULL){
  85.         char *end_char;
  86.         printf("%s\n", pch);
  87.         printf("after: %s\n", pch);
  88.         char *num = strtok_r(pch, " ", &end_char);
  89.         while(num != NULL){
  90.             printf("%s\n", num);
  91.             float float_num = atof(num);
  92.             printf("float: %f\n", float_num);
  93.             num = strtok_r(NULL, " ", &end_char);
  94.         }
  95.         pch = strtok_r(NULL, ";", &end_row);
  96.         free(num);
  97.     }
  98.     //printf("%s\n", str);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement