製作一個class,擁有兩個功能1、輸入合理成績 2 計算並輸出平均
Input:
輸入合理成績n(0<=n<=100)
若分數不合理則continue
輸入-1時結束
Output:
輸出平均
------
#include <iostream>
using namespace std;
class my_score
{
public:
my_score()
{
cnt = 0;
sum = 0;
}
void input_score(float n)
{
static float tem_cnt;
tem_cnt++;
cnt = tem_cnt;
score = n;
sum += score;
}
void CalculateAndPrint()
{
float ave = 0;
if(cnt)ave = sum/cnt;
cout<<ave<<endl;
}
private:
float score;
float sum,cnt;
};
int main()
{
float n;
my_score obj01;
for(;cin>>n;)
{
if(n==-1)break;
if(n<0||n>100)continue;
obj01.input_score(n);
}
obj01.CalculateAndPrint();
return 0;
}
留言列表