首页 新闻 搜索 专区 学院

C++新手题,谢谢帮忙

0
悬赏园豆:20 [已解决问题] 解决于 2020-11-30 22:52

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.

活动4
编写一个程序来计算二次方程的根。二次方程式为以下形式的方程式:
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>”。

C++
心律的主页 心律 | 初学一级 | 园豆:136
提问于:2020-11-30 21:32
< >
分享
最佳答案
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;
    }
}
展开代码内容
收获园豆:20
Conan-jine | 小虾三级 |园豆:1272 | 2020-11-30 22:40

可以添加个好友么?我的题有点多。希望大神能够帮忙。

心律 | 园豆:136 (初学一级) | 2020-11-30 22:46

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

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

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

心律 | 园豆:136 (初学一级) | 2020-11-30 23:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册