PTA 11

6-1 学生成绩的输入和输出(运算符重载) (25分)
现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。

输入时,首先要输入学生数(正整数)N。接着输入N组学生成绩,每组成绩包括两项:第一项是学生姓名,第二项是学生的成绩(整数)。

输出时,依次输出各个学生的序号(从1开始顺序编号),学生姓名,成绩等级(不小于60为PASS,否则为FAIL)

函数接口定义:
面向Student类对象的流插入和流提取运算符
裁判测试程序样例:
#include
#include
using namespace std;

/* 请在这里填写答案 */

int main(){
int i, repeat;
Student st;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>st;
cout<<st<<endl;
}
return 0;
}

输入样例:
3
Li 75
Zhang 50
Yang 99
输出样例:

  1. Li PASS
  2. Zhang FAIL
  3. Yang PASS

class Student{
   string name;
    int grade;
public:
  friend istream &operator>>(istream & in,Student & a){
  	in>>a.name>>a.grade;
  	return in;
  }
  friend ostream &operator<<(ostream & ot,Student & a){
  	static int c=1;
  	ot<<c<<". "<<a.name;
  	if(a.grade>=60)ot<<" PASS";
  	else ot<<" FAIL";
  	
  	c++;
  	return ot;
  }




};

6-2 日期类的设计与实现 (25分)
使用重载运算符(++,+=,<<等)实现日期类的操作。功能包括: 1)设置日期,如果日期设置不符合实际,则设置为默认日期(1900年1月1日) 2)在日期对象中向日期添加1或者加若干天(加入日期值后根据实际的日期进行月份、年份的变化) 3)重载流插入运算符进行日期的输出,其中月份要用名称表示

定义类MyDate:
class MyDate
主测试程序样例:
#include
#include
using namespace std;

/* 请在这里填写答案 */

int main()
{
int m,d,y;
MyDate d1,d2,d3;
cin>>m>>d>>y;
d1.setDate(m,d,y);

cin>>m>>d>>y;
d2.setDate(m,d,y);

cin>>m>>d>>y;
d3.setDate(m,d,y);

cout << "d1 is " << d1 << "\nd2 is " << d2;
cout << "\n\nd1 += 7 is " << ( d1 += 7 );
cout << "\n\n d2 is " << d2;
cout << "\n++d2 is " << ++d2;
cout << "\n\nTesting the prefix increment operator:\n"<< " d3 is " << d3 << endl;
cout << "++d3 is " << ++d3 << endl;
cout << " d3 is " << d3;
cout << "\n\nTesting the postfix increment operator:\n"<< " d3 is " << d3 << endl;
cout << "d3++ is " << d3++ << endl;
cout << " d3 is " << d3 <<endl;

}

输入样例:
在这里给出一组输入。例如:

13 38 100
12 31 2009
2 28 2000
输出样例:
在这里给出相应的输出。例如:

d1 is January 1, 1900
d2 is December 31, 2009

d1 += 7 is January 8, 1900

d2 is December 31, 2009
++d2 is January 1, 2010

Testing the prefix increment operator:
d3 is February 28, 2000
++d3 is February 29, 2000
d3 is February 29, 2000

Testing the postfix increment operator:
d3 is February 29, 2000
d3++ is February 29, 2000
d3 is March 1, 2000

//要判断day+1后是不是到了下一个月,是不是到了下一年。后面的后置++和+=调用前面重载好的前置++。

class MyDate{
  private:
  	 int year,month,day;
 public:
 	   bool isleap()const{
	 	if((year%4==0&&year%100!=0)||(year%400==0)){
	 		return true;
		 }
		 return false;
	 }
 	   void setDate(int a,int b,int c){
 	    year=(c<1900)?1900:c;
 	    month=(a<1||a>12)?1:a;
 	    switch(a){
 	    	case 4:case 6:case 9:case 11:{
 	    		day=(b>=1&&b<=30)?b:1;
				break;
			 }
		case 2:
		   	if(isleap()){
		   		day=(b>=1&&b<=29)?b:1;
			}
			else day=(b>=1&&b<=28)?b:1;
			break; 
		   default:day=(b<1||b>31)?1:b;
		 }
 	    
	 }
	
	 bool ismonthend()const{
	 	  switch(month){
 	    	case 4:case 6:case 9:case 11:{
 	    	   return  day==30;
			 }
		case 2:
		   	if(isleap()){
		   		return day==29;
			}
			else return day==28;
		default:return day==31;
		 }
	 }
	 MyDate &operator++(){
	 	if(ismonthend()){
	 		if(month==12){
	 			year++;
	 			month=1;
	 			day=1;
			 }
			 else {
			 	month++;
			 	day=1;
			 }
		 }
		 else day++;
		 return *this;
	 }
	const MyDate operator ++(int){
	 	 MyDate old=*this;
	 	    ++(*this);
	 	    return old;
	 }
	 MyDate &operator+=(int a){
	     for(int i=0;i<a;i++)
		++(*this);
	     return *this;
	 }
	friend ostream & operator <<(ostream &ot,const MyDate & d){
	const string m[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
	ot<<m[d.month]<<" "<<d.day<<", "<<d.year;
	return ot;
	}

};

6-3 有序数组(类模板) (25分)
实现一个类模板,它可以接受一组数据,能对数据排序,也能输出数组的内容。

每行输入的第一个数字为0,1,2或3:为0时表示输入结束; 为1时表示将输入整数,为2时表示将输入有一位小数的浮点数,为3时表示输入字符。

如果第一个数字非0,则接下来将输入一个正整数,表示即将输入的数据的数量。

从每行第三个输入开始,依次输入指定类型的数据。

类模板:
template
class MyArray
裁判测试程序样例:
#include
using namespace std;

/* 请在这里填写答案 */

template
MyArray::~MyArray(){ delete[] data;}

template
bool MyArray::check(){
int i;
for(i=0;i<size-1;i++)
if(data[i]>data[i+1]) { cout<<“ERROR!”<<endl;return false;}
return true;
}
int main( )
{
MyArray *pI;
MyArray *pF;
MyArray *pC;
int ty, size;
cin>>ty;
while(ty>0){
cin>>size;
switch(ty){
case 1: pI = new MyArray(size); pI->sort(); pI->check(); pI->display(); delete pI; break;
case 2: pF = new MyArray(size); pF->sort(); pF->check(); pF->display(); delete pF; break;
case 3: pC = new MyArray(size); pC->sort(); pC->check(); pC->display(); delete pC; break;
}
cin>>ty;
}
return 0;
}

输入样例:
1 3 2 3 1
2 4 1.5 2.6 3.7 0.5
3 2 A a
0
输出样例:
1 2 3
0.5 1.5 2.6 3.7
A a

template <class T>
class MyArray{
   T*data;
   int size;
public:
	MyArray(int s){
	     size=s;
	     data=new T[s];
	     for(int i=0;i<s;i++){
		cin>>data[i];
	     }
	}
	void sort(){

        for(int i=0; i < size -1; i++){
		int minn=i;
           for(int j=i+1; j < size ; j++)
		if(data[j]<data[minn])
			minn=j;
		swap(data[i],data[minn]);

           

	}
	}
	void display(){
		for(int i=0;i<size-1;i++)
			cout<<data[i]<<" ";
		cout<<data[size-1]<<endl;
	}
	~MyArray();
   bool check();
};

6-4 vector (25分)
本题要求实现一个Vector类模板,能实现数据的存储和访问。通过[]运算符访问时只能访问已经存在的元素,而通过add()方法访问时可以自动扩展内部存储空间。

注意,这个Vector的行为和std::vector是不同的

函数接口定义:
template
class Vector {

裁判测试程序样例:
#include
using namespace std;

/* 请在这里填写答案 */

int main()
{
Vector vint;
int n,m;
cin >> n >> m;
for ( int i=0; i<n; i++ ) {
// add() can inflate the vector automatically
vint.add(i);
}
// get_size() returns the number of elements stored in the vector
cout << vint.get_size() << endl;
cout << vint[m] << endl;
// remove() removes the element at the index which begins from zero
vint.remove(m);
cout << vint.add(-1) << endl;
cout << vint[m] << endl;
Vector vv = vint;
cout << vv[vv.get_size()-1] << endl;
vv.add(m);
cout << vint.get_size() << endl;
}

输入样例:
100 50
输出样例:
100
50
99
51
-1
100


template <class T>
class Vector{
   private:
   	T* data;
   	int size;
   	int maxsize;
   public:
   //数组下标初始为0,申请一个大小为maxx的空间
	Vector():size(0),maxsize(100){
	    data=new T[maxsize];
	}
	//自己写拷贝构造函数,进行深拷贝。
	Vector(const  Vector&v){
	    size=v.size;
	    
	    data=new T[v.maxsize];
	    
	    for(int i=0;i<v.size;i++){
		data[i]=v.data[i];
	    }
	}
	T& operator[](int i)const{
		if(i<0||i>=size)exit(-1);//下标异常  退出程序
		 return data[i];
	}
	int add(const T &x){
	   if(size==maxsize){
	   	maxsize*=2;
	   	T*temp=new T[maxsize];
	   	for(int i=0;i<size;i++)
	   		temp[i]=data[i];
		delete []data;
		data=temp;
	   }
	   data[size++]=x;
	   return size-1;
	    
	
	}
	void remove(int i){
	  if(i<0||i>=size)exit(-1);
	  for(int j=i+1;j<size;j++){
		data[j-1]=data[j];//从右到左覆盖前面的数
	  }
	   size--;
	}

	~Vector(){
		delete []data;
	}
	int get_size()const{
		return size;
	}
};
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值