用c++解一元二次方程

解方程

github项目地址

这两天得知初二的表妹学了一元二次方程,听说还不会解,我就想着试试用C语言编写解方程。

一元二次方程

用公式法

这种方法效果很好:

#include"funct.h"
void yyec1()
{
    double a, b, c;
    double x = MIN, y;
    cout << "-----------" << endl;
    cout << "对于ax^2+bx+c=0" << endl;
    cout << "依降次输入各项系数 a,b,c" << endl;
    cout << "-----------" << endl;
    cin >> a >> b >> c;
    x = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
    y = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
    cout << "-----------" << endl;
    cout << "x1="<<x << endl <<"x2="<< y << endl;
}

用试错法

这种好像不是太好:

#include"funct.h"
void yyec2()
{
    double a, b, c;
    double x = MIN, y;
    cout << "-----------" << endl;
    cout << "对于ax^2+bx+c=0" << endl;
    cout << "依降次输入各项系数 a,b,c" << endl;
    cout << "-----------" << endl;
    cin >> a >> b >> c;
     for (;; x++)
     {
         y = x / 10;
         if (a * y * y + b * y + c == 0)
         {
             cout << "-----------" << endl;
             cout << "x=" << y << endl;
             // break;
         }
     }
}

一元三次方程

#include"funct.h"
void yysc()
{
    double a, b, c, d;
    double x = MIN, y;
    cout << "-----------" << endl;
    cout << "对于ax^3+bx^2+cx+d=0" << endl;
    cout << "依降次输入各项系数 a,b,c,d" << endl;
    cout << "-----------" << endl;
    cin >> a >> b >> c >> d;
    for (;; x++)
    {
        y = x / 10;
        if (a * y * y * y + b * y * y + c * y + d == 0)
        {
            cout << "-----------" << endl;
            cout << "x=" << y << endl;
            break;
        }
    }
}

这个就和二次很像,不做多研究。

二元一次

公式法

#include"funct.h"
void eyyc1()
{
    double a, b, c, d, e, f, x, y;
    cout << "-----------" << endl;
    cout << "对于方程组:" << endl;
    cout << "1.   ax+by=c " << endl;
    cout << "2.   dx+ey=f " << endl;
    cout << "依次输入a,b,c,d,e,f:" << endl;
    cout << "-----------" << endl;
    cin >> a >> b >> c >> d >> e >> f;
    y = (c * d - a * f) / (b * d - a * e);
    x = (b * f - c * e) / (b * d - a * e);
    cout << "-----------" << endl;
    cout << "y=" << y << endl;
    cout << "x=" << x << endl;
}

公式法的错误确实少点,但这似乎不像计算机。

试错法

//此方法不好
#include"funct.h"
void eyyc2()
{
    double a, b, c, d, e, f;
    cout << "-----------" << endl;
    cout << "对于方程组:" << endl;
    cout << "1.   ax+by=c " << endl;
    cout << "2.   dx+ey=f " << endl;
    cout << "依次输入a,b,c,d,e,f:" << endl;
    cout << "-----------" << endl;
    cin >> a >> b >> c >> d >> e >> f;
    double x = MIN, y = MIN, xx, yy;
     if(d!=0){
         for (;; y++)
         {
             yy = y / 100;
             // cout<<yy<<endl;
             if (yy*b*d-a*e*yy==c*d-a*f)
             {
                 cout << "-----------" << endl;
                 cout << "y=" << yy << endl;
                 cout << "x=" << (f-e*yy)/d << endl;
                 break;
             }
             // else cout<<"0";
         }}
         else {
             for (;; y++)
         {
             yy = y / 100;
             // cout<<yy<<endl;
             if (yy*e==f)
             {
                 cout << "y=" << yy << endl;
                 cout << "x=" << (c-b*yy)/a << endl;
                 break;
             }
             // else cout<<"0";
         }

         }

}

主函数

#include"funct.h"
int main()
{
    
    while (1)
    {
        cout << "-----------" << endl;
        cout << "1.一元二次" << endl;
        cout << "2.一元三次" << endl;
        cout << "3.二元一次" << endl;
        cout << "-----------" << endl;
        int se;
        cin >> se;
        switch(se)
        {
            case 1:
                yyec1(); 
                //yyec2();
                break;
            case 2:
                yysc();
                break;
            case 3:
                eyyc1();
                //eyyc2();
                break;
            default:cout << "what are you inputting?" << endl; break;
        }
    }
    return 0;
}

头文件

#include<iostream>
#include <cmath>
#define MIN -100
using namespace std;
void yyec1();
void yyec2();
void yysc();
void eyyc1();
void eyyc2();

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值