C언어 정수 5개를 입력받아 선택정렬 (ver.2)
너에게제공
566 0 0
#include <iostream>
using namespace std;
class SortNumbers{
private :
int size;
int *num;
int *origin;
public :
SortNumbers();
void selectionSort();
void printNumber();
~SortNumbers(){ delete num,origin; }
};
SortNumbers :: SortNumbers(){
cout << "몇개의 정수? "; cin >> size;
num = new int[size];
origin = new int[size];
cout << size << "개의 정수를 입력하시오." << endl;
for(int i =0; i<size; i++){
cin >> num[i];
origin[i] = num[i];
}
}
void SortNumbers::selectionSort()
{
int min,temp;
for(int j=0 ; j<size - 1; j++)
{
min = j;
for(int k=j+1; k<size;k++)
if(num[k] < num[min])
min = k ;
if (j != min)
{
temp = num[min];
num[min] = num[j];
num[j] = temp;
}
}
}
void SortNumbers :: printNumber(){
cout << "입력한 숫자 : ";
for(int i =0; i<size; i++){ cout << origin[i] << " ";}
cout << endl;
cout << "선택 정렬한 결과 : ";
for(int j =0; j<size; j++){ cout << num[j] << " ";}
cout << endl;
}
int main(){
SortNumbers myNum;
myNum.selectionSort();
myNum.printNumber();
return 0;
}
using namespace std;
class SortNumbers{
private :
int size;
int *num;
int *origin;
public :
SortNumbers();
void selectionSort();
void printNumber();
~SortNumbers(){ delete num,origin; }
};
SortNumbers :: SortNumbers(){
cout << "몇개의 정수? "; cin >> size;
num = new int[size];
origin = new int[size];
cout << size << "개의 정수를 입력하시오." << endl;
for(int i =0; i<size; i++){
cin >> num[i];
origin[i] = num[i];
}
}
void SortNumbers::selectionSort()
{
int min,temp;
for(int j=0 ; j<size - 1; j++)
{
min = j;
for(int k=j+1; k<size;k++)
if(num[k] < num[min])
min = k ;
if (j != min)
{
temp = num[min];
num[min] = num[j];
num[j] = temp;
}
}
}
void SortNumbers :: printNumber(){
cout << "입력한 숫자 : ";
for(int i =0; i<size; i++){ cout << origin[i] << " ";}
cout << endl;
cout << "선택 정렬한 결과 : ";
for(int j =0; j<size; j++){ cout << num[j] << " ";}
cout << endl;
}
int main(){
SortNumbers myNum;
myNum.selectionSort();
myNum.printNumber();
return 0;
}