U E D R , A S I H C RSS

CPP Study_2005_1/STL성적처리_3



1. Info

{{| : 2005 08 03
: 0


미구

|}}

2. Input Text

 59 98 75 91 
 66 78 77 84 
박민 52 50 63 72 
 97 55 52 97 
 78 82 63 73 
 72 66 73 52 
구 95 62 94 53 
민 78 53 74 75 
 66 82 82 94 
노라 86 65 62 68 

3. Code

#include <fstream> 
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //sort
#include <numeric> //accumulate

using namespace std; 


typedef struct student_table_struct {
	string name;
	vector<int> score;
	unsigned int total;
	double avg;
} student_type;


void readdata(vector<student_type>& ztable); //로부다
bool zcompare(const student_type ele1, const student_type ele2); //sort 
void printdata(vector<student_type>& ztable); //력
void operation(vector<student_type>& ztable); //균

int main() 
{ 

		vector<student_type> ztable;
	
		//료를 는다.
		readdata(ztable);
		
		//균
		operation(ztable);

		//렬
		sort(ztable.begin(),ztable.end(),zcompare);
		
		//력
		printdata(ztable);

        return 0; 
} 



//로부다
void readdata(vector<student_type>& ztable){
	
	ifstream fin("data.txt");
	student_type tmp;
	int tmp2,i;

	while(1)
	{
		fin >> tmp.name;

		for(i=1;i<=4;i++)
		{
			fin >> tmp2;
			tmp.score.push_back(tmp2);
		}
		

		if(fin.eof()) break;
		ztable.push_back(tmp);
	}
	
}

//sort 
bool zcompare(const student_type ele1, const student_type ele2)
{	
	return ele1.avg > ele2.avg;
}

//기
void printdata(vector<student_type>& ztable)
{
		int cnt=0;
		cout << "\t국\t\t\t과\t\t균" << endl;
		for(vector<student_type>::iterator i=ztable.begin();i<ztable.end();++i)
		{
			
			cout << i->name << "\t" <<
					i->score[cnt*4+0] << "\t" << 
					i->score[cnt*4+1] << "\t" <<
					i->score[cnt*4+2] << "\t" <<
					i->score[cnt*4+3] << "\t" << 
					i->total << "\t" <<
					i->avg <<endl;
			cnt++;
			
		}


}

void operation(vector<student_type>& ztable)
{
		for(vector<student_type>::iterator i=ztable.begin() ;i<ztable.end();++i)
		{
			i->total = accumulate(i->score.begin()+i->score.size()-4,i->score.end(),0);
			i->avg = i->total/4.0;
		}

}


4. Result

                  59      98      75      91      324     81
김  66      78      77      84      323     80.75
  52      50      63      72      305     76.25
구  97      55      52      97      304     76
  78      82      63      73      301     75.25
  78      53      74      75      296     74
노라  66      82      82      94      281     70.25
민  72      66      73      52      280     70
  95      62      94      53      263     65.75
박민  86      65      62      68      237     59.25
Press any key to continue

5. Idle Talk

따 ㅠㅠ


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:42
Processing time 0.0093 sec