欢迎光临散文网 会员登陆 & 注册

第1讲 C++入门及简单的顺序结构

2023-02-18 20:44 作者:踏月随舟尽西风  | 我要投稿

编程是一种控制计算机的方式,和我们平时双击打开文件、关机、重启没有任何区别

一、软件环境

1. 编辑软件的安装与使用

       DEV C++、VS code(不推荐)

2.作业的评测与提交

       http://ybt.ssoier.cn:8088/

       https://www.luogu.com.cn/

       https://www.acwing.com/

二、编写一个简单的C++程序——手速练习


#include <iostream> // 头文件
using namespace std; // 名字空间
int main() // 主函数
{
	cout << "Hello World!" << endl;
 	return 0; // 程序正常结束
}  

三、语法基础

1. 变量的定义

常用变量类型及范围:

(1)布尔型 bool:true == 1 / false == 0 1B

(2)字符型 char:'a',' ','\n' 1B

(3)整型 int:-2^31 ~ +2^31 - 1  -2147483648 ~ 2147483647 4B

(4)浮点型:

    a. 单精度浮点数 float:1.23,2.5,1.235e2,6 ~ 7 位有效数字 4B

    b. 双精度浮点数 double:15 ~ 16 位有效数字 8B

(5)长整型 long long:-2^63 ~ +2^63 - 1 8B

(6)长浮点型 long double:18 ~ 19 位有效数字 16B / 12B / 8B

存储单位:1Byte = 8 bit,500 Mbps

单位进制:B KB GB TB 1024

变量必须先定义,才可以使用。不能重名。

变量定义的方式:

#include <iostream>
using namespace std;
int main()
{
	int a = 2;
	int b, c = a, d = 10 / 2;
	float e = 2.3, f = 1, g = 1.235e2;
	bool h = true, i = false;
	char j = 'a', k = 'b';
  
	long long l = 100000000000LL;
	return 0;
}

2. 输入输出

整数的输入输出:

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

运行结果:

AC:Accepted

WA:Wrong Answer

CE:Compiler Error 编译错误

TLE:Time Limit Error 超时

MLE:Memory Limit Error 超内存

SF:Segmentation Fault 数组越界

输入输出多个不同类型的变量:

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b << ' ' << a * b << endl;
    return 0;
}

scanf printf

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    int a, b;
    float c, d;
    char e, f;
    double g, h;
    long long i, j;
    scanf("%d%d", &a, &b);
    scanf("%f%f", &c, &d);
    printf("a + b = %d\na * b = %d\n", a + b, a * b);
    printf("%.2f %.3f", c, d); // 保留n位小数输出
    scanf("%c%c", &e, &f); // 会读入空格
    cin >> e >> f; // 不会读入空格
    scanf("%lf%lf", &g, &h);
    printf("%lf %lf", g, h);
    scanf("%lld%lld", &i, &j);
    printf("%lld %lld", i, j);
    
    /*
    int:%d
    float:%f
    double:%lf
    char:%c
    long long:%lld
    */
    return 0;
}

3. 表达式

整数的加减乘除四则运算:+ - * / %

/:5 / 2 = 2 隐式转换

%:5 % 2 = 1 

     -5 % 2 = -1 

     取余数,和数学中的余数定义(0≤余数≤除数)不同,结果取决于前面数的正负,只能整数

#include <iostream>

using namespace std;

int main()
{
    int a = 6 + 3 * 4 / 2 - 2;

    cout << a << endl;

    int b = a * 10 + 5 / 2;

    cout << b << endl;

    cout << 23 * 56 - 78 / 3 << endl;

    return 0;
}

浮点数(小数)的运算:

#include <iostream>

using namespace std;

int main()
{
    float x = 1.5, y = 3.2;

    cout << x * y << ' ' << x + y << endl;

    cout << x - y << ' ' << x / y << endl;

    return 0;
}

整型变量的自增、自减:a++ ++a a-- --a

#include <iostream>

using namespace std;

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

    cout << a << ' ' << b << endl;

    int c = ++ a;

    cout << a << ' ' << c << endl;

    return 0;
}

简写运算:+= -= *= /= %=

变量的强制类型转换:

#include <iostream>

using namespace std;

int main()
{
    float x = 123.12;
    int y = (int)x; // 下取整
    int a = 97;
    char c = (char)a;
    cout << x << ' ' << y << endl;
    cout << c << endl;

    char b = 'A';
    cout << (char)(b + 32) << endl;
    return 0;
}

不同变量做运算时发生隐式类型转换:朝向精度高的类型转换

4. 顺序语句

(1) 输出第二个整数

#include <iostream>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << b << endl;
    return 0;
}

(2) 计算 (a + b) * c的值

#include <iostream>

using namespace std;

int main()
{
    int a, b, c;

    cin >> a >> b >> c;

    cout << (a + b) * c << endl;

    return 0;
}

(3) 带余除法

#include <iostream>

using namespace std;

int main()
{
    int a, b;

    cin >> a >> b;

    int c = a / b, d = a % b;

    cout << c << ' ' << d << endl;

    return 0;
}

(4) 求反三位数

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;

    int a = n % 10;
    n = n / 10;
    int b = n % 10;
    n = n / 10;
    int c = n;

    cout << a << b << c << endl;

    return 0;
}

(5) 交换两个整数

#include <iostream>

using namespace std;

int main()
{
    int a = 3, b = 4;

    int t = a;
    a = b;
    b = t;

    cout << a << ' ' << b << endl;

    return 0;
}

(6) 输出菱形

#include <iostream>

using namespace std;

int main()
{
    char c;

    cin >> c;

    cout << "  " << c << endl;
    cout << " " << c << c << c << endl;
    cout << c << c << c << c << c << endl;
    cout << " " << c << c << c << endl;
    cout << "  " << c << endl;

    return 0;
}


第1讲 C++入门及简单的顺序结构的评论 (共 条)

分享到微博请遵守国家法律