lensfrex 3 years ago
commit 8d950d0cd4
Signed by: lensfrex
GPG Key ID: 947ADABD8533C476
  1. 21
      20220302_2.cpp
  2. 141
      20220303.cpp
  3. 103
      20220310.cpp
  4. 40
      p97_3-15.cpp

@ -0,0 +1,21 @@
#include <iostream>
using namespace std;
void splitDouble(double x, int &n, double &f) {
n = (int) x;
f = x - n;
}
int main() {
cout << "Enter one double number:\n";
double x = 0;
cin >> x;
int n = 0;
double f = 0;
splitDouble(x, n, f);
cout << "Integer part=" << n << " Fraction Part=" << f;
}

@ -0,0 +1,141 @@
#include <iostream>
using namespace std;
// 关于::的使用情况,我还是先去仔细看看Google的c++规范吧
class Date {
public:
Date();
Date(int year, int month, int day);
Date(const Date &date);
~Date();
bool operator==(const Date &date) const;
bool operator!=(const Date &date) const;
void printDate();
void setToNextDay();
void setDate(int year, int month, int day);
private:
int year = 1970;
int month = 1;
int day = 1;
int getMaxDayOfMonth() const;
};
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {}
Date::Date() : year(1900), month(1), day(1) {}
Date::Date(const Date &date) {
Date::year = date.year;
Date::month = date.month;
Date::day = date.day;
}
Date::~Date() {
std::cout << "Don'..........t.........go.........away.......... (weakly)" << std::endl;
}
bool Date::operator==(const Date &date) const {
return year == date.year &&
month == date.month &&
day == date.day;
}
bool Date::operator!=(const Date &date) const {
return !(year == date.year &&
month == date.month &&
day == date.day);
}
void Date::printDate() {
std::cout << "--------------------------" << endl;
std::cout << "- year: " << year << std::endl
<< "- month: " << month << std::endl
<< "- day: " << day << std::endl;
std::cout << "--------------------------" << endl;
}
void Date::setToNextDay() {
Date::day++;
// 检查一下是不是到了下一个月
if (Date::day > getMaxDayOfMonth()) {
Date::day = 1;
Date::month++;
// 再检查一下是不是到了下一年
if (Date::month > 12) {
Date::month = 1;
Date::year++;
}
}
}
void Date::setDate(int year, int month, int day) {
Date::year = year;
Date::month = month;
Date::day = day;
}
int Date::getMaxDayOfMonth() const {
switch (Date::month) {
default:
return 0;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (Date::year % 4 == 0) {
if (Date::year % 100 == 0) {
return (Date::year % 400) == 0 ? 29 : 28;
} else {
return 29;
}
}
return 28;
}
}
int main() {
Date defaultDate;
Date *constructorDate = new Date(2020, 3, 4);
std::cout << "[defaultDate]:" << std::endl;
defaultDate.printDate();
std::cout << "[constructorDate]:" << std::endl;
constructorDate->printDate();
std::cout << "[constructorDate - setDay()]:" << std::endl;
constructorDate->setDate(2021,12,31);
constructorDate->printDate();
std::cout << "[constructorDate - setToNextDay()]:" << std::endl;
constructorDate->setToNextDay();
constructorDate->printDate();
std::cout << "[Destructor]: ";
delete constructorDate;
}

@ -0,0 +1,103 @@
#include "iostream"
using namespace std;
class Date {
public:
Date();
Date(int year, int month, int day);
Date(const Date &date);
~Date();
int getYear() const;
int getMonth() const;
int getDay() const;
private:
int year = 1970;
int month = 1;
int day = 1;
};
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
std::cout << "Date::Date(int year, int month, int day)" << std::endl;
}
Date::Date() : year(1970), month(1), day(1) {}
Date::Date(const Date &date) {
cout << "Date::Date(const Date &p)" << endl;
Date::year = date.year;
Date::month = date.month;
Date::day = date.day;
}
Date::~Date() {
std::cout << "Date::~Date()" << std::endl;
}
int Date::getYear() const {
return year;
}
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
//-------------------------------------------------
class Person {
public:
Person();
Person(char *name, const Date &birthday);
~Person();
void showPerson();
private:
string name;
Date birthday;
};
Person::Person() {
cout << "Person::Default constructor Function is called." << endl;
}
Person::Person(char *name, const Date &birthday) : name(name), birthday(birthday) {
cout << "Person::Constructor Function is called." << endl;
}
Person::~Person() {
std::cout << "Person::~Person()" << std::endl;
}
void Person::showPerson() {
cout << "Person::showPerson()" << endl;
cout << "Name:" << name << " ";
cout << "Birthday:" << birthday.getYear() << '-' << birthday.getMonth() << '-' << birthday.getDay();
cout << endl;
}
int main() {
char name[20];
int y, m, d;
int Cas = 0;
cout << "Please input name & birthday:\n";
while (cin >> name >> y >> m >> d) {
Cas++;
cout << "CASE #" << Cas << ":" << endl;
Date d1(y, m, d);
Person person(name, d1);
person.showPerson();
}
}

@ -0,0 +1,40 @@
#include <iostream>
using namespace std;
int getPower(int x, int y) {
return (y == 0) ? 1 : x*getPower(x, y - 1);
}
double getPower(double x, int y) {
return (y == 0) ? 1 : x*getPower(x, y - 1);
}
// 不用三元运算符的写法
/*
int getPower(int x, int y) {
if (y == 0) {
return 1;
} else {
return x*getPower(x, y - 1);
}
}
double getPower(double x, int y) {
if (y == 0) {
return 1;
} else {
return x*getPower(x, y - 1);
}
}
*/
int main() {
// double x = 0;
// int y = 0;
int x = 0, y = 0;
cin >> x >> y;
cout << getPower(x, y);
}
Loading…
Cancel
Save