Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define GROW_BY 10
- void convertStringToFloat(char *);
- int main(int argc, char *arcv[]){
- //Define a user input as a string, loop over that string and separate each row of it with ;
- //so then the loop determines ';' it counts a new row and so on, determine the number
- //of rows by the first row in the matrix
- char matrix[100][100];
- char *str_p, *next_p, *tmp_p;
- int ch, need, chars_read;
- if(GROW_BY < 2){
- fprintf(stderr, "Growth constant is too small\n");
- exit(EXIT_FAILURE);
- }
- str_p = (char *)malloc(GROW_BY);
- if(str_p == NULL){
- fprintf(stderr, "No initial store\n");
- exit(EXIT_FAILURE);
- }
- next_p = str_p;
- chars_read = 0;
- while((ch = getchar()) != EOF){
- int row = 0;
- int col = 0;
- //parse the input characters from here, every ';' indicates a new row in the matrix
- if(ch == ']'){
- *next_p = 0;
- //printf("%s\n", str_p);
- convertStringToFloat(str_p);
- free(str_p);
- chars_read = 0;
- str_p = (char *)malloc(GROW_BY);
- if(str_p == NULL){
- fprintf(stderr, "No initial store \n");
- exit(EXIT_FAILURE);
- }
- next_p = str_p;
- continue;
- }
- if(chars_read == (GROW_BY - 1)){
- *next_p = 0;
- need = next_p - str_p + 1;
- tmp_p = (char *)malloc(need + GROW_BY);
- if(tmp_p == NULL){
- fprintf(stderr, "No more store\n");
- exit(EXIT_FAILURE);
- }
- strcpy(tmp_p, str_p);
- free(str_p);
- str_p = tmp_p;
- next_p = str_p + need - 1;
- chars_read = 0;
- }
- *next_p++ = ch;
- chars_read++;
- }
- if(str_p - next_p){
- *next_p = 0;
- fprintf(stderr, "Incomplete last line\n");
- printf("%s\n", str_p);
- }
- exit(EXIT_SUCCESS);
- }
- void convertStringToFloat(char *str){
- //First split the line into an array
- char ch;
- ch = *str;
- char *pch, *end_row;
- pch = strtok_r(str, ";", &end_row);
- while(pch != NULL){
- char *end_char;
- printf("%s\n", pch);
- printf("after: %s\n", pch);
- char *num = strtok_r(pch, " ", &end_char);
- while(num != NULL){
- printf("%s\n", num);
- float float_num = atof(num);
- printf("float: %f\n", float_num);
- num = strtok_r(NULL, " ", &end_char);
- }
- pch = strtok_r(NULL, ";", &end_row);
- free(num);
- }
- //printf("%s\n", str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement