切换灯光的程序?c++
program that toggle lights? c++
我需要你的帮助!我正在努力使这个程序工作,以解决在评论块中的谜题!
这是程序和我所处的位置。我想说的是,我得到了大部分,但我被困在了几个函数中。尤其是主要功能,我不知道该怎么办:\
// Purpose: This program solves the following puzzle outlined in
// the following steps:
//
// 1. Start with a long hallway full of lights that are
// each controlled by respective toggle switches.
// 2. All lights are initially off.
// 3. Start at the *first* light and run to the end of the
// hallway toggling *every* light.
// 4. Start at the *second* light and run to the end of the
// hallway toggling every *second* light.
// 5. Start at the *third* light and run to the end of the
// hallway toggling every *third* light.
// .
// .
// .
// 6. Toggle the *last* light.
// 7. Which lights are on, and which lights are off? You
// might be able to spot a mathematical pattern!
//
// We will use a Light class and a Hallway class to model
// the solution to this puzzle. The latter will contain an
// array of the former.
//
// Input: The number of lights in the hallway as an integer read
// in from the standard input stream. BE CAREFUL! The
// bigger the number you enter (i.e., the longer the
// hallway), the slower your program will run (why?).
//
// Output: The status of all of the lights in a hallway (on or off).
//---------------------------------------------------------------------
#include <iostream>
using namespace std;
const int MAX_LIGHTS = 1000;
void TurnOff();
//---------------------------------------------------------------------
// A simple class to model the behavior of a light bulb with a toggle
// switch.
//---------------------------------------------------------------------
class Light
{
private:
bool isOn;
public:
//------------------------------------------------------------------
// Default constructor. Creates a new light that is initially off.
// Params: (none)
//------------------------------------------------------------------
Light()
{
isOn = false;
}
//------------------------------------------------------------------
// Returns true if the light is on and false otherwise.
// Params: (none)
//------------------------------------------------------------------
bool IsOn() const
{
return isOn == true;
}
//------------------------------------------------------------------
// Turns the light off!
// Params: (none)
//------------------------------------------------------------------
void TurnOff()
{
isOn != true;
}
//------------------------------------------------------------------
// Turns the light on!
// Params: (none)
//------------------------------------------------------------------
void TurnOn()
{
isOn = true;
}
//------------------------------------------------------------------
// This function toggles the light from on to off and likewise
// on to off.
// Params: (none)
//------------------------------------------------------------------
void Toggle()
{
if ( IsOn())
TurnOn();
else
TurnOff();
}
//------------------------------------------------------------------
// Outputs a minimal textual representation of the light to the
// standard output.
// Params: (none)
//------------------------------------------------------------------
void Write() const
{
if (IsOn())
cout << "on";
else
cout << "off";
}
};
//---------------------------------------------------------------------
// A simple class to model a long hallway full of lights having toggle
// switches.
//---------------------------------------------------------------------
class Hallway
{
private:
int numLights;
Light lights[MAX_LIGHTS];
public:
//------------------------------------------------------------------
// Default constructor.
// Params: (none)
//------------------------------------------------------------------
Hallway()
{
numLights = 0;
}
//------------------------------------------------------------------
// This function models the actions of a person starting at the
// k-th light in the hallway and running to the end of the hallway
// toggling every k-th light along the way. It is very important
// that the loop-control variable i be initialized to k - 1!
// Valid range for k: 1 <= k <= numLights.
// Params: (in)
//------------------------------------------------------------------
void ToggleEveryKthLight(int k)
{
for (int i = k - 1; i < numLights; i += k)
lights[i].Toggle();
}
//------------------------------------------------------------------
// Reads in a hallway from the standard input. All of the lights
// in the hallway are initially in the off state.
// Params: (none)
//------------------------------------------------------------------
void Read()
{
cin >> numLights;
for ( int i; i < numLights; i++ )
TurnOff();
}
//------------------------------------------------------------------
// Displays the status of all of the lights in the hallway in a big
// list.
// Params: (none)
//------------------------------------------------------------------
void Write() const
{
for ( int i=0 ; i < MAX_LIGHTS; i++)
Write();
}
};
int main()
{
int hallway;
cin >> hallway;
// This is the loop that runs through all of the steps as outlined
// in the comment block at the top of this file. Start by toggling
// every light, then every other light, then every third light, etc.
for (int k = 1; k <= MAX_LIGHTS; k++)
{
hallway = MAX_LIGHTS;
}
//------------------------------------------------------------------
// Displaying the hallway.
//------------------------------------------------------------------
return 0;
}
将TurnOff()
替换为:
void TurnOff()
{
isOn = false;
}
或者,更简单:
void Toggle()
{
isOn = ! isOn;
}
在Read()
中,您没有初始化i
变量。将for
的开头替换为:
for( int i=0; ...
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址