用编程求一元二次方程的根
答案:3 悬赏:40 手机版
解决时间 2021-02-28 02:58
- 提问者网友:几叶到寒
- 2021-02-27 19:08
用编程求一元二次方程的根
最佳答案
- 五星知识达人网友:鸽屿
- 2021-02-27 20:35
用什么语言?
这是C++的实现
#include <iostream>
#include <cmath>
using namespace std;
void getValue(float,float,float);
int main()
{
float a,b,c;
cout << "请输入一次二次方程的a,b,c的值: " << endl;
cout << "a = ";
cin >> a;
cout <<"b = ";
cin >>b ;
cout<< "c = ";
cin>>c;
cout <<"你要解的方程为:" <<a <<"x * x + (" <<b <<")x +("<<c <<") = 0" << endl;;
getValue(a,b,c);
return 0;
}
void getValue(float a,float b,float c)
{
float delta = b * b - 4 * a * c;//delta 值
float answer;
if (delta < 0)
{
cout << "方程无解" << endl;
}
else if(delta == 0)
{
answer =( - b + sqrt(delta) ) / 2 * a;
cout <<"方程只有一个解:" << answer;
}
else
{
answer =( - b + sqrt(delta) ) / 2 * a;
cout << "方程有两个解,分别为:" << answer << " ,";
answer =( - b - sqrt(delta) ) / 2 * a;
cout << answer;
}
}
这是C++的实现
#include <iostream>
#include <cmath>
using namespace std;
void getValue(float,float,float);
int main()
{
float a,b,c;
cout << "请输入一次二次方程的a,b,c的值: " << endl;
cout << "a = ";
cin >> a;
cout <<"b = ";
cin >>b ;
cout<< "c = ";
cin>>c;
cout <<"你要解的方程为:" <<a <<"x * x + (" <<b <<")x +("<<c <<") = 0" << endl;;
getValue(a,b,c);
return 0;
}
void getValue(float a,float b,float c)
{
float delta = b * b - 4 * a * c;//delta 值
float answer;
if (delta < 0)
{
cout << "方程无解" << endl;
}
else if(delta == 0)
{
answer =( - b + sqrt(delta) ) / 2 * a;
cout <<"方程只有一个解:" << answer;
}
else
{
answer =( - b + sqrt(delta) ) / 2 * a;
cout << "方程有两个解,分别为:" << answer << " ,";
answer =( - b - sqrt(delta) ) / 2 * a;
cout << answer;
}
}
全部回答
- 1楼网友:杯酒困英雄
- 2021-02-27 22:04
C语言的
#include <stdio.h>
#include <math.h>
void main()
{
float a,b,c,p,x1,x2;
printf("input a,b and c:");
scanf("%f %f %f",&a,&b,&c);
p = b*b-4*a*c;
if(p > 0)
{
x1 = (-b+sqrt(p)/(2*a));
x2 = (-b-sqrt(p)/(2*a));
printf("x1 = %f,x2 = %f\n",x1,x2);
}
else if(p == 0)
{
x1 = x2 = (-b/(2*a));
printf("x1 = x2 = %f\n",x1);
}
else
printf("no answer!\n");
}
- 2楼网友:神鬼未生
- 2021-02-27 20:42
dim a as single, b as single, c as single, d as single
dim x1 as double, x2 as double
a = val(text1.text)
b = val(text2.text)
c = val(text3.text)
if a <> 0 then
d = b ^ 2 - 4 * a * c
if d > 0 then
x1 = (-b + sqr(d)) / (2 * a)
x2 = (-b - sqr(d)) / (2 * a)
label1.caption = "x1 = " & str(x1) & chr(10) & "x2 = " & str(x2)
elseif d = 0 then
x1 = -b / (2 * a)
x2 = -b / (2 * a)
label1.caption = "x1 = x2 = " & str(x1) & ",是二个相等的实数根!"
elseif d < 0 then
label1.caption = "此方程没有实数根!"
end if
else
if b > 0 then
x1 = -c / b
label1.caption = "x1 = " & cstr(x1) & "为一元一次方程!"
else
label1.caption = "错误!二次项与一次项不能同时为0,请重新输入!"
end if
end if
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯