细节: 1.想得到char类型的 数字 用于计算式,需要 int h = *(i+1) -'0'; 必须减去‘0’;
2.switch语句中,不要忘了用default收尾
#include<iostream>
#include<string>
int main(void)
{
int emm;
std::cin >> emm;
int emmm = 0;
double sum1[emm];
std::string s;
while(emm>0)
{
double sum = 0;
std::cin >> s;
for(auto i = s.begin(); i != s.end(); i++)
{
double j ;
switch (*i)
{
case 'C': j = 12.01; break;
case 'H': j = 1.008; break;
case 'O': j = 16.00; break;
case 'N': j = 14.01; break;
default: break;
}
int jj = 0;
while(isdigit(*(i+1)))
{
int h = *(i+1) - '0';
jj = jj*10 + h;
i++;
}
if(jj==0)
sum += j;
else
sum += jj*j;
}
emm--;
sum1[emmm] = sum;
emmm++;
}
for(int i = 0; i < emmm; i++)
{
printf("%.3lf\n",sum1[i]);
}
}