CPP基本语法笔记(上)

简介: CPP基本语法笔记

cpp基本语法笔记


一.变量、输入输出、表达式和顺序语句


1.固定格式


#include<iostream>
using namespace std;
int main()
{
    cout << "Hello World" << endl;
    return 0;
}u


2.变量类型


常用


类型 字节
bool – > false / true – > 0/1 1byte
char ‘c’, ‘a’, ’ ', ‘\n’ 1byte
int -1247483648 ~ 2147483647 4byte
float 1.23, 2.5, 1.235e2 6~7位有效数字 4byte
double 15~16位有效数字 8byte


更长


类型 字节
long long //-2^63 ~ 2^63-1 8byte
long double //18~19位有效数字 16byte


3.输入输出


a+b


cin输入 cout输出


#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin >> a >> b; //输入,方向 >>
    cout << a + b << a * b << endl;  //输出,方向 <<,   endl->回车
    return 0;
}


scanf输入 printf输出


#include<cstdio>
using namespace std;
int main()
{
    int a,b;
    scanf("%d%d", &a, &b); //%d读入整数类型,加&符号 
    printf("a + b =%d\na * b= %d\n", a + b, a * b);
    return 0;
}


浮点数:


#include<cstdio>
using namespace std;
int main()
{
    float a,b;
    scanf("%f%f", &a, &b); //%d读入整数类型,加&符号 
    printf("a + b =%.2f\na * b= %.3f\n", a + b, a * b);  //浮点数用%f,保留几位小数就在中间加点几
    return 0;
}


char型:


#include<cstdio>
using namespace std;
int main()
{
    char a,b;
    scanf("%c%c", &a, &b); //%c读入字符型,%c会读入空格,cin不会
    printf("%c%c", a , b);  
    return 0;
}


double/longlong型:


#include<cstdio>
using namespace std;
int main()
{
    double a,b;
    scanf("%lf%lf", &a, &b); 
    printf("%lf%lf", a , b);  
    long long a,b;
    scanf("%lld%lld", &a, &b); 
    printf("%lld%lld", a , b); 
    return 0;
} 


总结


int: %d


float: %f


double: %lf


char: %c


long long: %lld


4.表达式


b+=a -->b = b+a


b-=a -->b = b-a


b*=a -->b = ba


b/=a -->b = b/a


强制类型转换


int->float float->int(取整)


int ->char (ASCII字符表,相互转换)


5.顺序结构


从前往后,从上往下


二、printf语句与判断结构


1.printf输出格式


使用printf时最好加头文件


printf保留小数点后几位


#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    double f = 138682436874;
    printf("%.7lf\n", f);
    return 0;
}


左右对齐,保留小数点,空位补零


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int a = 1;
    int b = 23;
    int c = 456;
    double d = 12.45;
    printf("%5d!\n", a);  //占5个空位,右对齐
    printf("%-5d!\n", b); //占5个空位,左对齐
    printf("%05d!\n", c); //空位填补0,右对齐
    printf("%5.1lf!", d);   //占5位且保留小数位数为1
    return 0;
}


2.if语句


①最基本语法if-else


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int score;
    cin >> score;
    if (score >= 60)
    {
        cout << "及格" << endl;
    } //一条语句可以省略{}
    else
    {
        cout << "不及格" << endl;
    }
    cout << "结束" << endl;
    return 0;
}


②比较运算符


大于 a > b


小于 a < b


大于等于 a >= b


小于等于 a <= b


等于 a == b


不等于a != b


③if-else if


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int grade;
    cin >> grade;
    if (grade >= 85) cout << 'A' << endl;
    else //蕴含着grade<85
    {
        if (grade >= 70) cout << 'B' << endl;
        else //蕴含着grade<70
        {
            if (grade >= 60) cout << 'C' << endl;
            else //蕴含着grade<60
            {
                cout << 'D' << endl;
            }
        }
    }
}
可优化为——————>(c和cpp中不根据缩进判断语句结束(Python),根据分号判断)
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int grade;
    cin >> grade;
    if (grade >= 85) cout << 'A' << endl;
    else if (grade >= 70) cout << 'B' << endl;
    else if (grade >= 60) cout << 'C' << endl;
    else cout << 'D' << endl;
}


3.条件表达式


(1)与 && and


(2)或 || or


(3)非 ! not


#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a > b && (c > d || a > d)) cout << "" << endl;
    if (a > b || c > d && a > d) cout << "" << endl;
    if (!(a > b)) cout << "" << endl;
    return 0;
}


三、C++中的循环结构


循环语句——代码执行顺序


1.while循环


可以简单理解为循环版的if语句。if语句是判断一次,如果条件成立,则执行后面的语句;while是每次判断,如果成立,则执行循环体中的语句,否则停止。


求1~100中所有数的立方和


#include<iostream>
using namespace std;
int main()
{
    int i = 1;
    int sum = 0;
    while (i <= 100)
    {
        sum += i * i * i;
        i++;
    }
    cout << sum << endl;
    return 0;
}


斐波那契数列(水仙花)


f(1) = 1 f(2) = 1


f(n) = f(n-1) + f(n-2) n>=3


#include<iostream>
using namespace std;
int main()
{
   int a = 1, b = 1;
   int n;
   cin >> n;
   int i = 0;
   while (i < n - 1)
   {
       int c = a + b;
       a = b;
       b = c;
       i++;
   }
   cout << a <<endl;
   return 0;
}


避免写出死循环


2.do while循环


do while语句与while语句非常相似。唯一的区别是,do while语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。(不常用)


3.for循环


for (init-statement : condition: expression) //init语句; 条件语句; 表达式
{
    statement
}


init-statement可以是声明语句、表达式、空语句,一般用来初始化循环变量;


condition是条件表达式,和while中的条件表达式作用一样;可以为空,空语句表示true;


expression一般负责修改循环变量,可以为空。


#include <iostream>
using namespace std;
int main()
{
    for (int i = 0; i < 10; i ++ )
    {
        cout << i << endl;
    }
    return 0;
}


相关文章
|
9月前
|
Ubuntu 编译器 C语言
ARM-GCC与交叉编译
ARM-GCC与交叉编译
|
机器学习/深度学习 数据采集 人工智能
阿里巴巴首次揭秘电商知识图谱AliCoCo!淘宝搜索原来这样玩!
电商技术进入认知智能时代,将给亿万用户带来更加智能的购物体验。经过两年的探索与实践,阿里巴巴的电商认知图谱 AliCoCo 已成体系规模,并在搜索推荐等电商核心业务场景上取得佳绩,关于 AliCoCo 的文章《AliCoCo: Alibaba E-commerce Cognitive Concept Net》也已被国际顶会 SIGMOD 接收,这是阿里巴巴首次正式揭秘领域知识图谱。 本文将通过介绍 AliCoCo 的背景、定义、底层设计、构建过程中的一些算法问题,以及在电商搜索和推荐上的广泛应用,分享 AliCoCo 从诞生到成为阿里巴巴核心电商引擎的基石这一路走来的思考。
18331 1
阿里巴巴首次揭秘电商知识图谱AliCoCo!淘宝搜索原来这样玩!
ZYNQ-AXI总线的信号接口要求以及时序关系
ZYNQ-AXI总线的信号接口要求以及时序关系
824 0
ZYNQ-AXI总线的信号接口要求以及时序关系
|
5月前
|
网络协议 安全 容灾
哪些 DNS 服务器的响应速度快且稳定可靠?
哪些 DNS 服务器的响应速度快且稳定可靠?
3008 4
|
6月前
|
安全 程序员 API
几个被淘汰的Python库,请不要再用!
几个被淘汰的Python库,请不要再用!
198 0
|
机器学习/深度学习 编解码 PyTorch
CVPR 2023 | 主干网络FasterNet 核心解读 代码分析
本文分享来自CVPR 2023的论文,提出了一种快速的主干网络,名为FasterNet。核心算子是PConv,partial convolution,部分卷积,通过减少冗余计算和内存访问来更有效地提取空间特征。
6496 1
|
8月前
|
机器学习/深度学习 人工智能 开发者
AI音效生成器概述
这篇文章介绍了AI音效生成器如何助力提升创作体验。AI音效生成器,如ElevenLabs、Audiogen和LOVO AI,利用深度学习创造和编辑音效,为电影制作、游戏开发及播客制作人提供高效、高质量且多样的声音效果。这些工具节省了时间和成本,具有用户友好的界面,方便各类型创作者使用。考虑功能、易用性、音质、成本等因素,选择合适的AI音效生成器能显著增强内容的专业性和吸引力。
|
9月前
|
机器学习/深度学习 人工智能 运维
|
9月前
|
Rust 测试技术 开发工具
Rust中的Cargo:依赖管理与项目构建
本文将深入探讨Rust编程语言中的Cargo工具。Cargo不仅用于构建Rust项目,还是管理项目依赖的关键组件。我们将了解如何使用Cargo创建新项目、添加依赖项、以及如何通过Cargo进行构建和测试,从而确保项目的顺利开发与部署。
|
9月前
|
C语言
make的执行步骤以及常见的make命令,make distclean 以及和make clean的区别
make的执行步骤以及常见的make命令,make distclean 以及和make clean的区别
376 0

热门文章

最新文章