C++DAY3 运算符·赋值运算符与比较运算符

赋值运算符的作用:用于将表达式的值(将运算后的值赋予变量)赋给变量。

运算符术语示例结果
=赋值A=1,B=2;A=1,B=2;
+=加等于A=0,A+=2;A=2;
-=减等于A=5,A-=3;A=3;
*=乘等于A=2,A*=4;A=4;
/=除等于A=4,A/=2;A=2;
%=模等于A=11,A%=1;A=1;

运算时的加减乘除都是加减乘除=后的数(如A*=3,就是A的值乘3)。

#include<iostream>
using namespace std;

int main()
{
	//此时a的值为100(10*10)
	int a = 0;
	a = 10;
	a *= 10;
	cout << "a=" << a << endl;

	//此时b的值为3(9/3)
	int b = 0;
	b = 9;
	b /= 3;
	cout << "b=" <<b<< endl;

	//此时c的值为1(11%2)
	int c = 0;
	c = 11;
	c %= 2;
	cout << "c=" << c << endl;

	system("pause");

	return 0;
}

比较运算符的作用:用于表达式的比较,并返回一个真值或假值。

运算符术语示例结果
==相等于1==20(假)
!=不等于1!=21(真)
<小于1<21(真)
>大于1>20(假)
<=小于等于1<=21(真)
>=大于等于1>=20(假)

返回的值为1,则该式成立,为0不成立。

*在使用cout语句时,a==b一类的式要用()括起来(意为先运算)。

#include<iostream>
using namespace std;

int main()
{

int a = 10;
int b = 20;
//该式的值为0,即假
cout <<(a==b)<< endl;
//该式的值为1,即真
cout <<(a<b)<< endl;

system("pause");

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 中,我们可以通过重载运算符来实现日期之间的差值计算。具体步骤如下: 1. 定义一个 Date 类,包含年、月、日等属性,并实现自增自减运算符的重载。 ```c class Date { public: Date(int year, int month, int day): year_(year), month_(month), day_(day) {} Date& operator++(); // 自增运算符重载 Date operator++(int); // 后置自增运算符重载 Date& operator--(); // 自减运算符重载 Date operator--(int); // 后置自减运算符重载 private: int year_; int month_; int day_; }; ``` 2. 在重载运算符的实现中,我们需要注意日期的进位和借位问题。例如,当月份为 12 时,年份需要进位;当日期为 1 时,月份需要借位。 ```c Date& Date::operator++() { day_++; if (day_ > days_of_month(year_, month_)) { day_ = 1; month_++; if (month_ > 12) { month_ = 1; year_++; } } return *this; } Date Date::operator++(int) { Date temp(*this); ++(*this); return temp; } Date& Date::operator--() { day_--; if (day_ < 1) { month_--; if (month_ < 1) { month_ = 12; year_--; } day_ = days_of_month(year_, month_); } return *this; } Date Date::operator--(int) { Date temp(*this); --(*this); return temp; } ``` 3. 定义一个 days_between 函数,用于计算两个日期之间的天数差值。该函数可以通过调用重载的自增自减运算符来实现。 ```c int days_between(const Date& d1, const Date& d2) { Date d(d1); int count = 0; while (d != d2) { if (d2 > d) { ++d; count++; } else { --d; count--; } } return count; } ``` 4. 注意,我们还需要实现一个 days_of_month 函数,用于计算给定年份和月份的天数。 ```c int days_of_month(int year, int month) { static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) { return 29; } return days[month - 1]; } bool is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } ``` 完整代码如下: ```c #include <iostream> class Date { public: Date(int year, int month, int day): year_(year), month_(month), day_(day) {} Date& operator++(); // 自增运算符重载 Date operator++(int); // 后置自增运算符重载 Date& operator--(); // 自减运算符重载 Date operator--(int); // 后置自减运算符重载 bool operator==(const Date& other) const { return year_ == other.year_ && month_ == other.month_ && day_ == other.day_; } bool operator!=(const Date& other) const { return !(*this == other); } bool operator<(const Date& other) const { if (year_ < other.year_) return true; if (year_ > other.year_) return false; if (month_ < other.month_) return true; if (month_ > other.month_) return false; return day_ < other.day_; } bool operator>(const Date& other) const { return other < *this; } bool operator<=(const Date& other) const { return !(other < *this); } bool operator>=(const Date& other) const { return !(*this < other); } private: int year_; int month_; int day_; }; int days_of_month(int year, int month) { static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) { return 29; } return days[month - 1]; } bool is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } Date& Date::operator++() { day_++; if (day_ > days_of_month(year_, month_)) { day_ = 1; month_++; if (month_ > 12) { month_ = 1; year_++; } } return *this; } Date Date::operator++(int) { Date temp(*this); ++(*this); return temp; } Date& Date::operator--() { day_--; if (day_ < 1) { month_--; if (month_ < 1) { month_ = 12; year_--; } day_ = days_of_month(year_, month_); } return *this; } Date Date::operator--(int) { Date temp(*this); --(*this); return temp; } int days_between(const Date& d1, const Date& d2) { Date d(d1); int count = 0; while (d != d2) { if (d2 > d) { ++d; count++; } else { --d; count--; } } return count; } int main() { Date d1(2020, 1, 1); Date d2(2020, 1, 10); std::cout << "Days between " << d1 << " and " << d2 << ": " << days_between(d1, d2) << std::endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值