《哈工大C语言课程设计共15页word资料.doc》由会员分享,可在线阅读,更多相关《哈工大C语言课程设计共15页word资料.doc(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流哈工大C语言课程设计【精品文档】第 16 页Harbin Institute of Technology课程设计说明书(论文)课程名称: C语言课程设计 设计题目: 音乐程序与波特图 院 系:航天学院控制科学与工程系班 级: 设 计 者: 学 号: 指导教师: 设计时间: 哈尔滨工业大学教务处哈尔滨工业大学课程设计任务书 姓 名: 院 (系): 专 业: 班 号: 任务起至日期: 年月日至 年 月 日 课程设计题目: 已知技术参数和设计要求: 工作量: 工作计划安排: 同组设计者及分工: 指导教师签字_ 年 月 日 教研室主任意见: 教研室主任签字_ 年
2、 月 日*注:此任务书由课程设计指导教师填写。题目一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 0void play
3、music(int n,int *c);void typemusic();enum NOTESC10=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=10typedef enum NOTES SONG;SONG song1=C0,N
4、4+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,N
5、1,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 1n); printf(2.Play music by yours
6、elf.-Press 2n); 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); el
7、se 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(song1note!=END) fre=*c*song1note; dur=song1note+1; if(kbhit() break; if(fre) /*若频率值fre非0*/ outportb(0x43,0xb6
8、); /*初始化2号定时器 */ fre=(unsigned)(1193180L/fre); /*计算计数初值 */ outportb(0x42,(char)fre); /*先写计数初值的低8位*/ outportb(0x42,(char)(fre8); /*再写计数初值的高8位*/ control=inportb(0x61); /*读0x61的状态*/ outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ goal=(clock_t)dur+clock( ); while(goalclock( ); if(dur) outportb(0x61,c
9、ontrol); note+=2; else while(song2note!=END) fre=*c*song2note; dur=song2note+1; if(kbhit() break; if(fre) /*若频率值fre非0*/ outportb(0x43,0xb6); /*初始化2号定时器 */ fre=(unsigned)(1193180L/fre); /*计算计数初值 */ outportb(0x42,(char)fre); /*先写计数初值的低8位*/ outportb(0x42,(char)(fre8); /*再写计数初值的高8位*/ control=inportb(0x6
10、1); /*读0x61的状态*/ outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ goal=(clock_t)dur+clock( ); while(goalclock( ); if(dur) outportb(0x61,control); note+=2; getchar(); printf(Do you want to raise/lower the frequency? Press r/lnIf you do not want press nn); scanf(%c,&d); if(d=r) *c*=2; else if(d=l) *c
11、*=0.5; else if(d=n) *c=*c;void typemusic() char ch; clock_t goal; int fre=0,control,dur=2; printf(Play your own music!n(You can play on the keyboard by pressing Z-M,A-J,Q-U,1-7)n); printf(If you are bored press /n); do ch=getch(); if(ch=/); else switch(ch) case z:fre=C10;break; case x:fre=D10;break;
12、 case c:fre=E10;break; case v:fre=F10;break; case b:fre=G10;break; case n:fre=A10;break; case m:fre=B10;break; case a:fre=C0;break; case s:fre=D0;break; case d:fre=E0;break; case f:fre=F0;break; case g:fre=G0;break; case h:fre=A0;break; case j:fre=B0;break; case q:fre=C1;break; case w:fre=D1;break;
13、case e:fre=E1;break; case r:fre=F1;break; case t:fre=G1;break; case y:fre=A1;break; case u:fre=B1;break; case 1:fre=C2;break; case 2:fre=D2;break; case 3:fre=E2;break; case 4:fre=F2;break; case 5:fre=G2;break; case 6:fre=A2;break; case 7:fre=B2;break; default:printf(You typed wrong!Do not push me ar
14、ound.:(n); if(fre) /*若频率值fre非0*/ outportb(0x43,0xb6); /*初始化2号定时器 */ fre=(unsigned)(1193180L/fre); /*计算计数初值 */ outportb(0x42,(char)fre); /*先写计数初值的低8位*/ outportb(0x42,(char)(fre8); /*再写计数初值的高8位*/ control=inportb(0x61); /*读0x61的状态*/ outportb(0x61,control|0x03); /*将第0位,第1位置1,使喇叭发声*/ goal=(clock_t)dur+cl
15、ock( ); /* clock()是计时函数,而与其相关的数据类型是clock_t,返回当前程序占用的CPU时间,用clock()返回的值除于CLOCK_PER_SEC常量可以得到当前程序的运行时间,单位是秒 */ while(goalclock( ); if(dur) outportb(0x61,control); while (ch!=/);1.4 程序运行结果及自评:1.5 程序调试中遇到过的问题及解决办法在编辑弹奏的程序时,没有弄明白延时函数,无法让按键发声,后来明白了延时函数的原理,把时长设成4,就可以了,还有就是按/时直接退出,后来把if放到前面,就解决了。题目二2.1 题目详细
16、描述: 开始2.2 程序设计思路及流程图:选择所绘制曲线图形下绘制背景及坐标 输出图形下标注每隔一定时间画两点间的坐标计算各点坐标值判断是否退出循环2.3 程序清单及注释:#include #include #include #include float data1(float k,float m);float data2(float k,float m);void drawpic1();void drawpic2();void drawpic3();void h1(float z,float x1,int m);void h2(float z,float x1,int m);int main
17、() int a; char c=y; while(c=y) /*重复选择*/ printf(G(jw)=1/(1-w2*T2+2jwTn); printf(Choose the Bode Diagram:n); printf(1.Magnitude(dB)n); printf(2.Phase(deg)n); printf(3.Bothn); scanf(%d,&a); /*选择画图*/ while(a!=1&a!=2&a!=3) /*试错*/ printf(Do not push me around.:(); scanf(%d,&a); if(a=1) drawpic1(); /*画第一个*
18、/ else if(a=2) drawpic2(); /*画第二个*/ else drawpic3(); /*画两个*/ getch(); closegraph(); clrscr(); getchar(); printf(Do you want to continue?(y/n)n); scanf(%c%*c,&c); return 0;float data1(float k,float m) /*计算第一个函数*/ float s,w; s=sqrt(1/(1-k*k)*(1-k*k)+4*m*m*k*k); w=20*log10(s); return w;float data2(floa
19、t k,float m) /*第二个*/ float s,w,a; s=-atan(2*m*k/(1-k*k); w=s*(180/3.1415926); if(w0) w=w-180; w*=0.8; return w;void drawpic1() int driver=DETECT,mode,x00,x11,y00,y11,m,y01,y02,y03; float x1,x2,xstep,ystep,k; char stt10,str=0.1-blue 0.3-green 0.5-indigo 0.7-red 0.9-violet; double value; initgraph(&dr
20、iver,&mode, ); setbkcolor(9);setcolor(63);x00=100,y00=60,x11=x00+512,y11=y00+150; setlinestyle(0,1,1);rectangle(x00,y00,x11,y11); /*矩形框*/m=y00+50;x1=x00; xstep=(x11-x00)/512; ystep=(y11-y00)/3; settextstyle(0,0,1); for(k=0;k=4;k+)/*横轴*/ value=0.01*pow(10,k); gcvt(value,3,stt); outtextxy(x00+xstep*12
21、8*k,y11+8,stt); for(k=0;k=3;k+)/*纵轴*/ value=50-50*k; gcvt(value,3,stt); outtextxy(x00-35,y00+ystep*k,stt); x00=100,y00=60,x11=x00+512,y01=y00+150; for(k=0;k=9;k+)/*优化坐标系,标注刻度*/ line(x00+log10(k+1)*(x11-x00)/4,y00,x00+log10(k+1)*(x11-x00)/4,y00+3); line(x00+log10(k+1)*(x11-x00)/4,y01,x00+log10(k+1)*(
22、x11-x00)/4,y01-3); line(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y00
23、+3); line(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y01-3); s
24、etcolor(1); /*画图*/ setlinestyle(0,1,3); h1(0.1,x1,m); setcolor(2); h1(0.3,x1,m); setcolor(3); h1(0.5,x1,m); setcolor(4); h1(0.7,x1,m); setcolor(5); h1(0.9,x1,m); setcolor(63); outtextxy(x00-70,y00-30,str);void drawpic2() int driver=DETECT,mode,x00,x11,y00,y11,m,y01,y02,y03; float x1,x2,xstep,ystep,k
25、; char stt10,str=0.1-blue 0.3-green 0.5-indigo 0.7-red 0.9-violet; double value; initgraph(&driver,&mode, ); setbkcolor(9);setcolor(63);x00=100,y00=60,x11=x00+512,y11=y00+150; setlinestyle(0,1,1);rectangle(x00,y00,x11,y11);m=y00;x1=x00; xstep=(x11-x00)/512; ystep=(y11-y00)/4; settextstyle(0,0,1); fo
26、r(k=0;k=4;k+)/*横轴*/ value=0.01*pow(10,k); gcvt(value,3,stt); outtextxy(x00+xstep*128*k,y11+8,stt); for(k=0;k=4;k+)/*纵轴*/ value=-50*k; gcvt(value,3,stt); outtextxy(x00-35,y00+ystep*k,stt); x00=100,y00=60,x11=x00+512,y01=y00+150; for(k=0;k=9;k+)/*优化坐标系,标注刻度*/ line(x00+log10(k+1)*(x11-x00)/4,y00,x00+lo
27、g10(k+1)*(x11-x00)/4,y00+3); line(x00+log10(k+1)*(x11-x00)/4,y01,x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00
28、)/4*2+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4*3+
29、x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y01-3); setcolor(1); /*画图*/ setlinestyle(0,1,3); h2(0.1,x1,m); setcolor(2); h2(0.3,x1,m); setcolor(3); h2(0.5,x1,m); setcolor(4); h2(0.7,x1,m); setcolor(5); h2(0.9,x1,m); setcolor(63); outtextxy(x00-70,y00-30,str);void drawpic3(
30、) int driver=DETECT,mode,x00,x11,y00,y11,m,y01,y02,y03; float x1,x2,xstep,ystep,k; char stt10,str=0.1-blue 0.3-green 0.5-indigo 0.7-red 0.9-violet; double value; initgraph(&driver,&mode, ); setbkcolor(9);setcolor(63);x00=100,y00=60,x11=x00+512,y11=y00+150; setlinestyle(0,1,1);rectangle(x00,y00,x11,y
31、11);m=y00+50;x1=x00; xstep=(x11-x00)/512; ystep=(y11-y00)/3; settextstyle(0,0,1); for(k=0;k=4;k+)/*横轴*/ value=0.01*pow(10,k); gcvt(value,3,stt); outtextxy(x00+xstep*128*k,y11+8,stt); for(k=0;k=3;k+)/*纵轴*/ value=50-50*k; gcvt(value,3,stt); outtextxy(x00-35,y00+ystep*k,stt); x00=100,y00=60,x11=x00+512
32、,y01=y00+150; for(k=0;k=9;k+)/*优化坐标系,标注刻度*/ line(x00+log10(k+1)*(x11-x00)/4,y00,x00+log10(k+1)*(x11-x00)/4,y00+3); line(x00+log10(k+1)*(x11-x00)/4,y01,x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4+x00+
33、log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4+x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y00,(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4*2+x00+log10(k+1)*(x11-x00)/4,y01-3); line(x11-x00)/4*3+x00+log10(k+
34、1)*(x11-x00)/4,y00,(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y00+3); line(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y01,(x11-x00)/4*3+x00+log10(k+1)*(x11-x00)/4,y01-3); setcolor(1); setlinestyle(0,1,3); h1(0.1,x1,m); setcolor(2); h1(0.3,x1,m); setcolor(3); h1(0.5,x1,m); setcolor(4); h1(0.7,x1,m); setco
35、lor(5); h1(0.9,x1,m); setcolor(63); x00=100,y00=y11+70,x11=x00+512,y11=y00+150; setlinestyle(0,1,1);rectangle(x00,y00,x11,y11);m=y00;x1=x00; xstep=(x11-x00)/512; ystep=(y11-y00)/4; settextstyle(0,0,1); for(k=0;k=4;k+)/*横轴*/ value=0.01*pow(10,k); gcvt(value,3,stt); outtextxy(x00+xstep*128*k,y11+8,stt); for(k=0;k=4;k+)/*纵轴*/ value=-50*k; gcvt(value,3,stt); outtextxy(x00-35,y00+ystep*k,stt); x00=100,y00=60,x11=x00+512,y01=y00+150; y02=y01+70,y03=y02+150; for(k=0;k=9;k+)/*优化坐标系,标注刻度*/ line(x00+log10(k+1)*(x11-x00)/4,y02,x00+log10(k+1)*(x11-x00)/4,y02+3); line(x00+l
限制150内