如此..我正在为我的c ++类编写一个pentagon项目,老实说,我现在由于应聘的工作和其他类的表现不佳。所以..我们需要制作一个具有五角大楼类和菜单类的五边形程序。我管理过菜单类,但是不确定如何使用五角大楼类。无论如何,我目前需要的是-用平方根建立一个正确的方程式。
查找五边形面积的公式为:
A = s ^ 2 sqrt(of 25 + 10 sqrt(5))/(over)4
那我该怎么做呢?这是我当前在菜单类中拥有的:
// ==================
#include "StdAfx.h"
#include <string>
#include <iostream>
#include <cmath>
// ==================
// ================
// Class Inclusions
// ================
#include "MenuClass.h"
#include "Math.h"
// ================
// ====================
using namespace std;
// ====================
// ============
// Constructors
// ============
// ===================
// Default Constructor
// ====================
Menu::Menu( void ) {
userMenuSelection = Quit;
} // Constructor Menu
// =====================
Menu::~Menu( void ) {
cout << "====================================" << endl;
} // Destructor ~Menu
// =====================
// ==============================
// Accessor Member-Function Get()
// ==========================
MenuChoices Menu::Get( ) {
return userMenuSelection;
} // Accessor Method Get
// ========================
// =============================
// Mutator Member-Function Set()
// ========================================
void Menu::Set( MenuChoices newValue ) {
userMenuSelection = newValue;
} // Mutator Method Set
// =======================
// ==========================
// Member-Function Display( )
// ==========================
void Menu::Display( ) {
cout << "======================================" << endl;
cout << " MENU SELECTION " << endl;
cout << "======================================" << endl;
cout << "1: Calculate the Perimeter of Pentagon" << endl;
cout << "2: Calculate the Area of Pentagon" << endl;
cout << "3: Quit" << endl;
cout << "======================================" << endl;
cout << endl;
} // Member-Function Display
// ============================
// =========================
// Member-Function QueryUser
// =========================
void Menu::QueryUser( ) {
int selection;
cout << "Enter Menu Selection: ";
cin >> selection;
switch (selection){
case 1: userMenuSelection = Perimeter;
break;
case 2: userMenuSelection = Area;
break;
case 3: userMenuSelection = Quit;
default: userMenuSelection = Quit;
} // switch
// ===========
cout << endl;
} // Method QueryUser()
// =======================
// =================
// Method Continue()
// ========================
bool Menu::Continue( ) {
return userMenuSelection != Quit;
} // Method Continue
// ====================
// ==============================
// Member-Function ProcessCommand
// ==============================
void Menu::ProcessCommand( ) {
int numberA; // Length of Sides
int numberB; // Area
if (userMenuSelection == Quit ){
cout << "Thank you for using this type of program. Have a nice day!" << endl;
}
else if (userMenuSelection != Quit) {
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
// ==============================
switch ( userMenuSelection ) {
case Perimeter:
cout << "Perimeter = " << (5 * numberA) << endl;
break;
case Area:
// Equation of Area:
// s^2*sqrt(25+10(sqrt(5)))/4
// 10*sqrt(5) = 22.36067977
double area;
area = sqrt(numberA(1+1));
return area;
///cout << "Area = " << ((numberA*numberA) * (5 + 22.36067977)) / 4 << endl;
//int param;
//int result;
//param = 1;
//cout << result = sqrt (param) << endl;
break;
default: cout << "Warning: error state encountered." << endl;
}
cout << endl;
}
}
// ========================
请帮忙!
最佳答案
我认为您只是对将数学语法转换为代码感到困惑。
您的面积方程:
s^2*sqrt(25+10(sqrt(5)))/4
在C ++中是这样的:
double area = s * s * sqrt(25.0 + 10.0 * sqrt(5.0)) / 4.0;
之后,您将遇到错误:
return area;
ProcessCommand
函数是void
,这意味着您无法返回值。无论如何,这样做是没有意义的。也许您想用std::cout
输出它。关于c++ - C++中的五角大楼项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19530225/