当前位置:编程学习 > C/C++ >>

C++程序设计

编写一个学生类,学生包含姓名,学号,语文,数学,英语三科成绩及总成绩。
输入学生信息,按学生的总成绩排序并输出,
追问:sort()在哪?
答案:
程序没有排错处理,不过主体已经有了,你可以自己加强一下:


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;


class student {
public:
// 构造函数
student(string& _name, unsigned _id, double _chs, double _mat, double _eng)
: name(_name), id(_id), chs(_chs), mat(_mat), eng(_eng)
{}
// 返回总成绩
double total() const
{
return chs + mat + eng;
}
// 输出
friend ostream& operator << (ostream& os, const student& s) {
os << s.id << '\t' << s.name << '\t' << s.total() << '\t';
os << s.chs << '\t' << s.mat << '\t' << s.eng << endl;
return os;
}
private:
string name; // 姓名
unsigned id; // 学号
double chs; // 语文
double mat; // 数学
double eng; // 英语
};

// 用于std::sort函数比较的predicate
bool pred(student* a, student* b)
{
return a->total() > b->total();
}

int main()
{
string name;
int id, n;
double chs, mat, eng;

cout << "请输入要录入的记录数:";
cin >> n;
student** p = new student*[n];

for(int i = 0; i < n; ++i) {
cout << "请输入第" << n << "个学生的学号及姓名:";
cin >> id >> name;
cout << "请分别输入该学生的语文,数学和英语成绩:";
cin >> chs >> mat >> eng;
p[i] = new student(name, id, chs, mat, eng);
}

sort(p, p + n, pred);
cout << "\n按总成绩排列结果:\n";
cout << "ID\t姓名\t总成绩\t语文\t数学\t外语\n";
for(int i = 0; i < n; ++i) {
cout << *p[i];
delete p[i];
}
delete [] p;
}
程序没有排错处理,不过主体已经有了,你可以自己加强一下:#include <iostream>#include <string>#include <algorithm>using namespace std;class student {public:    // 构造函数    student(string& _name, unsigned _id, double _chs, double _mat, double _eng)    : name(_name), id(_id), chs(_chs), mat(_mat), eng(_eng)    {}    // 返回总成绩    double total() const    {        return chs + mat + eng;    }    // 输出    friend ostream& operator << (ostream& os, const student& s) {        os << s.id << '\t' << s.name << '\t' << s.total() << '\t';        os << s.chs << '\t' << s.mat << '\t' << s.eng << endl;        return os;    }private:    string name; // 姓名    unsigned id; // 学号    double chs;  // 语文    double mat;  // 数学    double eng;  // 英语};// 用于std::sort函数比较的predicatebool pred(student* a, student* b){    return a->total() > b->total();}int main(){    string name;    int id, n;    double chs, mat, eng;        cout << "请输入要录入的记录数:";    cin >> n;    student** p = new student*[n];        for(int i = 0; i < n; ++i) {        cout << "请输入第" << n << "个学生的学号及姓名:";        cin >> id >> name;        cout << "请分别输入该学生的语文,数学和英语成绩:";        cin >> chs >> mat >> eng;        p[i] = new student(name, id, chs, mat, eng);    }        sort(p, p + n, pred);    cout << "\n按总成绩排列结果:\n";    cout << "ID\t姓名\t总成绩\t语文\t数学\t外语\n";    for(int i = 0; i < n; ++i) {        cout << *p[i];        delete p[i];    }    delete [] p;}
正好心情好   帮你做做,刚写出来的,应该可以运行,如果出现一点问题就自己稍微修改一下

#include "stdio.h"
#include "string.h"
struct A
{
char name[20];
float chinese,math,english;
int no;
}B[5],C;
void main()
{
int i,j;
float K[5];
for(i=0;i<5;i++)
{
printf("请输入第%d位同学的姓名:",i+1);
scanf("%s",&B[i].name);
printf("请输入第%d位同学的学号:",i+1);
scanf("%d",&B[i].no);
printf("请输入第%d位同学的语文成绩:",i+1);
scanf("%d",&B[i].chinese);
printf("请输入第%d位同学的数学成绩:",i+1);
scanf("%d",&B[i].math);
printf("请输入第%d位同学的英语成绩:",i+1);
scanf("%d",&B[i].english);
K[i]=(B[i].chinese+B[i].math+B[i].english)/3;
}
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(K[i]<K[i+1])
{
strcpy(C.name,B[i].name);
C.no=B[i+1].no;
C.chinese=B[i+1].chinese;
C.math=B[i+1].math;
C.english=B[i].english;
strcpy(B[i+1].name,B[i].name);
B[i+1].no=B[i].no;
B[i+1].chinese=B[i].chinese;
B[i+1].math=B[i].math;
B[i+1].english=B[i].english;
strcpy(B[i].name,C.name);
B[i].no=C.no;
B[i].chinese=C.chinese;
B[i].math=C.math;
B[i].english=C.english;
}
}
for(i=0;i<5;i++)
{
printf("%s%4d%4d%4d%4d\n",B[i].name,B[i].no,B[i].chinese,B[i].math,B[i].english);
}
getch();
return(0);

}
#include<stdio.h>
#include<string.h>
struct student
{
int stuNo; //储存学号
float score[3]; //储存3门课程的分数
double avg; //平均分
double sum; //总分
};
void input(student stu[],int count) //定义输入函数
{
float sum=0;
int i;
printf("请输入学生的基本信息:\n");
printf("学号:");
scanf("%d",&stu[count].stuNo);
printf("数学成绩:");
scanf("%f",&stu[count].score[0]);
printf("英语成绩:");
scanf("%f",&stu[count].score[1]);
printf("语文成绩:");
scanf("%f",&stu[count].score[2]);
for(i=0;i<3;i++)
sum=sum+stu[count].score[i];
stu[count].sum=sum;
printf("学生总分是:%f\n",stu[count].sum);
stu[count].avg=sum/3.0;
printf("学生平均是:%f\n",stu[count].avg);

}
void sort(student stu[],int count) //排序
{
int i;
int j;
student temp;
for(i=0;i<count-1;i++)
for(j=0;j<count-i-1;j++)
if(stu[j].sum<stu[j+1].sum)
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
void output(student stu[],int count)
{
printf("进行排序后输出信息:\n");
for(int i=0;i<count;i++)
{
printf("学号:%d\n",stu[i].stuNo);
printf("数学成绩:%f\n",stu[i].score[0]);
printf("英语成绩:%f\n",stu[i].score[1]);
printf("语文成绩:%f\n",stu[i].score[2]);
printf("学生总分是:%f\n",stu[i].sum);
printf("学生平均是:%f\n",stu[i].avg);
}


}
void main()
{
student stu[10]; //结构体变量数组 保存10个学生的信息
char c;
int count=0;
do
{
input(stu,count);
count++;
printf("是否继续录入:Y or N\n");
scanf("%s",&c);
}while(c=='y'||c=='Y');
sort(stu,count);
output(stu,count);


}

上一个:关于C++
下一个:C++怎么学好?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,