如何从二次方程中获取 ABC 参数?

How to get ABC parameters from quadratic equation?

我想构建一个函数来获取方程(字符串),例如 ax^2+bx+c=0(例如:"3x^2+8=0")并获取 abc参数。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STR_LEN 25

void getABC(char str[]);
int a=0, b=0, c=0;

int main(void)
{
    char equation[STR_LEN]={0};

    printf("Enter an equation:\n");
    fgets(equation, STR_LEN, stdin);
    equation[strcspn(equation, "\n")]=0;

    getABC(equation);

    return 0;
}

void getABC(char str[])
{
    // how to get a, b and c?
}

你可以这样做

   int a ;
   int b ;
   int c ;

   printf("Enter Equation : ");
   scanf("%dx^2+%dx+%d" , &a , &b , &c);
   printf("%d %d %d" , a ,b , c);

例如如果你输入3x^2+4x+10,那么3将存储在a中,它会忽略x^2+然后存储4 in b 然后它将忽略 x+ 并将 10 存储在 c.