2017-01-14 3 views
-1

Ich möchte eine Funktion erstellen, die Gleichung (String) wie ax^2+bx+c=0 (ex: "3x^2+8=0") erhält und a, b, c Parameter erhalten.Wie erhält man ABC-Parameter aus der quadratischen Gleichung?

Hier ist mein Code:

#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

'sscanf' würde hier von Nutzen sein. –

+0

Bitte beantworten Sie wie ausgewählt, wenn es Ihre Anfrage gelöst hat. –

+0

In Ihrer Frage ist die Beispieleingabe '3x^2 + 8 = 0' Ohne ax^2 + bx + c, ohne b, ist es richtig, oder Sie haben das versehentlich geschrieben? –

Antwort

1

du

int a ; 
    int b ; 
    int c ; 

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

Für Beispiel stattdessen tun können, wenn Sie 3x^2+4x+10 eingeben, dann wird 3 in a gespeichert werden, wird es x^2 und + ignorieren und dann speichern 4 in b und dann wird es x und + ignorieren undspeichernin c.

Verwandte Themen