単一チェーンテーブルに基づくバブルソート簡易学生成績ソートプログラム


/*###########################################################################################################
#													    #
#    :												    #
#      ,               。								    #
#              、       ,							            #
#          n       :									    #
#1)       ,              ,          ,                。  #
#2)             、  、     。							    #
#													    #
############################################################################################################*/
//student.h
#include <iostream>
#include <string>
#include <iomanip> 
using namespace std;
//       studnet_dat
struct Student_dat
{
	string number;
	string name;
//	char gender;
	float score;
	Student_dat *next;
	
};
//     
template<class T>
class Student
{
public:
	void			setstu(Student_dat *);
	Student_dat		*stulist(Student_dat *&);
	void			bubbleSort(Student_dat *);//    
	void			stumean(Student_dat *);
	void			display(Student_dat *);
	void			getmean();
private:
	float mean;//   
};
template <class T>
void Student<T>::setstu(Student_dat *a)
{
	cout<<"  :";
	cin>>a->number;
	if (a->number=="0")return ;
	cout<<"  :";
	cin>>a->name;
//	cout<<"  ( / ):";
//	cin>>a->gender;
	cout<<"  :";
	cin>>a->score;
	
}
template <class T>
void Student<T>::display(Student_dat *top)
{
	Student_dat *p=top;
	p=p->next;
	int count=0;
	cout<<"  "<<'\t'<<"  "<<'\t'<<"  "<<'\t'<<"  "<<endl;
	while (p!=NULL)
	{
		
		cout<<setw(2)<<++count<<'\t';
		cout<<p->number<<'\t'<<p->name<<'\t'<<p->score<<endl;
		p=p->next;
		
	}
}
template <class T>
void Student<T>::getmean()
{
	cout<<"   :"<<mean<<endl;
}
template <class T>
void Student<T>::stumean(Student_dat *top)
{
	if (top->next==NULL)
		return;
	Student_dat *p=top;
	p=p->next;
	double sum=0;
	int i=0;
	while (p!=NULL)
	{
		i++;
		sum+=p->score;
		p=p->next;
		
	}
	mean=sum/(float)i;
	return;
	
}
template <class T>
Student_dat *Student<T>::stulist(Student_dat * &top)//        
{
	Student_dat *p,*star;
	star=top;
	top->next=star;
	cout<<"-----       -----"<<endl;

	while (1)
	{		
		p=new Student_dat;
		setstu(p);
		if (p->number=="0")break;
		star->next=p;
		star=p;
		cout<<"----------"<<endl;
	};
	star->next=NULL;
	return top;
}
template <class T>
void Student<T>::bubbleSort(Student_dat *top)//    
{
	cout<<"-----        -----"<<endl;
	int num=0;
	Student_dat *p=top;

	Student_dat *r;
	Student_dat *s;
	Student_dat q;//      
	string tempnum;
	string tempname;
	float tempscore;
	float  tempscore1;
	float  tempscore2;
	while (p!=NULL)
	{
		s=p;
		r=p->next;
		while(r!=NULL)
		{
			tempscore1=s->score;
			tempscore2=r->score;
			if (tempscore2<tempscore1)
			{
				s=r;//      s           
			}
			r=r->next;

		}
		if (s!=p)
		{
			q=(*p);//    
			tempnum=s->number;p->number=tempnum;
			tempname=s->name;p->name=tempname;
			tempscore=s->score;p->score=tempscore;

			tempnum=q.number;s->number=tempnum;
			tempname=q.name;s->name=tempname;
			tempscore=q.score;s->score=tempscore;

		}//p s        
		p=p->next;
	}

}
//main.cpp
#include "student.h"

void main()
{
	Student_dat *top=new Student_dat;
	Student<Student_dat> a;
	a.stulist(top);
	a.stumean(top);
	a.display(top);
	a.getmean();
	a.bubbleSort(top);a.display(top);

	a.getmean();
	delete top;
}