初学者在c++中调用函数,代码编译数学是错误的



所以我是c++的新手,我认为我了解如何从函数的角度了解基本知识,但我遇到了一些问题。在赋值中,我需要返回圆的距离、半径、周长、面积和直径。当前使用代码块,代码可以编译,但不能正常工作。感谢您的帮助!

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
double const PI = 3.1416;//constant global variable
double distance(double, double, double, double);//prototype for distance function after main
double radius (double, double, double, double, double);//prototype for radius after main
double circumference(double);//prototype for circumference after main
double area(double);//prototype for area after main
int main()
{
double x1, y1, x2, y2;
cout << fixed << showpoint << setprecision(2);
cout << "Enter the center X and Y coordinate of a circle (in that order with a space in between): ";
cin >> x1 >> y1;//center coordinates
cout << "Enter another X and Y coordinate in the same circle (in that order with a space in between): ";
cin >> x2 >> y2; // second set of coordinates
cout << "Circle's radius is: " << radius << endl;
cout << "Circle's circumference is: " << circumference << endl;
cout << "Circle's area is: " << area << endl;
double diameter = 2*radius(x1, y1, x2, y2, distance(x1, y1, x2, y2));
cout << "Circle's diameter is: " << diameter << endl;
return 0;
}//end main
double distance(double x1, double y1, double x2, double y2)
{
double distance = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
return distance;
}//end distance
double radius(double x1, double y1, double x2, double y2, double distance)
{
double radius = distance;
return radius;
}//end radius
double circumference(double radius)
{
double circumference = 2*PI*radius;
return circumference;
}//end circumference
double area(double radius)
{
double area = pow(PI*radius,2);
return area;
}//end area

我修复了您的代码:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
double const PI = 3.1416;//constant global variable
double distance(double, double, double, double);//prototype for distance function after main
double radius (double, double, double, double, double);//prototype for radius after main
double circumference(double);//prototype for circumference after main
double area(double);//prototype for area after main
int main()
{
double x1, y1, x2, y2;
cout << fixed << showpoint << setprecision(2);
cout << "Enter the center X and Y coordinate of a circle (in that order with a space in between): ";
cin >> x1 >> y1;//center coordinates
cout << "Enter another X and Y coordinate in the same circle (in that order with a space in between): ";
cin >> x2 >> y2; // second set of coordinates
cout << "Circle's radius is: " << radius << endl;
cout << "Circle's circumference is: " << circumference << endl;
cout << "Circle's area is: " << area << endl;
double diameter = 2*radius(x1, y1, x2, y2, distance(x1, y1, x2, y2));
cout << "Circle's diameter is: " << diameter << endl;
return 0;
}//end main
double distance(double x1, double y1, double x2, double y2)
{
double distance = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
return distance;
}//end distance
double radius(double x1, double y1, double x2, double y2, double distance)
{
double radius = distance;
return radius;
}//end radius
double circumference(double radius)
{
double circumference = 2*PI*radius;
return circumference;
}//end circumference
double area(double radius)
{
double area = pow(PI*radius,2);
return area;
}//end area

您的代码中存在许多问题:

  1. 未正确调用函数

想想你写的这些行:

cout << "Circle's radius is: " << radius << endl;
cout << "Circle's circumference is: " << circumference << endl;
cout << "Circle's area is: " << area << endl;

这里的问题是,你没有按照预期的方式调用函数。你在代码的末尾进行了函数定义,并将一些函数分别命名为半径、周长和面积。这里的问题是,当你试图调用函数时,你没有向函数中发送参数,这就是为什么它不能产生正确的结果。您必须按照函数定义/原型中提到的顺序传递适当的参数,以便将值传递到函数上以产生所需的结果。

  1. 数学错误

想想你写的这些行:

double area(double radius)
{
double area = pow(PI*radius,2);
return area;
}

圆的面积=πx r^2

pow(PI*radius,2) 

将返回一个等于(πxr(^2的值,所以在使用数学函数时要小心。

  1. 使用正确的编程实践

我不会认为这是代码中的一个问题,但如果你学会了编写高效的代码,它会让你的编码之旅变得更容易。你学得越多,你就会意识到使用它们的重要性,因为在这个基本问题上的差异几乎可以忽略不计。

考虑一下我为这个问题编写的代码:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
double const PI = 3.1416;//constant global variable
double distance(double, double, double, double);//prototype for distance function after main
double radius (double, double, double, double);//prototype for radius after main
double circumference(double);//prototype for circumference after main
double area(double);//prototype for area after main
int main()
{
double x1, y1, x2, y2;
cout << "Enter the center X and Y coordinate of a circle (in that order with a space in between): "<<endl;
cin >> x1 >> y1;//coordinates of point 1
cout << "Enter another X and Y coordinate in the same circle (in that order with a space in between): "<<endl;
cin >> x2 >> y2; // second set of point 2
cout << "Circle's radius is: " << radius(x1,y1,x2,y2) << endl;
cout << "Circle's circumference is: " << circumference(radius(x1,y1,x2,y2)) << endl;
cout << "Circle's area is: " << area(radius(x1,y1,x2,y2)) << endl;
cout << "Circle's diameter is: " << 2 * radius(x1, y1, x2, y2) << endl;
return 0;
}//end main
double distance(double x1, double y1, double x2, double y2)
{
double distance = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
return distance;
}//end distance
double radius(double x1, double y1, double x2, double y2)
{
double radius = distance(x1,y1,x2,y2)/2;
return radius;
}//end radius
double circumference(double radius)
{
double circumference = 2*PI*radius;
return circumference;
}//end circumference
double area(double radius)
{
double area = PI * pow(radius,2);
return area;
}//end area

在初级阶段,我会说你做得很好:D

回顾主题,然后回到这个问题上来,以便更好地理解该主题。此外,请记住,如果你想擅长编程,练习是关键。所以,接受这场磨难吧。

编码快乐!

您遇到的问题是,在C++中调用函数时,它必须采用foo(bar(格式,其中bar表示函数的输入参数。例如,在原型中,您用5个参数定义半径,但当您稍后调用它时,它没有要评估的参数。也许对C++有更多了解的人可以回答为什么在cout中调用radius会返回值1,但我相信这个值是程序试图执行调用时返回的退出代码。在这种情况下,调用radius或任何函数的正确方式是为其提供定义为接收的适当数量的参数,即radius(x1,y1,x2,y2(。

我还注意到,area((中计算圆面积的语句是不正确的——你可能想要PI*pow(radius,2(,因为它目前的情况是,首先计算PI*radius,然后对结果进行平方。另一件事是,在radius中,你已经声明距离是一个参数,当我认为你想从radius函数中调用距离时,必须填充这个参数,因此可以从radius的原型和实现中删除。附件是您的代码以及为更正问题而进行的小调整。

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
double const PI = 3.1416;//constant global variable
double distance(double, double, double, double);//prototype for distance function after main
double radius (double, double, double, double);//prototype for radius after main
double circumference(double);//prototype for circumference after main
double area(double);//prototype for area after main
int main()
{
double x1, y1, x2, y2;
cout << fixed << showpoint << setprecision(2);
cout << "Enter the center X and Y coordinate of a circle (in that order with a space in between): ";
cin >> x1 >> y1;//center coordinates
cout << "Enter another X and Y coordinate in the same circle (in that order with a space in between): ";
cin >> x2 >> y2; // second set of coordinates
cout << "Circle's radius is: " << radius(x1,y1,x2,y2) << endl;
cout << "Circle's circumference is: " << circumference(radius(x1,y1,x2,y2)) << endl;
cout << "Circle's area is: " << area(radius(x1,y1,x2,y2)) << endl;
double diameter = 2*radius(x1, y1, x2, y2);
cout << "Circle's diameter is: " << diameter << endl;
return 0;
}//end main
double distance(double x1, double y1, double x2, double y2)
{
double distance = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
return distance;
}//end distance
double radius(double x1, double y1, double x2, double y2)
{
double radius = distance(x1, y1, x2, y2);
return radius;
}//end radius
double circumference(double radius)
{
double circumference = 2*PI*radius;
return circumference;
}//end circumference
double area(double radius)
{
double area = PI*pow(radius,2);
return area;
}//end area

所以我能够重新处理它,并且我对将函数调用付诸实践有了更多的理解!我修正了数学,并为半径做了一个变量,这样我可以更有效地调用它!我也调整了一下功能。我有点迷失在定义中,哈哈。谢谢雅尔的帮助!

#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
//Part 1 - circle
double const PI = 3.1416;//constant global variable
double distance(double, double, double, double);//prototype for distance function after main
double radius (double, double, double, double);//prototype for radius after main
double circumference(double);//prototype for circumference after main
double area(double);//prototype for area after main
int main()
{
double x1, y1, x2, y2;
cout << fixed << showpoint << setprecision(2);
cout << "Enter the center X and Y coordinate of a circle (in that order with a space in between): ";
cin >> x1 >> y1;//center coordinates
cout << "Enter another X and Y coordinate in the same circle (in that order with a space in between): ";
cin >> x2 >> y2; // second set of coordinates
double r = radius(x1, y1, x2, y2);
cout << "Circle's radius is: " << r << endl;
cout << "Circle's circumference is: " << circumference(r) << endl;
cout << "Circle's area is: " << area(r) << endl;
double diameter = 2*r;
cout << "Circle's diameter is: " << diameter << endl;
return 0;
}//end main
double distance(double x1, double y1, double x2, double y2)
{
double distance = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
return distance;
}//end distance
double radius(double x1, double y1, double x2, double y2)
{
double radius = distance(x1, y1, x2, y2);
return radius;
}//end radius
double circumference(double radius)
{
double circumference = 2*PI*radius;
return circumference;
}//end circumference
double area(double radius)
{
double area = PI*pow(radius,2);
return area;
}//end area

最新更新