08.23 C++ day3

 

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

class my_string
{
private:
    char *str;
    int len;

public:
    my_string()
    {
        this->str=NULL;
        this->len=0;
    }       //无参构造

    my_string(char* s)
    {
        int l=strlen(s);
        this->str=new char[l];
        this->len=l+1;
        strcpy(this->str,s);
        cout<<"str:"<<s<<endl;

    }     //有参构造

    my_string(const my_string& s)
    {
        this->str=new char[s.len];
        this->len=s.len;
        strcpy(this->str,s.str);
        cout<<"str:"<<this->str<<endl;
    }//拷贝构造

    my_string& operator=(const my_string& s)
    {
        if(this != &s)
        {
            this->str=new char[s.len];
            this->len=s.len;
            strcpy(this->str,s.str);
            cout<<"str:"<<this->str<<endl;
        }
        return *this;
    }//拷贝赋值

    bool my_empty()
    {
        if(len==0)
        {
            return true;
        }else{
            return false;
        }
    }
    //bool my_empty()   判空

    //int my_size()     求长度
    //char *my_str()    转化为C风格字符串
};



int main()
{

    my_string m1();

    my_string m2("abcd");

    my_string m3(m2);

    my_string m4("dcba");
    m4=m3;


    cout<<"this m2:";
    if(m2.my_empty())
    {
        cout<<"true"<<endl;
    }else{
        cout<<"flase"<<endl;
    }

    cout << "Hello World!" << endl;
    return 0;
}

代码实现:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您的要求,以下是Python代码实现: ``` import csv constellation_list = [ ('Aries', '03.21-04.19'), ('Taurus', '04.20-05.20'), ('Gemini', '05.21-06.21'), ('Cancer', '06.22-07.22'), ('Leo', '07.23-08.22'), ('Virgo', '08.23-09.22'), ('Libra', '09.23-10.23'), ('Scorpio', '10.24-11.22'), ('Sagittarius', '11.23-12.21'), ('Capricorn', '12.22-01.19'), ('Aquarius', '01.20-02.18'), ('Pisces', '02.19-03.20') ] def get_constellation(month, day): day_in_month = [ 0, # 0 位未使用 20, # 1 - 1 月 50, # 2 - 2 月 79, # 3 - 3 月 110, # 4 - 4 月 141, # 5 - 5 月 172, # 6 - 6 月 203, # 7 - 7 月 234, # 8 - 8 月 265, # 9 - 9 月 295, # 10 - 10 月 325, # 11 - 11 月 355, # 12 - 12 月 ] if month < 1 or month > 12 or day < 1 or day > 31: return "输入日期错误!" if day < day_in_month[month]: return constellation_list[month-1][0] else: return constellation_list[month][0] def get_constellation_by_date(date): try: year = int(date[0:4]) month = int(date[4:6]) day = int(date[6:8]) except: return "输入日期错误!" return get_constellation(month, day) with open('const.csv', 'r', encoding="UTF-8") as csvfile: reader = csv.reader(csvfile) for row in reader: name = row[0] month = int(row[1]) day = int(row[2]) correct_name = get_constellation(month, day) if correct_name != name: print('有错:{} 的生日是{}月{}日,应该是{}座,而不是{}座'.format(name, month, day, correct_name, name)) print(get_constellation_by_date('19930918')) print(get_constellation_by_date('20200231')) ``` 上面的代码中,我们在代码最开始定义了星座列表`constellation_list`,里面存放了每个星座的名字及其起止日期。 接着,我们定义了一个`get_constellation()`函数,用于根据月份和日期计算对应的星座名称。该函数的实现过程为:首先,创建了一个存放每月最后一天的列表`day_in_month`,其中0位未使用,1~12位为相应月份的最后一天;然后,如果输入的月份或日期不合法,就返回一个“输入日期错误!”的提示;否则,当输入的日期小于相应月份的最后一天时,返回相应的星座名称,否则返回下一个月份的星座名称。 接着,我们定义了一个`get_constellation_by_date()`函数,用于读取`const.csv`文件并根据用户输入的出生日期获取对应的星座名称。该函数首先尝试将输入日期的年、月、日解析成整数,如果出错则返回一个“输入日期错误!”的提示;否则,调用`get_constellation()`函数计算对应的星座名称并返回。 最后,我们读取了`const.csv`文件并分别调用了`get_constellation_by_date()`函数获取了两个日期的星座名称,分别为白羊座和“输入日期错误!”(因为2月份只有28/29天,而这里输入的是30号,所以日期不合法)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值