[英]C++ program to find volume of a cone, volume of a sphere, the area of octagon, and the distance between two points
Here is my jacked up code rewritten a few times just not working.这是我的代码重写了几次只是不起作用。 Should be simple just not getting it thanks.应该很简单只是没有得到它谢谢。 C++ C++
For the different formulas, will need to do some different function calculations these either finding the square root of a value, finding a number raised to a power or both.对于不同的公式,将需要进行一些不同的函数计算,这些计算要么是找到值的平方根,要么是找到一个数字的幂,或者两者兼而有之。
Must use the cmath library functions "pow" and/or "sqrt" to do the calculations.必须使用 cmath 库函数“pow”和/或“sqrt”来进行计算。 Along with this you will use acos(-1) for an estimation on .与此同时,您将使用 acos(-1) 对 进行估计。 This value must be saved using a constant variable.必须使用常量变量保存该值。
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#define PI 3.1415926 //PI
using namespace std;
int option;
char countine = 'y';
double coneVolume;
double sphereVolume;
double octagonArea;
double pointDistance;
//Calculations functions
void cone(double r, double h)
{
cout <<"Volume = " << (1/3) * PI * pow ( r, 2 ) * h;
}
void sphere (double r)
{
cout << "Volume + " << (4/3) * pow ( r, 3 ) * PI;
}
void octagon (double s)
{
//cout << "Area = " << 2 (1 + sqrt (2))- pow ( s, 2);
}
void point (double x1, double x2, double y1, double y2)
{
//cout << "distance = " << ((sqrt pow((x2-x1),2) + pow((y2-y1),2);
}
int main ()
//The menu choices will be the following four calculations:
//Find the volume of a cone
//Find the volume of a sphere
//Find the area of octagon
//Find the distance between two points
{
char countinue = 'y';
while (countinue = 'y')
cout << "select program to run " <<endl;
// volume of cone
cout << "1.Find the volume of a cone "<<endl;
// volume of sphere
cout << "2.Find the volume of a sphere" <<endl;
// area of octagon
cout << "3.Find the Area of a octagon " << endl;
// distance of two points
cout << "4.Find the distance between two points " << endl;
cout << " Enter option ? ";
cin >> option;
switch (option)
{
// volume of cone
case 1:
//using namespace std;
option = true;
double r,h;
cout<<"\nEnter cone radius: ";
cin>>r;
cout<<"\nEnter cone height: ";
cin>>h;
cone = (r, h);
cout<<"\nThe volume of the cone is: "<<setprecision(2)
<<fixed<<cone<<endl<<endl;
break;
//volume sphere
case 2:
option = true;
double r;
cout<<"\nEnter sphere radius: ";
cin>>r;
sphere = (r);
cout<<"\nThe volume of the sphere is: "<<setprecision(2)
<<fixed<<sphere<<endl<<endl;
break;
//area octagon
case 3:
option = true;
double octagon;
cout<<"\nEnter the length of one side: ";
cin>>s;
octagon = octagonArea (sideLength);
cout<<"\nThe area of the octagon is: "<<setprecision(2)
<<fixed<<octagon<<endl<<endl;
break;
//distance
case 4:
option = true;
double distance;
cout<<"\nEnter the first x and y coordinates: ";
cin>>x1>>y1;
cout<<"\nEnter the second x and y coordinates: ";
cin>>x2>>y2;
distance = pointDistance (x1, x2, y1, y2);
cout<<"\nThe distance between points ("<<x1<<", "<<y1<<") and ("
<<x2<<", "<<y2<<") is: "<<setprecision(4)<<fixed<<distance<<endl<<endl;
break;
}
return 0;
}
The above comments were correct of course.上面的评论当然是正确的。
In addition, I found other issues, for example:此外,我还发现了其他问题,例如:
Here is the code I obtained, seems to work.这是我获得的代码,似乎有效。 Please don't simply copy it but try to understand the modifications.请不要简单地复制它,而是尝试理解修改。
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
static double const PI = acos(-1);
using namespace std;
//Calculations functions
double cone(double r, double h)
{
return (1.0/3.0) * PI * pow ( r, 2) * h;
}
double sphere (double r)
{
return (4.0/3.0) * pow ( r, 3 ) * PI;
}
double octagon (double s)
{
return 2 * (1 + sqrt (2)) * pow (s, 2);
}
double pointDistance (double x1, double x2, double y1, double y2)
{
return sqrt(pow((x2-x1),2) + pow((y2-y1),2));
}
int main ()
//The menu choices will be the following four calculations:
//Find the volume of a cone
//Find the volume of a sphere
//Find the area of octagon
//Find the distance between two points
{
char countine = 'y';
while (countine == 'y') {
int option;
cout << "select program to run " <<endl;
// volume of cone
cout << "1.Find the volume of a cone "<<endl;
// volume of sphere
cout << "2.Find the volume of a sphere" <<endl;
// area of octagon
cout << "3.Find the Area of a octagon " << endl;
// distance of two points
cout << "4.Find the distance between two points " << endl;
cout << " Enter option ? ";
cin >> option;
switch (option)
{
// volume of cone
case 1:
{
double r,h;
cout<<"\nEnter cone radius: ";
cin>>r;
cout<<"\nEnter cone height: ";
cin>>h;
double volume = cone(r, h);
cout<<"\nThe volume of the cone is: "<<setprecision(2) <<fixed<<volume<<endl<<endl;
}
break;
//volume sphere
case 2:
{
double r;
cout<<"\nEnter sphere radius: ";
cin>>r;
double volume = sphere(r);
cout<<"\nThe volume of the sphere is: "<<setprecision(2) <<fixed << volume << endl << endl;
}
break;
//area octagon
case 3:
{
cout<<"\nEnter the length of one side: ";
double s;
cin>>s;
double area = octagon (s);
cout<<"\nThe area of the octagon is: "<< setprecision(2) <<fixed << area << endl << endl;
}
break;
//distance
case 4:
{
double x1, y1, x2, y2;
cout<<"\nEnter the first x and y coordinates: ";
cin>>x1>>y1;
cout<<"\nEnter the second x and y coordinates: ";
cin>>x2>>y2;
double distance = pointDistance (x1, x2, y1, y2);
cout<<"\nThe distance between points ("<<x1<<", "<<y1<<") and (" <<x2<<", "<<y2<<") is: "
<<setprecision(4) << fixed << distance <<endl<<endl;
}
break;
}
cout << "Do you want to continue?";
cin >> countine;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.