编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。
生活随笔
收集整理的這篇文章主要介紹了
编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<iostream>
#include<math.h>
using namespace std;
void equation_1(int a,int b,int c);
void equation_2(int a,int b,int c);
void equation_3(int a,int b,int c);
int main()
{cout<<"請輸入二元方程的系數:"<<endl;int a,b,c;double temp;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);}return 0;
}
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);x2=(-b-sqrt(temp))/(2*a);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);x2=x1;cout<<"兩個相等的實數跟"<<endl;cout<<"x1=x2="<<x1<<endl;
}
void equation_3(int a,int b,int c)
{cout<<"沒有實數跟"<<endl;
}
總結
以上是生活随笔為你收集整理的编写一个求方程ax2 + bx + c = 0的根 的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。要求从主函数输入a,b,c的值并输出结果。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 进入保护模式(二)——《x86汇编语言:
- 下一篇: 圆章能随便刻吗_自己晒干的蒲公英能长期当