C++基本语法元素——数据类型和运算符

数据类型和运算符

#include  <iostream>
#include <string>  // 用c++风格的字符串时,要包含这个头文件
using namespace std;

int main() 
{
	cout << "hello world" << endl;


	short num1 = 10;
	cout << "short占用的内存空间为:" << sizeof(short) << endl;

	int num2 = 10;
	cout << "int占用的内存空间为:" << sizeof(num2) << endl;

	long num3 = 10;
	cout << "long占用的内存空间为:" << sizeof(num3) << endl;

	long long num4 = 10;
	cout << "long long占用的内存空间为:" << sizeof(num4) << endl;

	float f1 = 3.1415926f;
	cout << "f1=" << f1 << endl;
	cout << "float占用的内存空间为:" << sizeof(f1) << endl;   // 4字节,一个字节=8个比特

	double d1 = 3.1415926f;
	cout << "d1=" << d1 << endl;
	cout << "double占用的内存空间为:" << sizeof(d1) << endl;  // 8字节

	// 科学计数法
	float f2 = 3e2;
	cout << "f2=" << f2 << endl;

	float f3 = 3e-3;
	cout << "f3=" << f3 << endl;

	// 字符型变量
	char ch = 'a';     // 字符只能用单引号!字符串只能用双引号!
	cout << ch << endl;
	cout << "char字符型变量占用的内存空间为:" << sizeof(ch) << endl;  
	cout << ch << "字符对应的ASCII编码为:" << (int)ch << endl;

	// 转义字符

	// 换行符 \n
	cout << "hello world\n";

	// 反斜杠  \\

	cout << "\\" << endl;

	// 水平制表符 \t   作用:整齐地输出数据
	cout << "aaa\t hello world" << endl;
	cout << "aa\t hello world" << endl;
	cout << "aaaaaa\t hello world" << endl;

	// 字符串型变量
	// C风格
	char str[] = "hello_world";  // 注意:chat 字符串名 [] = "..."
	cout << str << endl;

	// C++风格
	string str2 = "hello_world";
	cout << str2 << endl;

	// 布尔类型bool
	bool flag = true;
	cout << "bool字符型变量占用的内存空间为:" << sizeof(flag) << endl;

	// 数据的输入
	// 整型
	// int a = 0;
	// cout << "请给整型变量a赋值:" << endl;
	// cin >> a;
	// cout << "此时整型变量a等于" << a << endl;

	// 运算符
	// 加减乘除
	int a1 = 10;
	int b1 = 3;

	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	cout << a1 / b1 << endl;   //  两个整数相除,结果依然是整数,将小数部分去除

	int a2 = 10;
	int b2 = 20;
	cout << a2 / b2 << endl;

	int a3 = 10;
	int b3 = 0;
	// cout << a3 / b3 << endl;  // 错误!除数不能为零

	double d2 = 0.5;
	double d3 = 0.22;
	cout << d2 / d3 << endl;

	// 取模运算
	int a4 = 10;
	int b4 = 3;
	cout << a4% b4 << endl;

	int a5 = 10;
	int b5 = 20;
	cout << a5% b5 << endl;

	int a6 = 10;
	int b6 = 0;
	// cout << a6 % b6 << endl;   // 错误,除数不能为零

	// 两个小数不能做取模运算
	
	// 前置递增和后置递增
	int a = 10;
	a++;
	cout << "a=" << a << endl;

	int b = 10;
	b++;
	cout << "b=" << b << endl;

	// 前置递增,先让变量+1,然后进行表达式运算
	int a7 = 10;
	int b7 = ++a7 * 10;
	cout << "a7=" << a7 << endl;
	cout << "b7=" << b7 << endl;

	// 后置递增,先进行表达式运算,然后让变量+1
	int a8 = 10;
	int b8 = a8++ * 10;
	cout << "a8=" << a8 << endl;
	cout << "b8=" << b8 << endl;

	// 赋值运算符
	int a9 = 10;
	a9 = 10;
	cout << "a9=" << a9 << endl;

	a9 += 2;
	cout << "a9=" << a9 << endl;

	a9 -= 2;
	cout << "a9=" << a9 << endl;

	a9 *= 2;
	cout << "a9=" << a9 << endl;

	a9 /= 2;
	cout << "a9=" << a9 << endl;

	a9 %= 2;
	cout << "a9=" << a9 << endl;

	// 比较运算符
	int a10 = 10;
	int b10 = 20;
	cout << (a10 == b10) << endl;
	cout << (a10 != b10) << endl;
	cout << (a10 < b10) << endl;
	cout << (a10 > b10) << endl;
	cout << (a10 <= b10) << endl;
	cout << (a10 >= b10) << endl;

	// 逻辑运算符
	int a11 = 10;
	cout << "!a11逻辑非" << !a11 << endl;  // 在c++中,除了0,都为真

	cout << !!a11 << endl;   // 逻辑非!

	int a12 = 0;
	int b12 = 10;
	cout << (a12 && b12) << endl;   // 逻辑与&&

	cout << (a12 || b12) << endl;   // 逻辑或||

	int aa = 0;
	cout << "!aa逻辑非" << !aa << endl;
	
	bool bb = true;
	cout << "布尔类型" << bb << endl;

	system("pause");

	return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值