哈工大C语言课程设计

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

H a r b i n I n s t i t u t e o f T e c h n o l o g y

课程设计说明书(论文)

课程名称: C语言课程设计

设计题目:音乐程序与波特图

院系:航天学院控制科学与工程系

班级:

设计者:

学号:

指导教师:

设计时间:

哈尔滨工业大学教务处

哈尔滨工业大学课程设计任务书

题目一

1.1题目详细描述:

播放音乐程序,实现了自选音乐曲目和直接使用键盘弹奏,而且可以在曲目播放结束后循环选择乐曲。

1.2程序设计思路及流程图:

1.3

#include

#include

#include

#include

#include

#include

#define N1 16

#define N2 8

#define N4 4

#define N8 2

#define N16 1

#define END 0

void playmusic(int n,int *c);

void typemusic();

enum NOTES{

C10=131,D10=147,E10=165,F10=175,G10=196,A10=220,B10=247,

C0=262,D0=296,E0=330,F0=349,G0=392,A0=440,B0=494,

C1=523,D1=587,E1=659,F1=698,G1=784,A1=880,B1=988,

C2=1047,D2=1175,E2=1319,F2=1397,G2=1568,A2=1760,B2=1976,S=10

};

typedef enum NOTES SONG;

SONG song1[]={C0,N4+N2,E0,N4,G0,N2,G0,N2,A0,N1,G0,N1,E0,N4+N2,

C0,N4,G0,N1/3,G0,N1/3,G0,N1/3,E0,N1,C0,N1,G10,N1/3,G10,N1/3,

G10,N1/3,G10,N1/3,G10,N1/3,G10,N1/3,C0,N1,END,END};

SONG song2[]={A0,N2,B0,N2,C1,N1+N2,B0,N2,C1,N1,E1,N1,B0,N1+N1,S,N1,E0,N1, A0,N1+N2,G0,N2,A0,N1,C1,N1,G0,N1+N1,S,N1,E0,N2,E0,N2,F0,

N1+N2,E0,N2,F0,N1,C0,N1,E0,N1+N1,S,N1,C1,N2,C1,N2,B0,N1+N2,370, N2,F0,N1,B0,N1,B0,N1+N2,S,N1,A0,N2,B0,N2,C1,N1+N2,B0,N2,C1,N1,

E1,N1,B0,N1+N2,END,END};

int main()

{

int m,n,c=1;

char b='y';

while(b=='y')

{

printf("Hello,what do you want to do by this program?\n");

printf("1.Listen to music.-------Press 1\n");

printf("2.Play music by yourself.-------Press 2\n");

scanf("%d",&m);

while(m!=1&&m!=2&&m!=3) /*选择方式*/

{

printf("You typed wrong!Do not push me around.:(\n");

scanf("%d",&m);

}

if(m==1)

{

printf("I have two musics,choose one!(Press 1/2)\n");

scanf("%d",&n);

while(n!=1&&n!=2)

{

printf("You typed wrong!Do not push me around.:(\n");

scanf("%d",&n);

}

playmusic(n,&c);

}

else if(m==2)

{

typemusic();

}

getchar();

printf("Do you want to continue?(y/n)\n");

scanf("%c",&b);

}

return 0;

}

void playmusic(int n,int *c)

{

int note=0,fre,dur,control;

char d;

clock_t goal;

if(n==1)

{

while(song1[note]!=END)

{

fre=*c*song1[note];

dur=song1[note+1];

if(kbhit()) break;

if(fre)

{ /*若频率值fre非0*/

outportb(0x43,0xb6); /*初始化2号定时器 */

fre=(unsigned)(1193180L/fre); /*计算计数初值 */

outportb(0x42,(char)fre); /*先写计数初值的低8位*/

outportb(0x42,(char)(fre>>8)); /*再写计数初值的高8位*/

control=inportb(0x61); /*读0x61的状态*/

outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ }

goal=(clock_t)dur+clock( );

while(goal>clock( ));

if(dur)

outportb(0x61,control);

note+=2;

}

}else

{

while(song2[note]!=END)

{

fre=*c*song2[note];

dur=song2[note+1];

if(kbhit()) break;

if(fre)

{ /*若频率值fre非0*/

outportb(0x43,0xb6); /*初始化2号定时器 */

fre=(unsigned)(1193180L/fre); /*计算计数初值 */

outportb(0x42,(char)fre); /*先写计数初值的低8位*/

outportb(0x42,(char)(fre>>8)); /*再写计数初值的高8位*/

control=inportb(0x61); /*读0x61的状态*/

outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ }

相关文档
最新文档