support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,13 @@
# please run `bazel run //practical_exercises/10_day_practice/day7/unary_operator:time_increase`
# please run `bazel run //practical_exercises/10_day_practice/day7/unary_operator:time_counter`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "time_increase",
srcs = ["time_increase.cpp"],
)
cc_binary(
name = "time_counter",
srcs = ["time_counter.cpp"],
)

View File

@@ -0,0 +1,48 @@
/* 计数器前后自增.cpp */
//设计一个计数器counter用类成员重载自增运算符实现计数器的自增用友元重载实现计数器的自减。
#include <iostream>
using namespace std;
class Counter {
private:
int n;
public:
Counter(int i = 0) : n(i){};
Counter operator++();
Counter operator++(int);
friend Counter operator--(Counter &c);
friend Counter operator--(Counter &c, int);
void display();
};
Counter Counter::operator++() {
++n;
return *this;
}
Counter Counter::operator++(int) {
Counter t = *this;
n++;
return t;
}
Counter operator--(Counter &c) {
--c.n;
return c;
}
Counter operator--(Counter& c, int) {
Counter t(c.n);
c.n--;
return t;
}
void Counter::display() { cout << "counter number=" << n << endl; }
int main(int argc, char const *argv[]) {
Counter a;
++a;
a.display();
a++;
a.display();
--a;
a.display();
a--;
a.display();
return 0;
}

View File

@@ -0,0 +1,75 @@
/* 秒钟自增运算.cpp */
//设计一个时间类Time它能够完成秒钟的自增运算。
#include <iostream>
using namespace std;
class Time {
private:
int hour, minute, second;
public:
Time(int h, int m, int s);
Time operator++();
//友元重载需要参数
friend Time operator--(Time &t);
void display();
};
Time::Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
if (hour >= 24)
hour = 0;
if (minute >= 60)
minute = 0;
if (second >= 60)
second = 0;
}
Time Time::operator++() {
++second;
if (second >= 60) {
second = 0;
++minute;
if (minute >= 60) {
minute = 0;
++hour;
if (hour >= 24)
hour = 0;
}
}
return *this;
}
Time operator--(Time &t) {
--t.second;
if (t.second < 0) {
t.second = 59;
--t.minute;
if (t.minute < 0) {
t.minute = 59;
--t.hour;
if (t.hour < 0)
t.hour = 23;
}
}
return t;
}
void Time::display() { cout << hour << ":" << minute << ":" << second << endl; }
int main(int argc, char const *argv[]) {
Time t1(23, 59, 59);
t1.display();
++t1; //隐式调用
t1.display();
t1.operator++(); //显式调用
t1.display();
Time t2(24, 60, 60);
t2.display();
++t2;
t2.display();
--t2;
t2.display();
return 0;
}