以下程序通过给定的分子式计算摩尔数 不含碳以外其他元素的化合物的质量, 氢,氮和氧。
在启动时,程序应在控制台上显示消息
Please enter the formula in the form CcHhNnOo (c, h, n, o - integers)
然后,用户必须根据上述模板输入公式。 如果任何原子的化学指数为0或1,则必须由数字给出。
例如,对于低级化合物,以蓝色输入文本:
C0H2N0O1 (water, H2O) MW = 18.015
C17H4N0O0 (methane, CH4) MW = 16.043
C7H5N3O6 (trinitrotoluene, C7H5N3O6) MW = 227.132
C9H13N103 (adrenaline, C9H13NO3) MW = 183.207
C12H22N0O11 (sucrose, C12H22O11) MW = 342.297
可以根据以下公式进行计算:
MW = nC.AC + nH.AH + nN.AN + nO.AO
其中MW是所需的摩尔质量。 nC, nH, nN and nO
分别是分子中碳,氢,氮和氧原子的数目
该化合物和AC, AH, AN and AO
是相应的原子质量:
AC = 12.011
AH = 1.008
AN = 14.007
AO = 15.999
输入公式后,程序必须计算并写入
控制台寻求摩尔质量。如果该公式不包含排列在顺序行中的原子,而不是摩尔表,该程序应显示以下消息:Wrong formula! Please use the form CcHhNnOo!
到目前为止,我们要做的是完整的程序,但是要删除很多检查,因为我们试图使程序简单到10行代码。到目前为止,该程序已在CodeBlocks中用C ++编写,因此我们更希望运行该程序。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char formula[512];
cout << "Please enter formula in the form CcHhNnOo (c, h, n, o - integers):" << endl;
cin >> formula;
if (formula[0] != 'C')
{
cout << "Wrong formula! Please use the form CcHhNnOo!" << endl;
return 0;
}
int len = strlen(formula);
char curr_element = ' ';
bool C_entered = false, H_entered = false, N_entered = false;
int nC = 0, nH = 0, nN = 0, nO = 0;
for (int i = 0; i < len; i++)
{
if (formula[i] >= '0' && formula[i] <= '9')
{
switch (curr_element)
{
case 'C':
nC *= 10;
nC += formula[i] - '0';
break;
case 'H':
nH *= 10;
nH += formula[i] - '0';
break;
case 'N':
nN *= 10;
nN += formula[i] - '0';
break;
case 'O':
nO *= 10;
nO += formula[i] - '0';
break;
}
}
else
{
switch (formula[i])
{
case 'C':
curr_element = 'C';
C_entered = true;
break;
case 'H':
curr_element = 'H';
H_entered = true;
break;
case 'N':
curr_element = 'N';
N_entered = true;
break;
case 'O':
curr_element = 'O';
break;
default:
cout << "Wrong formula! Please use the form CcHhNnOo!" << endl;
return 0;
}
if ((curr_element == 'H' && C_entered == false) ||
(curr_element == 'N' && H_entered == false) ||
(curr_element == 'O' && N_entered == false))
{
cout << "Wrong formula! Please use the form CcHhNnOo!" << endl;
return 0;
}
}
}
float weight = nC * 12.011f + nH * 1.008f + nN * 14.007f + nO * 15.999f;
cout << "MW = " << weight << endl;
return 0;
}
理想情况下,理想的情况是简化上面的代码。
答案 0 :(得分:0)
这里的复杂性不一定要降低,但至少要短一些:
#include <iostream>
#include <numeric>
#include <string>
#include <regex>
#include <cctype>
#include <map>
int main() {
std::string formula;
std::map<char, int> mapper;
std::cout << "Enter formula (CcHhNnOo): ";
std::getline( std::cin, formula );
if ( std::regex_match( formula.begin(), formula.end(), std::regex( "^((C[0-9]+)?)((H[0-9]+)?)((N[0-9]+)?)((O[0-9]+)?)$" ) ) ) {
std::vector<int> value;
char last_char = 'S';
for ( unsigned int i = 0; i < formula.size(); ++i ) {
char c = formula[i];
if ( (std::isalpha( c ) && last_char != 'S') || i == formula.size() - 1 ) {
if ( i == formula.size() - 1 ) value.push_back( static_cast<int>(c) - 48 );
int mul = 1;
mapper[last_char] = std::accumulate( value.rbegin(), value.rend(), 0, [&mul](int a, int b) {
int new_value = mul * b + a;
mul *= 10;
return new_value;
});
}
if ( std::isalpha( c ) ) {
value = std::vector<int>();
last_char = c;
} else { value.push_back( static_cast<int>(c) - 48 ); }
}
float weight = mapper['C'] * 12.011f + mapper['H'] * 1.008f + mapper['N'] * 14.007f + mapper['O'] * 15.999f;
std::cout << weight << "\n";
}
return 0;
}
请记住,如果只要求用户输入数字,则可以避免这种情况:
#include <iostream>
int main() {
int C, H, N, O;
std::cout << "Enter formula in order C H N O: ";
std::cin >> C >> H >> N >> O;
float weight = C * 12.011f + H * 1.008f + N * 14.007f + O * 15.999f;
std::cout << "MW = " << weight << "\n";
return 0;
}