前言
最近挖库存挖到了一个刚刚学习编程的时候用C语言写的生日祝福代码,其中用C语言代码用Beep函数控制蜂鸣器根据音调高低的不同演奏了一曲《天空之城》,满满都是怀念的感觉啊!
或许这没有什么技术难度,但是却是为了学习编程培养了浓厚的兴趣,记得当时我还用C语言实现了炫屏功能、琢磨出如何用C语言打印出十八种爱心和演奏了生日快乐歌等等,hahaha~还是挺好玩的。
谱子:
琴谱:
代码谱:
#include<iostream> #include <windows.h> #include<string> #include<math.h> #include<stdlib.h> #include <cstdio> void Tiansing(){ const int qdo=262; const int qre=294; const int qmi=330; //q前缀为低音,1后缀为高音,s前缀为半音阶 const int qfa=349; const int qso=392; const int qla=440; const int qsi=494; const int d=523; const int re=578; const int mi=659; const int fa=698; const int so=784; const int la=880; const int si=988; const int do1=1046; const int re1=1175; const int mi1=1318; const int fa1=1480; const int so1=1568; const int la1=1760; const int si1=1976; const int sqdo=277; const int sqre=311; const int sqfa=370; const int sqso=415; const int sqla=466; const int sdo=554; const int sre=622; const int sfa=740; const int sso=831; const int sla=932; const int sdol=1046; const int sre1=1245; const int sfa1=1480; const int sso1=1661; const int sla1=1865; int pai=400,ban=200; int ting=128; Sleep(1000); Beep(la,ban); Beep(si,ban); Sleep(ting); Beep(do1,pai+ban); Beep(si,ban); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(si,3*pai); Sleep(ting); Beep(mi,ban); Beep(mi,ban); Beep(la,ban+pai); Beep(so,ban); Sleep(ting); Beep(la,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(so,2*pai); Sleep(ting); Sleep(pai); Beep(mi,ban); Sleep(ting/2); Beep(mi,ban); Sleep(ting/2); Beep(fa,pai+ban); Beep(mi,ban); Sleep(ting); Beep(fa,ban); Beep(do1,ban+pai); Sleep(ting); Beep(mi,2*pai); Sleep(ting); Sleep(ban); Beep(do1,ban); Sleep(ting/2); Beep(do1,ban); Sleep(ting/2); Beep(do1,ban); Sleep(ting/2); Beep(si,ban+pai); Beep(sfa,ban); Sleep(ting); Beep(sfa,pai); Beep(si,pai); Sleep(ting); Beep(si,2*pai); Sleep(ting); Sleep(pai); Beep(la,ban); Beep(si,ban); Sleep(ting); Beep(do1,pai+ban); Beep(si,ban); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(si,2*pai); Sleep(ting); Sleep(pai); Beep(mi,ban); Sleep(20); Beep(mi,ban); Sleep(ting); Beep(la,pai+ban); Beep(so,ban); Sleep(ting); Beep(la,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(so,3*pai); Sleep(ting+ban); Beep(mi,ban); Sleep(ting/2); Beep(fa,pai); Sleep(ting); Beep(do1,ban); Beep(si,ban); Sleep(20); Beep(si,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(re1,ban); Sleep(20); Beep(re1,ban); Sleep(20); Beep(mi1,ban); Sleep(ting/2); Beep(do1,pai); Sleep(ting+pai); Beep(do1,pai); Beep(si,ban); Sleep(ting); Beep(la,ban); Sleep(20); Beep(la,ban); Sleep(ting); Beep(si,pai); Sleep(ting); Beep(sso,pai); Sleep(ting); Beep(sso,2*pai); Sleep(ting+pai); Beep(do1,ban); Beep(re1,ban); Sleep(ting); Beep(mi1,pai+ban); Beep(re1,ban); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(fa1,pai); Sleep(ting); Beep(re1,2*pai); Sleep(pai+ting); Beep(so,ban); Sleep(20); Beep(so,ban); Sleep(ting); Beep(do1,ban); Beep(si,ban); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(mi1,2*pai); Sleep(ting+2*pai); Beep(la,ban); Beep(si,ban); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(si,pai); Sleep(ting); Beep(re1,ban); Sleep(20); Beep(re1,ban); Sleep(ting); Beep(do1,pai+ban); Beep(so,ban); Sleep(20); Beep(so,pai); Sleep(pai+ting); Beep(fa1,pai); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(re1,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(mi1,4*pai); Beep(mi1,pai*2); Sleep(pai+ting); Beep(mi1,pai); Sleep(ting); Beep(la1,2*pai); Sleep(ting); Beep(so1,pai); Sleep(ting); Beep(so1,pai); Sleep(ting); Beep(mi1,ban); Sleep(ting/2); Beep(re1,ban); Sleep(ting); Beep(do1,pai); Sleep(ting+ban); Beep(do1,ban); Sleep(ting); Beep(re1,pai); Sleep(ting); Beep(do1,ban); Beep(re1,ban); Sleep(20); Beep(re1,ban); Sleep(ting); Beep(so1,pai); Sleep(ting); Beep(mi1,2*pai); Sleep(ting+pai); Beep(mi,pai); Sleep(ting); Beep(la1,2*pai); Sleep(ting); Beep(so1,2*pai); Sleep(ting); Beep(mi1,ban); Beep(re1,ban); Sleep(ting); Beep(do1,2*pai); Sleep(ting+ban); Beep(do1,ban); Sleep(ting); Beep(re1,pai); Sleep(ting); Beep(do1,ban); Beep(re1,ban); Sleep(20); Beep(re1,ban); Sleep(ting); Beep(si,pai); Sleep(ting); Beep(la,2*pai); Sleep(ting); Beep(la,ban); Beep(si,ban); Beep(do1,pai+ban); Beep(si,ban); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(si,3*pai); Sleep(ting); Beep(mi,ban); Beep(mi,ban); Beep(la,ban+pai); Beep(so,ban); Sleep(ting); Beep(la,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(so,2*pai); Sleep(ting); Sleep(pai); Beep(mi,ban); Sleep(ting/2); Beep(mi,ban); Sleep(ting/2); Beep(fa,pai+ban); Beep(mi,ban); Sleep(ting); Beep(fa,ban); Beep(do1,ban+pai); Sleep(ting); Beep(mi,2*pai); Sleep(ting); Sleep(ban); Beep(do1,ban); Sleep(ting/2); Beep(do1,ban); Sleep(ting/2); Beep(do1,ban); Sleep(ting/2); Beep(si,ban+pai); Beep(sfa,ban); Sleep(ting); Beep(sfa,pai); Beep(si,pai); Sleep(ting); Beep(si,2*pai); Sleep(ting); Sleep(pai); Beep(la,ban); Beep(si,ban); Sleep(ting); Beep(do1,pai+ban); Beep(si,ban); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(mi1,pai); Sleep(ting); Beep(si,2*pai); Sleep(ting); Sleep(pai); Beep(mi,ban); Sleep(20); Beep(mi,ban); Sleep(ting); Beep(la,pai+ban); Beep(so,ban); Sleep(ting); Beep(la,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(so,3*pai); Sleep(ting+ban); Beep(mi,ban); Sleep(ting/2); Beep(fa,pai); Sleep(ting); Beep(do1,ban); Beep(si,ban); Sleep(20); Beep(si,pai); Sleep(ting); Beep(do1,pai); Sleep(ting); Beep(re1,ban); Sleep(20); Beep(re1,ban); Sleep(20); Beep(mi1,ban); Sleep(ting/2); Beep(do1,pai); Sleep(ting+pai); Beep(la,4*pai); Sleep(1000); } int mian(){ Tiansing(); return 0; } 番外: #include <stdio.h> int main() { int n; int b=20; for(n=0;n<21;n++) { printf("\a\a"); printf("%d",b-n); } getchar(); return 0; }
Question:
书上说\a这个字符可以使电脑发出声音,试了下果然,大约是是1秒钟滴2下,然后我就编了个类似于倒计时的小程序,但发现这根本行不通,数字刷一下出来完了,声音还是一下一下响(估计这个响的声音与输出bel字符是不相符的),而且程序老崩溃。请问这是怎么回事啊?怎么才能做到,响两声后,在输出b的值呢?
Reply:
你是想声音和数字都慢慢的出来吗?你的程序太短小,在一瞬间就执行完了,其实声音可能响了20下,但是是在一瞬间同时发出来的,你分辨不出来而已。可以包含windows头文件,调用sleep函数延迟发声和打印,就可以出现有节奏的效果。一次发声只需要打印一个'\a'就可以了。
#include <stdio.h> #include<windows.h> // 包含windows头文件 int main() { int n; int b=20; for(n=0;n<21;n++) {