# C++新手题，谢谢帮忙

0

Activity 4
Write a program to calculate the roots of a quadratic equation. A quadratic equation is an equation of the form:
ax^2+bx+c=0
The roots of a quadratic equation are given by the formula:
x=(-b±√(b^2-4ac))/2a
Your program should do the following:

1. Ask the user to enter values for a, b, and c. For example, print “a = ” to the console and then read the value for a.
2. Determine if the equation has 0, 1, or 2 roots. Print a message display the number of roots, for example: “The equation has 2 roots” or “The equation has 1 root” (Technically 1 solution is called a double root, but we’ll just call it 1 root for this activity).
3. Calculate and print the roots (if any). For a single root, print “r = <VALUE>”. For 2 roots, print “r1 = <VALUE>” on one line and “r2 = <VALUE>” on the next.

ax ^ 2 + bx + c = 0

x =（-b±√（b ^ 2-4ac））/ 2a

1.要求用户输入a，b和c的值。例如，在控制台上打印“ a =”，然后读取a的值。
2.确定方程式是否具有0、1、2的根。打印一条消息，显示根数，例如：“等式具有2个根”或“等式具有1个根”（技术上1个解称为双根，但在此活动中我们仅称其为1个根） 。
3.计算并打印根（如果有）。对于单个根，打印“ r = <VALUE>”。对于2个根，在一行上打印“ r1 = <VALUE>”，在下一行上打印“ r2 = <VALUE>”。

0

#include <iostream>
#include <cmath>
using namespace std;

void print(double a,double b,double c);
void count(double a,double b,double c);

int main()
{
double a,b,c;
cout<<"Please input the a,b,c of the equation:"<<endl;
// cout<<"请输入方程的a，b，c："<<endl;
cout<<"\ta = ";
cin>>a;
cout<<"\tb = ";
cin>>b;
cout<<"\tc = ";
cin>>c;
cout<<"Yours input is:"<<endl;
// cout<<"你的输入是："<<endl;
print(a,b,c);
count(a,b,c);
return 0;
}

void print(double a,double b,double c)
{
cout<<'\t';
if(a!=0)
{
if(a==1)    cout<<"x^2";
else    cout<<a<<"x^2";
}
if(b!=0)
{
if(b==1)    cout<<"+x";
else if(b==-1)  cout<<"-x";
else if(b>0)    cout<<"+"<<b<<"x";
else if(b<0)    cout<<b<<"x";
}
if(c!=0)
{
if(c>0) cout<<"+";
cout<<c;
}
cout<<"=0"<<endl;
}

void count(double a,double b,double c)
{
double dierta,r,r1,r2;
dierta=b*b-4*a*c;
if(dierta<0)    cout<<"Equation has no root."<<endl;    // cout<<"等式没有根"<<endl;
else if(dierta==0)
{
r=-b/(2.0*a);
cout<<"Equation has one root:"<<endl;   // cout<<"等式具有1个根"<<endl;
cout<<"\tr = "<<r<<endl;
}
else if(dierta>0)
{
r1=(-b+sqrt(dierta))/(2.0*a);
r2=(-b-sqrt(dierta))/(2.0*a);
cout<<"Equation has two roots:"<<endl;  // cout<<"等式具有2个根"<<endl;
cout<<"\tr1 = "<<r1<<endl;
cout<<"\tr2 = "<<r2<<endl;
}
}

Conan-jine | 小虾三级 |园豆：1272 | 2020-11-30 22:40

@心律: 可不是免费劳动的噢

Conan-jine | 园豆：1272 (小虾三级) | 2020-11-30 22:47

@Conan-jine: 已经发布了 还是20 。谢谢啦。

您需要登录以后才能回答，未注册用户请先注册