希望有人帮我调一下这个c++程序

作者在 2010-05-13 22:29:04 发布以下内容

//定义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]);

}

数据结构 | 阅读 311 次
文章评论,共1条
missiyou
2010-05-14 16:56
1
#include&lt;iostream&gt;<br />
<br />
#include&lt;string.h&gt;<br />
using namespace std;<br />
<br />
#define N 10<br />
<br />
class Student<br />
<br />
{public:<br />
Student():num(0),ch(0.0),en(0.0),geo(0.0),sum(0.0)<br />
{<br />
&nbsp;&nbsp;memset(name, 0, 20);<br />
}<br />
<br />
Student(int n,char na[20],float c,float e,float g)<br />
<br />
{num=n;<br />
<br />
strcpy(name,na);<br />
<br />
ch=c;<br />
<br />
en=e;<br />
<br />
geo=g;<br />
<br />
sum=c+e+g;<br />
<br />
}<br />
<br />
void display()<br />
<br />
{cout&lt;&lt;num&lt;&lt;&quot;&nbsp;&nbsp;&quot;&lt;&lt;name&lt;&lt;&quot;&nbsp;&nbsp;&quot;&lt;&lt;ch&lt;&lt;&quot;&nbsp;&nbsp;&quot;&lt;&lt;en&lt;&lt;&quot;&nbsp;&nbsp;&quot;&lt;&lt;geo&lt;&lt;&quot;&nbsp;&nbsp;&quot;&lt;&lt;sum&lt;&lt;endl;}<br />
<br />
friend void search(Student stu[N],int num);&nbsp; &nbsp;&nbsp;&nbsp;//类的友元函数search<br />
<br />
friend void order(Student stu[N]);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//类的友元函数order<br />
<br />
private:<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; char name[20];<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; float ch;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; float en;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; float geo;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; float sum;<br />
<br />
};<br />
<br />
<br />
<br />
<br />
<br />
<br />
//Student的友元函数search<br />
<br />
void search(Student stu[N],int num)<br />
<br />
{<br />
int i;<br />
<br />
for(i=0;i&lt;N;i++)<br />
<br />
if(stu<i>.num=num) <br />
<br />
{cout&lt;&lt;&quot;学生信息:&quot;&lt;&lt;endl;<br />
<br />
cout&lt;&lt;&quot;学号&nbsp;&nbsp;&quot;&lt;&lt;&quot;姓名&nbsp;&nbsp;&quot;&lt;&lt;&quot;语文&nbsp;&nbsp;&quot;&lt;&lt;&quot;英语&nbsp;&nbsp;&quot;&lt;&lt;&quot;地理&nbsp;&nbsp;&quot;&lt;&lt;&quot;总和&nbsp;&nbsp;&quot;&lt;&lt;endl;<br />
<br />
stu<i>.display();<br />
<br />
if(i&gt;=N)cout&lt;&lt;&quot;没有找到该学生的信息&quot;&lt;&lt;endl;<br />
<br />
}<br />
}<br />
//Student的友元函数order<br />
<br />
void order(Student stu[N])<br />
<br />
{int j,k;<br />
<br />
Student st;<br />
<br />
for(j=0;j&lt;N-1;j++)<br />
<br />
for(k=j+1;k&lt;N;k++)<br />
<br />
if(stu[k].sum&gt;stu[j].sum)<br />
<br />
{st=stu[j];stu[j]=stu[k];stu[k]=st;}<br />
<br />
cout&lt;&lt;&quot;总成绩从大到小排序为:&quot;&lt;&lt;endl;<br />
<br />
cout&lt;&lt;&quot;学号&nbsp;&nbsp;&quot;&lt;&lt;&quot;姓名&nbsp;&nbsp;&quot;&lt;&lt;&quot;语文&nbsp;&nbsp;&quot;&lt;&lt;&quot;英语&nbsp;&nbsp;&quot;&lt;&lt;&quot;地理&nbsp;&nbsp;&quot;&lt;&lt;&quot;总和&nbsp;&nbsp;&quot;&lt;&lt;endl;<br />
<br />
for(int i=0;i&lt;N;i++)<br />
<br />
stu<i>.display();<br />
<br />
}<br />
<br />
int main()<br />
<br />
{<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; int num;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Student stu[N]={<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(1,&quot;小风&quot;,89,91,79),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(2,&quot;小花&quot;,79,89,95),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(3,&quot;小雪&quot;,69,79,99),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(4,&quot;小月&quot;,73,73,90),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(5,&quot;小赤&quot;,99,72,65),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(6,&quot;小橙&quot;,65,67,79),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(7,&quot;小绿&quot;,92,76,73),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(8,&quot;小青&quot;,74,83,91),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(9,&quot;小蓝&quot;,90,92,91),<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student(10,&quot;小紫&quot;,77,88,66)};<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;输入学号:&quot;;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; cin&gt;&gt;num;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; search(stu,num);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; order(stu);<br />
&nbsp; &nbsp; &nbsp; &nbsp; return 0;<br />
<br />
}
游客请输入验证码