Bonjour, j'ai codé une fonction (qui marche) qui me renvoie la composition atomique d'une espèce chimique (bien entendu, il y a sûrement plus performant que cette fonction)
donc NumbersAtoms("C7H16",'C') me renvoie bien 7, NumbersAtoms("CH3CHO",'H') me renvoie bien 4 et NumbersAtoms("C7KET21",'C') me renvoie bien 0 (résultat attendu)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 unsigned short NumbersAtoms(char * s,char atom) { /* inputs : s : string containing the name of a species atom : name of an atom : either 'C' or 'H' or 'O' or 'N' this function returns the numbers of atoms "atom" constituting a species */ unsigned short nb=0,i; /* check that all the alphabetic characters are either C or c or H or h or O or o or N or n */ for(i=0;i<strlen(s);++i) if(isalpha(s[i]) && s[i]!='C' && s[i]!='c' && s[i]!='H' && s[i]!='h' && s[i]!='O' && s[i]!='o' && s[i]!='N' && s[i]!='n' && s[i]!='-') return 0; char * p=strchr(s,atom); while(p!=NULL) { char * ss=strdup(p+1); for(i=0;isdigit(ss[i]) && i<strlen(ss);++i); if(i==0) { nb+=1; ++p; } else { ss[i]='\0'; nb+=strtod(ss,NULL); /* == strtod(ss,&ss); */ p+=i; free(ss); ss=NULL; } s=p; p=strchr(s,atom); } /* end of while(p!=NULL) */ return (unsigned short) nb; }
Ma question est : et si l'utilisateur rentre le c7h16 (lettres minuscules) comment faire ? Comment écrire d'un coup la recherche char * p=strchr(s,atom); sachant que je recherche un caractère majuscule ou minuscule ? Ma fonction ne traite que les lettres majuscules.
Merci







Répondre avec citation











Partager