请用C语言编写一个函数,其具有计算相对分子质量和查询相对原子质量两种功能
时间: 2024-03-08 14:51:22 浏览: 234
好的,我可以帮你编写这个函数。以下是示例代码:
```c
#include <stdio.h>
#include <string.h>
// 原子质量表
const double atomicMassTable[118] = {
1.008, 4.003, 6.941, 9.012, 10.81, 12.01, 14.01, 16.00, 19.00, 20.18,
22.99, 24.31, 26.98, 28.09, 30.97, 32.07, 35.45, 39.95, 39.10, 40.08,
44.96, 47.87, 50.94, 52.00, 54.94, 55.85, 58.93, 58.69, 63.55, 65.38,
69.72, 72.63, 74.92, 78.96, 79.90, 83.80, 85.47, 87.62, 88.91, 91.22,
92.91, 95.96, 98.00, 101.1, 102.9, 106.4, 107.9, 112.4, 114.8, 118.7,
121.8, 127.6, 126.9, 131.3, 132.9, 137.3, 138.9, 140.1, 140.9, 144.2,
145.0, 150.4, 152.0, 157.3, 158.9, 162.5, 164.9, 167.3, 168.9, 173.0,
175.0, 178.5, 181.0, 183.8, 186.2, 190.2, 192.2, 195.1, 197.0, 200.6,
204.4, 207.2, 208.9, 209.0, 210.0, 222.0, 223.0, 226.0, 227.0, 232.0,
231.0, 238.0, 237.0, 244.0, 243.0, 247.0, 247.0, 251.0, 252.0, 257.0,
258.0, 259.0, 262.0, 267.0, 270.0, 271.0, 270.0, 277.0, 276.0, 281.0,
280.0, 285.0, 284.0, 289.0, 288.0, 293.0, 294.0, 294.0, 294.0, 293.0,
294.0, 294.0, 293.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0,
294.0, 294.0, 294.0, 293.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0,
294.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0, 294.0
};
// 计算相对分子质量
double calculateMolecularMass(char* formula) {
double totalMass = 0.0;
int len = strlen(formula);
for (int i = 0; i < len; i++) {
if (formula[i] >= 'A' && formula[i] <= 'Z') {
// 大写字母,表示一个新的元素
int j = i + 1;
while (j < len && formula[j] >= 'a' && formula[j] <= 'z') {
j++;
}
int count = 1;
if (j > i + 1) {
count = atoi(formula + i + 1);
}
char symbol[3] = { formula[i], '\0', '\0' };
if (j > i + 1) {
symbol[1] = formula[j - 1];
}
int atomicNumber = 0;
if (strcmp(symbol, "H") == 0) {
atomicNumber = 1;
} else if (strcmp(symbol, "He") == 0) {
atomicNumber = 2;
} else if (strcmp(symbol, "Li") == 0) {
atomicNumber = 3;
} else if (strcmp(symbol, "Be") == 0) {
atomicNumber = 4;
} else if (strcmp(symbol, "B") == 0) {
atomicNumber = 5;
} else if (strcmp(symbol, "C") == 0) {
atomicNumber = 6;
} else if (strcmp(symbol, "N") == 0) {
atomicNumber = 7;
} else if (strcmp(symbol, "O") == 0) {
atomicNumber = 8;
} else if (strcmp(symbol, "F") == 0) {
atomicNumber = 9;
} else if (strcmp(symbol, "Ne") == 0) {
atomicNumber = 10;
} else if (strcmp(symbol, "Na") == 0) {
atomicNumber = 11;
} else if (strcmp(symbol, "Mg") == 0) {
atomicNumber = 12;
} else if (strcmp(symbol, "Al") == 0) {
atomicNumber = 13;
} else if (strcmp(symbol, "Si") == 0) {
atomicNumber = 14;
} else if (strcmp(symbol, "P") == 0) {
atomicNumber = 15;
} else if (strcmp(symbol, "S") == 0) {
atomicNumber = 16;
} else if (strcmp(symbol, "Cl") == 0) {
atomicNumber = 17;
} else if (strcmp(symbol, "K") == 0) {
atomicNumber = 19;
} else if (strcmp(symbol, "Ca") == 0) {
atomicNumber = 20;
} else if (strcmp(symbol, "Sc") == 0) {
atomicNumber = 21;
} else if (strcmp(symbol, "Ti") == 0) {
atomicNumber = 22;
} else if (strcmp(symbol, "V") == 0) {
atomicNumber = 23;
} else if (strcmp(symbol, "Cr") == 0) {
atomicNumber = 24;
} else if (strcmp(symbol, "Mn") == 0) {
atomicNumber = 25;
} else if (strcmp(symbol, "Fe") == 0) {
atomicNumber = 26;
} else if (strcmp(symbol, "Ni") == 0) {
atomicNumber = 28;
} else if (strcmp(symbol, "Co") == 0) {
atomicNumber = 27;
} else if (strcmp(symbol, "Cu") == 0) {
atomicNumber = 29;
} else if (strcmp(symbol, "Zn") == 0) {
atomicNumber = 30;
} else {
printf("Unknown element: %s\n", symbol);
return -1.0;
}
totalMass += atomicMassTable[atomicNumber - 1] * count;
i = j - 1;
} else {
printf("Invalid character: %c\n", formula[i]);
return -1.0;
}
}
return totalMass;
}
// 查询相对原子质量
double getAtomicMass(char* symbol) {
if (strcmp(symbol, "H") == 0) {
return atomicMassTable[0];
} else if (strcmp(symbol, "He") == 0) {
return atomicMassTable[1];
} else if (strcmp(symbol, "Li") == 0) {
return atomicMassTable[2];
} else if (strcmp(symbol, "Be") == 0) {
return atomicMassTable[3];
} else if (strcmp(symbol, "B") == 0) {
return atomicMassTable[4];
} else if (strcmp(symbol, "C") == 0) {
return atomicMassTable[5];
} else if (strcmp(symbol, "N") == 0) {
return atomicMassTable[6];
} else if (strcmp(symbol, "O") == 0) {
return atomicMassTable[7];
} else if (strcmp(symbol, "F") == 0) {
return atomicMassTable[8];
} else if (strcmp(symbol, "Ne") == 0) {
return atomicMassTable[9];
} else if (strcmp(symbol, "Na") == 0) {
return atomicMassTable[10];
} else if (strcmp(symbol, "Mg") == 0) {
return atomicMassTable[11];
} else if (strcmp(symbol, "Al") == 0) {
return atomicMassTable[12];
} else if (strcmp(symbol, "Si") == 0) {
return atomicMassTable[13];
} else if (strcmp(symbol, "P") == 0) {
return atomicMassTable[14];
} else if (strcmp(symbol, "S") == 0) {
return atomicMassTable[15];
} else if (strcmp(symbol, "Cl") == 0) {
return atomicMassTable[16];
} else if (strcmp(symbol, "K") == 0) {
return atomicMassTable[18];
} else if (strcmp(symbol, "Ca") == 0) {
return atomicMassTable[19];
} else if (strcmp(symbol, "Sc") == 0) {
return atomicMassTable[20];
} else if (strcmp(symbol, "Ti") == 0) {
return atomicMassTable[21];
} else if (strcmp(symbol, "V") == 0) {
return atomicMassTable[22];
} else if (strcmp(symbol, "Cr") == 0) {
return atomicMassTable[23];
} else if (strcmp(symbol, "Mn") == 0) {
return atomicMassTable[24];
} else if (strcmp(symbol, "Fe") == 0) {
return atomicMassTable[25];
} else if (strcmp(symbol, "Ni") == 0) {
return atomicMassTable[27];
} else if (strcmp(symbol, "Co") == 0) {
return atomicMassTable[26];
} else if (strcmp(symbol, "Cu") == 0) {
return atomicMassTable[28];
} else if (strcmp(symbol, "Zn") == 0) {
return atomicMassTable[29];
} else {
printf("Unknown element: %s\n", symbol);
return -1.0;
}
}
int main() {
// 计算相对分子质量
double relMass = calculateMolecularMass("H2O");
printf("Relative molecular mass of H2O: %.2f\n", relMass);
// 查询相对原子质量
double atomicMass = getAtomicMass("C");
printf("Relative atomic mass of C: %.2f\n", atomicMass);
return 0;
}
```
这个程序中,我们首先定义了一个原子质量表,其中包含了118个元素的相对原子质量。然后,我们实现了两个函数:`calculateMolecularMass`和`getAtomicMass`。
`calculateMolecularMass`函数用于计算给定分子式的相对分子质量。它接受一个字符串参数,表示分子式,返回一个浮点数,表示相对分子质量。该函数会遍历分子式中的每个元素,并根据原子质量表中对应元素的相对原子质量和元素数量来计算总的相对分子质量。
`getAtomicMass`函数用于查询给定元素的相对原子质量。它接受一个字符串参数,表示元素符号,返回一个浮点数,表示相对原子质量。该函数会根据给定符号在原子质量表中查找对应元素的相对原子质量。
在主函数中,我们分别演示了如何使用这两个函数来计算相对分子质量和查询相对原子质量。
阅读全文
相关推荐


















