解一元二次方程的C++实现
生活随笔
收集整理的這篇文章主要介紹了
解一元二次方程的C++实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一元二次方程的根的情況分為實根與虛根兩種,代碼如下
#include<iostream> #include<cmath> using namespace std;float *solve_equ(float, float, float);//a, b, c int main() {float a, b, c;cout << "enter a, b, c : " << endl;cin >> a >> b>>c;float *p = solve_equ(a, b, c);if (*p == 0){cout << "x1= " << *(p + 1) << endl;cout << "x2= " << *(p + 2) << endl;}else if (*p = 1){cout << "x1= " << *(p + 1) <<"+"<< *(p + 2) << 'i' << endl;cout << "x2= " << *(p + 1) << *(p + 3) << 'i' << endl;}else cout << "False!\n";system("pause");return 0; } float *solve_equ(float a, float b, float c) {float delta = b * b - 4 * a*c;float x1, x2, v1, v2, r; //v stands for virtual part; r stands for real partfloat *t;if (delta >= 0){int flag=0; //to tell the different kinds of solutions;x1 = float(-b + sqrt(delta)) / float(2 * a);x2 = float((-b - sqrt(delta))) / float((2 * a));t = (float *)malloc(sizeof(float) * 3);*t = flag;*(t+1) = x1;*(t+2) = x2;return t;}else{int flag=1;r = float(-b) / float(2 * a);v1 = float(sqrt(-delta)) / float(2 * a);v2 = -v1;t = (float *)malloc(sizeof(float) * 4);*t = flag;*(t+1) = r;*(t+2) = v1;*(t+3) = v2;return t;} }?
轉載于:https://www.cnblogs.com/Aurora-Borealis/p/11163238.html
總結
以上是生活随笔為你收集整理的解一元二次方程的C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: determination
- 下一篇: CocoaPods 安装与使用教程