65,211
社区成员
发帖
与我相关
我的任务
分享
// main.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "student.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int data1,data2;
string name;
student a[4];
//const int number = 4;
//student a[4];
for(int i;i<3;i++)
{
cin>>name;
cin>>data1>>data2;
a[i].set(name,data1,data2);
//student a[i] = student(name,data1,data2);
}
sort(a,3);
return 0;
}
//student.h
// student.h: interface for the student class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STUDENT_H__E6FD29E5_07B1_4FA3_9EA0_6C6C3C50B348__INCLUDED_)
#define AFX_STUDENT_H__E6FD29E5_07B1_4FA3_9EA0_6C6C3C50B348__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>
using namespace std;
class student
{
private:
string name;
int english,computer,total;
public:
student(string n,int e,int c);
student();
virtual ~student();
friend void sort(student a[],int n);
void set(string n,int e,int c);
};
#endif // !defined(AFX_STUDENT_H__E6FD29E5_07B1_4FA3_9EA0_6C6C3C50B348__INCLUDED_)
// student.cpp: implementation of the student class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "student.h"
#include <string>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
student::student()
{
}
student::~student()
{
}
student::student(string n,int e,int c)
{
name = n;
english = e;
computer =c;
total = e+c;
}
void sort(student a[],int n)
{
student temp;
for(int i=0;i<n;i++)
{
for(int j=n+1;j<n+1;j++)
{
if(a[i].total<a[j].total)
{
temp.total=a[i].total; a[i].total = a[j].total; a[j].total=temp.total;
a[i].name.swap(a[j].name);
}
}
}
}
void student::set(string n,int e,int c)
{
name = n;
english = e;
computer =c;
total = e+c;
}