본문 바로가기

과제모음

[C++]객체지향2

반응형

[[DynamicArray.h]]
#ifndef _DYNAMICARRAY_H_
#define _DYNAMICARRAY_H_

class DynamicArray{
private:
 int *IntAR;
 int _AR;
 int temp;
 int i,j;
public:
 void Alloc(int n);
 void Input();
 void Output();
 void Sort(int _sort);
 void Free();
}; 
#endif

[[main.cpp]]
#include <iostream>
#include "DynamicArray.h"
using std::cin; using std::cout;

int main()
{
 int n;
 cout << "정수형 배열 크기 입력 : ";
 cin >> n;

 DynamicArray da;
 da.Alloc(n);
 da.Input();

 cout << "\n[정렬 전 출력]\n";
 da.Output();

 da.Sort(1);

 cout << "\n[정렬 후 출력]\n";
 da.Output();

 da.Free();
 return 0;
}

[[DynamicArray.cpp]]
#include <iostream>
#include "DynamicArray.h"
using std::cin; using std::cout; using std::endl;

void DynamicArray::Alloc(int n)
{
 _AR = n;
 IntAR = new int [_AR];
}
void DynamicArray::Input()
{
 cout << "\n[정수 " << _AR << "개 입력]\n";
 for(i =0; i<_AR; i++)
 {
  cout << "입력 " << i+1 << " : ";
  cin >> IntAR[i];
 }
}

void DynamicArray::Output()
{
 for(i=0; i<_AR; i++)
 {
  cout << IntAR[i] << " ";
 }
 cout << endl;
}

void DynamicArray::Sort(int _sort)
{
 if(_sort == 1)
 {
  for(i=0; i<_AR; i++)
  {
   for(j=i+1; j<_AR; j++)
   {
    if(IntAR[i] > IntAR[j])
    {
     temp = IntAR[i];
     IntAR[i] = IntAR[j];
     IntAR[j] = temp;
    }
   }
  }
 }
 else if(_sort == 2)
 { 
  for(i=0; i<_AR; i++)
  {
   for(j=i+1; j<_AR; j++)
   {
    if(IntAR[i] > IntAR[j])
    {
     temp = IntAR[i];
     IntAR[i] = IntAR[j];
     IntAR[j] = temp;
    }
   }
  }
 }
}

void DynamicArray::Free()
{
 delete [] IntAR;
 
}


반응형

'과제모음' 카테고리의 다른 글

[C++]소멸자(Destructor)  (0) 2010.01.22
[C++]생성자(Constructor)  (0) 2010.01.22
[C++]객체지향(OOP)  (0) 2010.01.22
[C++]파일분할  (0) 2010.01.22
[C++]동적할당3  (0) 2010.01.22