c语言程序问题求解答
#include<stdio.h>#include<math.h>floatsquare(floatx){floatx2;x2=x*x;return(x2);}float...
#include<stdio.h>
#include<math.h>
float square(float x)
{
float x2;
x2=x*x;
return(x2);
}
float dist(float x1,float y1,float x2,float y2)
{
float a,b,dist;
a=x1-x2;
b=y1-y2;
dist=sqrt(square(a)+square(b));
return(dist);
}
main()
{
float x1,y1,x2,y2,distance;
printf("please input x1:");
scanf("%f",&x1);
printf("please input y1:");
scanf("%f",&y1);
printf("please input x2:");
scanf("%f",&x2);
printf("please input y2:");
scanf("%f",&y2);
distance=dist(x1,y1,x2,y2);
printf("distance=%f",dist);
}
不知道为什么返回的值是0? 展开
#include<math.h>
float square(float x)
{
float x2;
x2=x*x;
return(x2);
}
float dist(float x1,float y1,float x2,float y2)
{
float a,b,dist;
a=x1-x2;
b=y1-y2;
dist=sqrt(square(a)+square(b));
return(dist);
}
main()
{
float x1,y1,x2,y2,distance;
printf("please input x1:");
scanf("%f",&x1);
printf("please input y1:");
scanf("%f",&y1);
printf("please input x2:");
scanf("%f",&x2);
printf("please input y2:");
scanf("%f",&y2);
distance=dist(x1,y1,x2,y2);
printf("distance=%f",dist);
}
不知道为什么返回的值是0? 展开
1个回答
展开全部
程序稍微有点儿小问题,帮您改一下(最后一个printf):
#include <stdio.h>
#include <math.h>
double square(double x)
{
return x * x;
}
double dist(double x1, double y1, double x2, double y2)
{
double a, b, dist;
a = x1 - x2;
b = y1 - y2;
dist = sqrt(square(a) + square(b));
return dist;
}
int main(void)
{
double x1, y1, x2, y2, distance;
printf("Please input x1: ");
scanf("%lf", &x1);
printf("Please input y1: ");
scanf("%lf", &y1);
printf("Please input x2: ");
scanf("%lf", &x2);
printf("Please input y2: ");
scanf("%lf", &y2);
distance = dist(x1, y1, x2, y2);
printf("distance = %lf\n", distance);
return 0;
}
返回0是C语言标准规定的,main函数必须为int类型,不允许为void。但是在老式的编译器中,void也能通过,不过不建议您这么写。返回0表示程序正常运行后结束,没有任何问题。0将被返回给操作系统,操作系统想知道他上面的程序都运行如何,方便在结束时释放一些脚本。更多问题请追问。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询