好的,所以我需要创建一个五角大楼项目,我在这里有点困惑。 我的意思是,它工作得很好,但我错过了一个必须有一些代码的类。
作业是:
编写程序并将其命名为PentagonProject。除了包含main()函数的文件。应该有另外两个类,每个类都在自己的文件对中(.h; .cpp)。其中一个类应该是类MenuClass,另一个应该叫做五角大楼。 main()中应该有一个用户循环,它调用MenuClass成员函数DisplayMenu等等。菜单应该允许用户指定和更改五角大楼类的实例的尺寸,也称为对象。五边形应为常规(所有边都相同)。五角大楼有五面。我们将用s表示边的长度。然后,五边形的周长P和面积A如下:
方程:
P = 5s
A = s^2 sqrt ( of 25 + 10 sqrt (5) ) / (over) 4
菜单应该允许用户计算五角大楼类的实例的周长和面积。你的程序应该有一个五角大楼对象。
所以,正如所说的那样,该程序运行良好,但我没有得到的是我在五角大楼类中编码的具体和准确性。
我的五角大楼项目(或主要):
// PentagonProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "MenuClass.h"
using namespace std;
int main()
{
Menu menu;
do
{
menu.Display();
menu.QueryUser();
menu.ProcessCommand();
}
while(menu.Continue());
return 0;
}
这就是我的MenuClass的样子:
// ==================
#include "StdAfx.h"
#include <string>
#include <iostream>
// ==================
// ================
// 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
double area; // 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:
cout << "Area = " << numberA * numberA * sqrt(25.0 + 10.0 * sqrt(5.0)) / 4.0;
break;
default:
cout << "Warning: error state encountered." << endl;
}
cout << endl;
}
}
// ========================
那么......你怎么能看到这个程序在没有五角大楼课程的情况下工作..我的问题是我该怎么办才能让这个项目一次同时用于这两个课程
答案 0 :(得分:1)
您不应在ProcessCommand()
函数中进行计算。
而是创建一个五边形对象(下面的骨架代码)并使用它们。您可能需要更改此选项以满足您的确切需求,例如,如果您无法使用显示功能,则可以访问这些值。
有:
class Pentagon {
private:
int side;
int area;
int perimeter;
public:
Pentagon() { side = area = perimeter = 0; }
Pentagon(int a) { side = a; Area(); Perimeter(); }
void Side(int s) { side = s; Area(); Perimeter(); }
void Area() {
//Impement
area = 0;
std::cout << "I Should implement area based on side of " << side << std::endl;
}
void Perimeter() {
//Implement
perimeter = 0;
std::cout << "I Should implement perimeter based on side of " << side << std::endl;
}
void Display() {
//Handle output
std::cout << "A pentagon of side " << side << " has area " << area << " and perimeter " << perimeter << std::endl;
}
int GetPerimeter() {
return perimeter;
}
};
要使用它们,您可以执行以下操作:
int main() {
Pentagon p;
int t = 5; //Simulating input
p.Side(t); //Just use this with all input.
p.Display(); //If you want some generic display all function
//or create and use accessors
std::cout << p.GetPerimeter() << std::endl;
}