实现解一元二次方程(循环)
2018-06-18 03:58:27来源:未知 阅读 ()
// equation.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h> // 定义了浮点型的无穷小常量 const float FLT_EPSILON=1.192092896e-07F;
/*
* The function to caculate a Real coefficient eqution's(ax^2+bx+c=0) root.
* IN: a,b,c ---- the three real coefficient,
* OUT: r1, r2 ---- the two real roots or the real part of the complex roots.
* i1, i2 ---- the image part of the complex roots.
* RET: the status of the equation.
*/
int equation1(float a, float b, float c, float* r1, float* r2, float* i1, float* i2);
int equation2(float a, float b, float c, float& r1, float& r2, float& i1, float& i2);
int main(int argc, char* argv[])
{
float a;
float b;
float c;
char xuanze;
float r1;
float r2;
float i1;
float i2;
for(;;)
{
printf("是否进行一元二次方程计算?Y/N\n");
scanf("%c",&xuanze);
if(xuanze=='Y')
{
printf("Please input a,b,c of the equation ax^2+bx+c=0 [a b c Enter]: ");
scanf("%f %f %f", &a, &b, &c);
switch(equation2(a,b,c, r1, r2, i1, i2))
{
case 0:
printf("The equation %gx^2+%gx+%g=0 has no roots.\n", a, b, c);
break;
case 1:
printf("The equation %gx^2+%gx+%g=0 has one real root:\n \tx=%g.\n", a, b, c, r1);
break;
case 2:
printf("The equation %gx^2+%gx+%g=0 has two real roots:\n \tx1=%g, \tx2=%g.\n", a, b, c, r1, r2);
break;
case 3:
printf("The equation %gx^2+%gx+%g=0 has an arbitrary solution.\n", a, b, c);
break;
case 4:
printf("The equation %gx^2+%gx+%g=0 has a pair of conjugate complex roots:\n\tx1=%g+%gi, \tx2=%g-%gi.\n", a, b, c, r1, i1, r1, i1);
break;
default:
break;
}
_flushall();
}
else
{
return 0;
}
}
}
// printf("选择的是%c\n",xuanze);
/*
* The function to caculate a Real coefficient eqution's(ax^2+bx+c=0) root.
* IN: a,b,c ---- the three real coefficient,
* OUT: r1, r2 ---- the two real roots or the real part of the complex roots.
* i1, i2 ---- the image part of the complex roots.
* RET: the status of the equation.
* 0 ---- no solution.
* 1 ---- one real root.
* 2 ---- two real root.
* 3 ---- has an arbitrary solution.
* 4 ---- has a pair of conjugate complex roots.
*/
int eqution1(float a, float b, float c, float* r1, float* r2, float* image)
{
return 0;
}
/*
* The function to caculate a Real coefficient eqution's(ax^2+bx+c=0) root.
* IN: a,b,c ---- the three real coefficient,
* OUT: r1, r2 ---- the two real roots or the real part of the complex roots.
* i1, i2 ---- the image part of the complex roots.
* RET: the status of the equation.
* 0 ---- no solution.
* 1 ---- one real root.
* 2 ---- two real root.
* 3 ---- has an arbitrary solution.
* 4 ---- has a pair of conjugate complex roots.
*/
int equation2(float a, float b, float c, float& r1, float& r2, float& i1, float& i2)
{
int s;
double delta;
if (0==a){
if(0==b){
if(0==c){
s= 3;
}
else{
s=0;
}
}
else{
r1=-c/b;
s=1;
}
}
else
{
delta=b*b-4.0f*a*c;
if(delta>=-FLT_EPSILON && delta<=FLT_EPSILON) // delta==0
{
r1=r2=-b/2.0f/a;
s=1;
}
else if(delta>FLT_EPSILON){ // delta>0
r1=-b/2.0f/a+(float)sqrt(delta);
r2=-b/2.0f/a-(float)sqrt(delta);
s=2;
}
else{ // delta<0
r1=r2=-b/2.0f/a;
i1=(float)sqrt(-delta);
i2=-(float)sqrt(-delta);
s=4;
}
}
return s;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++冒泡排序 (基于函数模板实现) 2020-05-31
- opencv-12-高斯滤波-双边滤波(附C++代码实现) 2020-05-10
- 二叉排序树 2020-05-02
- 抽象宠物类的实现 代码参考 2020-04-29
- 虚函数实现多态性 代码参考 2020-04-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash