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

C++文件和类

答案:#include<fstream>

#include<iomanip>

#include<iostream>

#include<cstring>

#include<vector>

using namespace std;

class Student

{

protected:

int Num;//学号

char Name[20];//姓名

float programmer,database,operatsystem,network;//各科成绩

static int stuNo;//当前学号最大值

public:

Student();//构造函数

~Student();//析构函数

void SetName(char *);//设置姓名函数

char * GetName();//提取姓名函数

int GetNum();//提取学号函数

float average();//求平均成绩函数

void SetScore(float,float,float,float);//设置各科成绩

float Getprogrammer();//显示各科成绩

float Getdatabase();

float Getoperatsystem();

float Getnetwork();

};

int Student::stuNo=113;

Student::Student()

{

Num=stuNo++;//初始化学号

}

Student::~Student(){}

void Student::SetName(char * names)

{

strcpy(Name,names);//设置姓名

}

char * Student::GetName()

{

return Name;

}

int Student::GetNum()

{

return Num;

}

float Student::average()

{

return (programmer+database+operatsystem+network)/4;//计算平均成绩

}

void Student::SetScore(float pr,float da,float op,float ne)

{

programmer=pr;

database=da;

operatsystem=op;

network=ne;

}

float Student::Getprogrammer()

{

return programmer;

}

float Student::Getdatabase()

{

return database;

}

float Student::Getoperatsystem()

{

return operatsystem;

}

float Student::Getnetwork()

{

return network;

}

void main()

{

char names[20];

float pro,dat,ope,net;

Student s1,s2,s3,s4;

vector <Student *> vchar;//声明用于保存成员对象的向量容器

vchar.push_back(&s1);

vchar.push_back(&s2);

vchar.push_back(&s3);

vchar.push_back(&s4);

int i;

for(i=0;i<4;i++)

{

cout<<"请输入下一个学生的姓名:";

cin>>names;

vchar[i]->SetName(names);//设置姓名

cout<<"输入各科成绩:"<<endl;

cout<<"(programmer database operatsytem network):";

cin>>pro>>dat>>ope>>net;

vchar[i]->SetScore(pro,dat,ope,net);//设置各科成绩

}

ofstream ofile("studata.txt",ios_base::out);//创建一个输出文件流对象

ofile<<" 姓名 "<<setw(6)<<" 学号 "

<<"programmer database operatsytem network"<<endl;

for(i=0;i<4;i++)

{

ofile<<" "<<setiosflags(ios_base::left)<<setw(5)

<<vchar[i]->GetName()<<resetiosflags(ios_base::left)

<<" "<<vchar[i]->GetNum();

ofile<<setw(10)<<vchar[i]->Getprogrammer()

<<setw(10)<<vchar[i]->Getdatabase()<<setw(10)

<<vchar[i]->Getoperatsystem()<<setw(10)

<<vchar[i]->Getnetwork()<<endl;

}

ofile.close();//关闭文件

ifstream infile("studata.txt",ios_base::in);//创建一个输入文件流对象

ofstream oafile("avedata.txt",ios_base::out);//输出文件流对象

oafile<<" 姓名 "<<setw(6)<<" 学号 "

<<setw(10)<<"平均成绩"<<endl;

for(i=0;i<4;i++)

{

oafile<<" "<<setiosflags(ios_base::left)<<setw(5)

<<vchar[i]->GetName()<<resetiosflags(ios_base::left)

<<" "<<vchar[i]->GetNum();

oafile<<setw(11)<<vchar[i]->average()<<endl;

}

infile.close();//关闭文件

oafile.close();//关闭文件

ifstream insfile("avedata.txt",ios_base::in);//创建输入文件流对象

ofstream osfile("sortdata.txt",ios_base::out);//创建输出文件流对象

Student *temp;//定义一个对象指针

for(int j=0;j<4;j++)//平均成绩排序

for(i=0;i<3-j;i++)

if(vchar[i]->average()<vchar[i+1]->average())

{

temp=vchar[i];

vchar[i]=vchar[i+1];

vchar[i+1]=temp;

}

osfile<<" 姓名 "<<setw(6)<<" 学号 "

<<setw(10)<<"平均成绩"<<endl;

for(i=0;i<4;i++)

{

osfile<<" "<<setiosflags(ios_base::left)<<setw(5)

<<vchar[i]->GetName()<<resetiosflags(ios_base::left)

<<" "<<vchar[i]->GetNum();

osfile<<setw(11)<<vchar[i]->average()<<endl;

}

insfile.close();//关闭文件

osfile.close();//关闭文件

cout<<"信息已存入文件..."<<endl;

}

上一个:C++怎么编计算器
下一个:c++ 链接错误 高分求解

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