2011-11-14 131 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

相关问题