3.求解方程ax+bx+c=0的根,要求 (1)画出N-S流程图 (2)写出伪代码 (3)写出相应程序
3.求解方程ax+bx+c=0的根,要求 (1)画出N-S流程图 (2)写出伪代码 (3)写出相应程序
应该是Axx+Bx+C=0的根吧,我这写了个C语言的代码,至于第一步和第二步你还是看着代码自己完成吧,总不能完全依赖别人是吧...
// Solve Equation.cpp :Defines the entry point for the console application.
//
#include
#include
double a,b,c;
void initialize();//初始化工作
void Process();//求解过程
int main(int *argc,int *argv[])
{
int i;
char choice;
do{
i=0;
initialize();
Process();
printf("\t go on?(y/n)\n");
getchar();
scanf("%c",&choice);
if((choice=='y')||(choice=='Y'))
i=1;
}while(i);
return 0;
}
void initialize()
{
printf("-------------axx+bx+c=0----------------\n");
printf("please input the factors:\n");
printf("a=");
scanf("%lf",&a);
printf("b=");
scanf("%lf",&b);
printf("c=");
scanf("%lf",&c);
}
void Process()
{
double dt;//判别式
double x1,x2;
x1=x2=0;
dt=b*b-4*a*c;
if(0==a)
{
if(0==b)
printf("error:'a' and 'b' can`t be both zero!");
else if(0!=c)
printf("\tX=%lf\n-------the quation has only one root\n",-b/c);
else
printf("\tX=0\n-------the quation has only one root\n");
}
else
{
if(dt>=0)
{
dt=sqrt(dt);
x1=(-b+dt)/(2*a);
x2=(b+dt)/(2*a);
printf("\tX1=%lf",x1);
printf("\n\tX2=%lf",x2);
printf("\n-------the quation has two real root.");
}
else
{
dt=sqrt(-dt);
dt=dt/(2*a);
x1=(-b)/(2*a);
x2=b/(2*a);
printf("\tX1=%lf+j%lf",x1,dt);
printf("\n\tX2=%lf-j%lf",x2,dt);
printf("\n-------the quation has two imaginary root\n");
}
}
}