发新话题
打印

这道C++怎么改写

这道C++怎么改写

//想问的问题在注释里
#include<iostream.h>
#include
<string.h>
#include
<stdlib.h>
#include
<fstream.h>
//想在这里增加 #define MIN 1
class student //学生类
{
char name[20];
char sex[5];
char number[20];
int score[4];
int total;
int average;
public:
   
void add();
   
void output();  
    student();  
    student
&
operator=(student & T);
    friend istream
&
operator
>> (istream & scin, student &s1);
    friend ostream
&
operator
<< (ostream & scout, student &s2);


};
   
static
int n=0;
    student stu[
100];//想把这个改成student stu[MIN];
   
//在加上这句话 student *stu=new student[MIN];只是不知道该怎么加上去
   
//只是不知道如何加上去让程序能用指针动态分配内存,大家帮忙解决下啊~~谢谢拉

void menu() //菜单函数
{
    cout
<<endl<<endl;
    cout
<<endl<<endl;
    cout
<<"\t         设计:  *************************            "<<endl;
    cout
<<"\t* * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
    cout
<<"\t*             欢迎进入学生成绩管理系统            *"<<endl;
    cout
<<"\t*                                                 *"<<endl;
    cout
<<"\t*    1:输入       2:输出        3:退出            *"<<endl;
    cout
<<"\t*                                                 *"<<endl;
    cout
<<"\t* * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
    cout
<<endl;
    cout
<<"请选择你需要的选项:";
}




void student::add() //添加函数
{

int cx;
cout
<<"你想输入多少个学生的基本信息?"<<endl;
cin
>>cx;


for(int i=0;i<cx;i++)
{
cout
<<"请输入第"<<n+1<<"位学生信息"<<endl;
cin
>>stu[n];
n
++;

}

menu();
}
void student::output() //输出函数
{
if(n!=0)
{
cout
<<" ---------------------------------------------------------------------------\n";
cout
<<"学号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"
"<<"高等数学"<<"
"<<"英语"<<"
"<<"大学体育"<<"
"<<"C++面向程序设计"<<"
"<<"总分"<<"
"<<"平均分"<<endl;
cout
<<" --------------------------------------------------------------------------- \n";
for(int t=0;t<n;t++)
{
cout
<<stu[t];
}
menu();
}
else cout<<"对不起,请先选择 1 输入数据"<<endl;
}

student
& student::operator=(student & T) //重载=
{
strcpy(name,T.name);
strcpy(sex,T.sex);
strcpy(number,T.number);
for(int i=0;i<4;i++)score=T.score;
total
=T.total;
average
=T.average;
return ( *
this );
}
ostream
&
operator
<< (ostream & scout, student &s2) //重载<<
{
cout
<<"
"<<s2.number<<"
"<<s2.name<<"
"<<s2.sex<<"
"<<s2.score[0]
<<"
"<<s2.score[1]<<"
"<<s2.score[2]<<"
"<<s2.score[3]<<"
"<<s2.total<<"
"<<s2.average<<endl;
return scout;
}
istream
&
operator
>> (istream & scin,student &s1) //重载>>
{
cout
<<"学号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"
"<<"高等数学"<<"
"<<"英语"<<"
"<<"大学体育"<<"
"<<"C++面向程序设计"<<endl;
scin
>>s1.number>>s1.name>>s1.sex>>s1.score[0]>>s1.score[1]>>s1.score[2]>>s1.score[3];
s1.total
=s1.score[0]+s1.score[1]+s1.score[2]+s1.score[3];
s1.average
=s1.total/4;
return scin;
}

student::student()
//构造函数
{
strcpy(name,
"
");
strcpy(sex,
"
");
strcpy(number,
"
");
for(int i=0;i<4;i++)score=0;
total
=0;
average
=0;
}


void main()
{

student s;
menu();

char choice;
while(choice!=0)
{
cin
>>choice;
switch(choice)
{
case
'1': s.add(); break;
case
'2': s.output(); break;
case
'3': return;break;
default :cout<<"您的输入有误,请重试!\n";
}
}
return;
}

TOP

:006 :006 :006

TOP

我有点乱了。
悟空,其实当年我很想留在女儿国的。。。吾悔不听八戒之言呐!

TOP

动态分配内存直接在 main函数里写指针分配语句,不过要记得在程序结束时删除它。

TOP

发新话题