如何从二次方程得到ABC参数?

时间:2017-01-14 11:12:02

标签: c string get 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?
}

1 个答案:

答案 0 :(得分:1)

你可以这样做

   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+,然后存储{{1}在} 4中,它会忽略bx并将+存储在10中。