带有类的 c++ 中的五角大楼项目
Pentagon Project in c++ with classes
好的,所以我需要创建一个五角大楼项目,我在这里有点困惑。我的意思是,它工作得很好,但我缺少一个必须包含一些代码的类。
分配是:
编写一个程序并称之为五角大楼项目。除了包含 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;
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 错误-我无法在VS2019中打开新的Qt项目
- 带有类的 c++ 中的五角大楼项目