In mathematics, the distance of a point with coordinates (x,y) from the origin is the square root of the sum of x-squared and y-squared. Assume that Point has already been defined as a structured type with two double fields, x and y, define a function getR that takes as an argument a value of type Point and returns the distance of that point from the origin (as described above).在數學中點(X,Y)到原點間的距離等于x平方加y平方的平方根。假設這個點已經被定義在一個struct 類型,并有兩個double類型的變量X,Y屬于其中。
1 回答

夢里花落0921
TA貢獻1772條經驗 獲得超6個贊
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
struct point {
double x; double y;
};
double getR(struct point p)
{
double d;
d=sqrt(p.x*p.x+p.y*p.y);
return d;
}
int main( )
{
double d;
struct point data;
cout<<"Please input two double fields, x and y:";
cin>>data.x;
cin>>data.y;
d=getR(data);
cout<<setprecision(6)<<d<<endl;;
system("pause");
return 0;
}
添加回答
舉報
0/150
提交
取消