我想建立一个函数,它可以得到像ax^2+bx+c=0
这样的方程(字符串)(例如:“3x^2+8=0”
) >) 并获取 a
,b
,c
参数。
这是我的代码:
#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
存储在 b
中,然后它会忽略 x
和 +
并将 10
存储在 c
中。
关于c - 如何从二次方程得到ABC参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41649311/