带有类的 c++ 中的五角大楼项目

Pentagon Project in c++ with classes

本文关键字:五角大楼 项目 c++      更新时间:2023-10-16

好的,所以我需要创建一个五角大楼项目,我在这里有点困惑。我的意思是,它工作得很好,但我缺少一个必须包含一些代码的类。

分配是:

编写一个程序并称之为五角大楼项目。除了包含 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;
}

这是我的菜单类的样子:

//  ==================
#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;
    }
}
// ========================

所以..你怎么能看到程序在没有五角大楼类的情况下工作......我的问题是我应该怎么做才能让这个项目同时与两个类一起工作

您不应该在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;

}