I need to make a simple C program which will solve quadratic equation in expected format that needs to be like this:
ax^2 + bx + c = dx^2 + ex + f
I'm using scanf to read the input, and it works as expected. But I need to implement some input testing for my scanf reading which is like that right now:
scanf("%f x^2 + %f x + %f = %f x^2 + %f x + %f", &a, &b, &c, &d, &e, &f);
I need to printf("Wrong input\\n");
for every input like fe
'abc', '1 x^2 + 1 x + 1 = 0', 'x^3...'
I tried the if (scanf() != 1)
, but it prints the 'Wrong output' every time. Any ideas about how can I accomplish that?
if I enter only '1 x^2 + 1 x + 1 = 0' it waits for next possible input.
In that case read complete input using fgets
and parse the values using sscanf
as below.
char buf[100];
fgets(buf,sizeof buf,stdin);
int r = sscanf(buf,"%f x^2 + %f x + %f = %f x^2 + %f x + %f", &a, &b, &c, &d, &e, &f);
if (r!=6)
printf("Wrong input\n");
else
printf("correct\n");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.