簡體   English   中英

切換燈的程序? 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

for( int i=0; ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM