/**************************作者:李云*******************************/
/**************************时间:2017.9.6***************************/
/******输入Num个同学的姓名和英语成绩并排名,用友函数实现************/
#include<iostream>
#include<string>
//#include "StdAfx.h"
using namespace std;
class student
{
friend int Sort(student s[], int n);
private:
char name[10];
int EngScore;
int ComScore;
public:
//student(char *pName,int eScore,int cScore)
//{
// strcpy_s(name, pName);
// EngScore = eScore;
// ComScore = cScore;
//}
//student(){ };
void setName(char *stuName){ strcpy_s(name, stuName); }
void setEngScore(int score){ EngScore = score; }
void setComScore(int score){ ComScore = score; }
char *getName(){ return name; }
int getScore(){ return (EngScore + ComScore); }
};
//对象的成绩排序
int Sort(student s[], int n)
{
int k = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (s[i].getScore() < s[j].getScore())
{
//如果i的成绩和小于j的成绩和,则交换对象
student temp;
temp = s[i];
s[i] = s[j];
s[j] = temp;
k++;
}
}
}
return k;
}
int main()
{
int num;
char cinName[10];
int cineScore;
int cincScore;
cout << "请输入学生人数num=" ;
cin >> num;
student A[20];
for (int i = 0; i < num; i++)
{
cout << "请输入第" << i + 1 << "个同学的姓名:";
cin >> cinName;
cout << "请输入第" << i + 1 << "个同学的英语成绩:";
cin >> cineScore;
cout << "请输入第" << i + 1 << "个同学的计算机成绩:";
cin >> cincScore;
A[i].setName(cinName);
A[i].setEngScore(cineScore);
A[i].setComScore(cincScore);
}
Sort(A, num);
cout << "按成绩从小到大排序后:" << endl;
cout << "姓名\t分数" << endl;
for (int i = 0; i<num; i++)
{
cout << A[i].getName() << "\t" << A[i].getScore() << endl;
}
return 0;
}
c++小题目1
最新推荐文章于 2024-08-26 17:31:44 发布