我想构建一个函数,它获得类似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?
}
答案 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
中,它会忽略b
和x
并将+
存储在10
中。