zoukankan      html  css  js  c++  java
  • 编写一个求方程ax^2+bx+c=0的根的程序,用3个函数分别求当b^2-4ac大于零、等于零和小于零时的方程的根。要求从主函数输入a、b、c的值并输出结果-简单

    源程序:

    #include < iostream>

    #include < math.h >

    using namespace std;

    void equation_1(int a, int b, int c)

    {

      double x1, x2, temp;

      temp = b*b - 4 * a * c;

      x1 = (-b + sqrt(temp)) / (2 * a * 1.0);

      x2 = (-b - sqrt(temp)) / (2 * a * 1.0);

      cout << "两个不相等的实根" << endl;

      cout << "x1 = " << x1 << ", x2 = " << x2 << endl;

    }

    void equation_2(int a, int b, int c)

    {

      double x1, x2, temp;

      temp = b*b - 4 * a * c;

      x1 = (-b + sqrt(temp)) / (2 * a * 1.0);

      x2 = x1;

      cout << "两个相等的实根" << endl;

      cout << "x1 = " << x1 << ", x2 = " << x2 << endl;

    }

    void equation_3(int a, int b, int c)

    {

      double temp, real1, real2, image1, image2;

      temp = -(b*b - 4 * a * c);

      real1 = -b / (2 * a *1.0);

      real2 = real1;

      image1 = sqrt(temp);

      image2 = -image1;

      cout << "两个虚根" << endl;

      cout << "x1 = " << real1 << " + " << image1 << "j" << endl;

      cout << "x2 = " << real2 << " + " << image2 << "j" << endl;

    }

    void main()

    {

      int a, b, c;

      double temp;

      cout << "输入a,b,c 的值" << endl;

      cin >> a >> b >> c;

      cout << "方程为:" << a << "x*x+" << b << "x+" << c << " = 0" << endl;

      temp = b*b - 4 * a * c;

      if (temp > 0)

        equation_1(a, b, c);

      if (temp == 0)

        equation_2(a, b, c);

      if (temp < 0)

        equation_3(a, b, c);

      system("pause");

    }

    运行结果:

  • 相关阅读:
    动态表单之数据分页
    SQL Server 批量生成bcp命令
    SQL Server 全文索引的硬伤
    简单实用SQL脚本Part2:日期和时间函数
    使用SQL Server 扩展函数进行性能优化
    留念2010年5月5日
    C#获取URL参数值
    SQL Server扩展函数的基本概念
    SQL Server 空间换时间的数据库设计
    简单实用SQL脚本Part:查找SQL Server 自增ID值不连续记录
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11887810.html
Copyright © 2011-2022 走看看