C++ 五角大楼c+项目+;上课
好吧,我需要创建一个五角大楼项目,我有点困惑。 我的意思是,它工作得很好,但我缺少一个类,其中必须包含一些代码 任务是: 编写一个程序并称之为PentagonProject。除了包含main()函数的文件之外。应该还有另外两个类,每个类都在自己的一对文件(.h;.cpp)中。其中一个类应该是类MenuClass,另一个应该称为五角大楼。main()中应该有一个调用MenuClass成员函数DisplayMenu等的用户循环。菜单应允许用户指定和更改五角大楼类实例(也称为对象)的尺寸。五边形应为规则形(所有边均相同)。五角大楼有五面。我们将用s表示边的长度。那么五角大楼的周长P和面积A如下所示 方程式:C++ 五角大楼c+项目+;上课,c++,C++,好吧,我需要创建一个五角大楼项目,我有点困惑。 我的意思是,它工作得很好,但我缺少一个类,其中必须包含一些代码 任务是: 编写一个程序并称之为PentagonProject。除了包含main()函数的文件之外。应该还有另外两个类,每个类都在自己的一对文件(.h;.cpp)中。其中一个类应该是类MenuClass,另一个应该称为五角大楼。main()中应该有一个调用MenuClass成员函数DisplayMenu等的用户循环。菜单应允许用户指定和更改五角大楼类实例(也称为对象)的尺寸。五边形应为规
P = 5s
A = s^2 sqrt ( of 25 + 10 sqrt (5) ) / (over) 4
菜单应允许用户计算五角大楼类实例的周长和面积。你的程序应该有一个五角大楼对象
这么说吧,这个程序运行得很好,但我没有得到的是,我到底应该在五角大楼的课堂上编写什么以及如何编写代码 我的五边形项目(或主要项目):
//PentagonProject.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括“MenuClass.h”
使用名称空间std;
int main()
{
菜单;
做
{
menu.Display();
menu.QueryUser();
menu.ProcessCommand();
}
while(menu.Continue());
返回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;
}
}
// ========================
//==================
#包括“StdAfx.h”
#包括
#包括
// ==================
// ================
//类包含
// ================
#包括“MenuClass.h”
#包括“Math.h”
// ================
// ====================
使用名称空间std;
// ====================
// ============
//建设者
// ============
// ===================
//默认构造函数
// ====================
菜单::菜单(无效)
{
userMenuSelection=退出;
}//构造函数菜单
// =====================
菜单::~菜单(无效)
{
cout您不应该在ProcessCommand()
函数中进行计算
而是创建一个五角大楼对象(下面的骨架代码)并使用它们。你可能需要改变它以满足你的确切需要,比如如果你不能使用显示功能,就访问这些值
拥有:
class五角大楼{
私人:
内侧;
内部区域;
周长;
公众:
五角大楼(){side=area=permiture=0;}
五角大楼(INTA){side=a;Area();permiture();}
空边(ints){Side=s;面积();周长();}
空区(){
//弹劾
面积=0;
std::cout“五角大楼项目”(来自“秘密开发”和“树下生活”的导演)-现在有了很大改进的视觉评论!我刚刚回答了(在OP的辩护中,这是一种浪费,任务将业务逻辑与UI混为一谈。noobs会模糊界限,这是有道理的。+1思考好的。。我在主功能中如何称呼它?(PentagonProject)。你能帮我吗?PS:谢谢你的帮助!你可以看到:
// ==================
#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;
}
}
// ========================
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;
}