C++教程(2023-1-4)

第一章:C++介绍

1.第一个c++程序

c++程序:
编写c++程序的步骤: 创建项目  创建文件 编写代码 运行程序
//
// Created by ~风轻云淡~ on 2022/12/19.
//
#include "iostream"
using namespace std;
int main(){
    
    cout<<"helloWorld"<<std::endl;
    
    system("pause");

    
    return 0;

}

2.注释:在代码中加一些说明和解释 方便自己和其他程序员阅读代码
	两种格式:单行注释 //  通常放在一行代码的上方 对该代码说明
	
			多行注释
				/**/ t通常放在一段代码的上方
				
				
	//
// Created by ~风轻云淡~ on 2022/12/19.
//
#include "iostream"
using namespace std;
int main(){

    cout<<"helloWorld"<<std::endl;

    system("pause");
//单行注释

/*
 * 多行注射
 * */
    return 0;

}



//
// Created by ~风轻云淡~ on 2022/12/19.
//
#include "iostream"
using namespace std;
int main(){

    cout<<"helloWorld"<<std::endl;

    system("pause");
//单行注释

/*
 * 多行注射
 * */
    return 0;

    /*
     * main是一个程序的入口 每个程序都必须有这么一个函数 有且仅有一个
     * 
     * */
    
}


	

2.变量

变量存在的作用:方便我们管理内存空间

变量创建的语法:数据类型 变量名=变量的初始值;
				int   a	 =100;		


//
// Created by ~风轻云淡~ on 2022/12/19.
//

#include "iostream"
using namespace std;
int main(){
//    变量创建的语法 :


//变量创建的语法:
int  a=10;
cout <<"a="<<a<<endl;


    system("pause");
    return 0;

}




3.常量

作用:用于记录程序中不可更改的数据
c++定义常量的两种方式
1.#define 宏常量 :#define 常量名 常量值
		通常在文件的上方定义 表示一个常量
2.const 修饰的变量 const 数据类型 常量名=常量值
 通常在变量定义前加关键字const 修饰变量为常量 不可修改
 
 
 #include <iostream>

/*常量的定义方式:
 *      #define 宏常量
 *          2.const修饰的变量
 * */

//1.#define  N
#define Day 7
using namespace std;


int main() {

    cout<<"一周有:"<<Day<<"天"<<endl;
    
///2.const修饰的变量  也是常量
const int m=12;

cout<<"m的值为:"<<m<<endl;
    return 0;
}

 

4.编程常用的关键字

与java  python 中的一样:
class  return  int  float double 

因此不能使用关键字给变量或常量起名字
    
    
    

5.标识符命名规则

标识符:1.标识符不能是关键字
		int int=10  这样子不允许
	2.标识符只能由字母 下划线 数字组成
		int abc=10  int  _abc=20
	3.标识符中字母区分大小写
    		
	4.标识符是区分大小写的

第二章:数据类型

1.常见数据类型


#include <iostream>

#define Day 7
using namespace std;


int main() {

// 数据类型存在的意义:给变量分配合适的内存空间  区别就是占用的内存空间不同

/*
 * short  短整型
 *int 整型 
 * long 长整型
 * 
 * */

//1.短整型:short
short num1=10;

//2.整型 int
int num2=10;

//3.长整型long
long num3=100;

//4.双精度
double num4=900;

//5.浮点型
float num5=90000;

    return 0;
}



2.sizeof关键字

#include <iostream>

#define Day 7
using namespace std;


int main() {

// 数据类型存在的意义:给变量分配合适的内存空间  区别就是占用的内存空间不同

/*
 * short  短整型
 *int 整型
 * long 长整型
 *
 * */

//1.短整型:short
short num1=10;

//2.整型 int
int num2=10;

//3.长整型long
long num3=100;

//4.双精度
double num4=900;

//5.浮点型
float num5=90000;



//利用sizeof求出数据类型的内存占用大小
//语法:sizeof(数据类型/变量)
cout<<"num1数据类型是" <<sizeof(num1) <<endl;

cout<< "num2的数据类型是:"<<sizeof (num2)<< endl;



    return 0;
}

3.浮点型

float  浮点型
double  双精度
#include <iostream>

#define Day 7
using namespace std;


int main() {



//1.短整型:short
short num1=10;

//2.整型 int
int num2=10;

//3.长整型long
long num3=100;

//4.双精度
double num4=900;

//5.浮点型  默认情况下输出一个小数  会显示出6位有效数字
float num5=90.56f;

cout<< "num5="<< num5 <<endl;


//科学技术发:
float x=3e2;//3*10^2
cout<< "x的值为!"<< x<< endl;







    return 0;
}

4.字符型

char ch='a'  作用:用于显示单个字符
#include <iostream>

#define Day 7
using namespace std;


int main() {


//1.创建字符型变量
char ch='a';
cout<<"转换一下" << ch+92<<endl;

//注意:不能使用双引号创建
//创建的时候单引号内只能有一个字符
//char j='dhdh';   这个就是错误的

//字符对应的ASCII码:a 97  A-65


    return 0;
}

5.转义字符

转义字符

		\a
		\b
		\f
		\n
		\t
		#include <iostream>

#define Day 7
using namespace std;


int main() {


//1.换行符  \n
cout<< "你好\n  你好\n" <<endl;
//    2.水平制表符 \t  整齐输出数据
cout<< "asfdasf\thjhjgjgh\t"<<endl;
    return 0;
}

		

6.字符串类型

#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//    1.C语言字符串  cha   注意;char 字符串名[]
//等号后面要使用双引号包含起来
    char  str[]="hello word";

//    2.c++版本字符串
string string1="hello World";
cout<<string1 <<endl;
    

}

7.布尔类型

#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//布尔值:判断对错


bool flag= true;
cout<<flag<<endl;//输出1

bool j= false;
cout<<j<<endl;//输出0

}

8.数据的输入

作用:从键盘上获取数据
	关键字:cin>>'值'
	
		#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//1.整型
int a=0;
cout<<"请输入a的值"<<endl;
cin>>a;
cout<<"a的值为:"<<a<<endl;

//    2.浮点型
    float b=0;
    cout<<"请输入a的值"<<endl;
    cin>>b;
    cout<<"a的值为:"<<b<<endl;
//3.字符型
    char c='a';
    cout<<"请输入a的值"<<endl;
    cin>>a;
    cout<<"a的值为:"<<c<<endl;

//4.字符串类型
    string str="hello";
    cout<< "请输入"<< endl;
    cin>>str;
    cout<<"str的值为:"<<str<<endl;
}

第三章:数据的运算

1.算数,取模运算符

#include <iostream>

#define Day 7
using namespace std;

//使用c++风格的字符串要包含的头文件
#include "string"
int main() {

//1.加减乘除
int a1=10;
int a2=45;
cout<<a1+a2<<endl;
cout<<a1-a2<<endl;
cout<<a1*a2<<endl;
cout<<a1/a2<<endl;//两个指数相除 将小数部分去除

cout<<"你好"<<endl;

//2.取模运算:求两个相除的余数
cout<<a1%a2<<endl;
两个小数之间是不可以做取模运算的


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值