📄 subject_66179.htm
字号:
<p>
序号:66179 发表者:各个各个 发表日期:2003-12-23 02:00:05
<br>主题:一个c++问题
<br>内容:有一个题目是<BR>给出10个同学数据,(姓名,性别,三门课成绩)<BR>然后按三门课成绩的总分排序,并输出到一个文件。<BR>我写了代码<BR>可是编译有错,<BR>各位高手帮我看看。<BR>#include <iostream.h><BR>#include <windows.h><BR>#include <fstream.h><BR>#include <iomanip.h><BR>#define N 10<BR>class student<BR>{public: <BR> char name[10];<BR> char sex[10];<BR> int score[3];<BR> int zf;<BR> public: <BR> void set(char *na,int a,int b,int c)<BR> {strcpy(name,na);<BR> score[0]=a; <BR> score[1]=b;<BR> score[2]=c;<BR> zf=a+b+c;<BR> }<BR> void show()<BR> {cout<<setw(10)<<name<<setw(10)<<sex<<setw(6)<<score[0];<BR> cout<<setw(6)<<score[1];<BR> cout<<setw(6)<<score[2];<BR> cout<<setw(6)<<zf<<endl;<BR> }<BR> int tyzf(){return zf;}<BR> int tysex(){return sex;)<BR>};<BR><BR>void main()<BR>{char name[10];<BR> int i,j,x,y,z;<BR> student stu[N],temp;<BR> fstream myfile;<BR> myfile.open("f:\\tang\\f1.txt",ios::in);<BR> if(!myfile)<BR> {cout<<"can not open the file\n"<<endl;<BR> }<BR> cout<<"未排序前"<<endl;<BR> for(i=0;i<N;i++)<BR> {myfile>>na>>sex>>x>>y>>z;<BR> stu[i].set(na,sex,x,y,z);<BR> stu[i].show();<BR> }<BR> myfile.close(); <BR> cout<<"按总分排序后"<<endl;<BR> for(i=0;i<N-1;i++)<BR> for(j=i+1;j<N;j++)<BR> if (stu[i].tyzf()<stu[j].tyzf())<BR> {temp=stu[i];stu[i]=stu[j];stu[j]=temp;}<BR> for(i=0;i<N;i++)<BR> stu[i].show();<BR>fstream file;<BR> file.open("f:\\tang\\f2.txt",ios::out);<BR> for(i=0;i<N;i++)<BR> {<BR> file<<stu[i].name<<' '<<stu[i].sex<<' '<<stu[i].score[0]<<' '<BR><<stu[i].score[1]<<' '<<stu[i].score[2]<<' '<<stu[i].zf<<endl;<BR>}<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:kobo 回复日期:2003-12-23 03:55:53
<br>内容:<BR>#include <windows.h><BR>#include <iostream.h><BR>#include <fstream.h><BR>#include <iomanip.h><BR><BR>#define N 10<BR><BR>class student<BR>{<BR>public: <BR> char name[10];<BR> char sex[10];<BR> int score[3];<BR> int zf;<BR>public: <BR> void set(char na[],char se[],int a,int b,int c)<BR> {<BR> strcpy(name,na);<BR> strcpy(sex,se);<BR> score[0]=a; <BR> score[1]=b;<BR> score[2]=c;<BR> zf=a+b+c;<BR> }<BR> void show()<BR> {<BR> cout<<setw(10)<<name<<setw(10)<<sex<<setw(6);<BR> cout<<setw(6)<<score[0];<BR> cout<<setw(6)<<score[1];<BR> cout<<setw(6)<<score[2];<BR> cout<<setw(6)<<zf<<endl;<BR> }<BR> int tyzf(){return zf;}<BR>// int tysex(){return sex;}<BR>};<BR><BR>void main()<BR>{ <BR> char name[10],sex[10];<BR><BR> <BR> int i,j,x,y,z;<BR> x=y=z=0;<BR> student stu[N],temp;<BR> fstream myfile;<BR> myfile.open("D:\\f1.txt",ios::in);<BR> if(!myfile)<BR> {cout<<"can not open the file\n"<<endl;}<BR><BR> cout<<"未排序前"<<endl;<BR> for(i=0;i<N;i++)<BR> {<BR> myfile>>name>>sex>>x>>y>>z;<BR> stu[i].set(name,sex,x,y,z);<BR> stu[i].show();<BR> }<BR> myfile.close(); <BR> <BR> cout<<"按总分排序后"<<endl;<BR> for(i=0;i<N-1;i++)<BR> for(j=i+1;j<N;j++)<BR> if (stu[i].tyzf()<stu[j].tyzf())<BR> {temp=stu[i];stu[i]=stu[j];stu[j]=temp;}<BR> for(i=0;i<N;i++)<BR> stu[i].show();<BR> <BR> fstream file;<BR> file.open("d:\\f2.txt",ios::out);<BR> for(i=0;i<N;i++)<BR> file<<stu[i].name<<' '<<stu[i].sex<BR> <<' '<<stu[i].score[0]<<' '<<stu[i].score[1]<BR> <<' '<<stu[i].score[2]<<' '<<stu[i].zf<<endl;<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:各个各个 回复日期:2003-12-23 04:27:35
<br>内容:太感谢你了 <BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -