Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- double calculate(double* a, double* b, double* c, double* x1, double* x2) {
- //ax2 + bx = c
- //D = b2 - 4*a*c
- //x1 = -b + sqrt(D) / 2
- //x2 = -b - sqrt(D) / 2
- double D;
- D = *b * *b - 4 * *a * *c;
- if(D > 0) {
- *x1 = ((-1) * *b + sqrt(D)) / 2;
- *x2 = ((-1) * *b - sqrt(D)) / 2;
- return 0;
- } else if(D < 0) {
- return 2;
- } else {
- *x1 = ((-1) * *b + sqrt(D)) / 2;
- *x2 = *x1;
- return 0;
- }
- }
- int main(int args, char *argv[]) {
- double a, b, c;
- double x1, x2;
- a = atof(argv[1]);
- b = atof(argv[2]);
- c = atof(argv[3]);
- if(calculate(&a, &b, &c, &x1, &x2) == 0) {
- printf("x1: %lf\n", x1);
- printf("x2: %lf\n", x2);
- } else {
- fprintf(stderr, "No real roots\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement