• 목록
  • 아래로
  • 위로
  • 쓰기
  • 검색

C언어 예제4(Ver.2)

너에게제공 너에게제공
374 0 1

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

댓글 쓰기
권한이 없습니다. 로그인
에디터 모드

신고

"님의 댓글"

이 댓글을 신고하시겠습니까?

댓글 삭제

"님의 댓글"

이 댓글을 삭제하시겠습니까?

공유

퍼머링크