哪位高手帮忙看下我的C++程序哪里出错了(急!!!)
#include<iostream.h>#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<iomanip.h>
class Student
{
private:
char name[10];
char sex[4];
int age;
char number[18];
char school[40];
int chinese;
int math;
int english;
char cho1[10];
char cho2[10];
int sum;
public:
void set(char n[10],char s[4],int a,char num[18],char sh[40],int chi,int mat,int eng,char c1[10],char c2[10])
{
strcpy(name,n);
strcpy(sex,s);
age=a;
strcpy(number,num);
strcpy(school,sh);
chinese=chi;
math=mat;
english=eng;
strcpy(cho1,c1);
strcpy(cho2,c2);
sum=chinese+math+english;
}
void show();
char *getname(){return name;}
char *getsex(){return sex;}
int getage(){return age;}
char *getnumber(){return number;}
char *getschool(){return school;}
int getchinese(){return chinese;}
int getmath(){return math;}
int getenglish(){return english;}
char *choose1(){return cho1;}
char *choose2(){return cho2;}
int getsum(){return sum;}
};
void Student::show()
{
cout<<setiosflags(ios::left)<<setw(10)<<name<<setw(3)<<sex<<setw(3)<<age<<setw(14)<<number<<setw(18)<<school<<setw(4)<<chinese<<setw(4)<<math<<setw(4)<<english<<setw(3)<<cho1<<setw(3)<<cho2<<setw(3)<<sum<<endl;
}
struct node
{
Student student;
node *next;
};
int count=0;
node *OpenFile()
{
fstream infile("data.txt",ios::in|ios::nocreate);
if(!infile)
{
cout<<"can not open file\n";
exit(0);
}
node *p1,*p2,*head=0;
char n[20];char s[4];int a;char num[20];char sh[40];int chi;int mat;int eng;char c1[10];char c2[10];
while(infile>>n)
{
infile>>s>>a>>num>>sh>>chi>>mat>>eng>>c1>>c2;
p1=new node;
Student stu;
stu.set(n,s,a,num,sh,chi,mat,eng,c1,c2);
p1->student=stu;
if(head==0) {
head=p1;p2=p1;}
else {p2->next=p1;p2=p1;count++;}
}
p2->next=0;
infile.close();
return head;
}
void Show(node *head)
{
node *p=head;
while(p)
{
p->student.show();
p=p->next;
}
}
node *sort(node *head)
{
node *p;Student stu;
for(int i=0;i<count;i++)
{
p=head;
for(int j=0;j<count-i;j++)
{
if(p->student.getsum()<=p->next->student.getsum())
{
stu=p->student;p->student=p->next->student;p->next->student=stu;
}
p=p->next;
}
}
return head;
}
void tongji(node *head)
{
ofstream outfile("分数段统计.txt",ios::out);
if(!outfile)
{
cout<<"不能打开文件\n";
exit(0);
}
node *p=head;
for(int m=head->student.getsum();m>=5;m=m-5)
{
int a=0;
while((m-5)<p->student.getsum()&&p->next!=NULL)
{
if((m-5)<p->student.getsum()&&p->student.getsum()<=m)
{a++;}
p=p->next;
}
cout<<"("<<m-5<<","<<m<<"]"<<' '<<a<<endl;
}
}
void main()
{
node *head=0,*head1=0;
head=OpenFile();
head=sort(head);
cout<<"总分排名:\n";
Show(head);
tongji(head);
}
数据格式 崔绢 女 18 083201251253 泰安中学 134 142 134 A+ A
陈雪 女 17 083210253205 南通一中 142 128 124 A A+
卞磊 男 18 083205325142 南京一中 138 142 125 B B+
陈小冬 男 18 083256254523 南京外国语学校 125 134 105 A A
朱留军 男 18 083215235214 江阴中学 124 105 132 A B+
姜小昆 男 17 083201532541 安口中学 112 123 102 A B+
冯坤 女 18 083201523211 姜堰中学 109 132 120 A B
分数段人数统计时会少一个数据 如有6个数据 但分数段人数统计时只有5人(但总分排序人数没少 是正确的)