一个c++的简单程序,高手帮我调一下
//定义student类
//放在文件"student_class.h"里
#include<iostream.h>
#include<string.h>
#define N 10
class Student
{public:
Student(int n,char na[20],float c,float e,float g)
{num=n;
strcpy(name,na);
ch=c;
en=e;
geo=g;
sum=c+e+g;
}
void display()
{cout<<num<<" "<<name<<" "<<ch<<" "<<en<<" "<<geo<<" "<<sum<<endl;}
friend void search(Student stu[N],int num);
//类的友元函数search
friend void order(Student stu[N]);
//类的友元函数order
private:
int num;
char name[20];
float ch;
float en;
float geo;
float sum;
};
#include<iostream.h>
#include<string.h>
#include"student_class.h"
#define N 10
//Student的友元函数search
void search(Student stu[N],int num)
{int i;
for(i=0;i<N;i++)
if(stu[i].num=num)
{cout<<"学生信息:"<<endl;
cout<<"学号 "<<"姓名 "<<"语文 "<<"英语 "<<"地理 "<<"总和 "<<endl;
stu[i].display();
if(i>=N)cout<<"没有找到该学生的信息"<<endl;
}
//Student的友元函数order
void order(Student stu[N])
{int j,k;
Student st;
for(j=0;j<N-1;j++)
for(k=j+1;k<N;k++)
if(stu[k].sum>stu[j].sum)
{st=stu[j];stu[j]=stu[k];stu[k]=st;}
cout<<"总成绩从大到小排序为:"<<endl;
cout<<"学号 "<<"姓名 "<<"语文 "<<"英语 "<<"地理 "<<"总和 "<<endl;
for(i=0;i<N;i++)
stu[i].display();
}
void main()
{
int num;
Student stu[N]={
Student(001,"小风",89,91,79),
Student(002,"小花",79,89,95),
Student(003,"小雪",69,79,99),
Student(004,"小月",73,73,90),
Student(005,"小赤",99,72,65),
Student(006,"小橙",65,67,79),
Student(007,"小绿",92,76,73),
Student(008,"小青",74,83,91),
Student(009,"小蓝",90,92,91),
Student(010,"小紫",77,88,66)}
cout<<"输入学号:";
cin>>num;
search(stu[N],num);
order(stu[N]);
}
答案:这个题目你错的比较多,我该动也比较多,具体到语法和算法,以下程序能够正常计算结果,仅供参考:
1.头文件:student_class.h
#include<iostream>
#include<string>
#include<iomanip>
#define N 10
using namespace std;
class Student
{
private:
int num;
char name[20];
float ch;
float en;
float geo;
float sum;
public:
//modify by me
Student(int n,char* na,float c,float e,float g):num(n),ch(c),en(e),geo(g),sum(c+e+g)
{
strcpy(name,na);
}
Student(Student& st):num(st.num),ch(st.ch),en(st.en),geo(st.geo),sum(st.ch+st.en+st.geo)
{
strcpy(name,st.name);
}
//modify end
void display()
{
cout<<setw(2)<<num<<"
"<<name<<"
"<<ch<<"
"<<en<<"
"<<geo<<"
"<<sum<<endl;
}
friend void search(Student stu[N],int num);
friend void order(Student stu[N]);
const Student& operator=(Student& st)
{
if(this!=&st)
{
num=st.num;
strcpy(name,st.name);
ch=st.ch;
en=st.en;
geo=st.geo;
sum=st.sum;
}
return *this;
}
};
void search(Student* stu,int num)
{
int i;
for(i=0;i<N;i++)
if(stu[i].num == num)//modify ==
{
cout<<"学生信息:"<<endl;
cout<<"学号 "<<"姓名 "<<"语文 "<<"英语 "<<"地理 "<<"总和 "<<endl;
stu[i].display();
if(i>=N)
cout<<"没有找到该学生的信息"<<endl;
}
}//modify add by me
//modify 以下算法不合理,经过修改
void order(Student* stu)
{
for(int i=0; i<N; i++)
{
int k=i;
for(int j=k+1; j<N; j++)
{
if(stu[j].sum>stu[k].sum)
k=j;
}
Student* temp=new Student(stu[k]);
stu[k]=stu[i];
stu[i]=*temp;
}
cout<<"总成绩从大到小排序为:"<<endl;
cout<<"学号 "<<"姓名 "<<"语文 "<<"英语 "<<"地理 "<<"总和 "<<endl;
for(i=0;i<N;i++)
stu[i].display();
}
2.源文件:source.cpp
#include"student_class.h"
void main()
{
int num;
Student stu[N]=
{
Student(1,"小风",89,91,79),
Student(2,"小花",79,89,95),
Student(3,"小雪",69,79,99),
Student(4,"小月",73,73,90),
Student(5,"小赤",99,72,65),
Student(6,"小橙",65,67,79),
Student(7,"小绿",92,76,73),
Student(8,"小青",74,83,91),
Student(9,"小蓝",90,92,91),
Student(10,"小紫",77,88,66)
};//modify
cout<<"输入学号:";
cin>>num;
search(stu,num);//modify
order(stu);//modify
}
以下是运行结果:
输入学号:2
学生信息:
学号 姓名 语文 英语 地理 总和
2
小花
79
89
95
263
总成绩从大到小排序为:
学号 姓名 语文 英语 地理 总和
9
小蓝
90
92
91
273
2
小花
79
89
95
263
1
小风
89
91
79
259
8
小青
74
83
91
248
3
小雪
69
79
99
247
7
小绿
92
76
73
241
4
小月
73
73
90
236
5
小赤
99
72
65
236
10
小紫
77
88
66
231
6
小橙
65
67
79
211
Press any key to continue
上一个:我写的C++代码错在哪了?
下一个:怎样用C++编写一个学校人员管理系统?