入门C++编程,从零开始学习基础语法和实现程序

发布时间:2023-05-13

C++编程入门指南

C是一种高级编程语言,被广泛应用于开发操作系统、浏览器、游戏等软件,是计算机专业学生必修的一门课程。本文将介绍从零开始学习C编程的基础语法和实现程序的一些知识和技巧。

一、数据类型

C++中的基本数据类型包括整型、浮点型、字符型和布尔型,其中整型包括shortintlong三个类型,浮点型包括floatdouble两个类型,字符型是用来表示单个字符,布尔型只有两个值:truefalse

#include <iostream>
using namespace std;
int main()
{
    int num = 10;
    float f = 3.14;
    char ch = 'a';
    bool flag = true;
    cout << "num = " << num << endl;
    cout << "f = " << f << endl;
    cout << "ch = " << ch << endl;
    cout << "flag = " << flag << endl;
    return 0;
}

输出结果:

num = 10
f = 3.14
ch = a
flag = 1

可以看到,布尔型的true被输出为1false被输出为0

二、运算符

C++中的运算符包括算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符。 算术运算符包括加、减、乘、除和取模运算,关系运算符包括等于、不等于、大于、小于、大于等于和小于等于运算,逻辑运算符包括与、或和非运算,位运算符包括按位与、按位或、按位取反和按位异或运算,赋值运算符包括等于、加等于、减等于、乘等于、除等于和取模等于运算。

#include <iostream>
using namespace std;
int main()
{
    int a = 10, b = 20;
    cout << "a + b = " << a + b << endl;
    cout << "a - b = " << a - b << endl;
    cout << "a * b = " << a * b << endl;
    cout << "a / b = " << a / b << endl;
    cout << "a % b = " << a % b << endl;
    int c = 30, d = 30;
    cout << "c == d: " << (c == d) << endl;
    cout << "c != d: " << (c != d) << endl;
    cout << "c > d: " << (c > d) << endl;
    cout << "c < d: " << (c < d) << endl;
    cout << "c >= d: " << (c >= d) << endl;
    cout << "c <= d: " << (c <= d) << endl;
    bool e = true, f = false;
    cout << "e && f: " << (e && f) << endl;
    cout << "e || f: " << (e || f) << endl;
    cout << "!e: " << !e << endl;
    int g = 0x5, h = 0x3;
    cout << "g & h: " << (g & h) << endl;
    cout << "g | h: " << (g | h) << endl;
    cout << "~g: " << ~g << endl;
    cout << "g ^ h: " << (g ^ h) << endl;
    int i = 5, j = 10;
    i += j;
    cout << "i += j: " << i << endl;
    i -= j;
    cout << "i -= j: " << i << endl;
    i *= j;
    cout << "i *= j: " << i << endl;
    i /= j;
    cout << "i /= j: " << i << endl;
    i %= j;
    cout << "i %= j: " << i << endl;
    return 0;
}

输出结果:

a + b = 30
a - b = -10
a * b = 200
a / b = 0
a % b = 10
c == d: 1
c != d: 0
c > d: 0
c < d: 0
c >= d: 1
c <= d: 1
e && f: 0
e || f: 1
!e: 0
g & h: 1
g | h: 7
~g: -6
g ^ h: 6
i += j: 15
i -= j: 5
i *= j: 50
i /= j: 5
i %= j: 5

三、控制语句

C++中的控制语句包括顺序结构、选择结构和循环结构。 顺序结构是指按照代码的先后顺序依次执行每一条语句,例如:

#include <iostream>
using namespace std;
int main()
{
    int x = 10, y = 20, z = 30;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
    return 0;
}

输出结果:

x = 10
y = 20
z = 30

选择结构是指根据条件的真假来判断执行哪一条语句,包括if语句和switch语句。

#include <iostream>
using namespace std;
int main()
{
    int x = 10;
    if (x < 0) {
        cout << "x is negative" << endl;
    } else if (x == 0) {
        cout << "x is zero" << endl;
    } else {
        cout << "x is positive" << endl;
    }
    int y = 2;
    switch (y) {
        case 1:
            cout << "y is one" << endl;
            break;
        case 2:
            cout << "y is two" << endl;
            break;
        default:
            cout << "y is not one or two" << endl;
            break;
    }
    return 0;
}

输出结果:

x is positive
y is two

循环结构是指重复执行一段代码,包括while循环、do-while循环和for循环。

#include <iostream>
using namespace std;
int main()
{
    int x = 0;
    while (x < 5) {
        cout << "x = " << x << endl;
        x++;
    }
    int y = 0;
    do {
        cout << "y = " << y << endl;
        y++;
    } while (y < 5);
    for (int i = 0; i < 5; i++) {
        cout << "i = " << i << endl;
    }
    return 0;
}

输出结果:

x = 0
x = 1
x = 2
x = 3
x = 4
y = 0
y = 1
y = 2
y = 3
y = 4
i = 0
i = 1
i = 2
i = 3
i = 4

四、函数

函数是一段可以重复使用的代码,可以实现特定的功能。在C++中定义一个函数需要指定函数的返回类型、函数名称和参数列表,例如:

#include <iostream>
using namespace std;
int add(int a, int b) {
    return a + b;
}
int main()
{
    int x = 2, y = 3;
    cout << "x + y = " << add(x, y) << endl;
    return 0;
}

输出结果:

x + y = 5

五、数组

数组是一种存储一组数据的结构,C++支持静态数组和动态数组。静态数组在定义时需要指定数组的长度,动态数组可以在运行时进行分配和释放。

#include <iostream>
using namespace std;
int main()
{
    int arr1[5] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        cout << "arr1[" << i << "] = " << arr1[i] << endl;
    }
    int arr2[] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        cout << "arr2[" << i << "] = " << arr2[i] << endl;
    }
    int* arr3 = new int[5];
    for (int i = 0; i < 5; i++) {
        arr3[i] = i + 1;
        cout << "arr3[" << i << "] = " << arr3[i] << endl;
    }
    delete[] arr3;
    return 0;
}

输出结果:

arr1[0] = 1
arr1[1] = 2
arr1[2] = 3
arr1[3] = 4
arr1[4] = 5
arr2[0] = 1
arr2[1] = 2
arr2[2] = 3
arr2[3] = 4
arr2[4] = 5
arr3[0] = 1
arr3[1] = 2
arr3[2] = 3
arr3[3] = 4
arr3[4] = 5

六、指针

指针是C++中的重要概念,可以通过指针来访问内存中的数据。指针变量存储的是内存地址。

#include <iostream>
using namespace std;
int main()
{
    int num = 10;
    int* p = &num;
    cout << "num = " << num << endl;
    cout << "p = " << p << endl; // 输出的是指针变量p所指向的内存地址
    cout << "*p = " << *p << endl; // 输出的是p所指向的内存地址中的值
    int arr[5] = {1, 2, 3, 4, 5};
    int* q = arr; // 数组名本身就是一个指针
    for (int i = 0; i < 5; i++) {
        cout << "arr[" << i << "] = " << *(q + i) << endl;
    }
    return 0;
}

输出结果:

num = 10
p = 0x7ffee7b46bdc
*p = 10
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

七、文件操作

C++中的文件操作包括文件的读写和文件指针的操作。文件指针指向文件中的某个位置,可以通过文件指针进行读写。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream fout("data.txt"); // 打开文件用于写入
    for (int i = 1; i <= 5; i++) {
        fout << i << endl;
    }
    fout.close(); // 关闭文件
    ifstream fin("data.txt"); // 打开文件用于读取
    int num;
    while (fin >> num) { // 从文件中读取数据
        cout << num << endl;
    }
    fin.close(); // 关闭文件
    return 0;
}

输出结果:

1
2
3
4
5