C++基础入门---3.运算符【P16~P23】

3. 运算符

在这里插入图片描述
在这里插入图片描述

3.1 算数运算符,加减乘除运算

注:在除法运算中,除数不能为零。

# include<iostream>
using namespace std;
int main()
{
	int a = 10;
	int b = 3;
	cout << a + b << endl;
	cout << a - b << endl;
	cout << a * b << endl;
	cout << a / b << endl;//两整数相除,结果依然是整数,将小数部分舍弃

	double c = 0.5;
	double d = 0.22;
	cout << c / d << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

3.2 算数运算符,取模运算

注:取模运算的本质就是取余数。

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	int b = 3;

	cout << a % b << endl;
	//两小数不可以做取模运算

	system("pause");
	return 0;
}

3.3 算数运算符,递增递减

#include<iostream>
using namespace std;
int main()
{
	//前置递增
	int a = 10;
	++a;
	cout << " a= " << a << endl;
	//后置递增
	int b = 10;
	b++;
	cout << "b = " << b << endl;
	//前置递增,先让变量+1,然后进行表达式的计算
	int a1 = 10;
	int b1 = ++a1 * 10;
	cout << "a1= " << a1 << endl;
	cout << "b1= " << b1 << endl;

	//后置递增,先进行表达式的计算,再让变量+1
	int a2 = 10;
	int b2 = a2++ * 10;
	cout << "a2= " << a2 << endl;
	cout << "b2= " << b2 << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

3.4 赋值运算符

在这里插入图片描述

# include<iostream>
using namespace std;
int main()
{
	// =
	int a = 10;
	a = 100;
	cout << "a= " << a << endl;
	// +=
	a = 10;
	a += 2;
	cout << "a=" << a << endl;
	// -=
	a = 10;
	a -= 2;
	cout << "a= " << a << endl;
	// *=
	a = 10;
	a *= 2;
	cout << "a= " << a << endl;
	// /=
	a = 10;
	a /= 2;
	cout << "a= " << a << endl;
	// %=
	a = 10;
	a %= 2;
	cout << "a= " << a << endl;


	system("pause");
	return 0;
}

在这里插入图片描述

3.5 比较运算符

在这里插入图片描述

#include<iostream>
using namespace  std;
int main()
{
	int a = 10;
	int b = 20;
	cout << (a == b) << endl;
	cout << (a != b) << endl;
	cout << (a > b) << endl;
	cout << (a < b) << endl;

	system("pause");
	return 0;
}

3.6 逻辑运算符,非

在这里插入图片描述

#include<iostream>
using namespace  std;
int main()
{
	int a = 10;
	cout << (!a) << endl;
	cout << (!!a) << endl;
	
	system("pause");
	return 0;
}

3.7 逻辑运算符,与

#include<iostream>
using namespace  std;
int main()
{
	int a = 10;
	int b = 10;

	cout << (a&&b) << endl;

	a = 0;
	b = 10;
	cout << (a&&b) << endl;

	system("pause");
	return 0;
}

3.8 逻辑运算符,或

#include<iostream>
using namespace  std;
int main()
{
	int a = 10;
	int b = 10;

	cout << (a||b) << endl;

	a = 0;
	b = 10;
	cout << (a||b) << endl;

	a = 0;
	b = 0;
	cout << (a||b) << endl;

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在上一题Point2D和Point3D类的基础上,新建一个TestPointV2类,在TestPointV2类的main()方法中添加如下语句。 Scanner sc = new Scanner(System.in); System.out.println("Please enter the coordinates of p23:"); double p23x = sc.nextDouble(); double p23y = sc.nextDouble(); Point2D p23 = new Point2D(p23x, p23y); System.out.println("Please enter the coordinates of p31:"); double p31x = sc.nextDouble(); double p31y = sc.nextDouble(); double p31z = sc.nextDouble(); Point3D p33 = new Point3D(p31x, p31y, p31z); System.out.println("Please enter the coordinates of p24:"); double p24x = sc.nextDouble(); double p24y = sc.nextDouble(); double p24z = sc.nextDouble(); sc.close(); // The reference of the parent class refers to the object of the subclass. Point2D p24 = new Point3D(p24x, p24y, p24z); System.out.println("Does " + p23 + " coincide with " + p33 + "? -- "+ p23.equals(p33)); System.out.println("Does " + p33 + " coincide with " + p23 + "? -- "+ p33.equals(p23)); System.out.println("Does " + p33 + " coincide with " + p24 + "? -- "+ p33.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p33 + "? -- "+ p24.equals(p33)); System.out.println("Does " + p23 + " coincide with " + p24 + "? -- "+ p23.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p23 + "? -- "+ p24.equals(p23)); 假设引用变量p23、p33和p24所指点对象的坐标依次为(0, 0),(0, 0, 5),(0, 0, 5)。从键盘输入这三个点的坐标值,上述语句的运行结果如下: Please enter the coordinates of p23: 0 0 Please enter the coordinates of p31: 0 0 5 Please enter the coordinates of p24: 0 0 5 Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true 该结果显然不符合事实,请分析原因并改进Point2D类的代码,使得上述TestPointV2类的代码能够得到正确的运行结果。
05-05

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值