【C++】 求方程ax^2+bx+c=0的根,用3个函数分别求 5
求方程ax^2+bx+c=0的根,用3个函数分别求当b^2-4ac大于0等于0小于0时的根,并输出结果。从主函数输入a,b,c.#include<iostream>#in...
求方程ax^2+bx+c=0的根,用3个函数分别求当b^2-4ac大于0等于0小于0时的根,并输出结果。从主函数输入a,b,c.
#include <iostream>
#include <cmath>
using namespace std;
double f1(double);
double f2(double);
double f3(double);
int main()
{
double a,b,c,x1,x2;
cin>>a>>b>>c;
f1(a,b,c);
f2(a,b,c);
f3(a,b,c);
return 0;
}
double f1(double a,double b,double c)
{
if (b*b-4*a*c>0)
cout<<"x1="<<(-b+sqrt(b*b-4*a*c))/(2*a)<<" "<<"x2="<<(-b-sqrt(b*b-4*a*c))/(2*a)<<endl;
return 0;
}
double f2(double a,double b,double c)
{
if (b*b-4*a*c==0)
cout<<"x1=x2="<<-b/(2*a)<<endl;
return z;
}
double f3(double a,double b,double c)
{
if (b*b-4*a*c<0)
cout<<"无实数解"<<endl;
return 0;
}
编译提示
error C2660: 'f1' : function does not take 3 parameters
什么意思?怎么改? 展开
#include <iostream>
#include <cmath>
using namespace std;
double f1(double);
double f2(double);
double f3(double);
int main()
{
double a,b,c,x1,x2;
cin>>a>>b>>c;
f1(a,b,c);
f2(a,b,c);
f3(a,b,c);
return 0;
}
double f1(double a,double b,double c)
{
if (b*b-4*a*c>0)
cout<<"x1="<<(-b+sqrt(b*b-4*a*c))/(2*a)<<" "<<"x2="<<(-b-sqrt(b*b-4*a*c))/(2*a)<<endl;
return 0;
}
double f2(double a,double b,double c)
{
if (b*b-4*a*c==0)
cout<<"x1=x2="<<-b/(2*a)<<endl;
return z;
}
double f3(double a,double b,double c)
{
if (b*b-4*a*c<0)
cout<<"无实数解"<<endl;
return 0;
}
编译提示
error C2660: 'f1' : function does not take 3 parameters
什么意思?怎么改? 展开
5个回答
展开全部
#include <iostream>
#include <cmath>
using namespace std;
double f1(double,double,double);//声明和定义不相符
double f2(double,double,double);
double f3(double,double,double);
int main()
{
double a,b,c,x1,x2;
cin>>a>>b>>c;
f1(a,b,c);
f2(a,b,c);
f3(a,b,c);
return 0;
}
double f1(double a,double b,double c)
{
if (b*b-4*a*c>0)
cout<<"x1="<<(-b+sqrt(b*b-4*a*c))/(2*a)<<" "<<"x2="<<(-b-sqrt(b*b-4*a*c))/(2*a)<<endl;
return 0;
}
double f2(double a,double b,double c)
{
if (b*b-4*a*c==0)
cout<<"x1=x2="<<-b/(2*a)<<endl;
double z = -b/(2*a);//z是没有定义的
return z;
}
double f3(double a,double b,double c)
{
if (b*b-4*a*c<0)
cout<<"无实数解"<<endl;
return 0;
}
展开全部
意思:f1 这个函数不能接受三个参数,因为C/C++都是前向声明的,就是main中遇到f1的时候会向回查找,而你在main前面声明的f1是只有一个参数的;
改:把main前面声明的函数改成main后面定义的一样:
double f1(double a,double b,double c);
double f2(double a,double b,double c);
double f3(double a,double b,double c);
改:把main前面声明的函数改成main后面定义的一样:
double f1(double a,double b,double c);
double f2(double a,double b,double c);
double f3(double a,double b,double c);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<math.h>
using namespace std;
double func1(double a,double b,double c) //如果b*b-4ac > 0
{
double Result1 = (-b + sqrt((b*b)-4*a*c) )/(2*a);
double Result2 = (-b - sqrt((b*b)-4*a*c) )/(2*a);
cout << Result1 << endl << Result2 << endl;
return 0;
}
double func2(double a,double b,double c) //如果b*b-4ac == 0
{
double Result = (-b)/(2*a);
cout << Result << endl;
return 0;
}
double func3(double a,double b,double c) //如果b*b-4ac < 0
{
cout << "方程无解" << endl;
return 0;
}
int main()
{
double a,b,c;
cin >> a >> b >> c;
if((b*b)-4*a*c > 0)
{
func1(a,b,c);
}
else if((b*b)-4*a*c == 0)
{
func2(a,b,c);
}
else if((b*b)-4*a*c < 0)
{
func3(a,b,c);
}
return 0;
}
#include<math.h>
using namespace std;
double func1(double a,double b,double c) //如果b*b-4ac > 0
{
double Result1 = (-b + sqrt((b*b)-4*a*c) )/(2*a);
double Result2 = (-b - sqrt((b*b)-4*a*c) )/(2*a);
cout << Result1 << endl << Result2 << endl;
return 0;
}
double func2(double a,double b,double c) //如果b*b-4ac == 0
{
double Result = (-b)/(2*a);
cout << Result << endl;
return 0;
}
double func3(double a,double b,double c) //如果b*b-4ac < 0
{
cout << "方程无解" << endl;
return 0;
}
int main()
{
double a,b,c;
cin >> a >> b >> c;
if((b*b)-4*a*c > 0)
{
func1(a,b,c);
}
else if((b*b)-4*a*c == 0)
{
func2(a,b,c);
}
else if((b*b)-4*a*c < 0)
{
func3(a,b,c);
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
double f1(double);
double f2(double);
double f3(double);
改为:
double f1(double,double,double);
double f2(double,double,double);
double f3(double,double,double);
double f2(double);
double f3(double);
改为:
double f1(double,double,double);
double f2(double,double,double);
double f3(double,double,double);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>#include <cmath>using namespace std;double f1(double,double,double);//声明和定义不相符double f2(double,double,double);double f3(double,double,double);int main(){ double a,b,c,x1,x2; cin>>a>>b>>c; f1(a,b,c); f2(a,b,c); f3(a,b,c); return 0;}double f1(double a,double b,double c){ if (b*b-4*a*c>0) cout<<"x1="<<(-b+sqrt(b*b-4*a*c))/(2*a)<<" "<<"x2="<<(-b-sqrt(b*b-4*a*c))/(2*a)<<endl; return 0;}double f2(double a,double b,double c){ if (b*b-4*a*c==0) cout<<"x1=x2="<<-b/(2*a)<<endl; double z = -b/(2*a);//z是没有定义的 return z;}double f3(double a,double b,double c){ if (b*b-4*a*c<0) cout<<"无实数解"<<endl;return 0;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询