Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h> /* fflushstdin */
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int T;
  10.     int i, j, k;
  11.     float cte_C = 0;
  12.     int cte_N = 1;
  13.     int p_10;
  14.     float mass;
  15.  
  16.     string formula;
  17.     string aux;
  18.  
  19.     cin >> T;
  20.  
  21.     while(T)
  22.     {
  23.         fflush(stdin);
  24.         getline(cin, formula);
  25.  
  26.         mass = 0;
  27.  
  28.         for(i=0; i < formula.size(); i++)
  29.         {
  30.  
  31.  
  32.             switch(formula[i])
  33.             {
  34.                 case 'C' : cte_C = 12.01; break;
  35.                 case 'H' : cte_C = 1.008; break;
  36.                 case 'O' : cte_C = 16.00; break;
  37.                 case 'N' : cte_C = 14.01; break;
  38.             }
  39.  
  40.             j = 0;
  41.  
  42.             if(formula[i+1] >= '0' && formula[i+1] <= '9'){
  43.  
  44.                 cte_N = 0;
  45.  
  46.                 while(formula[i+1] >= '0' && formula[i+1] <= '9') /* Pegando o Numero */
  47.                 {
  48.  
  49.                     j++;
  50.                     i++;
  51.                 }
  52.  
  53.                 p_10 = 1;
  54.  
  55.                 for(k=i; j != 0; j--) /* Multiplicando Invertido */
  56.                 {
  57.                     cte_N += p_10 * (formula[k] - 48);
  58.                     k--;
  59.                     p_10 *= 10;
  60.                 }
  61.             }
  62.  
  63.             else{
  64.                 cte_N = 1;
  65.             }
  66.  
  67.  
  68.             mass += cte_C * cte_N;
  69.         }
  70.  
  71.         cout << mass << endl;
  72.  
  73.         T--;
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement