• 2022.6.26 C++——使用面向对象的思想实现栈和日期


    1. 实现日期
    //显示日期类
    class Date
    {
    private:
    	int year;
    	int month;
    	int day;
    	bool tag;
    	bool Is_Leap(int year)//判断是否为闰年
    	{
    		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    	}
    	int Get_YM_Day(int year, int month)
    	{
    		static int day[] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
    		                 //   0  1  2  3  4  5  6  7  8  9  10 11 12  
    		if (month == 2 && Is_Leap(year))
    		{
    			month = 0;
    		}
    		return day[month];
    	}
    public:
    	void Showdate() const
    	{
    		if (tag)
    		{
    			cout << "year: " << year << " month: " << month << " day:" << day << endl;
    		}
    		else
    		{
    			cout << "date is error" << endl;
    		}
    	}
    public:
    	Date(int y = 1, int m = 1,int d = 1):year(y),month(m),day(d),tag(true)
    	{
    		if (y < 1 || m < 1 || m>12 || d < 1 || d > Get_YM_Day(year,month))
    		{
    			tag = false;
    		}
    	}
    
    };
    int main()
    {
    	Date d1(2022, 2, 31); 
    	d1.Showdate();
    	Date d2(2022, 6, 26);
    	d2.Showdate();
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    运行结果:
    在这里插入图片描述
    2. 实现栈

    class Mystack
    {
    private:
    	enum{STACK_INIT_SIZE=10,STACK_INC_SIZE=2};
    	int* _data;//指向连续空间
    	int _capa;//容量大小
    	int _top;//栈顶指针,初始化为-1
    public:
    	Mystack() :_data(nullptr), _capa(STACK_INIT_SIZE), _top(-1)//缺省的构造函数
    	{
    		_data = new int[_capa];
    	}
    	Mystack(int sz) :_data(nullptr), _capa(sz), _top(-1)//带参的构造函数
    	{
    		_data = new int[_capa];
    	}
    	~Mystack()  //析构函数
    	{
    		delete[]_data;
    		_data = nullptr;
    		_capa = 0;
    		_top = -1;
    	}
    	size_t Size()//元素个数
    	{
    		return _top + 1;//初始化给的是-1,因此当前_top+1
    	}
    	bool Empty()  //判空
    	{
    		//return _top == -1;
    		return Size() == 0;
    	}
    	bool Full()  //判满
    	{
    		return Size() == _capa;
    	}
    	bool Push(int val) //入栈
    	{
    		if (Full())
    		{
    			return false;
    		}
    		else
    		{
    			_top = _top + 1;//_top++
    			_data[_top] = val;
    			//_data[++_top]=val;
    			return true;
    		}
    	}
    	int &Top()//取栈顶元素
    	{
    		return _data[_top];
    	}
    	const int& Top() const
    	{
    		return _data[_top];
    	}
    	bool Pop()//出栈
    	{
    		if (Empty())return false;
    		_top = _top - 1;
    		return true;
    	}
    	bool GetPop(int& val)//取栈顶和出栈一起
    	{
    		if (Empty())
    		{
    			return false;
    		}
    		else
    		{
    			val = _data[_top];
    			_top -= 1;
    			return true;
    		}
    	}
    };
    int main()
    {
    	Mystack mys;
    	for (int i = 0; i < 10; ++i)//入栈
    	{
    		cout << "Push" << i << " " << mys.Push(i) << endl;
    	}
    	/*while (!mys.Empty())
    	{
    		int x = mys.Top();
    		mys.Pop();
    		cout << x << endl;
    	}*/
    	int val;
    	while (mys.GetPop(val))
    	{
    		cout << val << endl;
    	}
    
    	return 0;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    运行结果:
    在这里插入图片描述

  • 相关阅读:
    抖音关键词搜索商品-API工具
    html5新增_canvas
    VUE路由总结
    GEE开发之Landsat_SR计算地表温度(不推荐)
    C++的动态分派在HotSpot VM中的重要应用
    LeetCode //C - 373. Find K Pairs with Smallest Sums
    M-LVDS收发器MS2111可pin对pin兼容SN65MLVD206
    k-means clustering algorithm,K均值算法
    【华为OD机试真题 python】发广播【2022 Q4 | 200分】
    (个人杂记)第九章 串口实验2
  • 原文地址:https://blog.csdn.net/weixin_58631717/article/details/125474264