获赞
1.7W
粉丝
2938
关注
396
看过 TA
6363
中国传媒大学
2018
运营
IP属地:北京
有问题请找社区小助手,此号暂停使用
私信
关注
2018-07-21 15:48
已编辑
社区规范更新号
Java方向活动帖:【牛客带你学编程】【Java方向】0基础小白入门培养计划!   牛客带你学编程活动总贴:【牛客带你学编程】0基础小白入门培养计划!      Java项目练习:第1期 练习时间:1月22日-2月5日(2周) 活动规则:     每一期一个项目,届时会开新帖发布        学员直接将答案提交到该贴评论区即可        两周后,公布导师参考答案        导师评选出当期最佳代码        奖励:牛客大礼包一份(牛客定制水杯 牛客定制笔...
温柔小猪:import java.util.Scanner; public class Main { public static boolean isLeapYear(int year) { return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int year = 0; while (in.hasNext()) { year = in.nextInt(); System.out.println("请输入任意一个年份:"); if (year < 0) { System.out.println("输入为错误的年份"); } else { if (isLeapYear(year)) System.out.println(year + "是闰年!"); else System.out.println(year + "是平年"); } } in.close(); } }
0 点赞 评论 收藏
分享
2018-02-27 11:31
已编辑
社区规范更新号
C++方向活动帖:【牛客带你学编程】【C++方向】0基础小白入门培养计划!&nbsp;&nbsp;&nbsp;牛客带你学编程活动总贴:【牛客带你学编程】0基础小白入门培养计划!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C++项目练习:第1期&nbsp;&nbsp;&nbsp;练习时间:1月22日-2月5日(2周)&nbsp;&nbsp;&nbsp;活动规则:&nbsp;&nbsp;&nbsp;每一期一个项目,届时会开新帖发布&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;学员直接将答案提交到该贴评论区即可&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;两周后,公布导师参考答案&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;导师评选出当期最佳代码&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;奖励:牛客大礼包一份(牛客定制水杯&nbsp;牛客定制笔&nbsp;牛客定制程...
温柔小猪:/* Problem:编写MyString的构造函数、拷贝构造函数、析构函数和赋值函数 Author:QiZhao Data:2018-02-04 Description: Copyright 2018 QiZhao. All rights reserved. */ #include<cstring> #include<iostream> #include<cstdlib> using namespace std; class MyString { public: MyString (const char *str = NULL); MyString (const MyString &other); ~ MyString(void); MyString & operator = (const MyString & other); friend ostream& operator<<(ostream &os, MyString &str); private: char *m_data; }; //构造函数 MyString::MyString(const char *str) { if(str == NULL) { m_data = new char[1]; *m_data = '\0'; } else { int len = strlen(str); m_data = new char[len + 1]; if(m_data = NULL) { cout << "内存分配失败" << endl; exit(-1); } strcpy(m_data, str); } cout << "调用构造函数" << endl; } //拷贝构造函数 MyString::MyString(const MyString &other) { int len = strlen(other.m_data); m_data = new char[len + 1]; if(m_data = NULL) { cout << "内存分配失败" << endl; exit(-1); } strcpy(m_data, other.m_data); cout << "调用拷贝构造函数" << endl; } //析构函数 MyString::~MyString(void) { delete[]m_data; m_data = NULL; cout << "调用析构函数" << endl; } //赋值函数 MyString& MyString::operator= (const MyString & other) { if(this != &other) { delete[]m_data; int len = strlen(other.m_data); m_data = new char[len + 1]; if(m_data = NULL) { cout << "内存分配失败" << endl; exit(-1); } strcpy(m_data, other.m_data); } return *this; } //输出 ostream& operator<<(ostream &os, MyString &str) { os << str.m_data; return os; } int main() { char str[] = "abcdef"; char *p = NULL; cout << "**********************构造函数测试**********************" << endl; MyString test1(str); MyString test2(p); cout << "**********************拷贝构造函数测试**********************" << endl; MyString test(test1); cout << "**********************赋值函数测试**********************" << endl; cout << "调用前:" << endl; cout << test1 << endl; cout << test2 << endl; cout << "调用后:" << endl; test2 = test1; cout << test1 << endl; cout << test2 << endl; cout << "**********************析构函数测试**********************" << endl; return 0; }
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客网在线编程
牛客网题解
牛客企业服务