SDUST 第四次作业

本文介绍了一种使用C++进行数据封装的方法,并详细探讨了运算符重载的应用,包括二元运算符、赋值运算符等。通过具体实例展示了时间类中运算符重载的实现。
/*这周的作业难度整体不大,主要是新学了一种封装类的方法,对于有联系的数字进行封装,
如时间这种东西,同时还有的便是,对一些非法数据的处理,采用这种方法很方便,
再有新学的便是,运算符重载,
运算符重载主要分为: 1对于简单二元运算符的重载参照原来的运算法则进行重载,不可随意改变;
2对赋值运算符()的重载,这个在类里面很常用,因为自定义数据类型进容器有时需要排序,所以这就需要有一个
自定义的排序方式
3.要牢记:(因为这个让在回忆时记不起来了,盲点):对普通一元运算符的重载
4.对【】的重载
5.对输入输出流的重载。
运算符重载的简单形式:
*/
/*  int operator + (const Point &p, const Point &q)
    {
       return p.x_ + q.x_;
    }
注意:如果传入的参数t为const类型,则连t.gets()这个操作都不被允许
*/
/*附上最后一道题的代码,包含的知识点挺全的有输入输出操作符的重载。*/


#include<iostream>
#include<iomanip>
using namespace std;

class Time
{
public:
    int flag;
public:
    Time():hh(0), mm(0), ss(0), s_(0), flag(0) {}
    Time(Time & t) : hh(t.getH()), mm(t.getM()), ss(t.getS()), s_(t.getS_()), flag(t.getF()) {}
    int getF() {return flag;}
    int getH() {hh = s_/3600; return hh;}
    int getM() {mm = (s_%3600)/60; return mm;}
    int getS() {ss = s_ % 60; return ss; }
    int getS_(){s_ = hh * 3600 + mm * 60 + ss; return s_;}
    Time& operator++() // 前增
    {
        if(flag == 0)
            {s_ = s_ + 1 + 86400; s_ = s_ % 86400;}
        return *this;
    }
    Time& operator++(int)
    {
        Time tt(*this);
        if(flag == 0)
        {
            s_ = s_ + 1 + 86400;
            s_ = s_ % 86400;
        }
        return tt;
    }
    Time& operator--()
    {
        if(flag == 0)
        {
            s_ = s_ - 1 + 86400;
            s_ = s_ % 86400;
        }
        return *this;
    }
    Time& operator--(int)
    {
        Time tt(*this);
        if(flag == 0)
        {
            s_ = s_ - 1 + 86400;
            s_ = s_ % 86400;
        }
        return tt;
    }
    friend ostream &operator<<(ostream &os, Time &t);
    friend istream &operator>>(istream &is, Time &t);
private:
    int hh, mm, ss, s_;
};


istream &operator>> (istream &is, Time &t)
{
    is >> t.hh >> t.mm >> t.ss;
    if(t.hh >= 24 || t.hh < 0 || t.mm >= 60 || t.mm  < 0 || t.ss >= 60 || t.ss < 0 || t.s_ > 86400)
        {
            t.flag = 1;
            if(t.hh == 24 && t.mm == 60 && t.ss == 60)
                t.flag = 2;
        }
    else t.flag = 0;
    t.s_ = t.hh * 3600 + t.mm * 60 + t.ss;
    return is;
}


ostream &operator<< (ostream &os, Time & t)
{
    if(t.flag == 0)
    {os << setw(2) << setfill('0') << t.getH() << ":";
     os << setw(2) << setfill('0') << t.getM() << ":";
     os << setw(2) << setfill('0') << t.getS();
    }
    else if(t.flag == 2)
    {
        os << "24:60:60";
    }
    else
    os << "error!!!" ;
    return os;
}

int main()
{
    Time t;
    int cases;
    cin>>cases;
    cout<<setw(8)<<left<<"++t"<<" ";
    cout<<setw(8)<<left<<"--t"<<" ";
    cout<<setw(8)<<left<<"t"<<" ";
    cout<<setw(8)<<left<<"t--"<<" ";
    cout<<setw(8)<<left<<"t++"<<" ";
    cout<<setw(8)<<left<<"t"<<right<<endl;
    for(int i = 1; i <= cases; ++i)
    {
        cin>>t;
        cout<<(++t)<<" ";
        cout<<(--t)<<" ";
        cout<<t<<" ";
        cout<<t--<<" ";
        cout<<t++<<" ";
        cout<<t<<endl;
    }
}
### 山东科技大学数据结构期末考试复习指南 对于准备参加山东科技大学数据结构期末考试的学生来说,了解考试形式和重点内容至关重要。根据往年的考试情况[^1],本学期的数据结构期末考试涵盖了多个重要方面: #### 考试概述 - **题型分布**:本次考试包括10道判断题、16道选择题以及三道函数题和两道编程题。 - **考试时间**:总时长为两个半小时。 #### 题目特点 - **基础题目为主**:大部分题目来源于平时作业,覆盖了从第一章的时间复杂度分析到最后一章的排序等内容。 - **难度适中**:相比前一年的试卷,整体难度有所降低,未涉及较为复杂的考研真题。 - **具体考点变化**:虽然今年的选择题没有涉及到KMP算法和关键路径的内容,但这并不意味着这些知识点不重要,在复习过程中仍需重视。 #### 关键知识点回顾 为了更好地应对可能出现在未来考试中的各类问题,建议重点关注以下几个方面的学习: - 时间复杂度计算方法及其应用实例; - 各种常见排序算法的工作原理及其实现方式; - 树形结构的操作,特别是关于二叉树的相关操作,比如删除特定子树等高级功能[^3]; ```cpp // 删除以x为根节点的子树示例代码 void removeSubtree(TreeNode* &root, int targetValue){ if (root == nullptr) return; // 如果当前结点是要移除的目标,则直接将其置为空并返回 if(root->val == targetValue){ deleteTree(root); root = nullptr; return; } // 递归处理左子树和右子树 removeSubtree(root->left ,targetValue); removeSubtree(root->right,targetValue); } // 辅助函数用于彻底清除整棵子树 void deleteTree(TreeNode*& node){ if(node != nullptr){ deleteTree(node->left ); deleteTree(node->right); delete node; node=nullptr; } } ``` 此段代码展示了如何通过递归来遍历整个二叉树,并找到指定值作为根节点的子树进行删除。需要注意的是,在实际编写程序时应当考虑边界条件和其他特殊情况下的正确性验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值