我刚刚学C没多久,现在做 这道 题卡住了,恳请各位们能教教我接下来怎么改
5条回答 默认 最新
- 斗迷飞鸟 2022-08-20 02:10关注
(1)原代码的问题
#include <iostream> //<iostream>是C++的头文件,不是C的头文件 #include <math.h> using namespace std; //C语言中不使用using namesapce std int main() { double a, b, c; scanf("%lf %lf %lf", &a, &b, &c); double x; x = b*b - 4 * a*c; if (x == 0) { printf("x1=x2=%.5lf", -b / 2 / a); } else if (x > 0) { printf("x1=%.5f;x2=%.5f", (-b + sqrt(x)) / 2 / a, (-b - sqrt(x)) / 2 / a); } else(x < 0); { //这里直接使用else就可以了 //在a=1,b=0,c=1的特殊情形下,输出结果的实部前面会有负号 printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi", -b / 2 / a, sqrt(-x) / 2 / a, -b / 2 / a, sqrt(-x) / 2 / a); } return 0; }
(2)修改后的代码
#include <math.h> #include <stdio.h> int main() { double a, b, c; scanf("%lf %lf %lf", &a, &b, &c); double x; x = b*b - 4*a*c; if (x == 0) { printf("x1=x2=%.5lf\n", -b / 2 / a); } else if (x > 0) { printf("x1=%.5lf;x2=%.5lf\n", (-b + sqrt(x)) / 2 / a, (-b - sqrt(x)) / 2 / a); } else { if (b == 0) { printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n", 0.0, sqrt(-x) / 2 / a, 0.0, sqrt(-x) / 2 / a); } else { printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n", -b / 2 / a, sqrt(-x) / 2 / a, -b / 2 / a, sqrt(-x) / 2 / a); } } return 0; }
(3)代码运行结果截图
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用