尝试创建包含简单方程的函数

Trying to create functions with simple equations inside

本文关键字:方程 函数 简单 包含 创建      更新时间:2023-10-16

我是C++编码新手,正在为学校做作业,但我被困住了,似乎无法弄清楚我做错了什么。赋值要求我们使用函数来执行一些涉及"直圆圆柱体"的方程。现在,我能够制作一个代码来执行它应该做的事情,但它不一定使用函数来计算答案。我当前的代码如下:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double  r = 0,
h = 0,
TSurfaceArea = 0,
LSurfaceArea = 0,
Volume = 0;
const double    PI = 3.cylinder   
cout << "Welcome to the right-circular clyinder area and volume calculator, this program will calculate three things: "
"nThe first, is the Total Surface Area."
"nThen we will see the Lateral Surface Area."
"nAnd Finally we will see the volume." << endl;
cout << "nPlease enter a value for the radius: ";
cin >> r;
cout << "nThank you! nnNow please enter a value for the height: ";
cin >> h;
TSurfaceArea = 2 * PI*r*(r + h);
LSurfaceArea = 2 * PI*r*h;
Volume = PI * r*h;
cout << "Thank you for your input! nnSo here are your results based on a radius of " << r << " and a height of " << h << ":" << endl;
cout << "nTotal Surface Area is " << TSurfaceArea << "nLateral Surface Area is " << LSurfaceArea << "nVolume is " << Volume << endl;
system("pause");
}

所以就像我提到的,这有效,但首先必须将值指定为"0",然后方程必须在代码中计算解决方案,直到用户输入"r"和"h"的数字。我确信有更好的方法来做到这一点,然后我正在研究如何将方程变成函数,我可以稍后调用,而不必每次我想用新值计算时都放置一个完整的方程,这就是讲师正在寻找的,但我一定做错了创建函数,因为我似乎找不到有效的方法。我试过这个无济于事:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double    PI = 3.14159;
double  r = 0,
h = 0,
TSurfaceArea() {
double tsa = 2 * PI*r*(r + h);
return tsa;
}, 
LSurfaceArea() {
double lsa = 2 * PI*r*h;
return lsa;
},
Volume() {
double v = PI * r*h;
return v;
};
cout << "Welcome to the right-circular clyinder area and volume calculator, this program will calculate three things: "
"nThe first, is the Total Surface Area."
"nThen we will see the Lateral Surface Area."
"nAnd Finally we will see the volume." << endl;
cout << "nPlease enter a value for the radius: ";
cin >> r;
cout << "nThank you! nnNow please enter a value for the height: ";
cin >> h;
cout << "Thank you for your input! nnSo here are your results based on a radius of " << r << " and a height of " << h << ":" << endl;
cout << "nTotal Surface Area is " << TSurfaceArea << "nLateral Surface Area is " << LSurfaceArea << "nVolume is " << Volume << endl;
system("pause");
}

现在我知道这应该相对容易解决,但我觉得我只是错过了一些东西,希望有人可以帮助我!提前感谢!

这里没有我能看到的物体。 你用C++作为"更好的C"。

我已经很久没有写过C或C++了,但我认为你想要更多这样的东西。 我把肉从主要内容中取出来强调我的观点:

#include <iostream>
#include <cmath>
using namespace std;
static const double    PI = 3.14159;  // surely you can do better than six digits
double topSurfaceArea(double r, double h) { return 2.0*PI*r*(r+h); } 
double lateralSurfaceArea(double r, double h) { return 2.0*PI*r*h; }
double volume(double r, double h) { return PI*r*r*h; }  // your formula was clearly wrong.
int main() { // removed body for simplicity }

lambda 可能是最接近你想要的,例如矩形

double width,height;
auto area = [&](){ return width*height; };
width = 5;
height = 10;
std::cout << area(); 

会打印50.但是,您最好先了解函数...

double area(double width,double height) {
return width * height;
}
int main() {
std::cout << area(5.0,10.0);
}

请注意,该函数声明了一个返回类型(double(,它接受一些参数(doubledouble(,并且在定义后既没有,也没有;