2014-04-15 50 views
-2

我需要你的幫助!我正試圖讓這個程序能夠解決註釋塊中的難題!程序,切換燈? 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; 
} 
+0

'TurnOff'函數應該將'isOn'設置爲'false'。 –

回答

1

替換TurnOff()有:

void TurnOff() 
{ 
    isOn = false; 
} 

或者簡單:

void Toggle() 
{ 
    isOn = ! isOn; 
} 

Read()你沒有初始化變量i。將for的開頭替換爲:

for(int i=0; ...