簡體   English   中英

C-計算給定公式中每個元素的分子分數的程序

[英]C - A program for calculation of molecular fraction of each element in a formula given

我需要編寫一個程序,通過輸入公式可以定義每個元素的質量分數。 像這樣:輸入:Na [Al(OH)4]
輸出:先生= 118
w(Na)= 19,49%
w(Al)= 22.88%
w(O)= 54,24%
w(高)= 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規則等。

嘗試尋找編譯器解析器和自然文本解釋器。

這個問題應該有用。

如何從字符串中解析公式?

除括號和括號分析外,其他所有內容均有效...

不,不是。

printf("%s: %f%c\n", periodicTable[l].sym, (periodicTable[l].mass / MrOfCompound) * 100.0, '%');

您忘記了用periodicTable[l].mass乘以periodicTable[l].counter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM