问题
Soo.. I'm writing a pentagon project for my c++ class, and to be honest I'm not really doing well right now due job and other classes. So.. we need to make a pentagon program that will have Pentagon class and Menu Class. I managed working Menu Class but I'm not sure how to work with Pentagon class. anyways, what I currently need is - to make a right equation with square roots.
Equation for finding a Area of pentagon is:
A = s^2 sqrt ( of 25 + 10 sqrt (5) ) / (over) 4
So how do I do that? This is what I currently have in my Menu Class:
// ==================
#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;
}
}
// ========================
Please help!
回答1:
I take it that you're just confused about translating math syntax into code.
Your equation for area:
s^2*sqrt(25+10(sqrt(5)))/4
Goes like this in C++:
double area = s * s * sqrt(25.0 + 10.0 * sqrt(5.0)) / 4.0;
After that, you have an error:
return area;
The ProcessCommand
function is void
, which means you can't return a value. It wouldn't make sense to do so anyway. Perhaps you want to output it with std::cout
instead.
来源:https://stackoverflow.com/questions/19530225/pentagon-project-in-c