user-0 提问: C语言用函数求根

回复:
user: user-1
content: 引用 3 楼 withszm 的回复:[quote=引用 2 楼 lilianjie001的回复:] #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书 可是我想要的是函数版的啊，不是函数的我也会了[/quote] 把这期中计算部分的代码，封装成函数有困难？

user: user-2
content: 引用 3 楼 withszm 的回复:[quote=引用 2 楼 lilianjie001的回复:] #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书 可是我想要的是函数版的啊，不是函数的我也会了[/quote]  先把基础打好！ 如果要函数，你把需要的部分放到函数里，该传的参数传到函数里，函数把要的结果返回，有什么难的

user: user-3
content: 引用 3 楼 withszm 的回复:[quote=引用 2 楼 lilianjie001的回复:] #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书 可是我想要的是函数版的啊，不是函数的我也会了[/quote]  函数模板 难道要cstring 类型的求根，还是要其他类对象的求根？ 不切实际，最后还是要模板特例化~~~  没意思~~~  就像这样 做空的模板函数 然后 模板特例化~~~double int float?

user: user-2
content: #include <stdio.h> #include<math.h>  void equal0(float a, float b, float c) { 	float disc, realpart,inagpart; 	disc=b*b-4*a*c; 	realpart=-b/(2*a); 	inagpart=sqrt(-disc)/(2*a); 	printf("has complex roots:\n"); 	printf("%8.4f+%8.4fi\n",realpart,inagpart); 	printf("%8.4f-%8.4fi\n",realpart,inagpart); }  void less0(float a, float b, float c) { 	printf("has two equal  roots :%8.4f\n",-b/(2*a)); }  void greater0(float a, float b, float c) { 	float disc, x1, x2; 	disc=b*b-4*a*c; 	x1=(-b+sqrt(disc))/(2*a); 	x2=(-b-sqrt(disc))/(2*a); 	printf("has distinct real roots:%8.4f and %8.4f\n",x1,x2); }   int main() { 	float a,b,c,disc; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			less0(a,b,c); 		else if (disc>1e-6) 			greater0(a,b,c); 		else 			equal0(a,b,c); 	} }

user: user-0
content: 引用 5 楼 paschen的回复:[quote=引用 3 楼 withszm 的回复:] [quote=引用 2 楼 lilianjie001的回复:] #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书 可是我想要的是函数版的啊，不是函数的我也会了[/quote]  先把基础打好！ 如果要函数，你把需要的部分放到函数里，该传的参数传到函数里，函数把要的结果返回，有什么难的[/quote] 刚学不久  还是个学渣啊  大神

user: user-0
content: 引用 6 楼 lilianjie001的回复:[quote=引用 3 楼 withszm 的回复:] [quote=引用 2 楼 lilianjie001的回复:] #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书 可是我想要的是函数版的啊，不是函数的我也会了[/quote] 把这期中计算部分的代码，封装成函数有困难？[/quote] 我才刚学，原谅我是学渣

user: user-0
content: 引用 2 楼 lilianjie001的回复: #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书 可是我想要的是函数版的啊，不是函数的我也会了

user: user-1
content: #include<math.h> main() { 	float a,b,c,disc,x1,x2,realpart,inagpart; 	scanf("%f,%f,%f",&a,&b,&c); 	printf("THE equation"); 	if(fabs(a)<=1e-6) 		printf("is not a quadratic\n"); 	else 	{ 		disc=b*b-4*a*c; 		if(fabs(disc)<=1e-6) 			printf("has two equal  roots :%8.4f\n",-b/(2*a)); 		else if (disc>1e-6) 		{ 			x1=(-b+sqrt(disc))/(2*a); 			x2=(-b-sqrt(disc))/(2*a); 			printf("has distinct real roots:%8.4f and %8.4\n",x1,x2); 		} 		else 		{ 			realpart=-b/(2*a); 			inagpart=sqrt(-disc)/(2*a); 			printf("has complex roots:\n"); 			printf("%8.4f+%8.4fi\n",realpart,inagpart); 			printf("%8.4f-%8.4fi\n",realpart,inagpart); 		} 	} } 没事多看看书

user: user-4
content: 再牛×的老师也无法代替学生自己领悟和上厕所！