2017-07-17 33 views
0

我對C++和編程一般還很新,所以要溫和。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

什麼是你的問題?這是相當廣泛的。 – Carcigenicate

+0

@Carcigenicate當我運行代碼時,我得到的數字和字母顯然是錯誤的。我不知道我做錯了什麼。所以我的問題是,如果有人能找到我得到錯誤答案的原因。 – Jcarlton55

+0

@ Jcarlton55如果您有任何更新(如您的評論),最好是用清晰的信息編輯您自己的問題。 – timiTao

回答

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; 
} 
+0

這是什麼意思?如果這是一個愚蠢的問題,我很抱歉。 @Randolf – Jcarlton55

+0

cout只是打印函數的地址,因爲你想用參數調用函數,就像在calculateRadius()中你需要調用calculateRadius(x1,x2,y1,y2),而不是calculateRadius。您需要指定從哪個值開始計算半徑,以便函數可以使用這些參數計算半徑並將其返回給您。 – Randolf

0

您還需要從int改變你的函數的返回類型爲float,因爲你用單精度工作指定參數。

0
#include <iostream> 
#include <cmath> 
#define PI 3.141592 

using namespace std; 

float calculateRadius(float x1, float x2, float y1, float y2); 
float calculateArea(float radius); 
float 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; 

    float radius = calculateRadius(x1,y1,x2,y2); 
    cout << "The radius of the circle is: " << radius << endl; 
    cout << "The diameter of the circle is: " << 2*radius <<endl; 
    cout << "The cirfumference of the circle is: " << calculatePerimeter(radius) << endl; 
    cout << "The area of the circle is: " << calculateArea(radius) << endl; 

    system("pause"); 
    return 0; 
} 

float calculateRadius(float x1, float y1, float x2, float y2) 
{ 
    float radius = sqrt(pow((x2 - x1),2) + pow((y2 - y1),2)); 
    return radius; 
} 

float calculateArea(float radius) 
{ 
    float area; 

    area = PI * pow(radius, 2); 

    return area; 
} 

float calculatePerimeter(float radius) 
{ 
    float perimeter; 

    perimeter = 2 * PI * radius; 

    return perimeter; 
} 
相關問題