C언어 학생 정보를 점수순으로 정렬
include <iostream>
#include <string>
using namespace std;
class student{
private:
string name;
int score;
public:
void setStudent(string n, int s)
{ name=n; score=s; }
void prnStudent()
{ cout<<name<<"t"<<score<<endl; }
string getName()
{ return name; }
int getScore()
{ return score; }
void setName(string n)
{ name = n; }
void setScore(int s)
{ score = s; }
};
void sortStudent(int k, student *s)
{
student *std=s;
int min;
int temp;
string str;
for(int i=0; i<k; i++)
{
min=i;
for(int j=i+1; j<k; j++)
{
if(std[j].getScore() < std[min].getScore())
min=j;
}
if(i!=min)
{
temp=std[i].getScore();
std[i].setScore(std[min].getScore());
std[min].setScore(temp);
str=std[i].getName();
std[i].setName(std[min].getName());
std[min].setName(str);
}
}
}
void main()
{
string name;
int score;
int a;
cout<<"몇 명이세요 : ";
cin>>a;
student *s = new student[a];
for(int i=0; i<a; i++)
{
cout<<i+1<<"번 째 분 성함이 어떻게되세요 : ";
cin>>name;
cout<<i+1<<"번 째 분 점수는 어떻게되세요 : ";
cin>>score;
(s+i)->setStudent(name,score);
}
for(i=0; i<a; i++)
{
(s+i)->prnStudent();
}
sortStudent(a,s);
cout<<endl<<endl;
for(i=0; i<a; i++)
{
(s+i)->prnStudent();
}
}
OR
class Student
{
private:
string name;
int score;
public:
void setStudent();
void printStudent();
string getName() { return name; }
void setName(string n) { name = n; }
int getScore() { return score; }
void setScore(int s) { score = s; }
};
void Student::setStudent()
{
cout << "이름 : " ;
cin >> name;
cout << "점수 : ";
cin >> score;
}
void Student::printStudent()
{
cout << "이름 : " << name << ", 점수 : " << score << endl;
}
void sortStudent(Student *s, int size)
{
int min, tScore;
string tName;
for(int i=0; i<size-1; i++)
{
min = i;
for(int j=i+1; j<size; j++)
if(s[j].getScore()<s[min].getScore())
min=j;
if(i !=min)
{
tScore = s[min].getScore();
s[min].setScore(s[i].getScore());
s[i].setScore(tScore);
tName = s[min].getName();
s[min].setName(s[i].getName());
s[i].setName(tName);
}
}
}
int main()
{
Student std[3];
for(int i=0;i<3;i++)
{
std[i].setStudent();
}
sortStudent(std,3);
for(i=0;i<3;i++)
std[i].printStudent();
}