**本程序由C语言实现元素信息的查询和分子相对分子质量的计算。**
这是我第一次写博客,(==本人C语言小白==)由于明天我就要期末考了,代码写得很匆忙,
计算分子式时最好不要输入带有括号的分子式。
等我期末考完,我会进一步完善这个程序。
最后期待的功能:
1.根据分子量计算化学式 2.进行化学方程式的配平
3.计算溶液的PH,Ka 4.计算质量分数等等
谢谢你的观看和点评。
#define _CRT_SECURE_NO_WARNINGS //这行代码确保vs可以使用scanf
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//声明元素结构体
typedef struct Element {
char symbol[4];
char name[4];
float aram;
float ram;
}Element;
Element element[37];
//存储元素的有关信息
char s[37][4] = { "","H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S",\
"Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr" };
char n[37][4] = { "","氢","氦","锂","铍","硼","碳","氮","氧","氟","氖","钠","镁","铝","硅","磷",\
"硫","氯","氩","钾","钙","钪","钛" ,"钒","铬","锰","铁","钴","镍","铜","锌","镓","锗","砷","硒","溴","氪" };
float am[37] = { 0,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.06,\
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.41,69.72,72.64,74.92,78.96,79.90,83.80 };
float m[37] = { 0,1,4,7,9,10.8,12,14,16,19,20,23,24,27,28,31,32,35.5,40,39,40,45,48,51,52,55,56,59,59,64,65,70,73,75,79,80,84 };
//声明函数
int search_num(char sym[]); //根据元素符号返回原子序数
void print_info(void); //打印元素信息
void count_mass(void); //计算分子质量
int main(void)
{
//将元素信息输入结构体变量
for (int i = 1; i <= 36; i++)
{
strcpy(element[i].symbol, s[i]);
strcpy(element[i].name, n[i]);
element[i].aram = am[i];
element[i].ram = m[i];
}
int w;
printf("************欢迎你使用ZCX元素计算器*************\n");
printf("************输入1进入元素查询******************\n");
printf("************输入2进入分子量计算****************\n");
printf("************输入0退出程序*********************\n");
while (1 == scanf("%d", &w)) {
switch (w)
{
case 1:
print_info();
break;
case 2:
count_mass();
break;
case 0:
printf("谢谢使用!\n");
return 0;
default:
printf("输入有误!请重新输入:\n");
break;
}
printf("请继续按主菜单输入:\n");
}
}
int search_num(char sym[])
{
for (int i = 1; i <= 36; i++)
if (!strcmp(element[i].symbol, sym))
return i;
return 0;
}
void print_info(void)
{
int k = 1, num = 0;
char sym[4];
while (k) {
int j = 1;
printf("请输入你要查询的元素的英文符号\n");
scanf("%s", sym);
num = search_num(sym);
if (num)printf("你输入的元素已找到!\n");
else {
printf("你输入的元素不存在!\n请再次输入!\n");
continue;
}
printf("请输入你要查询的信息:\n");
printf("************1.元素名称***************\n");
printf("************2.精确相对原子质量*******\n");
printf("************3.相对原子质量***********\n");
printf("************4.退出该元素查询*********\n");
printf("************0.退出查询系统***********\n");
while (j) {
if (1 == scanf("%d", &k) && k >= 0 && k <= 4) {
switch (k)
{
case 1:
printf("元素名称:%s\n", element[num].name);
break;
case 2:
printf("精确相对原子质量:%g\n", element[num].aram);
break;
case 3:
printf("相对原子质量:%g\n", element[num].ram);
break;
case 4:
printf("\n");
j = 0;
break;
default:
printf("谢谢使用!请您慢走!\n");
goto help;
break;
}
}
else
{
printf("输入错误!请重新输入!\n");
}
}
help:
printf("已成功退出!\n");
}
}
void count_mass(void)
{
int k = 1;
while (k) {
int n = 0, q = 0; //q表示括号的状态
float m = 0, m1 = 0; //m是分子量,m1是括号内分子量和
char molecular[20], molecular_symbol[4], ch, th, * p = molecular;
printf("请正确输入分子式:\n");
scanf("%s", molecular);
while (ch = *p++) {
th = *p;
if (ch == '(') {
q = 1;
continue;
}
if (ch == ')') {
q = 2;
continue;
}
if (q == 2) {
m += m1 * (ch - '0');
m1 = 0;
continue;
}
molecular_symbol[0] = ch;
molecular_symbol[1] = th;
molecular_symbol[2] = '\0';
n = search_num(molecular_symbol);
if (n)
{
th = *(p + 1);
if (!q) {
if (isdigit(th)) {
m += element[n].ram * (th - '0') + m1;
m1 = 0;
p += 2;
}
else {
m += element[n].ram + m1;
m1 = 0;
p++;
}
}
else {
if (isdigit(th)) {
m1 += element[n].ram * (th - '0');
p += 2;
}
else {
m1 += element[n].ram;
p++;
}
}
}
else
{
molecular_symbol[1] = '\0';
n = search_num(molecular_symbol);
if (n)
{
if (!q) {
if (isdigit(th)) {
m += element[n].ram * (th - '0') + m1;
m1 = 0;
p++;
}
else {
m += element[n].ram + m1;
m1 = 0;
}
}
else {
if (isdigit(th)) {
m1 += element[n].ram * (th - '0');
p++;
}
else
m1 += element[n].ram;
}
}
else {
printf("输入有误!请重新输入:\n");
break;
}
}
}
printf("%s的相对分子质量为: %g\n", molecular, m);
printf("您想计算下一个分子吗?输入1表示是,0表示否");
scanf("%d", &k);
}
}