初学者在c++中调用函数,代码编译数学是错误的
beginner calling functions in c++, code compiles math is wrong
所以我是 C++ 的新手,我认为我了解如何在函数方面了解基础知识,但我遇到了一些问题。在作业中,我需要 return 圆的距离、半径、周长、面积和直径的值。当前使用代码块,代码编译但不能正常工作。感谢您的帮助!
#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
您 运行 遇到的问题是,在 C++ 中调用函数时,它必须采用 foo(bar) 格式,其中 bar 表示函数的输入参数。例如,在您的原型中,您使用 5 个参数定义半径,但是当您稍后调用它时,它没有要评估的参数。也许对 C++ 有更多了解的人可以回答为什么在 cout 中调用 radius 会导致返回值 1,但是我相信这个值是程序尝试执行调用时返回的退出代码。在这种情况下,调用 radius 或任何函数的正确方法是为其提供定义为接收的适当数量的参数,即 radius(x1, y1, x2, y2).
我还注意到一些事情,area() 中用于计算圆面积的语句是不正确的,您需要 PI*pow(radius,2),因为它当前的方式是先计算 PI*radius,然后然后将结果平方。另一件事是,在 radius 中,您已将 distance 声明为参数,当我相信您打算从 radius 函数中调用 distance 时必须填充该参数,因此可以从 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;
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
你的代码有很多问题:
- 没有正确调用函数
考虑您写的这些行:
cout << "Circle's radius is: " << radius << endl;
cout << "Circle's circumference is: " << circumference << endl;
cout << "Circle's area is: " << area << endl;
这里的问题是您没有按照预期的方式调用函数。您在代码末尾进行了函数定义,并将一些函数分别命名为半径、周长和面积。这里的问题是,当您尝试调用该函数时,您没有将参数发送到您的函数中,这就是它不能产生正确结果的原因。您必须按照函数中提到的相同顺序传递适当的参数 definition/prototype,以便可以将值传递到您的函数以产生所需的结果。
- 数学错误
考虑您写的这些行:
double area(double radius)
{
double area = pow(PI*radius,2);
return area;
}
圆的面积 = π x r^2
pow(PI*radius,2)
将 return 等于 (π x r)^2 的值,因此在使用数学函数时要小心。
- 使用正确的编程习惯
我不会将其视为您的代码中的问题,但如果您学会编写高效的代码,它将使您的编码之旅变得更加轻松。你学得越多,你就会发现使用它们的重要性,因为在这种基本问题上的差异几乎可以忽略不计。
考虑一下我要为这个问题编写的代码:
#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
查看主题并返回此问题以更好地理解主题。此外,请记住,如果您想擅长编程,练习是关键。所以拥抱磨砺吧。
编码愉快!
所以我能够对其进行返工,并且我对将函数调用付诸实践有了更多的了解!我修正了数学并在 main 中为半径创建了一个变量,这样我可以更有效地调用它!我也稍微调整了这个功能。我有点迷失在定义中哈哈。谢谢大家的帮助!
#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
所以我是 C++ 的新手,我认为我了解如何在函数方面了解基础知识,但我遇到了一些问题。在作业中,我需要 return 圆的距离、半径、周长、面积和直径的值。当前使用代码块,代码编译但不能正常工作。感谢您的帮助!
#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
您 运行 遇到的问题是,在 C++ 中调用函数时,它必须采用 foo(bar) 格式,其中 bar 表示函数的输入参数。例如,在您的原型中,您使用 5 个参数定义半径,但是当您稍后调用它时,它没有要评估的参数。也许对 C++ 有更多了解的人可以回答为什么在 cout 中调用 radius 会导致返回值 1,但是我相信这个值是程序尝试执行调用时返回的退出代码。在这种情况下,调用 radius 或任何函数的正确方法是为其提供定义为接收的适当数量的参数,即 radius(x1, y1, x2, y2).
我还注意到一些事情,area() 中用于计算圆面积的语句是不正确的,您需要 PI*pow(radius,2),因为它当前的方式是先计算 PI*radius,然后然后将结果平方。另一件事是,在 radius 中,您已将 distance 声明为参数,当我相信您打算从 radius 函数中调用 distance 时必须填充该参数,因此可以从 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;
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
你的代码有很多问题:
- 没有正确调用函数
考虑您写的这些行:
cout << "Circle's radius is: " << radius << endl;
cout << "Circle's circumference is: " << circumference << endl;
cout << "Circle's area is: " << area << endl;
这里的问题是您没有按照预期的方式调用函数。您在代码末尾进行了函数定义,并将一些函数分别命名为半径、周长和面积。这里的问题是,当您尝试调用该函数时,您没有将参数发送到您的函数中,这就是它不能产生正确结果的原因。您必须按照函数中提到的相同顺序传递适当的参数 definition/prototype,以便可以将值传递到您的函数以产生所需的结果。
- 数学错误
考虑您写的这些行:
double area(double radius)
{
double area = pow(PI*radius,2);
return area;
}
圆的面积 = π x r^2
pow(PI*radius,2)
将 return 等于 (π x r)^2 的值,因此在使用数学函数时要小心。
- 使用正确的编程习惯
我不会将其视为您的代码中的问题,但如果您学会编写高效的代码,它将使您的编码之旅变得更加轻松。你学得越多,你就会发现使用它们的重要性,因为在这种基本问题上的差异几乎可以忽略不计。
考虑一下我要为这个问题编写的代码:
#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
查看主题并返回此问题以更好地理解主题。此外,请记住,如果您想擅长编程,练习是关键。所以拥抱磨砺吧。
编码愉快!
所以我能够对其进行返工,并且我对将函数调用付诸实践有了更多的了解!我修正了数学并在 main 中为半径创建了一个变量,这样我可以更有效地调用它!我也稍微调整了这个功能。我有点迷失在定义中哈哈。谢谢大家的帮助!
#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