🎾 Pixiv ID:72203573

终于开始写程序了,入门程序体验


向世界打个招呼,体验程序。

  • 话不多说我们直接开始第一个程序:
    下面的程序直接复制粘贴到编译器里就可以运行了
#include<iostream>
//头文件,函数库,预处理,都是它
//同样常用的还有 cstdio是C的标准库
#include<cstdlib>
//调用 system("pause");  的头文件
using namespace std;
//命名空间
int main()
//主函数
{
    cout << "Hello,World!"<<endl;
//输出内容
    system("pause");  
//程序停留,在编译器环境下运行不用加
    return 0;
//程序结束,返回一个0,表示没有异常
}
  • 然后我们就来一条一条来解读这个程序:
#include <iostream>
#include<cstdlib>

这是头文件,是对函数库的调用和申明,一般结构为:#include<>
尖括号内为库的名称,iostream就是C++的一个库,同样的常用的还有 cstdio是C的标准库;cstdlib是代码中system("pause"); 的头文件

  • 术语解释:

    其实对于头文件我们可以认为他们是一个个的仓库,里面有许许多多的工具供我们使用,但是我们很懒又不知道仓库在哪里,导致不会自己去拿工具,于是我们就告诉电脑这个仓库的名字和我们需要用到的工具让电脑去给我们拿。在C++程序就是这样一个个工具构建起来的,我们只需要在最开始告诉电脑这个程序里需要用得那些仓库,后面直接用工具就行了。iostreamcstdio是我们最为常用的仓库
    建立一一对应的关系:

比喻 术语
仓库 头文件
工具 函数/语句
using namespace std;

本行代码是对函数所在空间的一个命名,C++才有,纯C语言不用。

  • 术语解释:
    命名空间就是你暂时存放工具并且分类的一个工具盒子,其实并不用了解过多(单谈竞赛的话用不上)记住只要是C++程序把它加上就好。
比喻 术语
工具盒子 命名空间
int main(){}

主函数,每一个程序都有一个主函数,主函数永远第一个执行。(这个现在记住就好,现阶段不重要,只要记住每次加上就行了)
那么这里的重头戏来了!
就是下面这行代码:

cout<<""<<endl;

cout为C++中标准的输出函数。就是我用来把在电脑内部的运算结果或者一些文字输出出来的工具而已

system("pause");

暂停程序,避免在编译后生成的EXE文档执行后不能看到结果。(其实单纯只是为了能看见。)

return 0;

程序结束,返回一个0,表示没有异常。

以上的比喻一一对应表:

比喻 术语
仓库 头文件
工具 函数/语句
工具盒子 命名空间
  • 运行结果:
演示
QQq

恭喜你完成了第一个程序了!!!!

  • 下面我们可以对我们刚刚完成的一个程序进行修改(为所欲为
    魔改

方块内的内容可以任意改变(必须加" "),我们也可以多加几句cout


  • 关于第一个程序的题目:
  1. P1000 超级玛丽游戏
    这道题在洛谷的题解里面基本上是大神秀技术,可以参考,也有基础的写法
#include<iostream>
using namespace std;
int main()
{
cout<<"                ********"<<endl;
cout<<"               ************"<<endl;
cout<<"               ####....#."<<endl;
cout<<"             #..###.....##...."<<endl;
cout<<"             ###.......######              ###            ###"<<endl;
cout<<"                ...........               #...#          #...#"<<endl;
cout<<"               ##*#######                 #.#.#          #.#.#"<<endl;
cout<<"            ####*******######             #.#.#          #.#.#"<<endl;
cout<<"           ...#***.****.*###....          #...#          #...#"<<endl;
cout<<"           ....**********##.....           ###            ###"<<endl;
cout<<"           ....****    *****...."<<endl;
cout<<"             ####        ####"<<endl;
cout<<"           ######        ######"<<endl;
cout<<"##############################################################"<<endl;
cout<<"#...#......#.##...#......#.##...#......#.##------------------#"<<endl;
cout<<"###########################################------------------#"<<endl;
cout<<"#..#....#....##..#....#....##..#....#....#####################"<<endl;
cout<<"##########################################    #----------#"<<endl;
cout<<"#.....#......##.....#......##.....#......#    #----------#"<<endl;
cout<<"##########################################    #----------#"<<endl;
cout<<"#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#"<<endl;
cout<<"##########################################    ############"<<endl;
return 0;
}

2.1001:Hello,World!
至于为什么不做1000:,你们看看就知道了,我们下回再来分解

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

  • 一个好玩的程序:抢红包!!!!!

可以拿来玩玩,但是入门的同志们就暂时不用浪费时间读懂了,后面我们慢慢来。 大佬请随意,数据还有些问题`

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <math.h>
using namespace std;

int i, number;
int best;    //手气最佳 
float total;// 总金额 
float *a=new float[number];  //保存每个人的随机数。最多支持1024个人抢红包。
float *b=new float[number];  //保存每个人获得的红包金额。
float suma = 0; //随机数总和。
float sumb = 0; //红包总和。


int main()
{


    cout << "请输入红包金额:";
    cin >> total;
    if(total>=2147483647)
	    cout<<"金额过大"<<endl; 
    cout << "请输入红包个数:";
    cin >> number;
    if(number>=2147483647)
	    cout<<"人数过多"<<endl;
    /* 生成随机数 */
    // 设置种子
    srand((unsigned)time(NULL));
    int max = 0;
    for (i = 0; i < number; i++)
    {
        // 生成实际的随机数
        a[i] = rand() % 100;
                
        if (a[i] > max){
            max = a[i];
            best = i;//获取手气最佳
        }
        suma += a[i];
    }

    for (i = 0; i < number - 1; i++)
    {
        b[i] = a[i] / suma * total;//按照随机数计算每个人实际获得的金额
        sumb += round(b[i] * 100) / 100.0;//将红包金额保留两位小数
        //输出信息
        cout << "第" << setiosflags(ios::right)<< setw(3) << i + 1 << 
            "个人的红包是:" << setiosflags(ios::right) << setw(6) << 
            setiosflags(ios::fixed) << setprecision(2) << 
            round(b[i] * 100) / 100.0 ;
        if (best == i){
            cout << "(手气最佳)" << endl;
        }
        else {
            
            cout << endl;
        }
    }
    //最后一人的红包金额等于总金额减去前面的金额。
    cout << "第" << setiosflags(ios::right)<<
        setw(3) << number << "个人的红包是:" <<
        setiosflags(ios::right) << setw(6) << setiosflags(ios::fixed) <<
        setprecision(2) << round((total - sumb) * 100) / 100.0;
    if (best == number - 1){
        cout << "(手气最佳)" << endl;
    }
    else {

        cout << endl;
    }
    system("pause"); 
    return 0;
}
  • 效果图:

  • 运行图:输入
    运行图:输出

有问题直接给我发邮件xiemaths@gmail.com或者xyh798061533@163.com
QQ:2385708107
ZWT大佬QQ:1259249107
LYT大佬QQ:1622956497
(周末统一回复,原谅高二狗的苦)