如何处理括号内复合群的重数乘法?

问题描述 投票:0回答:1

我正在开发一个 C 程序,用于分析化学式并计算元素总数。我开发的

identifyCompound
函数可以正确识别单个元素,但我在处理括号内的组的多重性时遇到困难。

例如,在处理公式 Fe2(SO4)3 时,当前输出为 Fe2S + O4,总共 17 个元素,但预期结果将是 Fe2 + S3 + O12,同时仍保持正确的 17 个元素总数。

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

// Function to identify and count elements in a compound
int identifyCompound(char *input, int *index) {
    int numElements = 0;
    int multiplier = 0;
    int i = *index;

    while (input[i] != '\0') {
        if (isupper(input[i])) {
            // Found an element symbol
            if (numElements > 0) {
                printf(" + "); // Add '+' sign between elements
            }
            printf("%c", input[i]);
            numElements++;

            // Check if the next character is a lowercase letter (indicates more atoms of this element)
            if (islower(input[i + 1])) {
                printf("%c", input[i + 1]);
                i++; // Move to the next character (lowercase)
            }

            // Check if the next character is a digit (indicates the quantity of atoms of this element)
            if (isdigit(input[i + 1])) {
                multiplier = 0;
                while (isdigit(input[i + 1])) {
                    multiplier = multiplier * 10 + (input[i + 1] - '0');
                    i++; // Move to the next digit
                }
                printf("%d", multiplier);
                numElements += (multiplier - 1); // Add the number of additional atoms of this element
            }
        } else if (input[i] == '(') {
            // Start of a group within parentheses
            i++; // Move to the next character after '('
            numElements += identifyCompound(input, &i); // Recursive call to process the group
        } else if (input[i] == ')') {
            // End of a group within parentheses
            i++; // Move to the next character after ')'

            // Check if the next character is a digit (indicates multiplication of the group)
            if (isdigit(input[i])) {
                multiplier = 0;
                while (isdigit(input[i])) {
                    multiplier = multiplier * 10 + (input[i] - '0');
                    i++; // Move to the next digit
                }
                numElements *= multiplier; // Multiply the number of elements by the multiplier
            }
        }

        i++; // Move to the next character
    }

    *index = i; // Update the index to reflect the current position in the input
    return numElements;
}

int main() {
    char input[100];

    do {
        printf("Enter an element or compound (Press 'esc' to exit):\n");
        fgets(input, sizeof(input), stdin);

        // Remove the newline character (if present) from the input
        input[strcspn(input, "\n")] = '\0';

        if (strcmp(input, "esc") == 0) {
            break; // Exit the loop if the user entered 'esc'
        }

        int index = 0;
        int totalElements = identifyCompound(input, &index);

        printf("\nTotal elements in the compound: %d\n", totalElements);

    } while (1); // Infinite loop until the user decides to exit

    return 0;
}

如何修改identifyCompound函数以正确处理括号内组重数的乘法?例如,在处理公式 Fe2(SO4)3 时,如何确保输出包含 Fe2 + S3 + O12 而不是 Fe2S + O4,同时仍能准确计算元素总数?

预先感谢您的任何帮助或建议!

c
1个回答
0
投票

看着这段代码,让我印象最深刻的是你试图在

identifyCompound
中完成所有事情。这使得无法为解决方案的各个部分编写测试用例。

正如评论所指出的,问题的第一部分是解析输入。第二个是解释解析后的输入,并对原子进行计数。考虑第一部分和第二部分之间的接口 - 您将如何表示解析的结果?你不想再处理单个角色了。

'Fe'
是铁,不是两个字。

© www.soinside.com 2019 - 2024. All rights reserved.