목록으로 2013.03.19. 11:49
C언어

예제4(Ver.2)

youtube

PersonAge ver_2.JPG : 예제4(Ver.2)

PersonAge ver_2.JPG

PersonAge ver_2.cpp

 

#include<iostream>
using namespace std;
class PersonAge
{
private:
        int age[5];
        static int cnt;
        static int tot;
        static float avg;
        static int a;
        static int high;
        static int low;
        static int same;
public:
        PersonAge()
        {

        for(int i=0; i<a; i++)
        {
                cout<<"나이 :";
                cin>>age[i];
                cnt++;
                tot=age[i]+tot;
        }

        }
        void clacAvg(){avg=(float)tot/a;}
        void print(){cout<<"총 "<<a<<"명의 평균 나이 : "<<avg<<endl;}
        void count();
};

int PersonAge::a=5; //a 대신 5를 해도 됨
int PersonAge::cnt=0;
int PersonAge::tot=0;
float PersonAge::avg=0;
int PersonAge::high=0;
int PersonAge::low=0;
int PersonAge::same=0;

void PersonAge::count()
{
        for(int i=0; i<a; i++)
        {
                if(age[i]>avg)
                {
                        high++;
                }
                else if(age[i]<avg)
                {
                        low++;
                }
                else
                        same++;
        }
        cout<<"평균 나이 보다 많은 사람 수 : "<<high<<endl;
        cout<<"평균 나이 보다 적은 사람 수 : "<<low<<endl;
        cout<<"평균 나이와 같은 사람 수 : "<<same<<endl;
}


int main()
{
        PersonAge p;
        p.clacAvg();
        p.print();
        p.count();

}

댓글 1

댓글 작성 권한이 없습니다. 로그인하기

NO
TITLE
섬네일
profile 너에게제공 | 09. 10 | 조회
2
| 11. 27 | 조회
1