?? 極好的學生管理系統.cpp
字號:
//-----------------------------------------------------
//---------------學生成績管理系統----------------------
//-----------------------------------------------------
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
//----------------------------------------------------
//----------------------------------------------------
const int n=100; //n為學生總數
const int m=4; //定義有四門課程
int g=1; //連續輸入學生信息時表示人數的變量初始值為1
int p=-1; //非連續輸入時所需加的值初始值為-1
float a[m]; //每個學生的各四門課程成績
//----------------------------------------------------
//--------------------Stud----------------------------
class Stud //聲明類Stud
{
public: //類的公有成員
void Input();
void Display();
void Cout();
void Quit();
private: //類的私有成員
int numb;
char name[20];
float score;
float sum;
};
//----------------------------------------------------------
//----------------------------------------------------------
Stud st[n];
void Stud::Input() //輸入函數
{
char ch='y';
fstream outfile; //打開文件前先說明一個fstream類的對象
outfile.open("Stud.dat",ios::out|ios::app);//使用成員函數open()以追加寫方式打開文件
if(!outfile) //看文件是否被打開
{
cout<<"Stud.dat can't open.\n";
abort();
}
while(ch=='y')
{
st[g+p].sum=0;
cout<<"請輸入第"<<g+p<<"個學生的學號:";
cin>>st[g+p].numb;
cout<<"請輸入"<<g+p<<"個學生的姓名:";
cin>>st[g+p].name;
cout<<"請輸入"<<g+p<<"個學生的各四門課程成績:"<<endl;
for(int j=0;j<m;j++) //四門課程的成績相加得到st[g+p].sum
{
cin>>a[j];
st[g+p].sum+=a[j];
}
cout<<"第"<<g+p<<"個學生總分:"<<st[g+p].sum<<endl;
outfile.write((char *)&st[g+p].numb,sizeof(st[g+p].numb));//向文件中寫入信息
outfile.write((char *)&st[g+p].name,sizeof(st[g+p].name));//向文件中寫入信息
outfile.write((char *)&st[g+p].sum,sizeof(st[g+p].sum)); //向文件中寫入信息
outfile.close();
cout<<"是否繼續輸入?(y or n)"<<endl; //提出是否再輸入
cin>>ch;
switch(ch) //控制是否再輸入
{
case 'y': //再輸入人數加1
g++;
break;
case 'n': //不輸入退出
break;
}
}
}
//------------------------------------------------------------
//------------------------------------------------------------
void Stud::Display() //顯示函數
{
int i;
fstream infile ; //從文本文件中讀取信息
infile.open("stud.dat",ios::in|ios::app);//以使用成員函數open()以追加讀方式打開文件
if(!infile) //看文件是否被打開
{
cout<<"stud.dat can't open.\n";
abort();
}
for(i=1;i<=g+p;i++)
{
infile.read((char *)&st[i].numb,sizeof(st[i].numb));//用read()函數把信息讀出來
infile.read((char *)&st[i].name,sizeof(st[i].name));//用read()函數把信息讀出來
infile.read((char *)&st[i].sum,sizeof(st[i].sum)); //用read()函數把信息讀出來
infile.close();
}
for(int j=1;j<g+p;j++) //按學生的成績從高到低排序
for(int k=1;k<=g+p-j;k++)
if(st[k].sum<st[k+1].sum) //如符合條件進行交換
{
float t;
int b;
char na[20];
t=st[k].sum;
st[k].sum=st[k+1].sum;
st[k+1].sum=t;
b=st[k].numb;
st[k].numb=st[k+1].numb;
st[k+1].numb=b;
strcpy(na,st[k].name);
strcpy(st[k].name,st[k+1].name);
strcpy(st[k+1].name,na);
}
for(int m=1;m<=g+p;m++) //輸出排序后的成績
{
cout<<"第"<<m<<"名的學號為:"<<st[m].numb<<endl;
cout<<"第"<<m<<"名的姓名為:"<<st[m].name<<endl;
cout<<"第"<<m<<"名的總分為:"<<st[m].sum<<endl;
}
}
//---------------------------------------------------------
//---------------------------------------------------------
void Stud::Cout() //算平均成績
{
float Tsum=0;
for(int i=1;i<=g+p;i++) //求各學生總成績之和
{
Tsum+=st[i].sum;
}
float average=Tsum/(g+p); //平均成績
cout<<"平均分為:"<<average<<endl;
}
//----------------------------------------------------------
//-----------------------主函數-----------------------------
void main()
{
char f;
int s;
bool t;
t=true;
while(t)
{
do{
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
cout<<" 學生成績管理系統 "<<endl;
cout<<" "<<endl;
cout<<" 1. 成績輸入 "<<endl;
cout<<" 2. 成績顯示 "<<endl;
cout<<" 3. 統 計 "<<endl;
cout<<" A.學生總數B.平均分數 "<<endl;
cout<<" 0. 退 出 "<<endl;
cout<<" "<<endl;
cout<<" 請輸入一個選項! "<<endl;
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
cin>>s;
}while (s!=1&&s!=2&&s!=3&&s!=0);
switch(s){ //switch語句
case 1:
p++;
st[n].Input();
break;
case 2:
st[n].Display();
break;
case 3:
cout<<"請選擇統計的內容:A.學生人數B.平均分數(A OR B)?"<<endl;
do{
cin>>f;
}while (f!='A'&&f!='B');
switch(f){
case 'A':
cout<<"學生總數為:"<<g+p<<endl;
break;
case 'B':
st[n].Cout();
break;
default:
cout<<"輸入錯誤,請重新輸入!"<<endl;
}
break;
case 0:
t=false;
exit(0);
default:
cout<<"輸入錯誤,請重新輸入!";
}//switch語句的結束
} //while語句的結束
} //主函數的結束
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -