static const int M = 1 << (N-1);这句代码什么意思
时间: 2023-12-16 08:02:50 浏览: 40
这句代码定义了一个整形常量 M,它的值是 2 的 N-1 次方,其中 N 是一个预定义的常量,通常情况下 N 的值是在代码中定义的。
这里使用了位运算符 <<,表示对二进制数进行左移操作,例如 1 << 3 就是将二进制数 1 左移三位,变成 1000,即十进制数 8。所以,1 << (N-1) 就是将 1 左移 N-1 位,得到一个二进制数,其中只有最高位为 1,其它位都为 0,例如当 N=4 时,M=1000,即十进制数 8。
相关问题
class EncodingDefs { public: static const int RAW = 0; static const int COPYRECT = 1; static const int RRE = 2; static const int HEXTILE = 5; static const int TIGHT = 7; static const int ZRLE = 16; static const char *const SIG_RAW; static const char *const SIG_COPYRECT; static const char *const SIG_RRE; static const char *const SIG_HEXTILE; static const char *const SIG_TIGHT; static const char *const SIG_ZRLE; };
这是一个 C++ 类 EncodingDefs,它定义了一些常量和字符串常量,用于表示远程桌面协议中的编码类型。
其中,常量 RAW、COPYRECT、RRE、HEXTILE、TIGHT 和 ZRLE 分别表示编码类型的编号。这些常量的值是固定的,不能修改。
另外,类中还定义了一些字符串常量,包括 SIG_RAW、SIG_COPYRECT、SIG_RRE、SIG_HEXTILE、SIG_TIGHT 和 SIG_ZRLE。这些字符串常量是编码类型的名称,可以用于在日志或其他输出中表示编码类型的名称。这些字符串常量的值也是固定的,不能修改。
这个类的作用是提供一种便捷的方式来表示和访问远程桌面协议中的编码类型,使得代码更加易读和维护。
#include <iostream> #include <string> #include <assert.h> using namespace std; //在此处补充Date类的定义 class Date { private: int year, month, day; static const int monthDays[13]; public: Date(int y = 1, int m = 1, int d = 1) { year = y; month = m; day = d; } Date(const Date& date) { year = date.year; month = date.month; day = date.day; } Date& operator=(const Date& date) { year = date.year; month = date.month; day = date.day; return *this; } bool isLeap() const { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int monthDaysNum() const { if (month == 2 && isLeap()) { return 29; } return monthDays[month]; } void addOneDay() { day++; if (day > monthDaysNum()) { day = 1; month++; if (month > 12) { month = 1; year++; } } } void minusOneDay() { day--; if (day == 0) { month--; if (month == 0) { month = 12; year--; } day = monthDaysNum(); } } string toText() const { string res = ""; res += to_string(year) + "-"; if (month < 10) { res += "0"; } res += to_string(month) + "-"; if (day < 10) { res +=“0” ; } res += to_string(day); return res; } Date operator+(int n) const { Date res = *this; while (n--) { res.addOneDay(); } return res; } Date operator-(int n) const { Date res = *this; while (n--) { res.minusOneDay(); } return res; } }; const int Date::monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int y, m, d; cin >> y >> m >> d; Date d1(y,m,d); int n; cin >> n; cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl; cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl; return 0; }}纠正这个代码使得云行不出现错误输出: 2022-08-31 + 2 = 2022-09-02 2022-08-31 - 2 = 2022-08-29 期望输出: 2022-8-31 + 2 = 2022-9-2 2022-8-31 - 2 = 2022-8-29
纠正后的代码如下:
```c++
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
class Date {
private:
int year, month, day;
static const int monthDays[13];
public:
Date(int y = 1, int m = 1, int d = 1) {
year = y;
month = m;
day = d;
}
Date(const Date& date) {
year = date.year;
month = date.month;
day = date.day;
}
Date& operator=(const Date& date) {
year = date.year;
month = date.month;
day = date.day;
return *this;
}
bool isLeap() const {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int monthDaysNum() const {
if (month == 2 && isLeap()) {
return 29;
}
return monthDays[month];
}
void addOneDay() {
day++;
if (day > monthDaysNum()) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
void minusOneDay() {
day--;
if (day == 0) {
month--;
if (month == 0) {
month = 12;
year--;
}
day = monthDaysNum();
}
}
string toText() const {
string res = "";
res += to_string(year) + "-";
if (month < 10) {
res += "0";
}
res += to_string(month) + "-";
if (day < 10) {
res += "0";
}
res += to_string(day);
return res;
}
Date operator+(int n) const {
Date res = *this;
while (n--) {
res.addOneDay();
}
return res;
}
Date operator-(int n) const {
Date res = *this;
while (n--) {
res.minusOneDay();
}
return res;
}
};
const int Date::monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
int y, m, d;
cin >> y >> m >> d;
Date d1(y,m,d);
int n;
cin >> n;
cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl;
cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl;
return 0;
}
```
主要是修改了 `toText()` 函数中输出日期月份和日期天数不足两位时的处理方式,将原本的双引号修改为单引号。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)