c++|c++制作的“音乐计时器”,结束后播放《天空之城》
tips:这个程序我还做了一个下载的
【c++|c++制作的“音乐计时器”,结束后播放《天空之城》】这个计时器好像是全站为数不多的结束后有声音提醒的计时器,类似于秒表
我做的这个计时器,主要用到的函数是Sleep(),Sleep函数的头文件是windows.h
先介绍一下这个函数的用处:
Sleep函数可以使计算机程序(进程,任务或线程)进入休眠,简单来说就是让程序停止工作一个特定时间,单位是毫秒,比如:
#include
#include
#include
using namespace std;
int main(){
cout << "程序开始" << endl;
clock_t start = clock();
clock_t end;
Sleep(2000);
end = clock();
cout << "Sleep(2000)这个函数执行了" << (end - start) / 1000 << "秒";
return 0;
}
文章图片
上图是运行结果
然后要让电脑演奏音乐,要用Beep()函数,音乐就是《天空之城》
Beep函数的头文件依旧是windows.h
Beep()函数有两个参数一个是音高,另一个时长,第二个单位同样是毫秒,第一个我就不知道了,不过要一次一次这样反复的写还是相当麻烦的,所以可以把每个音都改成宏变量,就能方便不少,低音do是262,低音re是294,低音fa是330,低音fa是349,低音so是392,低音la是440,低音si是494,do是523,re是578,mi是659,fa是698,so是784,la是880,si是988,在高我就不写了,其他音都列在下面的代码里了
#define qdo 262
#define qre 294
#define qmi 330
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define do 523
#define re 578
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define do1 1046
#define re1 1175
#define mi1 1318
#define fa1 1480
#define so1 1568
#define la1 1760
#define si1 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdo1 1046
#define sre1 1245
#define sfa1 1480
#define sso1 1661
#define sla1 1865
接下来,要注意一下音乐中间的休止符和每个音符的长度,我就都设置8分(好像是)音符是400毫秒,停顿是128毫秒
下面是天空之城的演奏音乐
#include
#include
using namespace std;
#define qdo 262
#define qre 294
#define qmi 330
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define do 523
#define re 578
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define do1 1046
#define re1 1175
#define mi1 1318
#define fa1 1480
#define so1 1568
#define la1 1760
#define si1 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdo1 1046
#define sre1 1245
#define sfa1 1480
#define sso1 1661
#define sla1 1865
int days;
int hours;
int mintues;
int seconds
int main(){
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);
//只有1遍
return 0;
}
然后,就是计时器的制作了:
(1)做到每秒输出一次,不会受循环控制
要做到这一点,就要用到之前说的Sleep()函数,每过1秒,就循环1次,循环的计算时间基本可以忽略不计,每次循环Seconds--就行了
这一步的代码段如下
void CountDown_Seconds(int &Days,int &Hours,int &Mintues,int &Seconds){
for(;
Seconds >= 0;
Seconds--){
cout << "倒计" << Days << "" << Hours << "" << Mintues << "" << Seconds << "秒" << endl;
Sleep(1000);
system("cls");
}
}
(2)分钟计时
每分钟这个就直接每次循环调用CountDown_Seconds函数就可以了,每次60秒后把Seconds
改成59就可以了
void CountDown_Mintues(int &Days,int &Hours,int &Mintues,int &Seconds){
for (;
Mintues >= 0;
Mintues--){
CountDown_Seconds(Days,Hours,Mintues,Seconds);
Seconds = 59;
}
}
同样,小时和天也一样,不停调用分钟和小时就可以了,这两步就放在一起了:
void CountDown_Hours(int &Days,int &Hours,int &Mintues,int &Seconds){
for (;
Hours >= 0;
Hours--){
CountDown_Mintues(Days,Hours,Mintues,Seconds);
Mintues = 59;
}
}
void CountDown(int Days,int Hours,int Mintues,int Seconds){
for(;
Days >= 0;
Days--){
CountDown_Hours(Days,Hours,Mintues,Seconds);
Hours = 23;
}
}
剩下的就是输入部分了
void printMenu(){
cout << "欢迎进入间倒计系统"<> days >> hours >> mintues >> seconds;
cout << endl;
}
最后附上完整代码:
#include
#include
using namespace std;
#define qdo 262
#define qre 294
#define qmi 330
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define do 523
#define re 578
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define do1 1046
#define re1 1175
#define mi1 1318
#define fa1 1480
#define so1 1568
#define la1 1760
#define si1 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdo1 1046
#define sre1 1245
#define sfa1 1480
#define sso1 1661
#define sla1 1865
int days;
int hours;
int mintues;
int seconds;
void printMenu(){
cout << "欢迎进入间倒计系统"<> days >> hours >> mintues >> seconds;
cout << endl;
}
void CountDown_Seconds(int &Days,int &Hours,int &Mintues,int &Seconds){
for(;
Seconds >= 0;
Seconds--){
cout << "倒计" << Days << "" << Hours << "" << Mintues << "" << Seconds << "秒" << endl;
Sleep(1000);
//Sleep看清楚间
system("cls");
}}
void CountDown_Mintues(int &Days,int &Hours,int &Mintues,int &Seconds){
for (;
Mintues >= 0;
Mintues--){
CountDown_Seconds(Days,Hours,Mintues,Seconds);
Seconds = 59;
}
}
void CountDown_Hours(int &Days,int &Hours,int &Mintues,int &Seconds){
for (;
Hours >= 0;
Hours--){
CountDown_Mintues(Days,Hours,Mintues,Seconds);
Mintues = 59;
}
}
void CountDown(int Days,int Hours,int Mintues,int Seconds){
for(;
Days >= 0;
Days--){
CountDown_Hours(Days,Hours,Mintues,Seconds);
Hours = 23;
}
}
int main(){
int i = 1;
int set;
printMenu();
while (i <= 3){
Input();
cout << "确认???:" << days << "天" << hours << "时" << mintues << "分" << seconds << "秒" << endl;
cout << "确认请按1,否则请按0:";
cin >> set;
if (set){
break;
}
i++;
}
CountDown(days,hours,mintues,seconds);
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);
main();
return 0;
}
大家给个赞再走呗
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量