1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <sstream>
#include <string>
using namespace std;
#define np string::npos
int str_int(string a); //string to int
int nbr_element(string a, int ln); //retourne le nombre d'élément d'élimiter par ","
int main()
{string b;
string tmp; //pour la sous-chaine avec l'int
int e, fc, nb; //les position des "," et le nombre d'element
int x=0; //pour les boucles
int ln, pos=1; //pour la longeur de b, et la possition des copie dans res[]
getline(cin,b);
ln=b.length();
nb=nbr_element(b,ln);
int *res = new int[nb]; //on crée le tableau d'int
e=b.find_last_of(",") ; //on recopie le dernier
tmp.assign(b,e+1,ln-1);
res[nb-1]=str_int(tmp);
nb-=2;
e=b.find(",",0); //on recopie le premier
tmp.assign(b,0,e);
res[0]=str_int(tmp);
x=1, e=0;
while(x) //reperage des nombres et sauvgarde
{e=b.find(",",e+1);
fc=b.find(",",e+1); // a partir du nouveau e
if ((e!=np)&&(fc!=np))
{
tmp = b.substr(e+1,fc-e-1);
res[pos]=str_int(tmp);
pos++;
}
else{
x=0;
};
};
nb=nbr_element(b,b.length()); //affichage du resultat
for(x=0;x!=nb;x++)
cout << res[x] << "\t";
return 0;
};
int str_int(string a)
{
istringstream iss(a);
int tm;
iss>>tm;
return tm;
}
int nbr_element(string a, int ln)
{ int x=0, lc=0;
while(x!=np)
{x=a.find(",",x+1);
if (x!=np) //si a la possision x y'a une ","
lc++;
};
lc++;
return lc;
}; |
Partager