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

标签 c string get equation

我想建立一个函数,它可以得到像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/

相关文章:

c - 结构内部的结构数组

c - 像 gcc 这样的 C 编译器是否足够智能,可以进行位移位?

c - 选择行为

visual-studio - 是否有正则表达式可以在一个句子中找到两个不同的词?

javascript - 在启用 JavaScript 的情况下使用 wp_remote_get 吗? (WordPress)

php - 为 URL 中的同一 GET 变量传递多个值

php - 根据 id 和复选框更新记录

C 反向队列

c++ - 使用std:sort对cpp中的字符串数组进行排序

java - Java 字符串的默认初始值是多少?