C++基础语法

分享当时学习并敲出的代码,想从0基础学习的人尽可能分析之后再自己敲一遍代码

#include<iostream>
#include<string>    //这里是53行字符串的库
using namespace std;
/*int main() {
//内存检测定义:
	short num1 = 10;                   //sizeof 用于检测内部函数所占内存大小
	long long num2 = 6;
	cout << "short 所占的内存空间:" << sizeof(short) << endl;
	cout << "long long 所占的内存空间:" << sizeof(long long) << endl;
	system("pause");
	return 0;
}*/  //----------------------------------------------------------------------------------------------------
/*int main() {
//精确小数点:
	float num1 = 3.141;  //单精度 可精确至7位
	//float num1 = 3.141f; //后面可以接f,让电脑更易实别
	double num2 = 3.1415; //双精度 可精确至14位
	cout << "double 所占的内存空间:" << sizeof(double) << endl;
	system("pause");
	return 0;
}*/ //-----------------------------------------------------------------------------------------------------
/*int main() {
//科学计数法:
	float num1 = 3e2;    //3*10的2次方
	double num2 = 3e-2;  //3*10的负2次方
	cout << "num1 =  " << num1 << "   num2 = " << num2 << endl;
	system("pause");
	return 0;
}*/  //----------------------------------------------------------------------------------------------------
/*int main() {
//字符定义:
	char qw = 'h';  //创建字符型变量的时候一般用单引号
	char er = 'i';
	cout << qw << er << endl;
	cout << "char 所占的内存空间:" << sizeof(short) << endl;
	//字符变量对应的ASCLL编码
	cout << "h 对应的ASCLL编码的数值:"<<(int)qw << endl;  //每个字符都有一个自己对应的ascll码数值
	cout << "A 对应的ASCLL编码的数值:" << (int)'A' << endl;  //字符串要用单引号哦
	system("pause");
	return 0;
}*/   //------------------------------------------------------------------------------------
/*int main() {
//转译字符 \n等等:行数对齐
	cout << "hello world\n";  //  \n是换行符
	cout << "\\"<<endl;  //第一个反斜杠是告诉电脑我要输出一个反斜杠
	cout << "aaa\thello world" << endl;
	cout << "aaaa\thello world" << endl;
	cout << "aaaaa\thello world" << endl;  	//\t可以更整齐的输出
	cout << "aaaaaaaaaaaaaa\thello world" << endl;
	system("pause");
	return 0;
}*/  //-------------------------------------------------------------------------------------
 /*int main() {
//字符串定义
	string str1 = "hello world"; //字符串是双引号
	cout << str1 << endl;
	system("pause");
	return 0;
} */  //--------------------------------------------------------------------------------------
/*int main() {
//布尔变量
	bool flag1 = true;
	bool flag2 = false;
	cout << flag1 << endl;  //真为1
	cout << flag2 << endl;  //假为0
	system("pause");
	return 0;
}*/    //-----------------------------------------------------------------------------------
/*int main() {
//整型变量
	int a = 0;
	cout << "请给a赋整值:" << endl;
	cin >> a;  //输入a的值
	cout << "输出a值" << a << endl;
//浮点型变量
	float b = 1.2;
	cout << "请给b赋小数值:" << endl;
	cin >> b;  //输入b的值
	cout << "输出b值" << b << endl;
//字符型变量
	char c = 'h';
	cout << "请给c赋字符:" << endl;
	cin >> c;  //输入c的字符
	cout << "输出c值"<< c  << endl;
//字符串变量
	string str = "hi";
	cout << "请给str赋字符串值:" << endl;
	cin >> str;  //输入str的字符串
	cout << "输出str值" << str << endl;
//布尔变量
	bool flag = true;
	cout << "请给c赋值:" << endl;
	cin >> flag;  //输入c的字符
	cout << "输出flag值" << flag << endl;  //非0的值都是真,显示1
	system("pause");
	return 0;
}*/      //----------------------------------------------------------------------------------
/*int main() {
//数字复杂运算符
	int  num1 = 7;
	int num2 = 5;  //只要变量有一个为小数就可以除得小数
	cout << "数字之和为" << num1 + num2 << endl;
	cout << "数字除为" << num1 / num2 << endl;  //两个整数型不可取小数
	cout << num1 % num2 << endl;  //这是小数取余数,取余时必须用整数型
	++num1;  //先让变量加1
	num1++;  //后让变量加1
	int num3 = 4;
	int num6 = 4;             //必须设置两个变量,不然会影响之后程序
	int num4 = ++num3 * 10;  //(4+1)*10=50
	int num5 = num6++ * 10; //  4*10=40
	cout << "num4: " << num4 << "        num5: " << num5 << endl;
	cout << "num3: " << num3 << "        num6: " << num6 << endl;

	int a = 6;
	a += 2;
	cout << "a等于:" << a << endl;
	a *= 3;
	cout << "a等于:" << a << endl;
	system("pause");
	return 0;
}*/   //--------------------------------------------------------------------------------------
/*int main() {
//比较运算符
	int a = 5;
	int b = 6;
	cout << (a == b) << endl;  //0为假,1为真
	cout << (a != b) << endl;
	cout << (a < b) << endl;
	cout << (a <= b) << endl;
	cout << (a > b) << endl;
	cout << (a >= b) << endl;
	system("puase");
	return 0;

}*/      //------------------------------------------------------------------------------------
/*int main() {
//逻辑运算符
	int a = 10;
	int b = 0;
	//cout << !a << endl;
	//cout << !!a << endl;
	cout << (a&&b)<< endl;  //两个变量只有都不为0,才为真(1)
	cout << (a||b) << endl; //有一个为真即可

	system("puase");
	return 0;

}*/

/*int main() {    //if函数————————————————————————————————————————
	int score = 0;
	cout << "请输入你的分数: " << endl;
	cin >> score;
	cout << score << endl;

	if (750>score && score > 600) {  //当分数大于一个数小于一个数时要用&&并列

			//cout << "恭喜你考上了重本大学。" << endl;

		 if (700 > score && score > 640) {
			cout << "恭喜你考上了重本985大学。" << endl;
		}
		 else   {
			 cout << "恭喜你考入清华北大" << endl;
		 }
	}

	else if (600 > score  && score >510)
	{
		cout << "恭喜你考上了一本大学。" << endl;
	}
	else if (510 >score && score > 460)
	{
		cout << "恭喜你考上了二本大学。" << endl;
	}
	else
	{
		cout << "明年再来!" << endl;
	}

	system("puase");
	return 0;

} */ //————————————————————————————————————————————————————————


/*int main() {  //三目运算符——————————————————————————————————————————————
	int a = 0; int b = 20; int c = 30;
	a = (b > c ?  b: c);   //  ()是为了优先运算,b c之间用的是   “冒号”    ,如果b>c则a=b,如果b不大于c则a=c
	//在C++中三目运算符返回的值是变量,可以赋值
	(a > b ? b : a) = 100;
	cout << "a = " << a << endl;  //a=30,是因为43行输出a=c=30
	cout << "b = " << b << endl;  //b=100,是因为a>b,所以b的值定义成了100
	system("puase");
	return 0;
}*/  //————————————————————————————————————————————————————————


/*int main() {   //switch语句——————————————————————————————————————————————
	int score = 0;
	cout << "请您给这个电影打个分:";
	cin >> score;
	cout << "您打的分数为:" << score<<" 分" << endl;
	switch (score)
	{
	case 10:   //这里接的是   “冒号”
			cout << "你认为是经典电影" << endl; break;  //break是让其跳出循环的,不然他会执行接下的所以
	case 9:
		cout << "你认为是很棒的电影" << endl; break;
	case 8:
		cout << "你认为是可以的电影" << endl; break;
	case 7:
		cout << "你认为是一般的电影" << endl; break;
	case 6:
		cout << "你认为是有点差劲的电影" << endl; break;
	default:cout << "你认为是个烂片" << endl;  //default类似于 else功能
	}     //default这里接的是   “冒号”

	system("puase");
	return 0;
}*/
/*int main() {     //while循环语句——————————————————————————————————————————————
	int num = 0;
	while (num<10)
	{
		cout << num << endl;
		num+=2;

	}   //注意死循环

	system("puase");
	return 0;
}*/   //---------------------------------------------------------------------------------------------------------------
/*int main() {      //do while循环
	int num1 = 0;
	do    //先执行一次循环条件,注意num1不等于0时可以死循环
	{
		num1++;
		cout << num1 << endl;
	} while (num1<10);

	system("puase");
	return 0;
}*/          //______________________________________________________________________________________________________

/*int main() {     //for循环
	int i = 0;
	for (i = 1; i < 10; i++)  //注意它的逻辑语法
	{
		cout << i << endl;
	}
	system("puase");
	return 0;
}*/     //----------------------------------------------------------------------------------
/*int main() {            //for循环的for镶嵌套用
	for (int i = 1; i < 10; i++) {   //外层循环

		for (int i = 1; i < 10; i++)  //内层循环
		{
			cout << "* " ;
		}
		cout << endl;
	}
	system("puase");
	return 0;
	system("puase");
	return 0;
}*/  //
//break可以跳出for while swich 循环-------------------------------------------------
/*int main() {
	for (int i = 1; i < 10; i++) {
		cout << i << endl;
		if (i == 5) {
			break;
	}

	}
	system("puase");
	return 0;
}*/      //-------------------------------------------------------------------------
/*int main() {           //continue语句
	for (int i = 1; i < 10; i++) {
		if (i % 2 == 0) {  //这个数除以2后再取余数 0 2 4 6 8
			continue;
			//break会退出循环而continue不会
		}
		cout << i << endl;   //这里的目的是输出基数
	}
	system("puase");
	return 0;
}*/       //-------------------------------------------------------------------------
/*int main() {     //goto无条件跳转语句      尽量不要使用goto以免逻辑混乱
	cout << "1" << endl;
	goto FLAG;  //它会从1直接跳到FLAG执行5
	cout << "2" << endl;
	cout << "3" << endl;
	cout << "4" << endl;
	FLAG:  //这里是冒号
	cout << "5" << endl;
	system("puase");
	return 0;
}*/      //---------------------------------------------------------------------------
/*int main() {      //一维数组定义方式
	//数据类型  数组名 [ 数组长度];
	int arr[5];  //注意这里数组定义的元素是01234这五个数
	arr[0] = 0;
	arr[1] = 10;
	arr[2] = 20;
	arr[3] = 30;
	arr[4] = 40;
	//cout << arr[0] << endl;
	//cout << arr[1] << endl;
	//cout << arr[2] << endl;
	//cout << arr[3] << endl;
	//cout << arr[4] << endl;

	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}

	int arr2[]{ 10,20,30,40,50 };  //把数组数据定义进去后它会自动推测元素个数
	cout << arr2[0] << endl;
	cout << arr2[1] << endl;
	cout << arr2[2] << endl;
	cout << arr2[3] << endl;
	cout << arr2[4] << endl;
	system("puase");
	return 0;
}*/      //--------------------------------------------------------------------------------
/*int main() {     //一维数组 -数组名     注意数组名是一个常量不能改变
	int arr2[5]{ 10,20,30,40,50 };
	cout << "整个数组所占的空间: " << sizeof(arr2) << endl;
	cout << "每个元素所占的空间: " << sizeof(arr2[1]) << endl;  //任意哪个元素都可以
	cout << "元素数量为: " << sizeof(arr2) / sizeof(arr2[1]) << endl;

	cout << "数组首地址为:" << arr2 << endl;       //这是16进制打印
	cout << "数组首地址为:" << (int)arr2 << endl; //(int)是10进制
	cout << "第一个元素的位置:" << (int)&arr2[0] << endl;
	cout << "第二个元素的位置:" << (int)&arr2[1] << endl;
	system("puase");
	return 0;
}*/      //--------------------------------------------------------------------------------

/*int main() {   //一维数组 自动升序排序
	int arr[9]{ 4,2,8,0,5,7,1,3,9};

	//第一次交换运算的次数为8次 (42)(28)(80)(05)(57)(71)(13)(39)
	for (int i = 0; i < 9-1; i++)  //外层循环为排数行数
	{
		for (int j = 0; j < 8-i; j++) {    //8-i是为了随着行数减少你所要交换的顺序渐渐的也会减少
										  //第一次交换为8次,第二次为7次.........
			if (arr[j] > arr[j + 1]) {
				int temp = arr[j];    //这三行是交换运算方法
				arr[j] = arr[j + 1]; //
				arr[j + 1] = temp;  //
			}
	}

	}
	cout << "排序后的顺序:" << endl;
	for (int i = 0; i < 9; i++)  //最后输出值
	{
		cout << arr[i];
	}
	cout << endl;
	system("puase");
	return 0;
}*/       //---------------------------------------------------------------------------------------------
/*int main() {     //二维数组定义

	//方式一
	int arr2[2][3]={
		{1,2,3},
		{4,5,6}
	};
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 3; j++) {

			cout << arr2[i][j] << "   ";
		}
		cout << endl;
	}


	//方式二
	int arr3[2][3] = { 1,2,3,4,5,6 }; //多打数字的话会报错
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 3; j++) {

				cout << arr3[i][j] << "   ";
			}
			cout << endl;
		}

	//方式三
		int arr4[][3] = { 1,2,3,4,5,6 };  //排数可以推算但是列数是不能空的
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 3; j++) {

				cout << arr4[i][j] << "   ";
			}
			cout << endl;
		}

	system("puase");
	return 0;
}*/     //----------------------------------------------------------------------
/*int main() {     //二维数组名内存空间
	int arr2[2][3] = {
		{1,2,3},
		{4,5,6}
	};
	cout << "所占的内存空间:" << sizeof arr2 << endl;
	cout << "二维数组第一行所占的内存空间:" << sizeof(arr2[0]) << endl;
	cout << "二维数组第一行第一个元素所占的内存空间:" << sizeof(arr2[0][0]) << endl;
	cout << "二维数组的行数为: " << sizeof arr2 / sizeof(arr2[0]) << endl; //总数除以行
	cout << "二维数组第二行的元素个数为:" << sizeof(arr2[1]) / sizeof(arr2[1][0]) << endl;  //行除以个
	cout << "二维数组的首地址:" << (int)arr2 << endl;              //十进制
	cout << "二维数组的首地址:" << arr2 << endl;                  //十六进制
	cout << "二维数组的第二行首地址: " << (int)arr2[1] << endl;  //十进制
	cout << "二维数组的第二行第一个元素首地址: " << (int)&arr2[1][0] << endl;  //  &(取值符)才能取出元素地址
	system("puase");                                                           //固定语法
	return 0;
} */   //------------------------------------------------------------------------------------------------------
/*int main() {  //两种字符串数组定义方法
string  arr2[3]{ "one","two","three" };
string str[3] = { "one","two", "three" };
cout << arr2[0]<< endl;                                                     //
system("puase");                                                           //固定语法
return 0;
}*/
int main() {

	while (1)
	{
		for ( int i = 0; i <6; i++)
		{
			cout << "llll" << endl;
			//return(0);
		}
		//cout << "llll" << endl;
		return(2);
	}
}

代码的解析都写在程序里了

程序学习来自B站黑马程序员

https://www.bilibili.com/video/BV1et411b73Z

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RouDragon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值