C++学习1(数据类型、运算符)

#include <iostream>
using namespace std;
//注释:     先CTRL + K,然后CTRL + C(vs快捷键)
//取消注释: 先CTRL + K,然后CTRL + U

#define day 7
int main()
{
	//变量
	short num1= 2;
	cout << " num1 = " << num1 << endl;
	int num2 = 10;
	cout << " num2 = " << num2 << endl;
	//注意整型,短整型,长整型,长长整型的取值范围。
	//整型  short 2    int 4     long 4    long long 8
	//常量  define  常量名   常量值(宏常量)
	cout << "short占用的内存空间是 " << sizeof(short) << endl;
	cout << "short占用的内存空间是 " << sizeof(num1) << endl;
	cout << "一周有" << day <<"天"<< endl;

	//const 数据类型  常量名   常量值(修饰变量)
	const int month = 12;
	cout << "一年有" << month <<"个月"<< endl;
	cout << "hello world"<<endl;
	

	//浮点型数值
	float f1 = 3.14f;//加f 因为编译器默认是双精度浮点数
	double d1 = 3.1415926;//默认输出6位有效数字
	cout << "f1=" << f1 << endl;
	cout << "d1=" << d1 << endl;
	cout << "float占用的内存空间是 " << sizeof(float)<<"字节" << endl;
	cout << "double占用的内存空间是 " << sizeof(double) << "字节" << endl;

	//科学计数法
	float f2 = 3e2;//3*10^2
	cout << "f2=" << f2 << endl;
	float f3 = 3e-2;//3*10^-2
	cout << "f3=" << f3 << endl;

	//字符型
	char ch = 'a';//不能用双引号创建字符型变量  创建字符型变量时,单引号中只能有一个字符char ch = 'abcdef'错
	char ch1 = 'A';
	cout << "ch=" << ch << endl;
	cout << "字符型变量所占的内存空间" << sizeof(char) <<"byte"<< endl;
		//字符型变量对应的ASCII码
	cout <<"a的ASCII编码是"<< (int)ch << endl;
	cout << "A的ASCII编码是"<<(int)ch1 << endl;
	
	system("pause");
		return 0;
}
#include <iostream>
#include <iomanip>
#include <string>//用C++风格字符串的时候,要包含这个头文件
using namespace std;

int main()
{
	  //转义字符
	  //换行字符  \n

	cout << "hello  C++ \n";
	//反斜杠  \\

	cout << "\\ \n"; //此处两个\控制台只打印一个\

	// 水平制表符  \t  一个\t占八个位置  作用可以整齐输出数据
	cout << "aaaa\thello world" << endl;
	cout << "aaa\thello world" << endl;
	cout << "aaaaa\thello world" << endl;

	//字符串
	//C风格字符串   char   变量名[] = "字符串"
	char str[] = "hello world";
	//注意事项   char 字符名 []     等号后边用双引号包含起来字符串
	cout << str << endl;

	//C++风格字符串   
	string str2 = "hello world";
	//包含一个头文件  #include <string>
	cout << str2 << endl;

	// 布尔类型bool
	bool flag = true; //true 代表真
	cout << flag << endl;
	flag = false;
	cout << flag << endl;//false 代表假
	//本质上1为真0为假
	//bool类型数值所占内存
	cout <<"bool数据类型所占的内存空间"<< sizeof(bool)<<" byte" << endl;
	cout << "\n" << endl;
	//数据的输入
	//1、整型
	int a = 0;
	cout << "请输入整型数据变量" << endl;
	//   cin >> a;
	cout << "整型数据变量a为:" << a << endl;
	 //浮点型   字符型   字符串型   布尔型(布尔赋值只能输入0或任意数非零值都是真)同理
	cout << "\n" << endl;

	//运算符 四则运算+ - * /  +正号 -负号     %取余   
	int x1 = 10;
	int y1 = 3;
	int z1;
	z1 = x1 / y1;
	cout << "z1=" << z1 << endl;//注意避免除数为零
	int y2 = 20;
	double z2;
	z2 = (double)/*强制转换数据类型*/x1 / y2;//此输出为小数
	cout << "z2=" <<setprecision(2) << z2 <<endl;

	//注意两个整数除法商最终舍弃最后小数部分,只保留原有整数部分,但是两个小数相除(double)结果可以输出小数
	//想保留小数部分如下写
	//%  取模运算、注意两个小数之间不能做取模运算
	int b = 2, c;
	c = ++b;  //前置递增   b=3  c=3   先加再运算表达式
	c = b++;  //后置递增   b=3  c=2   先运算表达式再加
	//同理前置递减和后置递减
	cout << "\n" << endl;
	
		//  赋值运算符   
	// 例:
	int m = 0;
	m += 2;//m =m+2
	cout << " m的值为 " <<m<< endl;
	cout << "\n" << endl;
	m -= 2;//m =m-2
	cout << " m的值为 " << m << endl;
	//同理  *=   /=    %=
	cout << "\n" << endl;

	//比较运算符  ==相等于  !=不等于   <小于  >大于  <=小于等于  >=大于等于
	// 逻辑运算符 ! 非  &&与   ||或     C++中除了0都是真

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值