你所不知道的Hello World[C++实现]

要说OIer界内最简单的程序,那恐怕非Hello World莫属了, 那么这篇文章就介绍如何写Hello World(被打)。

  • 最简单的一种实现:

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


  • 当用户按下按键的时候显示Hello World,松开就消失。

#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;

int main(){
    while(1){
        if(_kbhit()){
            cout << "Hello World";
            system("cls");
            _getch();
        }
    }
    return 0;
}


  • 设置文字颜色及大小(需要安装图形库)

#include <graphics.h>
#include <conio.h>

int main(){
    initgraph(600,480);
    settextcolor(RGB(2,134,219));
    settextstyle(60, 0, _T("微软雅黑"));
    outtextxy(180,200,_T("Hello World"));
    _getch();
    closegraph();
    return 0;
}


运行截图:


  • 让电脑说:Hello World
#include <windows.h>
#include <mmsystem.h>
#include <conio.h>

int main(){
    mciSendString("open say.mp3 alias music",NULL,0,NULL);
    mciSendString("play music repeat");
    _getch();
    return 0;
}



运行上面的代码需要用录音器录制一段Hello World的音频,然后把它放在程序的目录内,命名为say.mp3(注意不要有二级后缀)

猜你喜欢

转载自www.cnblogs.com/Return-blog/p/12323000.html