2011-11-14 125 views
0

嗨我正在爲一個氣象站的課程,要求用戶輸入變量,並將小時數傳遞給數組:計算平均值,高點和低點的值。我得到它的工作,但想要使數組[元素]私有。是否有可能做到這一點?C++設置一個類中的數組元素的數量

這是我的代碼到目前爲止。預先感謝您的任何幫助。

布賴恩

#include <iostream> 
#include <iomanip> 

using namespace std; 

class WeatherStation 
{ 
public: 
    WeatherStation(); 
    void GetATemperatures(int[], int); 
    void DisplayATemperatures(int[], int); 
    void arrayCalcs(int[], int); 

private: 
    static const int aTemps = 24; 
    static const int atemps[aTemps]; 
}; 

WeatherStation::WeatherStation() 
{ 
    int atemps[aTemps]; 
} 

void WeatherStation::GetATemperatures(int atemps[], int aTemps) 
{ 
    for (int i = 0; i < aTemps; i++) 
    { 
     cout << "Please enter the temperature for " << i << ":00 "; 

     while(true) 
     { 
      cin >> atemps[i]; 

      if(atemps[i] >= -50 && atemps[i] <= 130) 
      { 
       break; 
      } else { 
       cout << "This temperature is not valid\n"; 
       cout << "Please enter a temperature between -50 and 130 degrees F \n"; 
       cout << "Please enter a new temperature: "; 
      } 
     } 
    } 
} 

void WeatherStation::DisplayATemperatures(int atemps[], int aTemps) 
{ 
    cout << setw (5) << "Hour" << setw(24)<< "Temperature \n"; 
    cout << "\n"; 

    for (int k = 0; k < aTemps; k++) 
    { 
     cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl; 
    } 

    cout <<"\n"; 
} 

void WeatherStation::arrayCalcs(int atemps[], int aTemps) 
{ 
    int sumA = 0; 

    double average = 0.0; 
    int minA = atemps[0]; 

    int maxA = atemps[0]; 

    int lowest = 0; 
    int highest = 0; 

    //Sum of the AM temps 
    for (int kk = 0; kk < aTemps; kk++) 
    { 
     sumA = sumA + atemps[kk]; 
    } 

    //calculation for average 

    average = sumA/aTemps; 

    //Figuring out the Min and Max AM temps 

    for (int MM = 0; MM < aTemps; MM++) 
    { 
     if(minA > atemps[MM]) 
     { 
      minA = atemps[MM]; 
     } 
     else if(maxA < atemps[MM]) 
     { 
      maxA = atemps[MM]; 
     } 

     lowest = minA; 
     highest = maxA; 
    } 


    //Display of the Calculation results 
    cout << "This is the average of todays temperatures: " << average <<endl; 
    cout <<endl; 
    cout << "Todays High temperature is: " << highest <<endl; 
    cout <<endl; 
    cout << "Todays Low temperature is: " << lowest <<endl; 
} 

int main() 
{ 
    cout <<"Welcome to the weather station.\n"; 
    cout <<"Please enter Ferenheit temperatures for calculations: \n"; 

    WeatherStation alpha; 
    alpha.GetATemperatures(atemps, aTemps); 
    alpha.DisplayATemperatures(temps, Temps); 
    alpha.arrayCalcs(temps,Temps); 

    cout << "\n"; 

    system("pause"); 
    return 0; 
} 
+2

你的代碼中有很多本地/全局命名衝突。當本地和全局名稱相同時,C++編譯器將始終使用本地。我不確定你是否知道這一點。無論如何,這只是一個觀察。你的問題不是很清楚,你能否澄清? – littleadv

+0

我會避免使用類似'aTemps'和'atemps'的變量名。 「aTemps」的一個更好的名字應該是「size」或「length」。 –

+0

爲了澄清,所有的函數都使用array- atemps [],並將該數組中的元素數量定義爲aTemps,等於24. – Brian

回答

1

1)是對陣列atemps[]?如果是這樣,它已經是私人的...有什麼問題?

2)爲什麼你的數組類是靜態的?不要這樣做沒有很好的理由(因爲這似乎是一項家庭作業,我幾乎肯定你沒有一個該死的好理由)。

3)你的構造函數中有無用的代碼行 - 這是函數中的唯一行。

4)你的教授不會接受你命名變量atempsaTemps - 如果他們忽視它,我會非常關心你接受的教育質量。這並不是說變量名稱本身就是一個大問題,而是你將它們命名得如此相似,因爲如果它是在真實代碼中發生的話,這是維護惡夢的祕訣。

編輯 - 基於我們的評論聊天,這裏是我的建議。我沒有試圖編譯這個,我不認爲這是編寫你的程序的最好的(甚至是建議的)方式......我的建議僅限於將數據留在你的對象中(以一種有空間的方式超出這個問題/討論的增長)。

#include <iostream> 
#include <iomanip> 

using namespace std; 

class WeatherStation 
{ 
public: 
    WeatherStation(); 
    void GetATemperatures(); 
    void DisplayATemperatures(); 
    void arrayCalcs(); 

private: 
    static const int aTemps = 24; 
    int atemps[aTemps]; 
}; 

WeatherStation::WeatherStation() 
{ 
} 

void WeatherStation::GetATemperatures() 
{ 
    for (int i = 0; i < aTemps; i++) 
    { 
     cout << "Please enter the temperature for " << i << ":00 "; 

     while(true) 
     { 
      cin >> atemps[i]; 

      if(atemps[i] >= -50 && atemps[i] <= 130) 
      { 
       break; 
      } else { 
       cout << "This temperature is not valid\n"; 
       cout << "Please enter a temperature between -50 and 130 degrees F \n"; 
       cout << "Please enter a new temperature: "; 
      } 
     } 
    } 
} 

void WeatherStation::DisplayATemperatures() 
{ 
    cout << setw (5) << "Hour" << setw(24)<< "Temperature \n"; 
    cout << "\n"; 

    for (int k = 0; k < aTemps; k++) 
    { 
     cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl; 
    } 

    cout <<"\n"; 
} 

void WeatherStation::arrayCalcs() 
{ 
    int sumA = 0; 

    double average = 0.0; 
    int minA = atemps[0]; 

    int maxA = atemps[0]; 

    int lowest = 0; 
    int highest = 0; 

    //Sum of the AM temps 
    for (int kk = 0; kk < aTemps; kk++) 
    { 
     sumA = sumA + atemps[kk]; 
    } 

    //calculation for average 

    average = sumA/aTemps; 

    //Figuring out the Min and Max AM temps 

    for (int MM = 0; MM < aTemps; MM++) 
    { 
     if(minA > atemps[MM]) 
     { 
      minA = atemps[MM]; 
     } 
     else if(maxA < atemps[MM]) 
     { 
      maxA = atemps[MM]; 
     } 

     lowest = minA; 
     highest = maxA; 
    } 


    //Display of the Calculation results 
    cout << "This is the average of todays temperatures: " << average <<endl; 
    cout <<endl; 
    cout << "Todays High temperature is: " << highest <<endl; 
    cout <<endl; 
    cout << "Todays Low temperature is: " << lowest <<endl; 
} 

int main() 
{ 
    cout <<"Welcome to the weather station.\n"; 
    cout <<"Please enter Ferenheit temperatures for calculations: \n"; 

    WeatherStation alpha; 
    alpha.GetATemperatures(); 
    alpha.DisplayATemperatures(); 
    alpha.arrayCalcs(); 

    cout << "\n"; 

    system("pause"); 
    return 0; 
} 
+0

謝謝你,我一定會清理命名變量。成員函數需要數組[]是atemp []和數組元素,即參數[aTemp]。如何傳遞這些信息並調用main中的對象的函數以使程序運行? – Brian

+0

@Brian - 我不確定我是否理解,因爲您的課程已經包含數據。通常在C++中,main()不會直接訪問那些數據;使用對象的一個​​原因是將其從外部代碼中隱藏起來。但是,如果main()實際擁有該數組並將其提供給該對象,則可以像定義GetATemperatures()那樣執行此操作 - 但是您不使用像這樣的對象變量,而是使用調用者(電源)。如果您的類最終會有更多的函數來訪問數據,您可以在本地複製數據或將指針保存到數組(及其長度)。 – mah

+0

好吧,我現在看到的代碼比以前更多了,而且更清楚一點。如果你從你的類中刪除了atemps和aTemps,並將它們放在main()中,你的代碼將會工作。或者,您可以將它們留在類中,並將它們從函數參數中刪除(以僅使用類元素) - 這將是面向對象編程的更期望的方法。 – mah

相關問題