C - 计算给定公式中每种元素的分子分数的程序

标签 c

我需要编写一个程序,通过输入的公式可以定义每个元素的质量分数。像这样的东西: 输入:Na[Al(OH)4]
输出:先生=118
w(Na) = 19,49%
w(铝) = 22,88%
w(O) = 54,24%
w(H) = 3.39%
除了括号和括号分析之外,一切都有效,尽管这很好(我会得到“好”),但我很感兴趣,为什么它不起作用

#include <stdio.h>
#include <ctype.h>
#include <string.h>

//Creating a struct which describes the element
static struct elementData {
    char sym[3];
    double mass;
    int counter; // for formula like NH4NO3, N needs to be counted twice
};


//Creating a periodic table (an array of elements' structs)
static struct elementData periodicTable[] = {
        {"H",  1.0079,  0}, // Not sure if we have to set counter to 0
        {"He", 4.0026,  0}, // as far as i know global variables are set to 0 by default,
        {"Li", 6.939,   0}, // but we'll do that just in case
        {"Be", 9.0122,  0},
        {"Na", 22.9898, 0},
        {"O",  15.9994, 0},
        {"Al", 26.9815, 0}
        //TODO: finish the table
};
static int tableSize = sizeof(periodicTable) / sizeof(periodicTable[0]); //Size of the table

static double MrOfCompound = 0; // Molecular mass of the compound also should be stored

/* "Declaring functions" block*/
void getFormula(int charArray[]);

double getArBySymbol(int symbolLetter1, int symbolLetter2);

void increaseIndexCounter(int symbolLetter1, int symbolLetter2);

void parseOneSymbol(int formula[], int i);

/*End of "declaring functions" block*/

int main() {
    int dontQuit = 1; //while it is not zero the program will be looping
    int formula[100]; // a char array for the formula
    //double temp; //Could have been useful in brackets and parentheses analysis
    while (dontQuit) {
        printf("Enter the formula of a compound or type q to quit\n");
        for (int j = 0; j < 100; ++j) { //The program will be looped, so need to clean formula every loop
            formula[j] = -1;
        }
        for (int k = 0; k < tableSize; ++k) { //The program will be looped, so need to clean counters every loop
            periodicTable[k].counter = 0;
        }
        MrOfCompound = 0;

        getFormula(formula); //Acquiring formula-input from user

        for (int i = 0; i < 100; i++) {
            switch (formula[i]) {
                case 'q':
                    dontQuit = 0;
                    break;
                case -1: // out of record range
                    break;
                    /* case '(': // Parentheses doesnt work
                         i++;
                         temp = MrOfCompound;
                         while(formula[i] != ')') {
                             parseOneSymbol(formula, i);
                             i++;
                         }
                         i++;
                         if(isdigit(formula[i])) {
                             i++;
                             MrOfCompound += (MrOfCompound - temp) * ((formula[i] - '0') - 1);
                         }
                         break; */
                    /*case '[': // Brackets doesnt work
                        i++;
                        temp = MrOfCompound;
                        while(formula[i] != ']')
                            parseOneSymbol(formula, i++);
                        if(isdigit(formula[++i]) != 0 )
                            MrOfCompound += (MrOfCompound - temp)*((formula[i] - '0') - 1);
                        break; */
                case '\0': //End of formula-line
                    break;
                default:
                    parseOneSymbol(formula, i);
            }
        }

        //Data output
        if(dontQuit) {
            printf("Mr: %f\n", MrOfCompound);
            for (int l = 0; l < tableSize; ++l) {
                if (periodicTable[l].counter != 0) {
                    printf("%s: %f%c\n", periodicTable[l].sym, (periodicTable[l].mass / MrOfCompound) * 100.0, '%');
                }
            }
        }
    }
}
/*Defining functions block*/

//A function that handles the input of formula
void getFormula(int s[]) {
    int c, i;
    for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
        s[i] = c;
    }
    s[i] = '\0'; // Line-terminator
}

/* A function which performs linear search through the periodic table
 * to find the symbol (either it consists of 2 chars or of 1)
 * and return its atomic weight Ar */
double getArBySymbol(int l1, int l2) {
    if (l2 == -1) {
        for (int i = 0; i < tableSize; ++i) {
            if (l1 == periodicTable[i].sym[0] && strlen(periodicTable[i].sym) == 1)
                return periodicTable[i].mass;
        }
    } else {
        for (int i = 0; i < tableSize; ++i) {
            if (l1 == periodicTable[i].sym[0] && l2 == periodicTable[i].sym[1])
                return periodicTable[i].mass;
        }
    }

}

/* A function which performs linear search through the periodic table
 * to find the symbol (either it consists of 2 chars or of 1)
 * and increases it's counter */

void increaseIndexCounter(int s1, int s2) {
    if (s2 == -1) {
        for (int i = 0; i < tableSize; ++i) {
            if (s1 == periodicTable[i].sym[0] && strlen(periodicTable[i].sym) == 1)
                periodicTable[i].counter++;
        }
    } else {
        for (int i = 0; i < tableSize; ++i) {
            if (s1 == periodicTable[i].sym[0] && s2 == periodicTable[i].sym[1])
                periodicTable[i].counter++;
        }
    }
}

//Function which handles parsing element symbols and indexes
void parseOneSymbol(int formula[], int i) {
    if (isupper(formula[i])) {
        if (formula[i + 1] == '\0' || isupper(formula[i + 1])) {
            increaseIndexCounter(formula[i], -1);
            MrOfCompound += getArBySymbol(formula[i], -1);
        } else if (isdigit(formula[i + 1])) {
            for (int j = 0; j < (formula[i + 1] - '0'); ++j)
                increaseIndexCounter(formula[i], -1);
            MrOfCompound += (formula[i + 1] - '0') * (getArBySymbol(formula[i], -1));
        } else if (islower(formula[i + 1]) && formula[i + 2] == '\0') {
            increaseIndexCounter(formula[i], formula[i + 1]);
            MrOfCompound += getArBySymbol(formula[i], formula[i + 1]);
        } else if (islower(formula[i + 1]) && isupper(formula[i + 2])) {
            increaseIndexCounter(formula[i], formula[i + 1]);
            MrOfCompound += getArBySymbol(formula[i], formula[i + 1]);
        } else if (islower(formula[i + 1]) && isdigit(formula[i + 2])) {
            for (int j = 0; j < (formula[i + 2] - '0'); ++j)
                increaseIndexCounter(formula[i], formula[i + 1]);
            MrOfCompound += (formula[i + 2] - '0') * (getArBySymbol(formula[i], formula[i + 1]));
        }

    }
}

最佳答案

在 C 或任何其他基本编程语言中,解析字符串和方程并不是一件小事。

您需要设置代码可以从表达式计算的规则和符号。定义所有元素,定义括号的 posfix/prefix/infix 规则等...

尝试寻找编译器解析和自然文本解释器。

这个问题应该有用。

How to parse a formula from a string?

关于C - 计算给定公式中每种元素的分子分数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47740257/

相关文章:

c - 检查字符数组是否为零的快速方法

c - 循环只执行一次指针赋值

c++ - c在c++实践中

c - 如何打印在 main 中声明并作为参数传递给 main 中返回类型为 void 的函数 sum 的两个数字的和。

c - 使用 write() 将用户输入的 char* 写入文件

c++ - 交换整数的代码适用于 C++,但不适用于 C

c - Visual Studio C程序: How to print symbols for card suits?

c - fork()执行过程

c - glib 正则表达式不能在 c 中工作

在两个结构体数组变量之间复制数据