Skip to content

Instantly share code, notes, and snippets.

@Zane2b2t
Created December 2, 2024 14:23
Show Gist options
  • Save Zane2b2t/1197e9c4cabbb08d018b75f492dd820e to your computer and use it in GitHub Desktop.
Save Zane2b2t/1197e9c4cabbb08d018b75f492dd820e to your computer and use it in GitHub Desktop.
a chatgpt c port of my java math thingy, i did not even read this and i won't until i learn c, works though i tested it!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#define MAX_EXPR_LEN 1024
// Helper functions for parsing
int isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}
// Function to apply basic operations
double applyOperation(double a, double b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return b != 0 ? a / b : 0; // Avoid division by zero
case '^': return pow(a, b);
default: return 0;
}
}
// Function to evaluate an expression recursively
double evaluate(const char *expr, double x, int *pos) {
double result = 0;
char op = '+';
double current = 0;
while (expr[*pos] != '\0') {
char c = expr[*pos];
if (isdigit(c) || c == '.') {
// Parse a number
char numberBuffer[MAX_EXPR_LEN];
int numLen = 0;
while (isdigit(expr[*pos]) || expr[*pos] == '.') {
numberBuffer[numLen++] = expr[*pos];
(*pos)++;
}
numberBuffer[numLen] = '\0';
current = atof(numberBuffer);
// Go back one position as we will increment after this loop
(*pos)--;
} else if (c == 'x') {
// Replace variable `x` with its value
current = x;
} else if (isOperator(c)) {
// Apply the previous operation
result = applyOperation(result, current, op);
op = c;
current = 0; // Reset current value for the next number
} else if (c == '(') {
// Handle parentheses recursively
(*pos)++;
current = evaluate(expr, x, pos);
} else if (c == ')') {
// Stop recursion at closing parenthesis
break;
}
(*pos)++;
}
// Apply the last pending operation
result = applyOperation(result, current, op);
return result;
}
// Wrapper function to evaluate an expression
double parseAndEvaluate(const char *expr, double x) {
int pos = 0;
return evaluate(expr, x, &pos);
}
// Function to determine if a function is Even, Odd, or Neither
const char *checkFunctionType(double fx, double fNegX) {
if (fabs(fx - fNegX) < 1e-6) { // f(x) ≈ f(-x), Even
return "Even";
} else if (fabs(fx + fNegX) < 1e-6) { // f(x) ≈ -f(-x), Odd
return "Odd";
} else {
return "Neither";
}
}
// Main program
int main() {
char input[MAX_EXPR_LEN];
double x;
printf("Enter your function f(x) = ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove newline character
if (strcmp(input, "exit") == 0) {
return 0;
}
printf("Enter a value for x to test the function: ");
scanf("%lf", &x);
// Evaluate f(x) and f(-x)
double fx = parseAndEvaluate(input, x);
double fNegX = parseAndEvaluate(input, -x);
// Print results
printf("\nTesting f(x) = %s\n", input);
printf("f(%.2f) = %.2f\n", x, fx);
printf("f(%.2f) = %.2f\n", -x, fNegX);
// Determine and display the function type
const char *functionType = checkFunctionType(fx, fNegX);
printf("Function type: %s\n", functionType);
return 0;
}
@Zane2b2t
Copy link
Author

Zane2b2t commented Dec 2, 2024

(has some logic errors but its chatgpt code), do not use this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment