C++基础(三)

本文介绍了C++中的转义字符,如 用于换行, 用于制表,以及作为反斜线。接着讲解了字符串类型,包括C风格和C++风格的声明方式。此外,还讨论了布尔类型bool的true和false值。文章还涵盖了数据输入cin的使用方法,以及不同类型的变量如何接收输入。最后,文章详细阐述了算术、赋值、比较和逻辑运算符的用法。
摘要由CSDN通过智能技术生成

1.转义字符

用于表示一些不能显示出来的ASCII字符

比较重要的转义字符:

(1)\n 换行

(2)\t水平制表

(3)\\反斜线字符“\”

#include<iostream>
using namespace std;

int main()
{
	// \n
	cout<<"helloworld\n"<<endl; //\n等价于<<

	// \\

	cout<<"\\"<<endl;

	//\t

	cout<<"456789\t22245"<<endl;
	cout<<"1234567\t22245"<<endl;
	cout<<"123489\t22245"<<endl;
	
	system("pause");

	return 0;
}

2.字符串型

用于表示一串字符

两种风格:

(1)C风格:char 变量名[] = “字符串值”

(2)C++风格:string 变量名 = “字符串值”,使用时要包含头文件#include <string>

3.布尔类型

用于代表真或假的值

(1)true 真,本质是1

(2)false 假,本质是0

bool类型占1个字节大小

#include<iostream>
using namespace std;

int main()
{
	//创建bool数据类型
	bool x=true;
	cout<<x<<endl;

	x=false;
	cout<<x<<endl;

	//查看bool类型所占内存空间
	cout<<"bool类型所占的内存空间:"<<sizeof(x)<<endl;
	
	system("pause");

	return 0;
}

 

4.数据的输入

用于从键盘获取数据

关键字:cin

语法:cin>>变量

#include<iostream>
using namespace std;
#include<string>

int main()
{
	//数据的输入
	//cin>>变量名

	//整型
	int a;
	cout<<"请给整型变量a赋值:"<<endl;
	cin>>a;
	cout<<"整型变量a的值为:"<<a<<endl;

	//浮点型
	float b;
	cout<<"请给浮点型变量b赋值:"<<endl;
	cin>>b;
	cout<<"浮点型变量b的值为:"<<b<<endl;

	//字符型
	char c;
	cout<<"请给字符型变量c赋值:"<<endl;
	cin>>c;
	cout<<"字符型变量c的值为:"<<c<<endl;

	//字符串型
	string d; //要使用string关键字要包含头文件#include<string>
	cout<<"请给字符串型变量d赋值:"<<endl;
	cin>>d;
	cout<<"字符串型变量d的值为:"<<d<<endl;
	
	//bool类型
	bool e=true;
	cout<<"请给bool类型e赋值:"<<endl;
	cin>>e;
	cout<<"bool类型的值为:"<<e<<endl; //bool类型的值只要是非0的都是真

	system("pause");

	return 0;
}

 

 5.运算符

用于执行代码运算

(1)算数运算符

+ 表示正数和加法运算

- 表示负数和减法运算

* 表示乘法运算

/ 表示除法运算

% 表示取模(取余)运算

++ 前置递增

++ 后置递增

-- 前置递减

-- 后置递减

#include<iostream>
using namespace std;
#include<string>

int main()
{
	int a=1;
	int b=2;

	// +
	cout<<"1+2="<<a+b<<endl;

	// -
	cout<<"1-2="<<a-b<<endl;

	//*
	cout<<"1*2="<<a*b<<endl;

	// /
	cout<<"1/2="<<a/b<<endl;

	// %
	cout<<"1%2="<<a%b<<endl; //两个小数不可以做取余运算

	//++
	int x=5;
	cout<<"x++="<<x++<<endl;
	x=5;
	cout<<"++x="<<++x<<endl;

	//--
	x=5;
	cout<<"x--="<<x--<<endl;
	x=5;
	cout<<"--x="<<--x<<endl;


	system("pause");

	return 0;
}

(2)赋值运算符

  给变量赋新的值

=

+=

-=

*=

/=

%=

#include<iostream>
using namespace std;
#include<string>

int main()
{
	//赋值运算符
	// =
	int a=100;
	a=10;
	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/=3;
	cout<<"a="<<a<<endl;

	// %/
	a=10;
	a%=3;
	cout<<"a="<<a<<endl;

	system("pause");

	return 0;
}

(3)比较运算符

== 相等于

!= 不等于

< 小于

> 大于

<= 小于等于

>= 大于等于

#include<iostream>
using namespace std;
#include<string>

int main()
{
	//比较运算符
	//==
	int a=10;
	int b=20;

	cout << (a==b) << endl; //由于运算符的优先级别,需要加括号,
	                        //否则endl无法判断输出,bool进行判断
	                        //0是假,1是真
	//!=
	cout << (a!=b) << endl;
	//<
	cout << (a<b) << endl;
	//>
	cout << (a>b) << endl;
	//<=
	cout << (a<=b) << endl;
	//>=
	cout << (a>=b) << endl;

	system("pause");

	return 0;
}

(4)逻辑运算符

与或非

#include<iostream>
using namespace std;
#include<string>

int main()
{
	//逻辑运算符
	//&& 与 
	int c=10; //非0数都为真 c为真
	int b=0; //b为假
	cout << (c && b) <<endl;

	//|| 或
	int e=0; //e为假
	int f=89; //f为真
	cout << (e||f) << endl;

	//! 非
	int a=10;
	cout << !a << endl; //在C++中除了0都为真
	cout << !!a << endl;

	system("pause");

	return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值