我对C ++和编程仍然很陌生,所以请保持温和。
给定圆上的中心和一个点,您可以使用此公式来查找圆的半径。编写一个程序,提示用户输入中心和圆上的一个点。然后程序应输出圆的半径,直径,周长和面积。您的程序必须至少具有以下功能:
calculateRadius:接收中心的x-y坐标并指向圆圈(由用户输入)并计算点之间的距离。该值将作为圆的半径返回。
calculateArea:接收圆的半径,计算并返回圆的面积。
calculatePerimeter:接收圆的半径,计算并返回圆的周长。
输出应清楚地显示结果圆的半径,面积和周长。
这是我到目前为止所提供的数字和字母作为输出。
#include <iostream>
#include <cmath>
#define PI 3.141592
using namespace std;
int calculateRadius(float x1, float x2, float y1, float y2);
int calculateArea(float radius);
int calculatePerimeter(float radius);
int main()
{
float x1;
float x2;
float y1;
float y2;
cout << "Please enter the first X Coordinate: " << endl;
cin >> x1;
cout << "Please enter the first Y Coordinate: " << endl;
cin >> y1;
cout << "Please enter the second X Coordinate: " << endl;
cin >> x2;
cout << "Please enter the second Y Coordinate: " << endl;
cin >> y2;
cout << "The radius of the circle is: " << calculateRadius << endl;
cout << "The diameter of the circle is: " << endl;
cout << "The cirfumference of the circle is: " << calculatePerimeter << endl;
cout << "The area of the circle is: " << calculateArea << endl;
system("pause");
return 0;
}
int calculateRadius(float x1, float x2, float y1, float y2)
{
float distance;
float radius;
distance = sqrt((pow(x2 - x1, 2)) + (pow(y2 - y1, 2)));
radius = (distance / 2);
return radius;
}
int calculateArea(float radius)
{
float area;
area = PI * pow(radius, 2);
return area;
}
int calculatePerimeter(float radius)
{
float perimeter;
perimeter = 2 * PI * radius;
return perimeter;
}
答案 0 :(得分:0)
您需要指定函数的参数
#include <iostream>
#include <cmath>
#define PI 3.141592
using namespace std;
int calculateRadius(float x1, float x2, float y1, float y2);
int calculateArea(float radius);
int calculatePerimeter(float radius);
int main()
{
float x1;
float x2;
float y1;
float y2;
cout << "Please enter the first X Coordinate: " << endl;
cin >> x1;
cout << "Please enter the first Y Coordinate: " << endl;
cin >> y1;
cout << "Please enter the second X Coordinate: " << endl;
cin >> x2;
cout << "Please enter the second Y Coordinate: " << endl;
cin >> y2;
cout << "The radius of the circle is: " << calculateRadius(x1,x2,y1,y2) << endl;
cout << "The diameter of the circle is: " << endl;
cout << "The cirfumference of the circle is: " << calculatePerimeter(x1) << endl;
cout << "The area of the circle is: " << calculateArea(x1) << endl;
system("pause");
return 0;
}
int calculateRadius(float x1, float x2, float y1, float y2)
{
float distance;
float radius;
distance = sqrt((pow(x2 - x1, 2)) + (pow(y2 - y1, 2)));
radius = (distance / 2);
return radius;
}
int calculateArea(float radius)
{
float area;
area = PI * pow(radius, 2);
return area;
}
int calculatePerimeter(float radius)
{
float perimeter;
perimeter = 2 * PI * radius;
return perimeter;
}
答案 1 :(得分:0)
您还需要将函数的返回类型从int更改为float,因为您正在使用单精度。