#include <stdlib.h> #include <math.h> #define EPS 1e-6 int main() { float a,b,c,disc,p,q; scanf("%f %f %f",&a,&b,&c); disc=b*b-4*a*c; p=-b/(2*a); q=sqrt(fabs(disc))/(2*a); if(fabs(disc)<=EPS) { printf("x1=%.3f x2=%.3f ",p,p); } else { if(disc>EPS) { printf("x1=%.3f x2=%.3f",p+q,p-q); } else { printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",p,q,p,q); } } return 0; } 课本的例题
#include<stdio.h> #include<math.h> #define EPS 1e-6 void Num(float a,float b,float c); int main() { float a,b,c; scanf("%f%f%f",&a,&b,&c); Num(a,b,c); return 0; } //计算一元二次方程的根 void Num(float a,float b,float c) { float disc,p,q; //disc为判别式 disc=b*b-4*a*c; p=-b/(2*a); q=sqrt(fabs(disc))/(2*a); if(fabs(disc)<=EPS) //判别式等于0,输出两个相等的实根 { printf("x1=%.3f x2=%.3f",p,p); } else { if(disc>EPS) //判别式大于0 { printf("x1=%.3f x2=%.3f",p+q,p-q); } else //判别式小于0 { printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",p,q,p,q); } } }
编程帅哥 2023-11-30 13:43:42 |
判别式应该用EPS来判断更加规范
#include <stdio.h> #include <math.h> double x1, x2; void function(double a, double b, double c) { double data = b * b - 4 * a * c; if (data >= 0) { x1 = (-b + sqrt(data)) / 2 * a; x2 = (-b - sqrt(data)) / 2 * a; printf("x1=%.3lf x2=%.3lf", x1, x2); } else { printf("x1=%.3lf+%.3lfi ", -b / (2 * a), sqrt(-data) / (2 * a)); printf("x2=%.3lf-%.3lfi", -b / (2 * a), sqrt(-data) / (2 * a)); } } int main() { double a, b, c; scanf("%lf%lf%lf", &a, &b, &c); function(a, b, c); return 0; }
#include <stdio.h> #include <math.h> void save1(int a); void save2(int m,int n,int a,int b,int c); void save3(int m,int n,int a,int b,int c); int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); float m,n,k; m=b*b; n=4*a*c; if((m-n)<0) save1(a); else { if((m-n)==0) save2(m,n,a,b,c); else { save3(m,n,a,b,c); } } return 0; } void save1(int a) { printf("无解"); } void save2(int m,int n,int a,int b,int c) { float x1,x2,p,q; p=-b/2*a; q=sqrt(m-n)/2*a; x1=x2=p+q; printf("x1=x2=%f",x1); } void save3(int m,int n,int a,int b,int c) {
黄小橘 2023-11-23 18:09:50 |
float x1,x2,p,q; p=-b/2*a; q=sqrt(m-n)/2*a; x1=p-q; x2=p+q; printf("x1=%f x2=%f",x1,x2); }
#include <stdio.h> #include<math.h> int main(void) { double a = 0.0, b = 0.0, c = 0.0; scanf("%lf %lf %lf", &a, &b, &c); double tel = pow(b, 2) - 4 * a * c; double x1 = 0.0, x2 = 0.0; if (tel > 0) { x1 = (-b + sqrt(tel)) / (2 * a); x2= (-b - sqrt(tel)) / (2 * a); printf("x1=%.3lf x2=%.3lf\n", x1, x2); } else if (tel == 0) { x1 =( - b )/ (2 * a); x2= ( - b) / (2 * a); printf("x1=%.3lf x2=%.3lf\n", x1, x2); } else { double m, n; m= -b / (2 * a); n = sqrt(-tel) / (2 * a); printf("x1=%.3lf+%.3lfi x2=%.3lf+%.3lfi\n", m, n, m, n); } return 0; }
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:674 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:553 |
九宫重排 (C++代码)浏览:1410 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:932 |
【明明的随机数】 (C++代码)浏览:834 |
不会做的浏览:954 |
WU-蓝桥杯算法提高VIP-Quadratic Equation (C++代码)浏览:1808 |
【蟠桃记】 (C语言代码)浏览:698 |
P1000 (C语言代码)浏览:911 |
循环入门练习6 (C语言代码)浏览:1058 |