• [面向对象课]第二周上机


    6-5 日期类的设计与实现

    分数 30
    作者 范鹏程
    单位 内蒙古师范大学

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

    定义类MyDate:

    class MyDate
    

    主程序样例:

    #include <string>
    #include <iostream>
    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
    

    代码:

    int day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    string mon[]={"","January","February","March","April","May","June",
    			"July","August","September","October","November","December"};
    class MyDate{
    private:
    	int y,m,d;
    public:
    	bool leap(int y){
    		if(y%4)return 0;
    		if(y%100==0&&y%400)return 0;
    		return 1;
    	}
    	int get_d(int y,int m){
    		if(m!=2)return ::day[m];
    		if(leap(y))return 29;
    		return 28;
    	}
    	void setDate(int _m,int _d,int _y){
    		m=_m;d=_d;y=_y;
    		if(m<1||m>12||d<1||d>get_d(y,m)){
    			y=1900;m=1;d=1;
    		}
    	}
    	MyDate operator++(){
    		d++;
    		if(d>get_d(y,m)){d=1;m++;}
    		if(m>12){m=1;y++;}
    		return *this;
    	}
    	MyDate operator++(int){
    		MyDate t=*this;
    		d++;
    		if(d>get_d(y,m)){d=1;m++;}
    		if(m>12){m=1;y++;}
    		return t;
    	}
    	MyDate operator +=(int x){
    		for(int i=0;i<x;i++)(*this)++;
    		return *this;
    	}
    	friend ostream& operator<<(ostream &out,MyDate t){
    		out<<mon[t.m]<<' '<<t.d<<", "<<t.y;
    		return out;
    	}
    };
    

    这个题是最后一个过的,一开始看到要重载+=感觉有点恶心,然后试了一下发现直接暴力跑就能过,很傻比
    以及稍微熟悉一下重载运算符的语法(单目运算符一般重载为成员函数)。

    7-6 集合的模拟实现(函数模板)

    分数 40
    作者 余春艳
    单位 福州大学

    我们可以用一个数组来模拟集合,add运算用以实现集合元素的增加,delete运算用于实现集合元素的删除,find运算用以实现集合元素的查找,但是目前集合元素类型未知,可以是int、char、double等基本数据类型,也可以是String、Time、Student等对象类型,要求采用模板函数实现集合元素的增加、删除和查找功能。

    三个模板函数如下:

    int addSet(T * myset, T elem,int len)

    int deleSet(T * myset, T elem, int len)

    int findElem(T * myset, T elem, int len)

    其中,addSet向集合中添加一个元素,deleSet从集合中删除一个元素,findElem判断elem是否是集合成员,三个函数分别返回元素插入位置,删除位置和存在位置。

    主函数有如下数据成员 :

    int intSet[100]

    double douSet[100]

    String StrSet[100] 分别是int类型、double类型、String的数组集合。

    int intLen, douLen, strLen分别是int类型、double类型、String的数组集合的长度

    完成上述函数模板和主函数,主函数根据输入的信息,建立初始的空集合,调用三个模板函数分别对intSet、douSet和StrSet执行相应的操作,并输出对应的集合信息。

    输入格式:

    每一行为一个集合操作,每行的第一个数字为集合元素类型,1为整型元素,2为浮点型元素,3为String类型,第二个数字为集合操作类型,1为插入,2为删除,3为查找,第三个为集合元素,集合元素类型视第一个数字给定的集合元素类型而定。输入0时标志输入结束。

    输出格式:

    输出当前操作的执行位置(插入位置、删除位置和存在位置)

    删除操作时,如果元素X不存在,输出“X is not exist!”。

    插入操作时,如果集合已满,输出“Full Set.”若元素已存在,输出“X is already exist!”

    查找操作时,如果找不到元素,输出“X is not exist!”。

    输入样例:

    1 1 1
    
    1 1 2
    
    1 3 1
    
    1 2 1
    
    1 2 3
    
    1 3 1
    
    2 1 1.1
    
    2 1 2.2
    
    2 1 3.3
    
    2 3 1.1
    
    2 2 2.2
    
    2 2 2.2
    
    3 1 abc
    
    3 1 bcd
    
    3 3 abc
    
    3 2 abc
    
    3 3 abc
    
    0
    

    输出样例:

    0
    
    1
    
    0
    
    0
    
    3 is not exist!
    
    1 is not exist!
    
    0
    
    1
    
    2
    
    0
    
    1
    
    2.2 is not exist!
    
    0
    
    1
    
    0
    
    0
    
    abc is not exist!
    

    代码:

    #include <iostream>
    #define endl '\n'
    using namespace std;
    
    template<typename T>
    int addSet(T * myset, T elem,int len){
    	if(len>=100){
    		cout<<"Full Set."<<endl;
    		return -1;
    	}
    	for(int i=0;i<len;i++)if(elem==myset[i]){
    		cout<<elem<<" is already exist!"<<endl;
    		return -1;
    	}
    	myset[len]=elem;
    	return len;
    }
    template<typename T>
    int deleSet(T * myset, T elem, int len){
    	for(int i=0;i<len;i++)if(myset[i]==elem){
    		for(int j=i+1;j<len;j++)myset[j-1]=myset[j];
    		return i;
    	}
    	cout<<elem<<" is not exist!"<<endl;
    	return -1;
    }
    template<typename T>
    int findElem(T * myset, T elem, int len){
    	for(int i=0;i<len;i++)if(myset[i]==elem){
    		cout<<i<<endl;
    		return i;
    	}
    	cout<<elem<<" is not exist!"<<endl;
    	return -1;
    }
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);cout.tie(0);
    	int intSet[100];
    	double douSet[100];
    	string strSet[100];
    	int intLen=0,douLen=0,strLen=0;
    	int op,ty;
    	while(cin>>ty&&ty){
    		cin>>op;
    		if(op==1){
    			if(ty==1){
    				int x;cin>>x;
    				int r=addSet(intSet,x,intLen);
    				if(r!=-1){
    					intLen++;
    					cout<<r<<endl;
    				}
    			}else if(ty==2){
    				double x;cin>>x;
    				int r=addSet(douSet,x,douLen);
    				if(r!=-1){
    					douLen++;
    					cout<<r<<endl;
    				}
    			}else{
    				string x;cin>>x;
    				int r=addSet(strSet,x,strLen);
    				if(r!=-1){
    					strLen++;
    					cout<<r<<endl;
    				}
    			}
    		}else if(op==2){
    			if(ty==1){
    				int x;cin>>x;
    				int r=deleSet(intSet,x,intLen);
    				if(r!=-1){
    					intLen--;
    					cout<<r<<endl;
    				}
    			}else if(ty==2){
    				double x;cin>>x;
    				int r=deleSet(douSet,x,douLen);
    				if(r!=-1){
    					douLen--;
    					cout<<r<<endl;
    				}
    			}else{
    				string x;cin>>x;
    				int r=deleSet(strSet,x,strLen);
    				if(r!=-1){
    					strLen--;
    					cout<<r<<endl;
    				}
    			}
    		}else{
    			if(ty==1){
    				int x;cin>>x;
    				findElem(intSet,x,intLen);
    			}else if(ty==2){
    				double x;cin>>x;
    				findElem(douSet,x,douLen);
    			}else{
    				string x;cin>>x;
    				findElem(strSet,x,strLen);
    			}
    		}	
    	}
    	return 0;
    }
    

    (这个String确实是string)

  • 相关阅读:
    发送邮件以及数据导出
    GridView的使用(高度封装,不怎么灵活,repeat可替代)
    索引学习(一)
    JVM 学习笔记(二)
    JVM 学习笔记(一)
    JDBC 基础知识总结
    需要学习的点
    The Unified Modeling Language(UML)
    向往2的年代
    SQL 各种连接:内连接,外连接(左外,右外,完全外)
  • 原文地址:https://www.cnblogs.com/yoshinow2001/p/16336989.html
Copyright © 2020-2023  润新知