Day2:2023.4.19

一、变量

1、命名:字母数字下划线美元符号,不能使用数字开头,不能使用关键字或保留字,区分大小写

二、数据类型

1、基本数据类型

 2、转义字符

\t        制表符

\n        换行

\u        转为16进制

3、引用数据类型

String:可以拼接(使用+);  需要使用" "

String类型的字符串比较使用equals()         例:x=sex.equals("男");        表示x变量为男

4、数据类型转换

自动类型转换:小转大

强制类型转换:大转小(丢失精度);例:int x=(int)1.5;

整形默认为Int,浮点型默认为double

5、运算符

算数运算符:+ - * / % ++ --

        ps: j=i++是先把i的值给j,然后+1;j=++i是先把i的值+1,再给j

赋值运算符:=   +=   -=   /=   *=   %=

关系运算符:>   <    >=    <=    ==    !=

逻辑运算符:&&    ||    !

        ps:  &&是短路或,当表达式左面为flase时,即停止计算,节省计算量

三目运算符:     ? : ;

        例:求a,b最大值:     max=a>b?a:b;

        嵌套:? (? ;) : ;

位运算符:& (按位与)     |(按位或)     ^(按位异或)     ~(按位取反)     >>(符号数右移)                     <<(左移)     >>>(无符号数右移)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Date.h文件内容如下: ```c++ #ifndef DATE_H #define DATE_H class Date { public: Date(int y, int m, int d); // 构造函数 void addDays(int days); // 加上days天 void subDays(int days); // 减去days天 int diffDays(const Date& other) const; // 计算与另一个日期之间相差的天数 void forwardDays(int days); // 距离当前日期向前days天的日期 void backwardDays(int days); // 距离当前日期向后days天的日期 void printDate() const; // 输出日期 private: int year, month, day; // 年、月、日 }; #endif // DATE_H ``` Date.cpp文件内容如下: ```c++ #include "Date.h" #include <iostream> // 数组存放每月天数,0为非闰年,1为闰年 int day_month[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31}}; // 是否为闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 计算给定日期距离2000年1月1日的天数 int calcDays(int year, int month, int day) { int days = 0; for (int i = 2000; i < year; i++) { days += isLeapYear(i) ? 366 : 365; } int leap = isLeapYear(year) ? 1 : 0; for (int i = 0; i < month - 1; i++) { days += day_month[leap][i]; } days += day; return days; } // 构造函数 Date::Date(int y, int m, int d) { year = y; month = m; day = d; } // 加上days天 void Date::addDays(int days) { int total_days = calcDays(year, month, day) + days; int y = 2000, m = 1, d = 1; while (total_days > (isLeapYear(y) ? 366 : 365)) { total_days -= isLeapYear(y) ? 366 : 365; y++; } int leap = isLeapYear(y) ? 1 : 0; while (total_days > day_month[leap][m - 1]) { total_days -= day_month[leap][m - 1]; m++; } d = total_days; year = y; month = m; day = d; } // 减去days天 void Date::subDays(int days) { int total_days = calcDays(year, month, day) - days; int y = 2000, m = 1, d = 1; while (total_days > (isLeapYear(y) ? 366 : 365)) { total_days -= isLeapYear(y) ? 366 : 365; y++; } int leap = isLeapYear(y) ? 1 : 0; while (total_days > day_month[leap][m - 1]) { total_days -= day_month[leap][m - 1]; m++; } d = total_days; year = y; month = m; day = d; } // 计算与另一个日期之间相差的天数 int Date::diffDays(const Date& other) const { int days1 = calcDays(year, month, day); int days2 = calcDays(other.year, other.month, other.day); return days1 - days2; } // 距离当前日期向前days天的日期 void Date::forwardDays(int days) { subDays(days); } // 距离当前日期向后days天的日期 void Date::backwardDays(int days) { addDays(days); } // 输出日期 void Date::printDate() const { std::cout << year << "." << month << "." << day << std::endl; } ``` main.cpp文件内容如下: ```c++ #include "Date.h" #include <iostream> int main() { // 测试数据 Date now(2023, 6, 1); Date other(2020, 10, 1); // 相差天数 std::cout << "现在是:"; now.printDate(); std::cout << "另一天是:"; other.printDate(); std::cout << "相差天数:" << now.diffDays(other) << std::endl; // 距离当前日期向前100天的日期 std::cout << "距现在向前100天是:"; now.forwardDays(100); now.printDate(); // 距离当前日期向后100天的日期 std::cout << "距现在向后100天是:"; now.backwardDays(200); now.printDate(); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值