迭代法c语言 用c语言呼叫函式写个程式,求方程ax*x+bx+c=0的根,要求用3个函式分别求当b*b-4ac大于零、等于零 求大神

用c语言呼叫函式写个程式,求方程ax*x+bx+c=0的根,要求用3个函式分别求当b*b-4ac大于零、等于零 求大神
用c语言呼叫函式写个程式,求方程ax*x+bx+c=0的根,要求用3个函式分别求当b*b-4ac大于零、等于零 求大神
这是我初学C时写的题,翻出来给你看看,三个函式无非就是你分三种情况,你自己加个if就好了,我这题是预设有两个不相同的解,不想改了,应该能看懂,不懂你问我
sqrt函式是math.h中提供的,是开根的意思
a,b,c是输入的
Description
程式设计计算并输出一元二次方程ax^2+bx+c=0的两个实根,其中a、b、c的值由使用者从键盘输入,假设a、b、c的值能保证方程有两个不相等的实根(即b^2-4ac>0)。
Input
输入三个实数,以空格隔开。
Output
按从大到小的顺序输出两个实根,每个资料占7列、小数点后保留两位小数,不足7列右对齐
Sample Input
2 6 1
Sample Output
-0.18 -2.82
HINT
注意%m.nf的作用
#include<stdio.h>
#include<math.h>
int
main()
{
x1,x2是结果
double a,b,c,x1,x2;
scanf("%lf %lf %lf",&a,&b,&c);
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
if(x1>x2)
{
printf("%7.2lf%7.2lfn",x1,x2);
}
if(x1<x2)
{
printf("%7.2lf%7.2fn",x2,x1);
}
return 0;
}
求文件: 编写一个求方程ax*x+bx+c=0的根,要求用3个函式分别求当b*b-4ac大于零、等于零和小于零时的根。要
x=-b加减根号下b的平方-4ac除以2a 。
b*b-4ac>0,x有两个值,
b*b-4ac=0,x有一个值,
b*b-4ac<0,x无值
C语言程式设计方程ax*x+bx+c=0的根用3个函式求当b*b-4ac大于0等于0小于0时的根并输出结果.主函式输入a,b,c值
#include #include fun1(float a,float b,float c){float m,n,d;d=b*b-a*c*4;m=-b/(2*a); n=sqrt(-d)/(2*a); printf("方程有虚数1:%.2f+%.2fi
",m,n); printf("方程有虚数2:%.2f-%.2fi
",m,n);}fun2(float a,float b,float c){printf("方程有唯一解:%.2f
",-b/(2*a));}fun3(float a,float b,float c){float x1,x2,d;d=b*b-a*c*4; x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); printf("方程有两个解%.2f %.2f
",x1,x2);}int main(){ float a,b,c; scanf("%f%f%f",&a,&b,&c); if(b*b<a*c*4) fun1(a,b,c); else if(b*b==a*c*4) fun2(a,b,c); else fun3(a,b,c); return 0;}辛苦的写出来了!除错通过望采纳!C语言程式设计方程ax*x+bx+c=0的根用3个函式求当b*b-4ac大于0等于0小于0时的根并输出结果.主函式输入a,b,c值
求方程ax2+bx+c=0的根,用3个函式分别求当b2-4ac大于0,等于0,和小于0时的根并输出结果。从主函式a,b,c输
#include <cstdio>
#include <cmath>
b^2-4ac == 0
void fun1(double &a,double &b,double &c,double &d){
double ans = -b/(2*a);
printf("b^2-4ac == 0 , x1 = x2 = %lf.n",ans);
}
b^2-4ac > 0
void fun2(double &a,double &b,double &c,double &d){
double ans1,ans2;
ans1 = (-b+sqrt(d)) / (2*a);
ans2 = (-b-sqrt(d)) / (2*a);
printf("b^2-4ac > 0 , x1 = %lf , x2 = %lf.n",ans1,ans2);
}
b^2-4ac < 0
void fun3(double &a,double &b,double &c,double &d){
double real,imar;
real = -b/(2*a);
imar = sqrt(-d) / (2*a);
printf("b^2-4ac < 0 , x1 = %lf+%lfi , x2 = %lf-%lfi.n",real,imar,real,imar);
}
int main(){
double a,b,c,d;
printf("please input a,b,c.n");
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){
d = b*b-4*a*c;
if(d==0) fun1(a,b,c,d);
else if(d>0) fun2(a,b,c,d);
else fun3(a,b,c,d);
printf("please input a,b,c.n");
}
}
用c语言呼叫函式实现3个数求和的程式
#include <stdio.h>#include <stdlib.h>int Fun(int a, int b){ return a+b;}int main(int argc, char *argv[]) { int a,b,c; scanf("%d %d %d",&a,&b,&c); int sum=Fun(Fun(a,b),c); printf("nsum=%d",sum); return 0;}求方程ax~+bx+c=0的根,用3个函式分别求当b~-4ac大于0等于0和小于0时的根并输出结果.从主函式输出abc的值
#include <iostream>
#include <cmath>
using namespace std;
void f(double a,double b, double c) 定义函式要为空型别
{double x1,double x2;
x1=(-b+sqrt(b*b-4*a*c))/(2*a); 注意分母要用括号全部括起来
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
}
void k(double a,double b, double c)
{double x;
x=-b/(2*a);
cout<<"x1=x2="<<x<<endl;
}
void m()
{cout<<"error!"<<endl;
}
int main()
{double a,b,c,z;
cout<<"enter a,b,c:";
cin>>a>>b>>c;
z=b*b-4*a*c;
if(z>0)
f(a,b,c);
if(z==0)
k(a,b,c);
if (z<0)
m();}
求方程 a*x^2+b*x+c=0的根,用3个函式分别求当b^2-4*a*c大于0、等于0、小于0时的根并输出结果。从主函式输
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
double x1,x2;
printf("请输入三个整数");
scanf("%d%d%d",&a,&b,&c);
if(pow(b,2)-4*a*c<0)
printf("无解");
else
x1=(-b+sqrt(pow(b,2)-4*a*c)/(2*a));
x2=(-b-sqrt(pow(b,2)-4*a*c)/(2*a));
printf("x1=%lfn,x2=%lfn",x1,x2);
}
现写的 给点分吧 忽忽~~
求方程ax^2+bx+c=0的根,用3个函式分别求当b^2-4ac大于0,等于0,和小于0时的亘并输出,两个数由键盘输入
10 input "输入方程的系数a,b,c=o";a,b,c
if a=0 then "一元二次方程的系数a不能为0”:goto 10
y=b*b-4*a*c
if y>=0 then
PRINT "x1=";(-b+sqr(y))/(2*a),"x2=";(-b-sqr(y))/(2*a)
else
PRINT "此一元二次方程无实数解"
end
求方程ax2+bx+c=0的根,用三个函式分别求当b2-4ac>0,等于0和小于0的根并输出结果。从主函式输入a,b,c的值
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int Judge( double a,double b,double c)
{
double result = b*b - 4*a*c;
if( result == 0 )
return 0;
else if( result > 0 )
return 1;
else
return -1;
}
void ComputeGreatThanZero( double a,double b, double c )
{
printf( "x1 = %gf,x2 = %gf",(-b+sqrt(b*b-4*a*c))/(2*a),((-b-sqrt(b*b-4*a*c))/(2*a)) );
}
void ComputeEqualZero( double a,double b ,double c )
{
printf( "x1 = x2 = %g",-b/(2*a));
}
void ComputeBelowZero( double a,double b,double c )
{
double delta = sqrt(4*a*c-b*b);
double temp = delta/(2*a);
printf( "x1 = %g ",-b/(2*a));
if( temp > 0 )
printf( " + %gi,",temp);
else
printf( " - %gi,",fabs(temp) );
printf( "x2 = %g",-b/(2*a) );
if( delta > 0 )
printf( " - %gi",fabs( temp));
else
printf( " + %gi", fabs(temp));
}
void main()
{
double a,b,c;
int flag;
scanf( "%lf%lf%lf",&a,&b,&c);
if( a == 0 )
{
printf( "n a = 0n");
exit(1);
}
flag = Judge( a,b,c);
if( flag > 0 )
ComputeGreatThanZero(a,b,c);
else if( flag == 0 )
ComputeEqualZero(a,b,c);
else
ComputeBelowZero(a,b,c);
}
zd_44.cpp : Defines the entry point for the console application.
#include <math.h>
#include <stdio.h>
float x1,x2,disc,p,q;
greater_than_zero(float a,float b)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
}
equal_to_zero(float a,float b)
{
x1=x2=(-b)/(2*a);
}
smaller_than_zero(float a,float b)
{
p=-b/(2*a);
q=sqrt(abs(disc))/(2*a);
}
int main(int argc, char* argv[])
{
float a,b,c;
printf("Input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
printf("nequation:%5.2f*x*x+%5.2f*x+%5.2f=0n",a,b,c);
disc=b*b-4*a*c;
printf("root:n");
if(disc>0)
{
greater_than_zero(a,b);
printf("x1=%5.2ftx2=%5.2fnn",x1,x2);
}
else if(disc==0)
{
equal_to_zero(a,b);
printf("x1=%5.2ftx2=%5.2fnn",x1,x2);
}
else
{
smaller_than_zero(a,b);
printf("x1=%5.2f+%5.2fitx2=%5.2f-%f5.2in",p,q,p,q);
}
printf("Hello World!n");
return 0;
}
执行结果:
Input a,b,c:7,4,3
equation: 7.00*x*x+ 4.00*x+ 3.00=0
root:
x1=-0.29+ 0.59i x2=-0.29-0.5890155.2i
Hello World!
Press any key to continue