(常规更新)1.C++入门第一讲——编程如此简单

本文从编程的基本概念出发,探讨了计算机编程的原理,并着重介绍了C++编程,包括程序、计算机语言、编辑和编译的定义。讲解了C++的开发环境,如Microsoft Visual Studio、Dev-C++和Code::Blocks等。最后,通过实例展示了如何编写简单的""Hello,world""程序,强调了头文件、名字空间和主函数的作用。" 132850162,19453851,加权双曲线定位的DV-HOP算法及MATLAB实现,"['算法', '无线通信', '数据处理', 'MATLAB编程', '定位技术']
摘要由CSDN通过智能技术生成

格言:自立才能自主,靠人更须靠己。有志之人立长志,无志之人常立志。千里之行,始于足下;艰难困苦,玉汝于成。少壮不努力,老大徒伤悲。✊✊✊
📫 如果您觉得文章里有错误的地方,欢迎指正!和大家一起学习,共同进步
🔥 如果感觉博主的文章还不错的话,还请 👍 关注、点赞、收藏三连支持 👍 一下博主哦

目录

 

引子

一.什么是编程?

 二.编程的概念

1.程序

计算机语言

2.编辑和编译

3.开发环境

C++开发环境介绍:

 1、Microsoft Visual C++ 6.0

2、Microsoft Visual Studio

3、Dev-C++

4、C语言开发环境 v2

 5、Code::Blocks

6、JetBrains Clion

三.编程在实际生活中的应用

四.程序设计

【1.1】输出打印Hello,world

 1.头文件

2.名字空间

 3.主函数


引子

为什么计算机能管理庞大的系统?为什么计算机能按照人的命令去执行(十万个为什么)?面对计算机,每个人都有自己的或多或少的疑问,那么今天我们就来揭开计算机神秘的面纱!

一.什么是编程?

编程是编定程序的中文简称,就是让计算机代码解决某个问题,对某个计算体系规定一定的运算方式,使计算体系按照该计算方式运行,并最终得到相应结果的过程。

为了使计算机能够理解人的意图,人类就必须将需解决的问题的思路、方法和手段通过计算机能够理解的形式告诉计算机,使得计算机能够根据人的指令一步一步去工作,完成某种特定的任务。这种人和计算体系之间交流的过程就是编程。

编程:设计具备逻辑流动作用的一种“可控体系”【注:编程不一定是针对计算机程序而言的,针对具备逻辑计算力的体系,都可以算编程。

(来源:百度百科)

个人理解:为了使计算机能够理解我们的意图,人类就必须将需解决的问题的思路、方法和手段通过计算机能够理解的形式告诉计算机,使得计算机能够根据人的指令一步一步去工作,完成某种特定的任务。这种人和计算体系之间交流的过程就是编程

 二.编程的概念

1.程序

程序(计算机程序)是一组计算机能识别和执行的指令,运行于电子计算机上,满足人们某种需求的信息化工具

计算机语言

计算机语言就是人与计算机之间“交谈”的语言。计算机语言有很多,比如说BASIC语言,C语言,C++语言,Java语言,Python语言等,每个语言都有每个语言的规则,即使是同一个语言,要是版本不同,规则可能也会有少许不同(比如Visual C++和Dev-C++)

2.编辑和编译

编辑

程序编辑是计算机用语,根据一定算法,用一种高级语言编写出源程序

编译

编译程序(Compiler,compiling program),也称为编译器,是指把用高级程序设计语言书写的源程序,翻译成等价的机器语言格式目标程序翻译程序。编译程序属于采用生成性实现途径实现的翻译程序。它以高级程序设计语言书写的源程序作为输入,而以汇编语言或机器语言表示的目标程序作为输出。编译出的目标程序通常还要经历运行阶段,以便在运行程序的支持下运行,加工初始数据,算出所需的计算结果。

两者之间的区别

 编辑是指程序代码、界面等的输入、构建,编辑等,在这期间,开发工具会对输入的代码进行一般的语法检查等。主要是指代码的输入、编辑。

编译是指开发工具的编译程序对编辑过的代码进行转换,以便生成可以执行的代码文件。编译一般是在编辑工作完成以后进行的。

3.开发环境

C++开发环境介绍:

 1、Microsoft Visual C++ 6.0

VC 6.0现在已经集成在Microsoft Visual Studio上面。

它曾经是最为经典的编译工具,但是由于年代久远,此工具微软官方早已不在维护。现在此工具有很多BUG和兼容性问题,(微软原版的)无法安装在Windows 10系统上面。

由于这个软件太经典了,一些第三方的开发者依然在维护这个工具,很多学校/教材也依然在使用这个工具

2、Microsoft Visual Studio

Visual Studio是目前最为流行的Windows平台应用程序的商业集成开发环境。

功能强大,界面友好。但是空间占用、资源占用都比较大,操作也复杂。最新版本为2019。

3、Dev-C++

Dev-C++ 是Windows环境下的轻量级C/C++ 集成开发环境。遵循C++ 11标准,同时兼容C++98标准。

Dev-C++ 功能简洁,易于学习和使用,但同时该软件功能不完善,存在一些问题,并且从未在商业级的软件开发中使用过,只适合于初学者和教学使用。遗憾的是,我的教材使用该软件进行教学

Dev-C++ 由Orwell公司继续更新开发(现已停止),最新版本为5.11,使用GCC 4.9,上次更新时间为2015年4月。这是官方博客以及下载链接

Dev-C++现在被一些公司或者第三方开发者接手:

1、 Embarcadero Dev-C++,本文写时,上次更新时间为2020 年7月。

2、 banzhusoft(斑竹软件) Dev-C++(推荐),本文写时,上次更新时间为2020年9月。

此作者对于原软件做了大量的工作,进行了大幅度的优化,非常适合中国的初学者强烈推荐

4、C语言开发环境 v2

开发者说:在线C语言开发环境依然处于测试阶段,语言标准和标准库支持还不完整。

经过作者测试没有发现任何问题,非常好用,推荐

 5、Code::Blocks

Code::Blocks 是开放源码跨平台C/C++集成开发环境。

简单易用,免费,免安装,应该是最适合初学者使用的集成开发环境,强烈推荐

已经通过gcc8.1支持C++最新标准C++20。该软件附带了最新版的MinGW

6、JetBrains Clion

JetBrains Clion是最为流行(仅次微软)的C & C++商业级集成开发环境,功能非常强大。

免安装,即开即用,中文汉化,简单易上手。

三.编程在实际生活中的应用

编程在生活中的应用太多了,比如:游戏、风扇、空调等一些电子设备板上用的都是编程

举个例子

以下是一个狼人杀游戏的代码(原文链接):

#include<bits/stdc++.h>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<windows.h>
using namespace std;
struct IDname
{
    int geshu;
    string NAME;
};
IDname jue_se[100];
struct ID
{
    int num;
    bool life;
    string name;
    int know;
    int how;
};
ID player[21];
int n, MY, kill1, kill2;
char a;
bool jieyao = 1, duyao = 1;
int lieren, shouwei = 0;
void init1()
{
    jue_se[1].NAME = "村民 ";
    jue_se[2].NAME = "狼人 ";
    jue_se[3].NAME = "女巫 ";
    jue_se[4].NAME = "预言家 ";
    jue_se[5].NAME = "猎人 ";
    jue_se[6].NAME = "守卫 ";
}
void init2(int nn)
{
    switch (nn)
    {
    case 6:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 2;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 0;
        jue_se[5].geshu = 0;
        jue_se[6].geshu = 0;
        break;
    case 7:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 2;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 0;
        jue_se[6].geshu = 0;
        break;
    case 8:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 3;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 0;
        jue_se[6].geshu = 0;
        break;
    case 9:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 3;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 0;
        break;
    case 10:
        jue_se[1].geshu = 4;
        jue_se[2].geshu = 3;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 0;
        break;
    case 11:
        jue_se[1].geshu = 4;
        jue_se[2].geshu = 4;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 0;
        break;
    case 12:
        jue_se[1].geshu = 4;
        jue_se[2].geshu = 4;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 1;
        break;
    default:
        cout << "输入错误,再见" << endl;
        exit(0);
        break;
    }
}
int van[10] = { 7,4,6,43,35,1,2,8,20,19 };
void init3(int nn)
{
    srand(time(0));
    Sleep(rand() % 44);
    int x = 10000;
    int t = rand();
    srand(time(NULL));
    int y = van[(rand() % 100 * van[rand() % 10] + t) % 10];
    if (nn <= 6)
        x = abs(x * 6 / y) % 3 + 1;
    else if (nn <= 8)
        x = abs(x * 7 / y) % 4 + 1;
    else if (nn <= 11)
        x = abs(x * 8 / y) % 5 + 1;
    else if (nn <= 14)
        x = abs(x * 9 / y) % 6 + 1;
    do
    {
        if (nn <= 6)
            x = x % 3 + 1;
        else if (nn <= 8)
            x = x % 4 + 1;
        else if (nn <= 11)
            x = x % 5 + 1;
        else if (nn <= 14)
            x = x % 6 + 1;
        if (jue_se[x].geshu > 0)
        {
            player[nn].name = jue_se[x].NAME;
            if (player[nn].name == "猎人 ")
                lieren = nn;
            if (player[nn].name == "守卫 ")
                shouwei = nn;
            player[nn].life = 1;
            player[nn].num = nn;
            player[nn].know = 0;
            jue_se[x].geshu--;
            player[nn].how = 0;
            break;
        }
    } while (jue_se[x].geshu == 0);
}
void printhhh()
{
    int cm = 0;
    int sz = 0;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 0)
            continue;
        else if (player[i].name == "村民 ")
            cm++;
        else if (player[i].name == "女巫 " || player[i].name == "预言家 " || player[i].name == "猎人 " || player[i].name == "守卫 ")
            sz++;
    }
    if (sz == 0 || cm == 0)
        cout << "狼人阵营胜利" << endl;
    else
        cout << "好人阵营胜利" << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << left << setw(3) << player[i].num << ": " << player[i].name << " ";
        if (player[i].life == 0)
            cout << "死亡" << "\t";
        else
            cout << "存活" << "\t";
        if (player[i].how == 0)
            cout << "最终存活 " << endl;
        else if (player[i].how == 1)
            cout << "最终被狼人杀死" << endl;
        else if (player[i].how == 2)
            cout << "最终被投票投死" << endl;
        else if (player[i].how == 3)
            cout << "最终被女巫毒死" << endl;
        else if (player[i].how == 4)
            cout << "最终被猎人射杀" << endl;
    }
    system("pause");
    system("pause");
    system("pause");
}
void print(int day, int ti)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    if (ti == 0)
        SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "\t\t\t\t第" << day << "天 ";
    if (ti == 0)
        cout << "白天" << endl;
    else
        cout << "夜晚" << endl;
    cout << "我的位置:" << MY << "号" << endl;
    for (int i = 1; i <= 6; i++)
    {
        cout << player[i].num << "号位 ";
    }
    cout << endl;
    for (int i = 1; i <= 6; i++)
    {
        if (player[i].life == 1)
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            cout << "存活 ";
        }
        else
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << "已死亡 ";
        }
    }
    if (ti == 0)
        SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << endl;
    for (int i = 1; i <= 6; i++)
    {
        if (player[i].know == 0)
            cout << "未知 ";
        else if (player[i].know == 1)
        {
            if (player[i].name == "狼人 ")
                cout << "狼人 ";
            else
                cout << "好人 ";
        }
        else if (player[i].know == 2)
            cout << player[i].name << " ";
    }
    cout << endl << endl;
    for (int i = 7; i <= n; i++)
    {
        if (i < 10)
            cout << player[i].num << "号位 ";
        else
            cout << player[i].num << "号位 ";
    }
    cout << endl;
    for (int i = 7; i <= n; i++)
    {
        if (player[i].life == 1)
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            cout << "存活 ";
        }
        else
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << "已死亡 ";
        }
    }
    if (ti == 0)
        SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << endl;
    for (int i = 7; i <= n; i++)
    {
        if (player[i].know == 0)
            cout << "未知 ";
        else if (player[i].know == 1)
        {
            if (player[i].name == "狼人 ")
                cout << "狼人 ";
            else
                cout << "好人 ";
        }
        else if (player[i].know == 2)
            cout << player[i].name << " ";
    }
    cout << endl << endl;
}
int shou = 0;
void shoushui(int hhh, int hhhh)
{
    int x;
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "守~卫~请~睁~眼~~~" << endl;
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    if (MY == shouwei && player[MY].life == 1)
    {
        cout << "请问你要守护谁?" << endl << "输入:";
        cin >> x;
        while (x == shou || x<1 || x>n || player[x].life == 0)
        {
            cout << "输入错误,请重新输入" << endl << "输入:";
            cin >> x;
        }
        shou = x;
    }
    else if (player[shouwei].life == 1)
    {
        cout << "请问你要守护谁?" << endl;
        Sleep(rand() % 98);
        srand(time(0));
        x = rand() % n + 1;
        while (x == shou || player[x].life == 0)
        {
            Sleep(rand() % 98);
            srand(time(0));
            x = rand() % n + 1;
        }
        shou = x;
    }
    else
    {
        cout << "请问你要守护谁?" << endl;
        Sleep(3000);
        shou = -1;
    }
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "守~卫~请~闭~眼~~~" << endl;
}
struct tou
{
    int xxx;
    int num;
    int toupiaoquan;
};
tou TOU[13];
bool cmp(tou x, tou y)
{
    if (x.xxx == y.xxx)
        return x.num < y.num;
    return x.xxx > y.xxx;
}
bool cmp1(tou x, tou y)
{
    return x.num < y.num;
}
void toupiao(int ddd, int nnn)
{
    //--------1--------
    int x;
    Sleep(2000);
    system("cls");
    print(ddd, nnn);
    cout << "现在大家请投票";
    for (int i = 1; i <= 3; i++)
    {
        cout << ".";
        Sleep(500);
    }
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        TOU[i].num = i;
        TOU[i].toupiaoquan = 1;
        TOU[i].xxx = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 1)
        {
            Sleep(3000);
            if (i == MY)
            {
                cout << "请投票...(0弃权)" << endl;
                cin >> x;
                while (player[x].life == 0 && x != 0)
                {
                    cin >> x;
                }
                if (x == 0)
                    cout << MY << "号玩家弃权" << endl;
                else
                    cout << MY << "号玩家投给了" << x << "号玩家" << endl;
            }
            else
            {
                srand(time(0));
                if (player[i].name == "狼人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i))
                    {
                        Sleep(rand() % 98);
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else if (player[i].name == "预言家 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i))
                    {
                        Sleep(rand() % 98);
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || x == i))
                    {
                        Sleep(rand() % 98);
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
            }
            if (x != 0)
                TOU[x].xxx++;
        }
    }
    Sleep(3000);
    sort(TOU + 1, TOU + n + 1, cmp);
    if (TOU[2].xxx != TOU[1].xxx)
    {
        cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
        player[TOU[1].num].life = 0;
        player[TOU[1].num].how = 2;
        Sleep(3000);
        return;
    }
    else
    {
        TOU[1].toupiaoquan = 0;
        TOU[2].toupiaoquan = 0;
        system("cls");
        print(ddd, nnn);
        cout << TOU[1].num << "号," << TOU[2].num << "号";
        int i;
        for (i = 3; i <= n; i++)
        {
            if (TOU[i].xxx == TOU[1].xxx)
            {
                TOU[i].toupiaoquan = 0;
                cout << "," << TOU[i].num << "号";
            }
            else
                break;
        }
        if (i == n + 1)
        {
            for (int i = 1; i <= n; i++)
                TOU[i].toupiaoquan = 1;
        }
        cout << "平票" << endl;
    }
    //--------2--------
    sort(TOU + 1, TOU + n + 1, cmp1);
    cout << "请再次投票";
    for (int i = 1; i <= 3; i++)
    {
        cout << ".";
        Sleep(500);
    }
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 1 && TOU[i].toupiaoquan == 1)
        {
            Sleep(3000);
            if (i == MY)
            {
                cout << "请投票...(0弃权)" << endl;
                cin >> x;
                while ((player[x].life == 0 || TOU[x].toupiaoquan == 1) && x != 0)
                {
                    cin >> x;
                }
                if (x == 0)
                    cout << MY << "号玩家弃权" << endl;
                else
                    cout << MY << "号玩家投给了" << x << "号玩家" << endl;
            }
            else
            {
                srand(time(0));
                if (player[i].name == "狼人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else if (player[i].name == "预言家 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
            }
            if (x != 0 && TOU[i].toupiaoquan == 1)
                TOU[x].xxx++;
        }
    }
    Sleep(3000);
    sort(TOU + 1, TOU + n + 1, cmp);
    if (TOU[2].xxx != TOU[1].xxx)
    {
        cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
        player[TOU[1].num].life = 0;
        player[TOU[1].num].how = 2;
        Sleep(3000);
        return;
    }
    else
    {
        TOU[1].toupiaoquan = 0;
        TOU[2].toupiaoquan = 0;
        system("cls");
        print(ddd, nnn);
        cout << TOU[1].num << "号," << TOU[2].num << "号";
        int i;
        for (i = 3; i <= n; i++)
        {
            if (TOU[i].xxx == TOU[1].xxx)
            {
                TOU[i].toupiaoquan = 0;
                cout << "," << TOU[i].num << "号";
            }
            else
                break;
        }
        if (i == n + 1)
        {
            for (int i = 1; i <= n; i++)
                TOU[i].toupiaoquan = 1;
        }
        cout << "平票" << endl;
    }
    //--------3--------
    sort(TOU + 1, TOU + n + 1, cmp1);
    cout << "请再次投票";
    for (int i = 1; i <= 3; i++)
    {
        cout << ".";
        Sleep(500);
    }
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 1 && TOU[i].toupiaoquan == 1)
        {
            Sleep(3000);
            if (i == MY)
            {
                cout << "请投票...(0弃权)" << endl;
                cin >> x;
                while ((player[x].life == 0 || TOU[x].toupiaoquan == 1) && x != 0)
                {
                    cin >> x;
                }
                if (x == 0)
                    cout << MY << "号玩家弃权" << endl;
                else
                    cout << MY << "号玩家投给了" << x << "号玩家" << endl;
            }
            else
            {
                srand(time(0));
                if (player[i].name == "狼人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else if (player[i].name == "预言家 " || player[i].name == "猎人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
            }
            if (x != 0 && TOU[i].toupiaoquan == 1)
                TOU[x].xxx++;
        }
    }
    Sleep(3000);
    sort(TOU + 1, TOU + n + 1, cmp);
    if (TOU[2].xxx != TOU[1].xxx)
    {
        cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
        player[TOU[1].num].life = 0;
        player[TOU[1].num].how = 2;
    }
    else
    {
        cout << "投票结束,无人出局" << endl;
    }
    Sleep(5000);
}
bool game_over()
{
    int pingmin = 0;
    int langren = 0;
    int shenzhi = 0;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 0)
            continue;
        if (player[i].name == "狼人 ")
            langren++;
        else if (player[i].name == "村民 ")
            pingmin++;
        else if (player[i].name == "女巫 " || player[i].name == "预言家 " || player[i].name == "猎人 ")
            shenzhi++;
    }
    if (shenzhi == 0 || langren == 0 || pingmin == 0)
        return 1;
    return 0;
}
void night()
{
    system("cls");
    system("color 0f");
    print(1, 1);
    cout << "天~黑~请~闭~眼~~~" << endl;
    if (n >= 12)
        shoushui(1, 1);
    Sleep(3000);
    system("cls");
    print(1, 1);
    cout << "狼~人~请~睁~眼~~~" << endl;
    if (player[MY].name == "狼人 ")
    {
        Sleep(1000);
        cout << "你的同伴有:";
        for (int i = 1; i <= n; i++)
        {
            if (i == MY)
                continue;
            if (player[i].name == "狼人 ")
            {
                cout << player[i].num << "号 ";
                player[i].know = 2;
            }
        }
        Sleep(3000);
        cout << endl << "请问你们要杀谁:" << endl << "输入:";
        cin >> kill1;
        Sleep(1500);
        system("cls");
        print(1, 1);
        cout << "今晚你们要杀的是" << kill1 << "号玩家" << endl;
    }
    else
    {
        Sleep(4000);
        system("cls");
        print(1, 1);
        cout << "请问你们要杀谁?" << endl;
        do
        {
            Sleep(rand() % 18);
            srand(time(0));
            int x = rand() % n + 1;
            if (player[x].name != "狼人 " && player[x].life == 1)
            {
                kill1 = x;
                break;
            }
        } while (1);
        Sleep(5000);
    }
    Sleep(3000);
    system("cls");
    print(1, 1);
    cout << "狼~人~请~闭~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(1, 1);
    cout << "女~巫~请~睁~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(1, 1);
    if (player[MY].name == "女巫 " && player[MY].life == 1)
    {
        Sleep(1000);
        if (jieyao == 1)
        {
            cout << "今晚" << kill1 << "号玩家被杀" << endl;
            Sleep(500);
            cout << "请问你要救吗???" << endl << "A. 救 B.不救" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                system("cls");
                print(1, 1);
                cout << "请问你要毒吗???" << endl;
                Sleep(2000);
                system("cls");
                print(1, 1);
                cout << "今晚" << kill1 << "号玩家被你解救" << endl;
                jieyao = 0;
                if (shou != kill1)
                    kill1 = 0;
            }
            else
            {
                if (shou == kill1)
                    kill1 = 0;
                Sleep(2000);
                system("cls");
                print(1, 1);
                cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
                cin >> a;
                if (a == 'A')
                {
                    cout << "请问你要毒谁???" << endl << "输入:";
                    cin >> kill2;
                    while (player[kill2].life != 1)
                    {
                        cout << "输入错误,请重新输入:" << endl;
                        cin >> kill2;
                    }
                    duyao = 0;
                }
            }
        }
        else
        {
            if (shou == kill1)
                kill1 = 0;
            Sleep(2000);
            system("cls");
            print(1, 1);
            cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                cout << "请问你要毒谁???" << endl << "输入:";
                cin >> kill2;
                while (player[kill2].life != 1)
                {
                    cout << "输入错误,请重新输入:" << endl;
                    cin >> kill2;
                }
                duyao = 0;
            }
        }
    }
    else
    {
        bool b = 0;
        cout << "请问你是否要用解药???" << endl;
        int FFF = 0, kkkk;
        for (int i = 1; i <= n; i++)
        {
            if (player[i].life == 1 && player[i].name == "村民 ")
                FFF++;
            if (player[i].name == "女巫 ")
                kkkk = i;
        }
        if (jieyao == 1 && player[kkkk].life == 1)
        {
            if (FFF == 1)
            {
                if (shou == kill1)
                    jieyao = 1;
                else
                    jieyao = 0;
                kill1 = 0;
                b = 1;
            }
            else
                for (int i = 1; i <= n; i++)
                {
                    if (player[i].name == "女巫 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                    else if (player[i].name == "预言家 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                }
        }
        Sleep(3000);
        if (b == 0 && duyao == 1 && player[kkkk].life == 1)
        {
            system("cls");
            print(1, 1);
            cout << "请问你是否要用毒药???" << endl;
            srand(time(0));
            int x = rand() % 2;
            Sleep(1500);
            cout << "请问你要毒谁???" << endl;
            if (x == 1)
            {
                duyao = 0;
                int y = rand() % n + 1;
                while ((player[y].name == "女巫 " || player[y].name == "预言家 " || y == kill1) || player[y].life == 0)
                    y = rand() % n + 1;
                kill2 = y;
            }
        }
        else
        {
            Sleep(3000);
            system("cls");
            print(1, 1);
            cout << "请问你是否要用毒药???" << endl;
            Sleep(3000);
            cout << "请问你要毒谁???" << endl;
            Sleep(3000);
        }
    }
    Sleep(3000);
    system("cls");
    print(1, 1);
    cout << "女~巫~请~闭~眼~~~" << endl;
    if (n > 6)
    {
        Sleep(3000);
        system("cls");
        print(1, 1);
        cout << "预~言~家~请~睁~眼~~~" << endl;
        if (player[MY].name == "预言家 ")
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl << "输入:";
            int x;
            cin >> x;
            player[x].know = 1;
            Sleep(2000);
            system("cls");
            print(1, 1);
            cout << "他的身份是:";
            if (player[x].name == "狼人 ")
                cout << "狼人" << endl;
            else
                cout << "好人" << endl;
            Sleep(3000);
        }
        else
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl;
            Sleep(3000);
            system("cls");
            print(1, 1);
            cout << "他的身份是:......";
            Sleep(3000);
        }
        Sleep(3000);
        system("cls");
        print(1, 1);
        cout << "预~言~家~请~闭~眼~~~" << endl;
    }
    Sleep(3000);
    if (kill1 != 0)
        player[kill1].life = 0;
    if (kill2 != 0)
        player[kill2].life = 0;
    player[kill1].how = 1;
    player[kill2].how = 3;
    system("cls");
    system("color F0");
    print(2, 0);
}
void night2(int hhh, int hhhh)
{
    system("cls");
    system("color 0f");
    print(hhh, hhhh);
    cout << "天~黑~请~闭~眼~~~" << endl;
    if (n >= 12)
        shoushui(hhh, hhhh);
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "狼~人~请~睁~眼~~~" << endl;
    if (player[MY].name == "狼人 " && player[MY].life == 1)
    {
        Sleep(3000);
        cout << endl << "请问你们要杀谁:" << endl << "输入:";
        cin >> kill1;
        Sleep(1500);
        system("cls");
        print(hhh, hhhh);
        cout << "今晚你们要杀的是" << kill1 << "号玩家" << endl;
    }
    else
    {
        Sleep(4000);
        system("cls");
        print(hhh, hhhh);
        cout << "请问你们要杀谁?" << endl;
        do
        {
            srand(time(0));
            int x = rand() % n + 1;
            if (player[x].name != "狼人 " && player[x].life == 1)
            {
                kill1 = x;
                break;
            }
        } while (1);
        Sleep(5000);
    }
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "狼~人~请~闭~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(hhh, hhhh);
    cout << "女~巫~请~睁~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(hhh, hhhh);
    if (player[MY].name == "女巫 " && player[MY].life == 1)
    {
        Sleep(1000);
        if (jieyao == 1)
        {
            cout << "今晚" << kill1 << "号玩家被杀" << endl;
            Sleep(500);
            cout << "请问你要救吗???" << endl << "A. 救 B.不救" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                system("cls");
                print(hhh, hhhh);
                cout << "请问你要毒吗???" << endl;
                Sleep(2000);
                system("cls");
                print(hhh, hhhh);
                cout << "今晚" << kill1 << "号玩家被你解救" << endl;
                jieyao = 0;
                if (shou != kill1)
                    kill1 = 0;
            }
            else
            {
                if (shou == kill1)
                    kill1 = 0;
                Sleep(2000);
                system("cls");
                print(hhh, hhhh);
                cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
                cin >> a;
                if (a == 'A')
                {
                    cout << "请问你要毒谁???" << endl << "输入:";
                    cin >> kill2;
                    while (player[kill2].life != 1)
                    {
                        cout << "输入错误,请重新输入:" << endl;
                        cin >> kill2;
                    }
                    duyao = 0;
                }
            }
        }
        else if (duyao == 1)
        {
            if (shou == kill1)
                kill1 = 0;
            Sleep(2000);
            system("cls");
            print(hhh, hhhh);
            cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                cout << "请问你要毒谁???" << endl << "输入:";
                cin >> kill2;
                while (player[kill2].life != 1)
                {
                    cout << "输入错误,请重新输入:" << endl;
                    cin >> kill2;
                }
                duyao = 0;
            }
        }
        else
        {
            Sleep(2000);
            system("cls");
            print(hhh, hhhh);
            cout << "请问你要毒吗???" << endl;
        }
    }
    else
    {
        bool b = 0;
        cout << "请问你是否要用解药???" << endl;
        int FFF = 0, kkkk;
        for (int i = 1; i <= n; i++)
        {
            if (player[i].life == 1 && player[i].name == "村民 ")
                FFF++;
            if (player[i].name == "女巫 ")
                kkkk = i;
        }
        if (jieyao == 1 && player[kkkk].life == 1)
        {
            if (FFF == 1)
            {
                if (shou == kill1)
                    jieyao = 1;
                else
                    jieyao = 0;
                kill1 = 0;
                b = 1;
            }
            else
                for (int i = 1; i <= n; i++)
                {
                    if (player[i].name == "女巫 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                    else if (player[i].name == "预言家 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                }
        }
        Sleep(3000);
        if (b == 0 && duyao == 1 && player[kkkk].life == 1)
        {
            system("cls");
            print(hhh, hhhh);
            cout << "请问你是否要用毒药???" << endl;
            srand(time(0));
            int x = rand() % 2;
            Sleep(1500);
            cout << "请问你要毒谁???" << endl;
            if (x == 1)
            {
                duyao = 0;
                int y = rand() % n + 1;
                while ((player[y].name == "女巫 " || player[y].name == "预言家 " || y == kill1) || player[y].life == 0)
                    y = rand() % n + 1;
                kill2 = y;
            }
        }
        else
        {
            Sleep(3000);
            system("cls");
            print(hhh, hhhh);
            cout << "请问你是否要用毒药???" << endl;
            Sleep(3000);
            cout << "请问你要毒谁???" << endl;
            Sleep(3000);
        }
    }
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "女~巫~请~闭~眼~~~" << endl;
    if (n > 6)
    {
        Sleep(3000);
        system("cls");
        print(hhh, hhhh);
        cout << "预~言~家~请~睁~眼~~~" << endl;
        if (player[MY].name == "预言家 " && player[MY].life == 1)
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl << "输入:";
            int x;
            cin >> x;
            player[x].know = 1;
            Sleep(2000);
            system("cls");
            print(hhh, hhhh);
            cout << "他的身份是:";
            if (player[x].name == "狼人 ")
                cout << "狼人" << endl;
            else
                cout << "好人" << endl;
            Sleep(3000);
        }
        else
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl;
            Sleep(3000);
            system("cls");
            print(hhh, hhhh);
            cout << "他的身份是:......";
            Sleep(3000);
        }
        Sleep(3000);
        system("cls");
        print(hhh, hhhh);
        cout << "预~言~家~请~闭~眼~~~" << endl;
    }
    Sleep(3000);
    if (kill1 != 0)
        player[kill1].life = 0;
    if (kill2 != 0)
        player[kill2].life = 0;
    player[kill1].how = 1;
    player[kill2].how = 3;
    system("cls");
    system("color F0");
    print(hhh + 1, 0);
}
bool lr = 0;
void panduanlieren()
{
    if (lr == 1)
        return;
    if (MY == lieren)
    {
        cout << "请射杀一名玩家" << endl;
        int x;
        cin >> x;
        while (player[x].life != 1)
        {
            cout << "输入错误,请重新输入" << endl;
            cin >> x;
        }
        Sleep(1000);
        cout << lieren << "号猎人发动技能,开枪带走了" << x << "号" << endl;
        player[x].life = 0;
        player[x].how = 4;
    }
    else if (n >= 9)
    {
        srand(time(0));
        int x = rand() % n + 1;
        while (player[x].life != 1)
        {
            x = rand() % n + 1;
        }
        Sleep(1000);
        cout << lieren << "号猎人发动技能,开枪带走了" << x << "号" << endl;
        player[x].life = 0;
        player[x].how = 4;
    }
    lr = 1;
}
void print1()
{
    cout << "天亮了,昨晚";
    if (kill1 != 0 || kill2 != 0)
    {
        cout << kill1 << "号";
        if (kill2 != 0)
        {
            cout << "," << kill2 << "号";
            kill2 = 0;
        }
        cout << "被杀" << endl;
    }
    else
        cout << "是平安夜" << endl;
}
int main()
{
    system("cls");
    cout << " " << "狼人杀online" << endl;
    cout << "请输入人数个数:" << endl;
    scanf("%d", &n);
    cout << "加载时间长,请耐心等待";
    init1();
    init2(n);
    int k = 1;
    do
    {
        srand(time(0));
        init3(k);
        cout << ".";
        Sleep(17);
        k++;
    } while (k <= n);
    system("cls");
    system("color F0");
    cout << "游戏即将开始";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    Sleep(1500);
    cout << endl << endl << "请大家查看身份牌......" << endl;
    Sleep(45);
    srand(time(0));
    MY = rand() % n + 1;
    cout << "您的身份是:" << player[MY].name << endl;
    Sleep(500);
    cout << "在" << player[MY].num << "号位上" << endl;
    system("pause");
    system("cls");
    player[MY].know = 2;
    print(1, 0);
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night();
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(2, 0);
    system("cls");
    print(2, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(2, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(3, 0);
    system("cls");
    print(3, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(3, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(4, 0);
    system("cls");
    print(4, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(4, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(5, 0);
    system("cls");
    print(5, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(5, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(6, 0);
    system("cls");
    print(6, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(6, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(7, 0);
    system("cls");
    print(7, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    while (1)
        system("pause");
    return 0;
}

大家可以打开编辑器,自己去试一试(不会用的话可以看看这个博客如何使用DEV-C++(超详细)_devc++_我爱OJ的博客-CSDN博客),真的十分有趣

好了,我们回归正题

四.程序设计

【1.1】输出打印Hello,world

程序如下:

#include <iostream>
using namespace std;
int main()
{
	cout<<"Hello, world!";
	return 0;
}

在编写完这个程序以后,大家估计会有很多疑问:为什么程序要这样编写呢?代码之间要有什么规则么?

要回答以上问题,我们必须弄清C++程序的程序结构

 1.头文件

看程序的第一行:

#include <iostream>

C/C++ 编译采用分离编译模式。在一个项目中,有多个源文件存在,但是它们总会有一些相同的内容,比如用户自定义类型、全局变量、全局函数的声明等。将这些内容抽取出来放到头文件中,提供给各个源文件包含,就可以避免相同内容的重复书写,提高编程效率和代码安全性。所以,设立头文件的主要目的是:提供全局变量、全局函数的声明或公用数据类型的定义,从而实现分离编译和代码复用。

概括地说,头文件有如下三个作用

(1)加强类型检查,提高类型安全性。
使用头文件,可有效地保证自定义类型的一致性。虽然,在语法上,同一个数据类型(如一个 class)在不同的源文件中书写多次是允许的,程序员认为他们是同一个自定义类型,但是,由于数据类型不具有外部连接特性,编译器并不关心该类型的多个版本之间是否一致,这样有可能会导致逻辑错误的发生。考察如下程序。

(2)减少公用代码的重复书写,提高编程效率。
程序开发过程中,对某些数据类型或者接口进行修改是在所难免的,使用头文件,只需要修改头文件中的内容,就可以保证修改在所有源文件中生效,从而避免了繁琐易错的重复修改。

(3)提供保密和代码重用的手段。
头文件也是 C++ 代码重用机制中不可缺少的一种手段,在很多场合,源代码不便(或不准)向用户公布,只要向用户提供头文件和二进制库即可。用户只需要按照头文件的接口声明来调用库函数,而不必关心接口的具体实现,编译器会从库中连接相应的实现代码。

2.名字空间

程序的第二行:

using namespace std;

英语翻译为:使用标准的名字空间

假设这样一种情况,当一个班上有两个名叫 Zara 的学生时,为了明确区分它们,我们在使用名字之外,不得不使用一些额外的信息,比如他们的家庭住址,或者他们父母的名字等等。

同样的情况也出现在 C++ 应用程序中。例如,您可能会写一个名为 xyz() 的函数,在另一个可用的库中也存在一个相同的函数 xyz()。这样,编译器就无法判断您所使用的是哪一个 xyz() 函数。

因此,引入了命名空间这个概念,专门用于解决上面的问题,它可作为附加信息来区分不同库中相同名称的函数、类、变量等。使用了命名空间即定义了上下文。本质上,命名空间就是定义了一个范围。

我们举一个计算机系统中的例子,一个文件夹(目录)中可以包含多个文件夹,每个文件夹中不能有相同的文件名,但不同文件夹中的文件可以重名。

 3.主函数

看第三行:

int main(){

}

主函数的定义

  • 一般而言,编写一个能运行在操作系统上的程序,都需要一个主函数
  • 主函数意味着建立一个独立进程,既是程序的入口,又是程序的出口;
  • 主函数的返回值类型必须是int,这样返回值才能传递给程序的激活者(如操作系统);

主函数的返回值用于说明程序的退出状态,如果返回0,则代表程序正常退出,如果返回非0,则代表程序异常退出
注:最好不要把太多的细节方面的逻辑直接放在主函数内,这样不利于维护和扩展。主函数应该尽量简洁,具体的实现细节应该封装到被调用的子函数里面去。

下次预告:整数算术运算

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值