提交 6bf443e4 编写于 作者: 独孤过's avatar 独孤过

跨库移动代码

上级 2595242e
#include "Complex.h"
Complex operator+(const Complex &left, const Complex &right)
{
return Complex(left.realPart + right.realPart, left.imaginaryPart + right.imaginaryPart);
}
Complex operator-(const Complex &left, const Complex &right)
{
return Complex(left.realPart - right.realPart, left.imaginaryPart - right.imaginaryPart);
}
Complex operator*(const Complex &left, const Complex &right)
{
return Complex(left.realPart*right.realPart - left.imaginaryPart*right.imaginaryPart, \
left.realPart*right.imaginaryPart + left.imaginaryPart*right.realPart);
}
Complex operator/(const Complex &left, const Complex &right)
{
double denominator = right.realPart*right.realPart + right.imaginaryPart*right.imaginaryPart;
return Complex((left.realPart*right.realPart + left.imaginaryPart*right.imaginaryPart) / denominator, \
(-left.realPart*right.imaginaryPart + left.imaginaryPart*right.realPart) / denominator);
}
Complex& operator+=(Complex &left, const Complex &right)
{
left.realPart += right.realPart;
left.imaginaryPart += right.imaginaryPart;
return left;
}
Complex& operator-=(Complex &left, const Complex &right)
{
left.realPart -= right.realPart;
left.imaginaryPart -= right.imaginaryPart;
return left;
}
Complex& operator*=(Complex &left, const Complex &right)
{
return left = Complex(left.realPart*right.realPart - left.imaginaryPart*right.imaginaryPart, \
left.realPart*right.imaginaryPart + left.imaginaryPart*right.realPart);
}
Complex& operator/=(Complex &left, const Complex &right)
{
double denominator = right.realPart*right.realPart + right.imaginaryPart*right.imaginaryPart;
return left = Complex((left.realPart*right.realPart + left.imaginaryPart*right.imaginaryPart) / denominator, \
(-left.realPart*right.imaginaryPart + left.imaginaryPart*right.realPart) / denominator);
}
std::ostream & operator <<(std::ostream &output, const Complex &complex)
{
if (complex.imaginaryPart > 0)
output << complex.realPart << '+' << complex.imaginaryPart << 'i';
else if (complex.imaginaryPart == 0)
output << complex.realPart;
else
output << complex.realPart << complex.imaginaryPart << 'i';
return output;
}
std::istream & operator >>(std::istream &input, Complex &complex)
{
char imaginaryUnit;
return input >> complex.realPart >> complex.imaginaryPart >> imaginaryUnit;
}
\ No newline at end of file
#pragma once
#include <iostream>
class Complex
{
double realPart;
double imaginaryPart;
friend std::ostream & operator <<(std::ostream &output, const Complex &complex);
friend std::istream & operator >>(std::istream &input, Complex &complex);
public:
Complex(const double real = 0, const double imag = 0)
:realPart(real), imaginaryPart(imag) {}
friend Complex operator+(const Complex &left, const Complex &right);
friend Complex operator-(const Complex &left, const Complex &right);
friend Complex operator*(const Complex &left, const Complex &right);
friend Complex operator/(const Complex &left, const Complex &right);
friend Complex& operator+=(Complex &left, const Complex &right);
friend Complex& operator-=(Complex &left, const Complex &right);
friend Complex& operator*=(Complex &left, const Complex &right);
friend Complex& operator/=(Complex &left, const Complex &right);
};
\ No newline at end of file
#include <iostream>
#include "Complex.h"
int main()
{
Complex a(1, 2), b(0, 1);
Complex c;
std::cout << "һ";
std::cin >> c;
std::cout << c - a << std::endl;
std::cout << (c /= b) << std::endl;
std::cout << a + b << std::endl;
std::cout << b + 9.7 << std::endl;
std::cout << 9.7 + b << std::endl;
return 0;
}
\ No newline at end of file
#include "Date.h"
std::istream& operator>> (std::istream &input, Date &object)
{
char delimiter;
input >> object.year >> delimiter >> object.month >> delimiter >> object.day;
return input;
}
std::ostream& operator<< (std::ostream &output, const Date &object)
{
output << object.year << '/';
if (object.month < 10)
output << '0';
output << object.month << '/';
if (object.day < 10)
output << '0';
output << object.day;
return output;
}
#include <time.h>
Date::Date()
{
time_t timen;
struct tm *tb = localtime(&(timen = time(NULL)));
year = tb->tm_year + 1900;
month = tb->tm_mon + 1;
day = tb->tm_mday;
}
Date::Date(int year, unsigned month, unsigned day)
{
setYear(year);
setMonth(month);
setDay(day);
}
Date& Date::operator = (const Date &other)
{
year = other.year;
month = other.month;
day = other.day;
return *this;
}
bool operator==(const Date &left, const Date &right)
{
if (left.year == right.year && left.month == right.month && left.day == right.day)
return true;
return false;
}
bool operator>(const Date &left, const Date &right)
{
if (left.year > right.year)
return true;
if (left.year == right.year && left.month > right.month)
return true;
if (left.year == right.year && left.month == right.month && left.day > right.day)
return true;
return false;
}
bool operator<(const Date &left, const Date &right)
{
if (left.year < right.year)
return true;
if (left.year == right.year && left.month < right.month)
return true;
if (left.year == right.year && left.month == right.month && left.day < right.day)
return true;
return false;
}
bool operator>=(const Date &left, const Date &right)
{
if (left.year > right.year)
return true;
else if (left.year == right.year && left.month > right.month)
return true;
else if (left.year == right.year && left.month == right.month && left.day >= right.day)
return true;
return false;
}
bool operator<=(const Date &left, const Date &right)
{
if (left.year < right.year)
return true;
else if (left.year == right.year && left.month < right.month)
return true;
else if (left.year == right.year && left.month == right.month && left.day <= right.day)
return true;
return false;
}
void Date::setYear(const int year)
{
this->year = year;
if (year % 4 == 0 && month == 2 && day > 28)
day = 28;
}
bool Date::setMonth(const unsigned month)
{
if (month > 0 && month <= 12)
{
this->month = month;
if (month == 2)
{
if (isLeapYear())
{
if (day > 29)
day = 29;
}
else if (day > 28)
day = 28;
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
day = 30;
return true;
}
return false;
}
bool Date::setDay(const unsigned day)
{
if (month == 2 && day <= 29)
{
if (isLeapYear())
{
this->day = day;
return true;
}
else if (day < 29)
{
this->day = day;
return true;
}
}
else if ((month > 0 && month < 8 && month % 2 == 1 || month >= 8 && month <= 12 && month % 2 == 0) && day <= 31)
{
this->day = day;
return true;
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && day < 31)
{
this->day = day;
return true;
}
return false;
}
\ No newline at end of file
#pragma once
#include <iostream>
class Date
{
int year;
unsigned month;
unsigned day;
friend std::istream& operator>> (std::istream&, Date&);
friend std::ostream& operator<< (std::ostream&, const Date&);
public:
Date();
Date(int, unsigned, unsigned);
Date& operator = (const Date&);
friend bool operator == (const Date &left, const Date &right);
friend bool operator > (const Date &left, const Date &right);
friend bool operator < (const Date &left, const Date &right);
friend bool operator >= (const Date &left, const Date &right);
friend bool operator <= (const Date &left, const Date &right);
int getYear()
{
return year;
}
unsigned getMonth()
{
return month;
}
unsigned getDay()
{
return day;
}
void setYear(const int year);
bool setMonth(const unsigned month);
bool setDay(const unsigned day);
bool isLeapYear()
{
return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
};
\ No newline at end of file
#include <iostream>
#include "Date.h"
int main()
{
Date date;
std::cout << date << std::endl;
date.setYear(-100);
date.setMonth(2);
date.setDay(29);
std::cout << date << std::endl;
return 0;
}
\ No newline at end of file
#include "Fraction.h"
using std::ostream;
using std::istream;
Fraction::Fraction(long molecule, long denominator) throw(std::invalid_argument)
{
getSimplestFraction(molecule, denominator);
this->m_iMolecule = molecule;
this->m_iDenominator = denominator;
}
ostream& operator<<(ostream &os, const Fraction &fraction)
{
os << fraction.m_iMolecule << '/' << fraction.m_iDenominator;
return os;
}
istream& operator>>(istream &is, Fraction &fraction)
{
char cOperator;
is >> fraction.m_iMolecule >> operator >> fraction.m_iDenominator;
return is;
}
#include <cmath>
Fraction operator+(const Fraction &left, Fraction right) noexcept
{
if (left.m_iMolecule == 0)
return right;
else if (right.m_iMolecule == 0)
return left;
// 求两分数分母的最小公倍数
long commonNumber = Fraction::minCommonMultiple(left.m_iDenominator, right.m_iDenominator);
// 两分数通分并求分子之和
right.m_iMolecule *= commonNumber / right.m_iDenominator;
right.m_iMolecule += commonNumber / left.m_iDenominator*left.m_iMolecule;
// 存放通分后的分母
right.m_iDenominator = commonNumber;
// 求分数的最简式
Fraction::getSimplestFraction(right.m_iMolecule, right.m_iDenominator);
// 返回运算结果
return right;
}
Fraction operator-(const Fraction &left, Fraction right) noexcept
{
// 减数为零
if (right.m_iMolecule == 0)
return left;
// 被减数为零
else if (left.m_iMolecule == 0)
{
right.m_iMolecule = -right.m_iMolecule;
return right;
}
// 求两分数分母的最小公倍数
long commonNumber = Fraction::minCommonMultiple(left.m_iDenominator, right.m_iDenominator);
// 两分数通分并求分子之差
right.m_iMolecule *= commonNumber / right.m_iDenominator;
right.m_iMolecule = commonNumber / left.m_iDenominator*left.m_iMolecule - right.m_iMolecule;
// 存放通分后的分母
right.m_iDenominator = commonNumber;
// 求分数的最简式
Fraction::getSimplestFraction(right.m_iMolecule, right.m_iDenominator);
// 返回运算结果
return right;
}
Fraction operator*(const Fraction &left, Fraction right) noexcept
{
if (left.m_iMolecule == 0 || right.m_iMolecule == 0)
return 0;
// 分子相乘
right.m_iMolecule *= left.m_iMolecule;
// 分母相乘
right.m_iDenominator *= left.m_iDenominator;
// 求分数的最简式
Fraction::getSimplestFraction(right.m_iMolecule, right.m_iDenominator);
// 返回运算结果
return right;
}
Fraction operator/(const Fraction &left, Fraction right) throw(std::invalid_argument)
{
// 除数为零
if (right.m_iMolecule == 0)
throw std::invalid_argument("The divisor cannot be zero!\n");
// 被除数分子为零
if (left.m_iMolecule == 0)
return 0;
// 用临时变量存放结果分子
long lNumber = left.m_iMolecule*right.m_iDenominator;
// 计算结果分母
right.m_iDenominator = left.m_iDenominator*right.m_iMolecule;
// 存放结果分子
right.m_iMolecule = lNumber;
// getSimplestFraction中已有此步骤
/*if (right.m_iDenominator < 0)
{
right.m_iDenominator = -right.m_iDenominator;
right.m_iMolecule = -right.m_iMolecule;
}*/
// 求分数的最简式
Fraction::getSimplestFraction(right.m_iMolecule, right.m_iDenominator);
// 返回运算结果
return right;
}
Fraction& operator+=(Fraction &left, const Fraction &right) noexcept
{
if (left.m_iMolecule == 0)
return left = std::move(right);
else if (right.m_iMolecule == 0)
return left;
// 求两分数分母的最小公倍数
long commonNumber = Fraction::minCommonMultiple(left.m_iDenominator, right.m_iDenominator);
// 两分数通分并求分子之和
left.m_iMolecule *= commonNumber / left.m_iDenominator;
left.m_iMolecule += commonNumber / right.m_iDenominator*right.m_iMolecule;
// 存放通分后的分母
left.m_iDenominator = commonNumber;
// 求分数的最简式
Fraction::getSimplestFraction(left.m_iMolecule, left.m_iDenominator);
// 返回运算结果
return left;
}
Fraction& operator-=(Fraction &left, const Fraction &right) noexcept
{
// 减数为零
if (right.m_iMolecule == 0)
return left;
// 被减数为零
else if (left.m_iMolecule == 0)
{
left = std::move(right);
left.m_iMolecule = -left.m_iMolecule;
return left;
}
// 求两分数分母的最小公倍数
long commonNumber = Fraction::minCommonMultiple(left.m_iDenominator, right.m_iDenominator);
// 两分数通分并求分子之差
left.m_iMolecule *= commonNumber / left.m_iDenominator;
left.m_iMolecule -= commonNumber / right.m_iDenominator*right.m_iMolecule;
// 存放通分后的分母
left.m_iDenominator = commonNumber;
// 求分数的最简式
Fraction::getSimplestFraction(left.m_iMolecule, left.m_iDenominator);
// 返回运算结果
return left;
}
Fraction& operator*=(Fraction &left, const Fraction &right) noexcept
{
if (left.m_iMolecule == 0 || right.m_iMolecule == 0)
{
left.m_iMolecule = 0;
left.m_iDenominator = 1;
return left;
}
// 分子相乘
left.m_iMolecule *= right.m_iMolecule;
// 分母相乘
left.m_iDenominator *= right.m_iDenominator;
// 求分数的最简式
Fraction::getSimplestFraction(left.m_iMolecule, left.m_iDenominator);
// 返回运算结果
return left;
}
Fraction& operator/=(Fraction &left, const Fraction &right) throw(std::invalid_argument)
{
// 除数为零
if (right.m_iMolecule == 0)
throw std::invalid_argument("The divisor cannot be zero!\n");
// 被除数分子为零
if (left.m_iMolecule == 0)
return left;
// 用临时变量存放结果分子
long lNumber = left.m_iMolecule*right.m_iDenominator;
// 计算结果分母
left.m_iDenominator = left.m_iDenominator*right.m_iMolecule;
// 存放结果分子
left.m_iMolecule = lNumber;
// getSimplestFraction中已有此步骤
/*if (left.m_iDenominator < 0)
{
left.m_iDenominator = -left.m_iDenominator;
left.m_iMolecule = -left.m_iMolecule;
}*/
// 求分数的最简式
Fraction::getSimplestFraction(left.m_iMolecule, left.m_iDenominator);
// 返回运算结果
return left;
}
bool operator>(const Fraction &left, const Fraction &right)
{
return (left - right).m_iMolecule > 0;
}
bool operator<(const Fraction &left, const Fraction &right)
{
return (left - right).m_iMolecule < 0;
}
bool operator==(const Fraction &left, const Fraction &right)
{
return (left - right).m_iMolecule == 0;
}
bool operator>=(const Fraction &left, const Fraction &right)
{
return (left - right).m_iMolecule >= 0;
}
bool operator<=(const Fraction &left, const Fraction &right)
{
return (left - right).m_iMolecule <= 0;
}
long Fraction::maxCommonDivisor(long first, long second)
{
if (first == 0 || second == 0)
return -1;
if (first < second) //当first小于second时实现两值互换
{
first ^= second;
second ^= first;
first ^= second;
}
int temp = first%second; //first对second取余赋给temp
while (temp != 0) //当temp不为0时执行循环体语句
{
first = second; //将second赋给first
second = temp; //将temp的值赋给second
temp = first%second; //继续取余并赋给temp
}
return second;
}
long Fraction::minCommonMultiple(long first, long second)
{
return first / maxCommonDivisor(first, second)*second;
}
void Fraction::getSimplestFraction(long &molecule, long &denominator) throw(std::invalid_argument)
{
// 分母为零
if (denominator == 0)
throw std::invalid_argument("The denominator cannot be zero!\n");
// 分子为零
if (molecule == 0)
{
molecule = 0;
denominator = 1;
return;
}
// 分母小于零
if (denominator < 0)
{
molecule = -molecule;
denominator = -denominator;
}
long temp = maxCommonDivisor(std::abs(molecule), denominator);
molecule /= temp;
denominator /= temp;
}
#pragma once
#include <iostream>
#include <exception>
class Fraction
{
long m_iMolecule;
long m_iDenominator; // ·Öĸ×ÜÊÇÕýÕûÊý
friend std::ostream& operator<<(std::ostream &os, const Fraction &fraction);
friend std::istream& operator>>(std::istream &is, Fraction &fraction);
public:
Fraction(long molecule = 0, long denominator = 1) throw(std::invalid_argument);
Fraction(const Fraction &fraction) \
:m_iMolecule(fraction.m_iMolecule), m_iDenominator(fraction.m_iDenominator){}
friend Fraction operator+(const Fraction &left, Fraction right) noexcept;
friend Fraction operator-(const Fraction &left, Fraction right) noexcept;
friend Fraction operator*(const Fraction &left, Fraction right) noexcept;
friend Fraction operator/(const Fraction &left, Fraction right) throw(std::invalid_argument);
friend Fraction& operator+=(Fraction &left, const Fraction &right) noexcept;
friend Fraction& operator-=(Fraction &left, const Fraction &right) noexcept;
friend Fraction& operator*=(Fraction &left, const Fraction &right) noexcept;
friend Fraction& operator/=(Fraction &left, const Fraction &right) throw(std::invalid_argument);
friend bool operator>(const Fraction &left, const Fraction &right);
friend bool operator<(const Fraction &left, const Fraction &right);
friend bool operator==(const Fraction &left, const Fraction &right);
friend bool operator>=(const Fraction &left, const Fraction &right);
friend bool operator<=(const Fraction &left, const Fraction &right);
//operator double(){ return (double)m_iMolecule / m_iDenominator; }
static long maxCommonDivisor(long first, long second);
static long minCommonMultiple(long first, long second);
static void getSimplestFraction(long &molecule, long &denominator) throw(std::invalid_argument);
};
#include "Fraction.h"
#include <iostream>
#include <random>
#include <ctime>
#include <algorithm>
Fraction generate(long upperLimit)
{
static std::default_random_engine random(time(NULL));
static std::uniform_int_distribution<long> distribution(0, upperLimit);
long molecule = distribution(random);
long denominator = distribution(random) + 1;
if (distribution(random) % 2)
molecule = -molecule;
return Fraction(molecule, denominator);
}
using std::cout;
using std::move;
int main()
{
Fraction left = move(generate(10000)), right = move(generate(10000)), buff, result;
std::random_device builder;
switch (builder() % 4)
{
case 0:
buff = move(left + right);
cout << left << " + ";
if (right < 0)
cout << '(' << right << ')';
else
cout << right;
cout << " = ";
break;
case 1:
buff = move(left - right);
cout << left << " - ";
if (right < 0)
cout << '(' << right << ')';
else
cout << right;
cout << " = ";
break;
case 2:
buff = move(left*right);
cout << left << " * ";
if (right < 0)
cout << '(' << right << ')';
else
cout << right;
cout << " = ";
break;
case 3:
buff = move(left / right);
cout << left << " / ";
if (right < 0)
cout << '(' << right << ')';
else
cout << right;
cout << " = ";
break;
}
std::cin >> result;
if (result == buff)
cout << "正确!\n";
else
cout << "错误!\n";
return 0;
}
#include "Time.h"
std::istream& operator>>(std::istream &is, Time &time)
{
char delimiter;
is >> time.hours >> delimiter >> time.minutes >> delimiter >> time.seconds;
time.totalSeconds = time.hours * 3600 + time.minutes * 60 + time.seconds;
return is;
}
std::ostream& operator<<(std::ostream &os, const Time &time)
{
os << time.hours << ':' << time.minutes << ':' << time.seconds;
return os;
}
#include <ctime>
Time::Time()
{
time_t time = std::time(NULL);
struct tm *localTime = std::localtime(&time);
hours = localTime->tm_hour;
minutes = localTime->tm_min;
seconds = localTime->tm_sec;
totalSeconds = hours * 3600 + minutes * 60 + seconds;
}
Time::Time(const long long &seconds)
{
if (!setTime(seconds))
new(this)Time();
}
Time::Time(const long long &hours, const int &minutes, const int &seconds)
{
if (hours < 0
&& (minutes < 0 || minutes >= 60)
&& (seconds < 0 || seconds >= 60))
new(this)Time();
else
{
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
this->totalSeconds = this->hours * 3600 + this->minutes * 60 + this->seconds;
}
}
bool Time::setTime(const long long &seconds)
{
if (seconds < 0)
return false;
this->totalSeconds = seconds;
this->hours = this->totalSeconds / 3600;
this->minutes = this->totalSeconds / 60 % 60;
this->seconds = this->totalSeconds % 60;
return true;
}
bool Time::setHours(const long long &hours)
{
if (hours < 0)
return false;
this->totalSeconds += (this->hours - hours) * 3600;
this->hours = hours;
return true;
}
bool Time::setMinutes(const int &minutes)
{
if (minutes < 0 || minutes >= 60)
return false;
this->totalSeconds += (this->minutes - minutes) * 60;
this->minutes = minutes;
return true;
}
bool Time::setSeconds(const int &seconds)
{
if (seconds < 0 || seconds >= 60)
return false;
this->totalSeconds += (this->seconds - seconds);
this->seconds = seconds;
return true;
}
Time operator+(const Time &left, const Time &right)
{
return Time(left.getTime() + right.getTime());
}
Time operator-(const Time &left, const Time &right)
{
return Time(left.getTime() - right.getTime());
}
bool operator>(const Time &left, const Time &right)
{
return left.getTime() > right.getTime();
}
bool operator<(const Time &left, const Time &right)
{
return left.getTime() < right.getTime();
}
bool operator==(const Time &left, const Time &right)
{
return left.getTime() == right.getTime();
}
bool operator>=(const Time &left, const Time &right)
{
return left.getTime() >= right.getTime();
}
bool operator<=(const Time &left, const Time &right)
{
return left.getTime() <= right.getTime();
}
\ No newline at end of file
#pragma once
#include <iostream>
class Time
{
private:
long long totalSeconds;
long long hours;
int minutes;
int seconds;
friend std::istream& operator>>(std::istream&, Time&);
friend std::ostream& operator<<(std::ostream&, const Time&);
public:
Time();
Time(const long long &seconds);
Time(const long long &hours, const int &minutes, const int &seconds);
bool setTime(const long long &seconds);
long long getTime()const
{
return totalSeconds;
}
bool setHours(const long long &hours);
long long getHours()const
{
return hours;
}
bool setMinutes(const int &minutes);
int getMinutes()const
{
return minutes;
}
bool setSeconds(const int &seconds);
int getSeconds()const
{
return seconds;
}
friend Time operator+(const Time &left, const Time &right);
friend Time operator-(const Time &left, const Time &right);
friend bool operator>(const Time &left, const Time &right);
friend bool operator<(const Time &left, const Time &right);
friend bool operator==(const Time &left, const Time &right);
friend bool operator>=(const Time &left, const Time &right);
friend bool operator<=(const Time &left, const Time &right);
};
\ No newline at end of file
#include <iostream>
#include "Time.h"
int main()
{
Time t1;
std::cout << "the time is now ";
std::cout << t1 << std::endl;
Time t2(19, 27, 30);
std::cout << "the custom time is ";
std::cout << t2 << std::endl;
Time t3 = t1 + t2;
std::cout << "the sum of time is ";
std::cout << t3;
return 0;
}
\ No newline at end of file
#include "stdafx.h"
#include "Timer.h"
Timer::Timer(const std::function<void()> &callback, \
const int timeInterval, const int accuracy)
{
// 使用标准库的placement new,在构造函数中,第二次调用自己的构造函数,\
于是在已经分配好的内存上执行,而不是分配新的内存
new (this)Timer(accuracy);
setTimer(callback, timeInterval);
}
bool Timer::setTimer(const std::function<void()> &callback, const int timeInterval)
{
if (!bClosed)
return false;
bClosed = false;
this->timeInterval = timeInterval;
this->callback = callback;
thread = std::thread(&Timer::execute, this);
return !(bClosed = false);
}
void Timer::killTimer()
{
bClosed = true;
thread.join();
}
void Timer::execute()
{
while (!bClosed)
{
auto begin = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - begin < std::chrono::milliseconds(timeInterval))
std::this_thread::sleep_for(std::chrono::milliseconds(accuracy));
if (!bClosed)
callback();
}
}
\ No newline at end of file
#pragma once
#include <atomic>
#include <thread>
class Timer
{
std::atomic_int timeInterval;
std::atomic_int accuracy;
bool bClosed;
std::function<void()> callback;
std::thread thread;
public:
static const int DEFAULT_TIME_INTERVAL = 100;
static const int DEFAULT_ACCUCARY = 10;
Timer(const int accuracy = DEFAULT_ACCUCARY)
:bClosed(true)
{
this->accuracy = accuracy;
}
Timer(const std::function<void()> &callback, \
const int timeInterval = DEFAULT_TIME_INTERVAL, const int accuracy = DEFAULT_ACCUCARY);
Timer(const Timer&) = delete;
~Timer()
{
if (!bClosed)
killTimer();
}
Timer& operator=(const Timer&) = delete;
bool setTimer(const std::function<void()> &callback, const int timeInterval);
void startTimer();
void killTimer();
void setTimeInterval(int timeInterval)
{
this->timeInterval = timeInterval;
}
int getTimeInterval()
{
return timeInterval;
}
void setAccuracy(int accuracy)
{
this->accuracy = accuracy;
}
int getAccuracy()
{
return accuracy;
}
protected:
void execute();
};
\ No newline at end of file
#include <iostream>
#include "Timer.h"
int main()
{
Timer timer;
timer.setTimer([&timer]()
{
std::cout << timer.getTimeInterval() << ' ';
}, 100);
//std::this_thread::sleep_for(std::chrono::milliseconds(102));
std::this_thread::sleep_for(std::chrono::seconds(2));
return 0;
}
\ No newline at end of file
#pragma once
namespace forever
{
/*
C++14中的std::move实现例子
函数返回值类型推导
标准库的别名模板std:remove_reference_t
*/
template<typename T>
decltype(auto) move(T&& param)
{
using ReturnType = std::remove_reference_t<T>&&;
return static_cast<ReturnType>(param);
}
//template <class T>
//inline void swap(T &x, T &y)
//{
// x ^= y;
// y ^= x;
// x ^= y;
//}
// STL源码形式
template <class T>
inline void swap(T &left, T &right)
{
int temp = left;
left = right;
right = temp;
}
template <class T>
inline const T& max(const T& left, const T& right)
{
return left < right ? right : left;
}
template <class T>
inline const T& min(const T& left, const T& right)
{
return right < left ? right : left;
}
}
\ No newline at end of file
#include "algorithm.h"
#include <iostream>
#include <ctime>
int main()
{
int x = 9, y = 10;
std::clock_t begin = std::clock();
for (int i = 0; i < 1000000; ++i)
forever::swap(x, y);
std::clock_t end = std::clock();
std::cout << x << ' ' << y << std::endl;
std::cout << end - begin;
return 0;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册