# include & ltiostream.h & gt
# include & ltfstream.h & gt
# include & ltiomanip.h & gt
# include & ltwindows.h & gt
靜態int n;
結構學生
{
int stu _ number
char stu _ name[10];
char stu _ sex[4];
int stu _ age
int數學;
int英語;
int cplu _ plus
int total
浮動平均值;
};
班級學生
{
學生男生;
公共:
int getnum();
void in_put(學生*p1,int num);
void in_file(struct student *p,int數);
void readout(struct student * p);
void A _ calculate(struct student * p);
void T _ calculate(struct student * p);
int Cacu_aver(結構學生*p,int數);
int Cacu_total(結構學生*p,int數);
void out _ put(struct student * p);
};
void stu::in _ put(student * p 1,int num)
{
int I;
for(I = 0;我& ltnumi++)
{
cout & lt& lt"學生編號:"
CIN & gt;& gtp1[i]。stu _ number
cout & lt& lt"名稱:"
CIN & gt;& gtp1[i]。stu _ name
cout & lt& lt"性別:"
CIN & gt;& gtp1[i]。stu _ sex
cout & lt& lt"年齡:"
CIN & gt;& gtp1[i]。stu _ age
cout & lt& lt"數學成績:"
CIN & gt;& gtp1[i]。數學;
cout & lt& lt"英語成績:" < & ltendl
CIN & gt;& gtp1[i]。英語;
cout & lt& lt" c++分數:"
CIN & gt;& gtp1[i]。cplu _ plus
p1[i]。總計= 0;
p1[i]。平均值= 0;
系統(“cls”);
}
}
void stu::in_file(結構student *p,int number)
{
int I;
ofstream ofs("feifei.txt ",IOs::out);
如果(!ofs)
{
cerr & lt& lt“打不開文件!”& lt& ltendl
退出(1);
}
其他
{
for(I = 0;我& lt號碼;i++)
{
ofs & lt& ltp[i]。stu _ number & lt& lt\n '
& lt& ltp[i]。stu _ name & lt& lt\n '
& lt& ltp[i]。stu _ sex & lt& lt\n '
& lt& ltp[i]。stu _ age & lt& lt\n '
& lt& ltp[i]。數學& lt& lt\n '
& lt& ltp[i]。英語& lt& lt\n '
& lt& ltp[i]。cplu _ plus & lt& lt\ n ';
}
cout & lt& ltendl & lt& lt"數據已經輸入電腦了!"& lt& ltendl
ofs . close();
}
}
void stu::readout(學生*p)
{
int i,m;
ifstream ifs("feifei.txt ",IOs::in | IOs::binary);
如果(!ifs)
{
cerr & lt& lt“打不開文件!”& lt& ltendl
退出(1);
}
其他
{
for(I = 0;我& ltn;i++)
{
}
}
ifs . close();
}
void stu::T _ calculate(struct student * p)
{
int i,j;
結構學生溫度;
for(I = 0;我& ltn;i++)
{
p[i]。總計=p[i]。數學+p[i]。英語+p[i]。cplu _ plus
cout & lt& ltp[i]。stu _ name & lt& lt“的總得分是:”
}
for(I = 0;我& ltn;i++)
{
for(j = I+1;j & ltn;j++)
{
如果(p[i]。總計& gtp[j]。總計)
{
temp = p[j];
p[j]= p[I];
p[I]= temp;
}
}
}
}
void stu::A _ calculate(struct student * p)
{
int i,j;
結構學生溫度;
for(I = 0;我& ltn;i++)
{
for(j = I+1;j & ltn;j++)
{
如果(p[i]。stu _ age & gtp[j]。stu_age)
{
temp = p[j];
p[j]= p[I];
p[I]= temp;
}
}
}
}
int stu::Cacu_aver(結構學生*p,int數)
{
int i,flag = 1;
for(I = 0;我& ltn;i++)
{
p[i]。平均值=(p[i]。數學+p[i]。英語+p[i]。cplu _ plus)/3.0;
如果(p[i]。stu_number==number)
{
cout & lt& ltp[i]。stu _ name & lt& lt“是”的平均分
flag = 0;
}
}
如果(旗幟!= 0 & amp& ampi==n)
{
cout & lt& lt"!您輸入的號碼不存在,請檢查並重新輸入”
}
返回標誌;
}
int stu::Cacu_total(結構學生*p,int數)
{
int i,flag = 1;
for(I = 0;我& ltn;i++)
{
p[i]。總計=p[i]。數學+p[i]。英語+p[i]。cplu _ plus
如果(p[i]。stu_number==number)
{
cout & lt& ltp[i]。stu _ name & lt& lt“是”的總得分
flag = 0;
}
}
如果(旗幟!= 0 & amp& ampi==n)
{
cout & lt& lt" - !您輸入的號碼不存在,請檢查後重新輸入-" < & lt;endl
}
返回標誌;
}
void stu::out _ put(struct student * p)
{
int I;
cout & lt& lt"學生信息列表在這裏:" & lt& ltendl & lt& ltendl
cout & lt& lt" " " & lt& ltsetw(10)& lt;& lt右& lt& lt“學號”< & lt" " " & lt& ltsetw(8)& lt;& ltleft & lt& lt"名稱"
cout & lt& ltsetw(6)& lt;& lt右& lt& lt《數學》< & ltsetw(6)& lt;& lt右& lt& lt“英語”
cout & lt& ltsetw(8)& lt;& lt右& lt& lt"總分"
for(I = 0;我& ltn;i++)
{
cout & lt& lt“第壹”
cout & lt& ltsetw(6)& lt;& ltleft & lt& ltp[i]。stu _ sex & lt& ltsetw(6)& lt;& lt右& lt& ltp[i]。stu _ age & lt& ltsetw(6)& lt;& lt右& lt& ltp[i]。數學;
cout & lt& ltsetw(6)& lt;& lt右& lt& ltp[i]。英語& lt& ltsetw(6)& lt;& lt右& lt& ltp[i]。cplu _ plus
如果(p[i]。總計!=0)
{
cout & lt& ltsetw(8)& lt;& lt右& lt& ltp[i]。總計;
}
如果(p[i]。壹般!=0)
{
cout & lt& ltsetw(10)& lt;& lt右& lt& ltp[i]。壹般;
}
cout & lt& ltendl & lt& ltendl
}
}
int main()
{
斯圖男孩;
學生*per,* outp
int m;
cout & lt& lt"請從下面的菜單中選擇您想要的操作選項:"
做
{
cout & lt& lt"1.輸入學生信息並將其保存到文件中”
cout & lt& lt"2.按年齡顯示學生的各種信息"
cout & lt& lt"3.按成績從高到低顯示學生信息”< & ltendl
cout & lt& lt"4.輸入學號,計算學生的平均分並顯示出來”
cout & lt& lt"5.輸入學生編號,計算學生的總成績並顯示出來”
cout & lt& lt"6.根據學生的總成績進行排名並顯示排名結果”
cout & lt& lt"7.退出系統”
CIN & gt;& gtm;
系統(“cls”);
開關(m)
{
案例1:
{
cout & lt& lt請輸入您要輸入的學生人數:“& lt& ltendl
CIN & gt;& gtn;
per =新生[n];
boy.in_put(per,n);
boy.in_file(per,n);
刪除[]per;
打破;
}
案例二:
{
outp =新生[n];
boy . readout(outp);
好家夥。a _ caculate(outp);
boy . out _ put(outp);
刪除[]outp;
打破;
}
案例三:
{
outp =新生[n];
boy . readout(outp);
好家夥。t _ caculate(outp);
boy . out _ put(outp);
刪除[]outp;
打破;
}
案例4:
{
int數,flag
字符標誌1;
outp =新生[n];
boy . readout(outp);
做
{
cout & lt& lt請輸入您要查找的學生人數:“& lt& ltendl
CIN & gt;& gt號碼;
旗幟=男孩。Cacu_aver(outp,number);
if (flag==1)
{
cout & lt& lt“妳想再輸入壹次嗎?y '/' N ' " & lt;& ltendl
CIN & gt;& gtflag 1;
}
其他
打破;
}
while(flag 1 = = ' Y ' | | flag 1 = = ' Y ');
boy.in_file(outp,n);
刪除[]outp;
打破;
}
案例5:
{
int數,flag
字符標誌1;
outp =新生[n];
boy . readout(outp);
做
{
cout & lt& lt請輸入您要查找的學生人數:“& lt& ltendl
CIN & gt;& gt號碼;
旗幟=男孩。Cacu_total(outp,number);
if (flag==1)
{
cout & lt& lt“妳想再輸入壹次嗎?y '/' N ' " & lt;& ltendl
CIN & gt;& gtflag 1;
}
其他
打破;
}
while(flag 1 = = ' Y ' | | flag 1 = = ' Y ');
boy.in_file(outp,n);
刪除[]outp;
打破;
}
案例6:
{
outp =新生[n];
boy . readout(outp);
boy . out _ put(outp);
打破;
}
}
}
而(m!=7);
返回0;
}
簡單提醒壹下,要多接觸自己,不然很難學好。