以下来自点击打开链接
#include <iostream>
#include <iomanip>
#include<algorithm>
#include <string>
#include <vector>
using namespace std;
class Problem
{
public:
Problem();
int GetGrade();
static void SetPunish();
private:
int accept_time;
int wrong_times;
static int punish;
};
Problem::Problem()
{
int temp;
cin>>temp;
if(temp<0)
{
this->wrong_times = -temp;
this->accept_time = 0;
}
else
{
if(temp==0)
{
this->wrong_times = 0;
this->accept_time = 0;
}
else
{
this->accept_time = temp;
if( cin.get()=='(')
{
cin>>this->wrong_times;
cin.get();
}
else
{
this->wrong_times = 0;
}
}
}
}
int Problem::GetGrade()
{
if( this->accept_time==0 )
{
return 0;
}
else
{
return this->accept_time + this->wrong_times * this->punish;
}
}
void Problem::SetPunish()
{
cin>>Problem::punish;
}
class Student
{
public:
Student();
void Calculate();
int GetGrade();
int GetAccept();
string& GetName();
void print();
static void SetPeople();
private:
string name;
Problem *questions;
int accept_problem;
int grade;
static int people;
};
Student::Student()
{
cin>>this->name;
this->questions = new Problem[this->people];
this->Calculate();
}
inline string& Student::GetName()
{
return this->name;
}
inline int Student::GetAccept()
{
return this->accept_problem;
}
inline int Student::GetGrade()
{
return this->grade;
}
void Student::Calculate()
{
int i, grade;
this->accept_problem = 0;
this->grade = 0;
for(i = 0; i < this->people; i++)
{
grade = this->questions[i].GetGrade();
if( grade )
{
this->grade += grade;
this->accept_problem += 1;
}
}
}
void Student::SetPeople()
{
cin>>Student::people;
}
bool compare(Student &a,Student &b);
bool compare(Student &a,Student &b)
{
if( a.GetAccept() < b.GetAccept() )
{
return false;
}
if( a.GetAccept() > b.GetAccept() )
{
return true;
}
if( a.GetGrade() < b.GetGrade() )
{
return true;
}
if( a.GetGrade() > b.GetGrade() )
{
return false;
}
if( a.GetName().compare( b.GetName() ) < 0 )
{
return true;
}
else
{
return false;
}
}
void Student::print()
{
cout<<setw(10)<<left<<this->GetName()<<" "<<right<<setw(2)<<this->GetAccept()<<" "<<right<<setw(4)\
<<this->GetGrade()<<endl;
}
int Problem::punish = 0;
int Student::people = 0;
int main()
{
Student *p;
vector<Student>students;
Student::SetPeople();
Problem::SetPunish();
while( cin.peek()!=EOF )
{
p = new Student();
students.push_back(*p);
cin.get();
}
sort(students.begin(),students.end(),compare);
int i = students.size(),j;
for(j = 0; j < i; j++)
{
students[j].print();
}
return 0;
}
设有以下输入:
1 20
Smith 10
John 20
John的第1个字符被该程序中cin.get()吃掉,变成了ohn。
另外,有时会多出1个数值全为0的记录。
问题出在cin.peek()!=EOF 这个条件,它不能及时地发挥作用。还是要判读入的人名是否为空。
还有,程序中的变量名people改为problem_number较为确切。
以下修改AC:
#include <iostream>
#include <iomanip>
#include<algorithm>
#include <string>
#include <vector>
using namespace std;
class Problem
{
public:
Problem();
int GetGrade();
static void SetPunish();
private:
int accept_time;
int wrong_times;
static int punish;
};
Problem::Problem()
{
int temp;
char c;
cin>>temp;
if(temp<0)
{
this->wrong_times = -temp;
this->accept_time = 0;
}
else
{
if(temp==0)
{
this->wrong_times = 0;
this->accept_time = 0;
}
else
{
this->accept_time = temp;
if( (c=cin.get())=='(')
{
cin>>this->wrong_times;
cin.get();
}
else
{
this->wrong_times = 0;
}
}
}
}
int Problem::GetGrade()
{
if( this->accept_time==0 )
{
return 0;
}
else
{
return this->accept_time + this->wrong_times * this->punish;
}
}
void Problem::SetPunish()
{
cin>>Problem::punish;
}
class Student
{
public:
Student();
void Calculate();
int GetGrade();
int GetAccept();
string& GetName();
void print();
static void SetPeople();
private:
string name;
Problem *questions;
int accept_problem;
int grade;
static int people;
};
Student::Student()
{
cin>>this->name;
if (!this->name.empty())//加
{
this->questions = new Problem[this->people];
this->Calculate();
}
}
inline string& Student::GetName()
{
return this->name;
}
inline int Student::GetAccept()
{
return this->accept_problem;
}
inline int Student::GetGrade()
{
return this->grade;
}
void Student::Calculate()
{
int i, grade;
this->accept_problem = 0;
this->grade = 0;
for(i = 0; i < this->people; i++)
{
grade = this->questions[i].GetGrade();
if( grade )
{
this->grade += grade;
this->accept_problem += 1;
}
}
}
void Student::SetPeople()
{
cin>>Student::people;
}
bool compare(Student &a,Student &b);
bool compare(Student &a,Student &b)
{
if( a.GetAccept() < b.GetAccept() )
{
return false;
}
if( a.GetAccept() > b.GetAccept() )
{
return true;
}
if( a.GetGrade() < b.GetGrade() )
{
return true;
}
if( a.GetGrade() > b.GetGrade() )
{
return false;
}
if( a.GetName().compare( b.GetName() ) < 0 )
{
return true;
}
else
{
return false;
}
}
void Student::print()
{
cout<<setw(10)<<left<<this->GetName()<<" "<<right<<setw(2)<<this->GetAccept()<<" "<<right<<setw(4)\
<<this->GetGrade()<<endl;
}
int Problem::punish = 0;
int Student::people = 0;
int main()
{
Student *p;
vector<Student>students;
Student::SetPeople();
Problem::SetPunish();
while( cin.peek()!=EOF )
{
p = new Student();
if (!p->GetName().empty())//加
students.push_back(*p);
//删 cin.get();
}
sort(students.begin(),students.end(),compare);
int i = students.size(),j;
for(j = 0; j < i; j++)
{
students[j].print();
}
return 0;
}