I started studying my first language ‘C’ and I’m trying to solve a linear system. But I’m having trouble putting together a function that equates the terms on the left side with the right side of the equality.
When entering the equation (example: N2 + H2 = NH3): N2 + H2 = NH3
I obtain its elements and its unknowns from the equation:
N2 = 2a
H2 = 2b
NH3 = 3c
and with the function below I expected
2a = c
2b = 3c
But I only get 2a = 3a
// Function to equalize the unknowns on the right side with the left side of the equation
void equalize_unknowns(char equation[]) {
int i, len;
char left_side[MAX_LEN];
char right_side[MAX_LEN];
char unknown = 'a'; // Initial unknown
// Divide the equation into left and right sides
sscanf(equation, "%[^=]=%s", left_side, right_side);
printf("nSetting up the system:n");
// Iterate over the left and right sides to equalize the unknowns
char *token_left = strtok(left_side, "+");
char *token_right = strtok(right_side, "+");
while (token_left != NULL && token_right != NULL) {
char element_left[MAX_LEN];
char element_right[MAX_LEN];
int coef_left, coef_right;
sscanf(token_left, "%[^0-9]%d", element_left, &coef_left);
sscanf(token_right, "%[^0-9]%d", element_right, &coef_right);
printf("%d%c = %d%cn", coef_left, unknown, coef_right, unknown);
token_left = strtok(NULL, "+");
token_right = strtok(NULL, "+");
}
}
Code without the function that assembles the linear system
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 100
// Function to check if a character is a digit
int is_digit(char c) {
return (c >= '0' && c <= '9');
}
// Function to process the equation and print the elements with their unknowns
void process_equation(char equation[]) {
int i, len;
int coefficient = 1; // Initialize coefficient as 1 (if none is specified)
char element[MAX_LEN];
char unknown = 'a'; // Initial unknown
printf("Elements and their unknowns in the equation:n");
for (i = 0; i < strlen(equation); i++) {
if (isalpha(equation[i])) { // If it's a letter (element)
int j = 0;
while (isalpha(equation[i])) {
element[j++] = equation[i++];
}
element[j] = '';
// Check if the next character is a number (coefficient)
if (is_digit(equation[i])) {
coefficient = 0;
while (is_digit(equation[i])) {
coefficient = coefficient * 10 + (equation[i++] - '0');
}
}
// Print the element with its associated unknown
printf("%s%d = %d%cn", element, coefficient, coefficient, unknown++);
coefficient = 1; // Reset coefficient for the next term
}
}
}
// Function to equalize the unknowns on the right side with the left side of the equation
int main() {
char equation[MAX_LEN];
printf("Enter the chemical equation (example: N2 + H2 = NH3): ");
fgets(equation, MAX_LEN, stdin);
// Remove the 'n' added by fgets
equation[strcspn(equation, "n")] = '';
// Process the equation and print the elements with their unknowns
process_equation(equation);
// Equalize the unknowns on the right side with the left side of the equation
return 0;
}
Unsigned Index is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.