本题主要考察-构造函数的定义和操作符重载、友元函数等
根据后缀和程序样例输出,完成分数类和相关函数的定义,
输入:
-6 12
8 -16
输出:
1/2
1/1
-1/2
/==
-1/2 - -1/2 = 0/1
输入:
3 7
2 6
输出:
1/2
1/1
-1/2
!=
-1/2 - 3/7 = -13/14
注意:
1、要求最简分数,
2、如果是负分数,则确保负号在分子上,即确保分母永远是正整数。
//本题主要考察-构造函数的定义和操作符重载、友元函数等
//
//根据后缀和程序样例输出,完成分数类和相关函数的定义,
//
//输入:
//
//-6 12
//8 -16
//
//输出:
//
//1/2
//1/1
//-1/2
//==
//-1/2 - -1/2 = 0/1
//
//输入:
//
//3 7
//2 6
//
//输出:
//
//1/2
//1/1
//-1/2
//!=
//-1/2 - 3/7 = -13/14
//
//注意:
//
//1、要求最简分数,
//
//2、如果是负分数,则确保负号在分子上,即确保分母永远是正整数。
#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
void simplify()
{
if(denominator == 0)
{
throw invalid_argument("Denominator cannot be zero.");
}
if(denominator < 0)
{
numerator = -numerator;
denominator =-denominator;
}
int gcd_val = gcd(abs(numerator), abs(denominator));
numerator /= gcd_val;
denominator /= gcd_val;
}
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public:
Fraction(int num = 1,int denom= 1) :numerator(num),denominator(denom){
simplify();
}
friend istream & operator>>(istream & in,Fraction &f)
{
in >> f.numerator>>f.denominator;
f.simplify();
return in;
}
friend ostream& operator<<(ostream & out,const Fraction &f)
{
if (f.numerator == 0) {
out << "0/1";
}
else
{
out <<f.numerator <<"/" <<f.denominator;
}
return out;
}
Fraction operator-(const Fraction &other) const
{
int newNumerator = numerator * other.denominator - denominator * other.numerator;
int newDenominator = denominator * other.denominator;
Fraction result(newNumerator, newDenominator);
result.simplify();
return result;
}
bool operator!=(const Fraction &other) const
{
return numerator != other.numerator || denominator != other.denominator;
}
bool operator==(const Fraction &other) const
{
return numerator == other.numerator && denominator == other.denominator;
}
};
//StudybarCommentBegin
int main(int argc, char *argv[]) {
Fraction a(1, 2), b;
Fraction const c(1,-2);
cout << a << "\n" << b << "\n" << c << '\n';
cin >> a >> b;
if (c != b)
cout << "!=" << endl;
else
cout<<"=="<<endl;
cout <<c<<" - "<<a<<" = "<< c - a <<endl ;
return 0;
}
//StudybarCommentEnd
2、下一秒的时间++重载(增加后置++,与返回引用的考察点)
实现时间下一秒的时间
时间 + 秒数 得到 时间
样例输入
23 15 0
66
样例输出
23:15:00
23:15:01
23:16:07
23:15:01
23:15:02
23:16:08
23:16:09
样例输入
23 58 0 120
样例输出
23:58:00
23:58:01
00:00:01
23:58:01
23:58:02
00:00:02
00:00:03
void Time::printTime()
{
cout<<setfill(‘0’)<<setw(2)<<hour
<<“:”<<setw(2)<<minute<<“:”
<<setw(2)<<second<<endl;
}
//实现时间下一秒的时间
//
//时间 + 秒数 得到 时间
//
//样例输入
//
//23 15 0
//
//66
//
//样例输出
//
//23:15:00
//23:15:01
//23:16:07
//23:15:01
//23:15:02
//23:16:08
//23:16:09
//
//样例输入
//
//23 58 0 120
//
//样例输出
//
//23:58:00
//23:58:01
//00:00:01
//23:58:01
//23:58:02
//00:00:02
//00:00:03
//
//void Time::printTime()
//{
// cout<<setfill('0')<<setw(2)<<hour
// <<":"<<setw(2)<<minute<<":"
// <<setw(2)<<second<<endl;
//}
//StudybarCommentBegin
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
//StudybarCommentEnd
class Time
{
private:
int hour;
int minute;
int second;
void normalize()
{
minute += second / 60;
second %= 60;
hour += minute / 60;
minute %= 60;
hour %= 24;
if(hour < 0)
{
hour += 24;
}
}
public:
Time(int h = 0, int m = 0, int s = 0): hour(h), minute(m), second(s){
normalize();
}
void setTime(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
normalize();
}
void printTime() const
{
cout << setfill('0') <<setw(2) <<hour <<":"
<< setw(2) <<minute<<":"
<< setw(2) <<second <<endl;
}
Time& operator++()
{
second++;
normalize();
return * this;
}
Time operator++(int)
{
Time temp = *this;
++(*this);
return temp;
}
friend Time operator+(int seconds,const Time& t)
{
return Time(t.hour,t.minute,t.second+seconds);
}
};
//StudybarCommentBegin
int main()
{
int hour, minute, second;
int increase;
Time t1(23, 45, 0), t2, t3(t1);
cin >> hour >> minute >> second >> increase;
t1.setTime(hour, minute, second);
t1.printTime();
t2 = ++t1; //This is for ++t1
t2.printTime();
t3 = increase + t1; //This is for friend function
t3.printTime();
t1 = t2++; //This is for t2++
t1.printTime();
t2.printTime();
t1 = (++t3)++; // This is for left value (return the reference of the object )
t1.printTime();
t3.printTime();
return 0;
}
//StudybarCommentEnd
3、类单目运算符重载-实现日期的下一天
Description
输入“年 月 日” ,输出这个日期的下一天的日期
Input
三个整数 比如 1992 6 30 (用空格分隔)
Output
1992 年6月 30日的下一天的三个数字 1992 7 1
Sample Input
1992 6 30
Sample Output
1992 7 1
1992 6 30
Sample Input
2001 12 31
Sample Output
2002 1 1
2001 12 31
闰 年2月有29日,平年没有,
6月等没有31日
//Description
//输入“年 月 日” ,输出这个日期的下一天的日期
//
//Input
//三个整数 比如 1992 6 30 (用空格分隔)
//
//Output
//1992 年6月 30日的下一天的三个数字 1992 7 1
//Sample Input
//1992 6 30
//Sample Output
//1992 7 1
//1992 6 30
//
//
//
//Sample Input
//2001 12 31
//Sample Output
//2002 1 1
//2001 12 31
//
//
//
//闰 年2月有29日,平年没有,
//6月等没有31日
#include <iostream>
using namespace std;
class date {
private:
int y, m, d;
public:
date() : y(0), m(0), d(0) {} // 添加默认构造函数
date(int year, int month, int day) : y(year), m(month), d(day) {}
int year() const { return y; }
int month() const { return m; }
int day() const { return d; }
bool is_leap_year() const {
return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0);
}
int days_in_month() const {
if (m == 2) {
return is_leap_year() ? 29 : 28;
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
return 30;
} else {
return 31;
}
}
date operator++(int) { // 后缀++
date temp = *this;
increment();
return temp;
}
void increment() {
d++;
if (d > days_in_month()) {
d = 1;
m++;
if (m > 12) {
m = 1;
y++;
}
}
}
};
//StudybarCommentBegin
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
int year, month, day;
cin >> year >> month >> day;
date D1(year, month, day); // 直接使用带参数的构造函数
date D2 = D1++; // 使用复制构造函数
cout << D1.year() << " " << D1.month() << " " << D1.day() << endl;
cout << D2.year() << " " << D2.month() << " " << D2.day() << endl;
return 0;
}
//StudybarCommentEnd
4、下一秒的日期
要求:实现下一秒的日期时间
日期时间 + 秒数 得到新的日期时间
两个日期时间相差多少秒
样例输入:
2016 2 28 23 59 59
2016 3 1 0 0 0
62
样例输出:
2016/2/28 23:59:59
2016/3/1 00:00:00
2016/2/29 00:00:00
2016/2/29 00:01:02
86400
void Time::printTime()
{
cout<<year<<“/”<<month<<“/”<<day
<<" “<<setfill(‘0’)<<setw(2)<<hour
<<”:“<<setw(2)<<minute<<”:"
<<setw(2)<<second<<endl;
}
//StudybarCommentBegin
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
//StudybarCommentEnd
class Time {
private:
int year, month, day, hour, minute, second;
// 判断是否为闰年
bool isLeapYear(int y) const { // 添加 const
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
// 获取某年某月的天数
int daysInMonth(int y, int m) const { // 添加 const
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (m == 2 && isLeapYear(y)) {
return 29;
}
return days[m - 1];
}
// 计算从 1970 年 1 月 1 日 00:00:00 到当前时间的总秒数
int getTotalSeconds() const {
int totalSeconds = 0;
// 计算年份差
for (int y = 1970; y < year; ++y) {
totalSeconds += isLeapYear(y) ? 366 * 24 * 3600 : 365 * 24 * 3600;
}
// 计算月份差
for (int m = 1; m < month; ++m) {
totalSeconds += daysInMonth(year, m) * 24 * 3600;
}
// 计算天数差
totalSeconds += (day - 1) * 24 * 3600;
// 计算小时、分钟和秒
totalSeconds += hour * 3600;
totalSeconds += minute * 60;
totalSeconds += second;
return totalSeconds;
}
public:
Time(int y = 1970, int m = 1, int d = 1, int h = 0, int min = 0, int s = 0)
: year(y), month(m), day(d), hour(h), minute(min), second(s) {}
void setTime(int y, int m, int d, int h, int min, int s) {
year = y;
month = m;
day = d;
hour = h;
minute = min;
second = s;
}
void printTime() {
cout << year << "/" << month << "/" << day << " "
<< setfill('0') << setw(2) << hour << ":"
<< setw(2) << minute << ":"
<< setw(2) << second << endl;
}
Time& operator++() {
second++;
if (second >= 60) {
second = 0;
minute++;
if (minute >= 60) {
minute = 0;
hour++;
if (hour >= 24) {
hour = 0;
day++;
if (day > daysInMonth(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
}
}
return *this;
}
Time operator+(int seconds) {
Time temp(*this);
temp.second += seconds;
while (temp.second >= 60) {
temp.second -= 60;
temp.minute++;
if (temp.minute >= 60) {
temp.minute = 0;
temp.hour++;
if (temp.hour >= 24) {
temp.hour = 0;
temp.day++;
if (temp.day > daysInMonth(temp.year, temp.month)) {
temp.day = 1;
temp.month++;
if (temp.month > 12) {
temp.month = 1;
temp.year++;
}
}
}
}
}
return temp;
}
int operator-(const Time& other) const {
return getTotalSeconds() - other.getTotalSeconds();
}
};
//StudybarCommentBegin
int main() {
int year, month, day, hour, minute, second;
int increase;
Time t1(2016, 3, 31, 23, 45, 0), t2, t3(t1), t4;
cin >> year >> month >> day >> hour >> minute >> second;
t1.setTime(year, month, day, hour, minute, second);
cin >> year >> month >> day >> hour >> minute >> second;
t2.setTime(year, month, day, hour, minute, second);
cin >> increase;
t1.printTime();
t2.printTime();
t3 = ++t1;
t3.printTime();
t4 = t1 + increase;
t4.printTime();
int capacity = t2 - t1;
cout << capacity << endl;
}
//StudybarCommentEnd