I’m working on a C program that analyzes chemical formulas and counts the total number of elements. The identifyCompound
function I’ve developed can correctly identify individual elements, but I’m having difficulty handling the multiplicities of groups within parentheses.
For example, when processing the formula Fe2(SO4)3, the current output is Fe2S + O4 with a total of 17 elements, but the expected result would be Fe2 + S3 + O12 while still maintaining the correct total of 17 elements.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to check if a character is an uppercase letter
int isUpperCaseLetter(char c) {
return (c >= 'A' && c <= 'Z');
}
// Function to check if a character is a lowercase letter
int isLowerCaseLetter(char c) {
return (c >= 'a' && c <= 'z');
}
// Function to check if a character is a digit
int isDigit(char c) {
return (c >= '0' && c <= '9');
}
// 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] != '') {
if (isUpperCaseLetter(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 (isLowerCaseLetter(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")] = '';
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: %dn", totalElements);
} while (1); // Infinite loop until the user decides to exit
return 0;
}
How can I modify the identifyCompound function to correctly handle the multiplication of group multiplicities within parentheses? For example, when processing the formula Fe2(SO4)3, how can I ensure that the output includes Fe2 + S3 + O12 instead of Fe2S + O4, while still accurately counting the total number of elements?
Thank you in advance for any help or suggestions!
Unsigned Index is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.