切换灯光的程序?c++

program that toggle lights? c++

本文关键字:程序 c++      更新时间:2023-10-16

我需要你的帮助!我正在努力使这个程序工作,以解决在评论块中的谜题!

这是程序和我所处的位置。我想说的是,我得到了大部分,但我被困在了几个函数中。尤其是主要功能,我不知道该怎么办:\

// 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; ...