- 设计一个类Stud,其中包括一个对学生数据的操作。输入一系列的数据(学号,姓名和成绩)存放在文件stud.dat中。并从该文件中读出这些数据并显示出来。建立两个普通函数fun1 ( ), fun2( )来完成基本要求中的两个要求。
# include <fstream >
# include <iostream >
# include <iomanip >
class Stud
{
int no ;
char name =[ 10 ] ;
int score ;
public :
void getdata ( )
{
cout << “(学号,姓名和成绩) :” ;
cin >> no >> name >> score ;
}
void disp( )
{
cout <<setw( 6 ) <<no <<setw( 10 )<<name <<setw( 6 ) <<score<<endl;
}
} ;
void func1 ( )
{
ofstream output( “stud.dat” ) ;
Stud s ;
int n ;
cout <<”输入数据:”<<endl ;
cout <<”学生人数:”<<endl ;
cin >> n ;
for ( int I =0 ; I< n ; I++ )
{
cout << “第”<<I +1 <<”个学生 ” ;
s.getdata( ) ;
output.write( (char * )&s, sizeof( s) ) ;
} ;
output.close( ) ;
}
void fun2 ( )
{
ifstream input( “stud.dat” ) ;
Stud s ;
cout <<”输出数据:”<<endl ;
cout <<” 学号 姓名 成绩”<<endl ;
input.read( (char *)&s , sizeof( s ) ) ;
while ( input )
{
s.disp( );
input.read( (char *)&s , sizeof( s ) ) ;
} ;
input.close( ) ;
}
void main( )
{
int sel ;
do
{
cout<<”选择(1:输入数据 2:输出数据 其他退出 ):” ;
cin >> sel ;
switch(sel )
{
case 1 :func1( ) ;break ;
case 2 :func2( ) ;break ;
}
}while (sel = = 1||sel = = 2) ;
}
- 在完成第一题的基础上,用OOP程序设计的基本理念,设计开发学生成绩管理系统。要求有以下功能:各科成绩录入、查询、求学生的总成绩、平均成绩、求综合测评、按照某一门或若干门成绩排序。