初学者在 C++ 中调用函数,代码编译数学是错误的
Posted
技术标签:
【中文标题】初学者在 C++ 中调用函数,代码编译数学是错误的【英文标题】:beginner calling functions in c++, code compiles math is wrong 【发布时间】:2021-04-19 04:45:53 【问题描述】:所以我是 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
【问题讨论】:
您使用的输入是什么?预期和实际输出是多少?您可能打算调用这些函数并使用返回值cout << "Circle's radius is: " << radius << endl;
-> cout << "Circle's radius is: " << radius(...) << endl;
我的输入主要来自用户。我要求一个中心坐标和另一个坐标,都在一个圆圈中。尽管你怎么说,这更有意义。非常感谢。我仍在积极努力。
【参考方案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, 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;
这里的问题是你没有按照他们应该的方式调用函数。您在代码末尾定义了函数,并将一些函数分别命名为半径、周长和面积。这里的问题是,当您尝试调用该函数时,您没有将参数发送到您的函数中,这就是为什么它不会产生正确的结果。您必须按照函数定义/原型中提到的相同顺序传递适当的参数,以便将值传递给您的函数以产生所需的结果。
-
数学错误
考虑一下你写的这些行:
double area(double radius)
double area = pow(PI*radius,2);
return area;
圆的面积 = π x r^2
pow(PI*radius,2)
将返回一个等于 (π 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
查看主题并返回此问题以更好地理解该主题。此外,请记住,如果您想擅长编程,练习是关键。所以,拥抱磨砺吧。
编码愉快!
【讨论】:
非常感谢您对我的问题的详尽解释。这对我帮助很大。还。非常感谢您用我的面积表达式发现数学错误。我正在尽力掌握这个概念。当我大声说出来时,我可以做到,付诸实践是我滑倒并感到迷茫的地方!虽然你的崩溃有很大帮助!谢谢你,我希望你有一个美好的一天 没问题老兄! :) 乐于助人。【参考方案2】:您遇到的问题是,在 C++ 中调用函数时,它必须采用 foo(bar) 格式,其中 bar 代表函数的输入参数。例如,在您的原型中,您使用 5 个参数定义半径,但是当您稍后调用它时,它没有要评估的参数。也许对 C++ 有更多了解的人可以回答为什么在 cout 中调用 radius 会导致返回值 1 但是我相信这个值是程序尝试执行调用时返回的退出代码。在这种情况下,调用 radius 或任何函数的正确方法是为其提供适当数量的参数,这些参数被定义为接收,即 radius(x1, y1, x2, y2)。
我还注意到一些事情,area() 中计算圆面积的语句不正确,您希望 PI*pow(radius,2) 因为它当前的方式是首先计算 PI*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
【讨论】:
非常感谢您帮助我并指出一些问题。我已经通读了这些 cmets,并且也在积极研究我的代码。以确保我在实践中完全掌握这些概念。我非常感谢你们所有人。谢谢 :] 祝你有美好的一天!【参考方案3】:所以我能够对其进行返工,并且我对将函数调用付诸实践有了更多的了解!我修正了数学并在 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++ 中调用函数,代码编译数学是错误的的主要内容,如果未能解决你的问题,请参考以下文章
在Android环境下编译调用c++出现以下错误,大神们这是啥原因呀??我已经配置NDK了。