开发者

Parse int and string

Hi I'm not sure if this is the right place to ask this question. Anyway I have written this code to parse a molecule formula and split it into atoms and amount of each atoms.

For instance if I input "H2O" I will for the atom array get {"H", "O"} and in the amount array I will get {2, 1}. I haven't taken account for amount that is larger than 9, since I don't think there are molecule which can bind to something that is larger than 8.

Anyway I'm quite newbie, so I wonder if this piece of code can be made better?

   string formula = "H2O";
   int no, k = 0, a = 0;
   string atom[10];
   int amount[10];
   bool flag = true;
   stringstream ss(formula);

   for(int i = 0; i < formula.size(); ++i)
   {

      no = atoi(&formula[i]);
      if(no == 0 && (flag || islowe开发者_如何学JAVAr(formula[i]) )  )
      {
         cout << "k = " << k << endl;
         atom[k] += formula[i];
         flag = false;
         cout << "FOO1 " << atom[k] << endl;
         amount[a] = 1;
      }
      else if(no != 0)
      {
         amount[a] = no;
         cout << "FOO2 " << amount[a] << endl;
         a++;
         flag = true;
         k++;
      }
      else
      {
         k++;
         a++;
         atom[k] = formula[i];
         cout << "FOO3 " << atom[k] << endl;
         amount[a] = 1;

         flag = false;
      }

      cout << no << endl;
   }


Have you considered an approach with regular expressions? Do you have access to Boost or TR1 regular expressions? An individual atom and its count can easily be represented as:

(after edits based on comments)

([A-Z][a-z]{0,2})([0-9]*)

Then you just need to repeatedly find this pattern in your input string and extract the different parts.


There are many potential improvements that could be made, of course. But as a newbie, I guess you only want the immediate ones. The first improvement is to change this from a program that has a hard coded formula to a program that reads a formula from the user. Then try testing yout program by inputting different formulae, and check that the output is correct.


What if you modified it to be like this algorithm? This would maybe be less code, but would definitely be more clear:

// while not at end of input
     // gather an uppercase letter
     // gather any lowercase letters
     // gather any numbers
     // set the element in your array

This could be implemented with 3 very simple loops inside of your main loop, and would make your intentions to future maintainers much more obvious.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜