尝试创建包含简单方程的函数
Trying to create functions with simple equations inside
我是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
(,它接受一些参数(double
和double
(,并且在定义后既没有,
也没有;
。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 尝试创建包含简单方程的函数
- 我必须重新计算每个函数的私有变量方程吗?
- 难以将函数转换为单个方程(constexpr)