Saturday, May 22, 2010

I want a program whith c++ (it should get a string a=[12 3;4 5.5] and put the numbers in a matrix )?

it should understand size of matrix and put numbers in it,s arrey

I want a program whith c++ (it should get a string a=[12 3;4 5.5] and put the numbers in a matrix )?
I didn't find out what exactly you want but if you mean that you must enter a string like [12,13,25.5,123,12] and the program puts them into a array it's very easy. else it's not the case you can mail me and I'll tell you the soloution.


NOTE: I didn't test the program cuz i'm right now at cofenet and it might not work correctly and depending on your compiler you may change some of lines. I recommend Microsoft Visual Studion 6 or Borland C++ for you








//this programs wrote in a old style and


//works with many compilers


#include %26lt;iostream.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;





void main()


{


double *array;


char string[255], *temp,*ptr, *tptr, number[10]; //or more


cout %26lt;%26lt; "Enter your desigered string: " %26lt;%26lt;endl;


cin.getline(string, 255);


temp = new char [strlen(string) + 1];


strcpy(temp , string);


int size;


ptr = strtok(temp, ',');


for (size = 1; ptr != NULL; size++)


ptr = strtok(NULL, ',');


array = new double [++size];


int i = 0;


ptr = strtok(string, ',');


strncpy(number, string + 1, ptr - string - 1); //ignore [


number[ptr - string] = '\0';


array [i] = atof(number);


tptr = ptr;


ptr = strtok(NULL, ',');


while (ptr != NULL)


{


strncpy(number, tptr + 1, ptr - tptr - 1);


number[ptr - tptr] = '\0';


array [++i] = atof(number);


tptr = ptr;


ptr = strtok(NULL, ',');


}


for (int j = 0; j %26lt; size; j++)


cout %26lt;%26lt; "The array [" %26lt;%26lt; j %26lt;%26lt; "] = " %26lt;%26lt; array[j] %26lt;%26lt; endl;


}


No comments:

Post a Comment