Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h> /* fflushstdin */
- #include <string>
- using namespace std;
- int main()
- {
- int T;
- int i, j, k;
- float cte_C = 0;
- int cte_N = 1;
- int p_10;
- float mass;
- string formula;
- string aux;
- cin >> T;
- while(T)
- {
- fflush(stdin);
- getline(cin, formula);
- mass = 0;
- for(i=0; i < formula.size(); i++)
- {
- switch(formula[i])
- {
- case 'C' : cte_C = 12.01; break;
- case 'H' : cte_C = 1.008; break;
- case 'O' : cte_C = 16.00; break;
- case 'N' : cte_C = 14.01; break;
- }
- j = 0;
- if(formula[i+1] >= '0' && formula[i+1] <= '9'){
- cte_N = 0;
- while(formula[i+1] >= '0' && formula[i+1] <= '9') /* Pegando o Numero */
- {
- j++;
- i++;
- }
- p_10 = 1;
- for(k=i; j != 0; j--) /* Multiplicando Invertido */
- {
- cte_N += p_10 * (formula[k] - 48);
- k--;
- p_10 *= 10;
- }
- }
- else{
- cte_N = 1;
- }
- mass += cte_C * cte_N;
- }
- cout << mass << endl;
- T--;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement