C++一道题请教高手
- 提问者网友:趣果有间
- 2021-05-07 01:13
- 五星知识达人网友:封刀令
- 2021-05-07 02:35
- 1楼网友:鸠书
- 2021-05-07 03:55
输入方式: 比如: 1 回车 3 回车 2回车. 望采纳!
#include <iostream> #include <math.h> using namespace std;
void fun(double a,double b,double c) { double d,x1,x2; d=pow(b,2)-4*a*c; if(d==0) { x1=x2=(-b)/(2*a); cout<<"方程有两个不同的实根:"<<endl <<"x1=x2="<<x1<<endl; } else if(d>0) { x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); cout<<"方程有两个不同的实根:"<<endl <<"x1="<<x1<<" "<<"x2="<<x2<<endl; } else { cout<<"方程无实根"<<endl; } }
int main() { double a,b,c; cout<<"请输入一元二次方程的三个系数:"<<endl; cout<<"a=";cin>>a; cout<<"b=";cin>>b; cout<<"c=";cin>>c; fun(a,b,c); //调用函数. return 0; }
- 2楼网友:老鼠爱大米
- 2021-05-07 03:21
在DEV-C++编译器上通过检测
#include <iostream> #include <stdio.h> #include <math.h> using namespace std; int main() { void fun(double a,double b,double c); double a,b,c; cout<<"请输入a,b,c的值:"<<endl; cin>>a>>b>>c; fun(a,b,c); system("PAUSE"); return 0; } void fun(double a,double b,double c) { float disc,x1,x2,p,q; disc=pow(b,2)-4*a*c; if(disc>=0) { p=-b/(2*a); q=sqrt(disc)/(2*a); x1=p+q; x2=p-q; cout<<"x1="<<x1<<" "<<"x2="<<x2; } else cout<<"Error"; }