#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
class Student
{
private:
string name;
int cplus;
int math;
int english;
int sum;
static int num;
void Sum(){sum=cplus+english+math;}
public:
Student(){}
~Student(){num--;}
Student *next;
string Name(){return name;}
int Cplus(){return cplus;}
int Math(){return math;}
int English(){return english;}
static int GetNum(){return num;}
friend istream& operator>>(istream& is,Student &ss)
{
is>>ss.name>>ss.cplus>>ss.math>>ss.english;
++num;
ss.Sum();
return is;
}
friend ostream&operator<<(ostream& os,const Student &ss)
{
os<<ss.name<<"\t"<<ss.cplus<<"\t"<<ss.math<<"\t"<<ss.english<<"\t"<<ss.sum;
return os;
}
};
//class StudentList
//{
//private:
// int n;
// Student *head;
//public:
// StudentList(int nn=200):n(nn)
// {
// Student*p,*q;
// for(int i=0;i<n;i++)
// {
// p=new Student;
// if(0==i)
// head=p;
// else q->next=p;
// q=p;
// }
// p->next=NULL;
// }
// ~StudentList()
// {
// Student*p=head;
// while(p!=NULL)
// {
// Student*q=p;
// p=p->next;
// delete q;
// }
// }
// void append(Student *ss)
// {
// if(head==NULL)
// head=ss;
// }
//};
int Student::num=0;
int main()
{
ifstream fin;
fin.open("score.txt");
if(!fin){cerr<<"文件未能打开。。";exit(1);}
Student *head=NULL;
Student *p1,*p2;
// Student *p1=new Student
// fin>>*p1;head=p1;cout<<*head<<endl;
while(!fin.eof())
{
p1=new Student;
fin>>*p1;
if(!head)head=p1;
else p2->next=p1;
p2=p1;
}
p1->next=NULL;
fin.close();
ofstream fout("score2.txt");
Student *q=head;
for(int i=0;i<Student::GetNum();i++)
{
if((*q).Cplus()>=60&&(*q).Math()>=60&&(*q).English()>=60)
{
fout<<*q<<endl;
q=q->next;
}
else q=q->next;//没有else程序崩溃,原因应该是结尾访问超出界限吧
}
fout.close();
return 0;
}
挂科的不进新名单(链表)(没答案)
最新推荐文章于 2019-12-15 21:15:16 发布