Commit 46596508 by tranvu

Lab 6 tasks

parent de8eab89
Showing with 97 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char* getInput();
char* getElement();
int main() {
char* function;
function = getInput();
int funSize = strlen(function) + 1;
char* a;
char* b;
char* c;
a = getElement(function);
int startB = strlen(a) + 4;
char newFunc[funSize];
int location = 0;
for (int i = startB - 1; i < funSize; i++) {
newFunc[location++] = function[i];
}
b = getElement(newFunc);
int startC = strlen(b) + 1 + startB;
location = 0;
for (int i = startC - 1; i < funSize; i++) {
newFunc[location++] = function[i];
}
c = getElement(newFunc);
int A = atoi(a);
int B = atoi(b);
int C = atoi(c);
float result1 = (-B + sqrt(pow(B, 2) - 4 * A * C)) / (2 * A);
float result2 = (-B - sqrt(pow(B, 2) - 4 * A * C)) / (2 * A);
if (!isnan(result1)) printf("Result 1: %f\n", result1);
else printf("Result 1 is not real\n");
if (!isnan(result2)) printf("Result 2: %f\n", result2);
else printf("Result 2 is not real\n");
free(function);
free(a);
free(b);
free(c);
return (EXIT_SUCCESS);
}
char* getElement(char function[]) {
char* element;
int funSize = strlen(function);
int size = 1, location = 0;
element = malloc(size * sizeof(char));
for (int i = 0; i < funSize; i++) {
if (function[i] != 'x') {
element[location++] = function[i];
if (location > size) {
element = realloc(element, ++size * sizeof(char));
}
} else {
element[location] = '\0';
return element;
}
}
element[location] = '\0';
return element;
}
char* getInput() {
int size = 9;
char* function;
function = malloc(size * sizeof(char));
printf("Please input function: ");
char c;
int location = 0;
while((c = getc(stdin)) != 10) {
function[location++] = c;
if (location > size) {
function = realloc(function, ++size * sizeof(char));
}
}
function[location] = '\0';
return function;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment