Created
December 2, 2024 14:23
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(has some logic errors but its chatgpt code), do not use this