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

C언어 학생 정보를 점수순으로 정렬

너에게제공 너에게제공
569 0 0

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();
}

신고공유스크랩

댓글 0

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

신고

"님의 댓글"

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

댓글 삭제

"님의 댓글"

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

공유

퍼머링크