#include<iostream> using namespace std; struct student { int num; char name[10]; int score1; int score2; int score3; float aver; }; void main() { void input(struct student stu[]); void scoresort(struct student stu[]); void output(struct student stu[]); struct student stu[3],*p=stu; input(p); scoresort(p); output(p); } void input(struct student stu[]) { int i; printf("NUM\tNAME\tSCORE1\tSCORE2\tSCORE3\n"); for(i=0;i<3;i++) { scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].score1,&stu[i].score2,&stu[i].score3); stu[i].aver=(stu[i].score1+stu[i].score2+stu[i].score3)/3; } printf("\n"); cout<<"Before Sort:"<<endl; printf("NUM\tNAME\tSCORE1\tSCORE2\tSCORE3\tAVERAGE\n"); for(i=0;i<3;i++) printf("%d\t%s\t%d\t%d\t%d\t%f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].aver); printf("\n"); } void scoresort(struct student stu[]) { int i=0,j,k; struct student p; for(;i<2;i++) { k=i; for(j=i+1;j<3;j++) if(stu[j].aver>stu[k].aver) k=j; if(k!=i) { p=stu[i];stu[j]=stu[k];stu[k]=p; } } void output(struct student stu[]) { int i; cout<<"After Sort:"<<endl; printf("NUM\tNAME\tSCORE1\tSCORE2\tSCORE3\tAVERAGE\n"); for(i=0;i<3;i++) printf("%d\t%s\t%d\t%d\t%d\t%f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].aver); printf("\n"); }
提示错误:
c:\documents and settings\administrator\local settings\temp\vmware-administrator\vmwarednd\17b02f10\55555.cpp(92) : error C2601: “output” : local function definitions are illegal
c:\documents and settings\administrator\local settings\temp\vmware-administrator\vmwarednd\17b02f10\55555.cpp(105) : fatal error C1004: unexpected end of file found
错哪了,要咋改啊!
解决方案
10
scoresort里面少了些 } 右大括号
16
大括号不对称,这样改:
#include<iostream> using namespace std; struct student { int num; char name[10]; int score1; int score2; int score3; float aver; }; void main() { void input(struct student stu[]); void scoresort(struct student stu[]); void output(struct student stu[]); struct student stu[3],*p=stu; input(p); scoresort(p); output(p); } void input(struct student stu[]) { int i; printf("NUM\tNAME\tSCORE1\tSCORE2\tSCORE3\n"); for(i=0;i<3;i++) { scanf("%d%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].score1,&stu[i].score2,&stu[i].score3); stu[i].aver=(stu[i].score1+stu[i].score2+stu[i].score3)/3; } printf("\n"); cout<<"Before Sort:"<<endl; printf("NUM\tNAME\tSCORE1\tSCORE2\tSCORE3\tAVERAGE\n"); for(i=0;i<3;i++) printf("%d\t%s\t%d\t%d\t%d\t%f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].aver); printf("\n"); } void scoresort(struct student stu[]) { int i=0,j,k; struct student p; for(;i<2;i++) { k=i; for(j=i+1;j<3;j++) if(stu[j].aver>stu[k].aver) k=j; if(k!=i) { p=stu[i];stu[j]=stu[k];stu[k]=p; } } } void output(struct student stu[]) { int i; cout<<"After Sort:"<<endl; printf("NUM\tNAME\tSCORE1\tSCORE2\tSCORE3\tAVERAGE\n"); for(i=0;i<3;i++) printf("%d\t%s\t%d\t%d\t%d\t%f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].aver); printf("\n"); }
2
函数声明,建议不要放在main函数里,放在函数之外,main函数之上最好;