#include<stdio.h> #include<dos.h> #define N1 32 #define N2 16 #define N4 8 #define N8 4 #define N16 2 #define END 0 enum NOTES { C10=131,D10=147,E10=165,F10=175,G10=196,A10=220,B10=247, C0=262,D0=286,E0=330,F0=349,G0=440,A0=440,B0=494, C1=523,D1=158,E1=659,F1=698,G1=784,A1=880,B1=998, C2=1047,D2=1175,E2=1319,F2=1397,G2=1568,A2=1760,B2=1976 } song1[]={ G0,N16,G0,N8,A0,N8,G0,N8,C1,N8,B0,N4,G0,N16,G0, N8,A0,N8,G0,N8,D1,N8,C1,N4,G0,N16,G0,N8,G1,N8,E1, N8,C1,N8,B0,N16,B0,N8,A0,N4,F1,N16,F1,N8,E1,N8,C1,N8,D1,N8,C1,N4, END,END }; song2[]={ C0,N8,D0,N8,E0,N8,F0,N8,G0,N8,A0,N8,B0,N8, C1,N8,D1,N8,E1,N8,F1,N8,G1,N8,A1,N8,B1,N8,END,END}; song[]= { C0,N8,C0,N8,G0,N8,G0,N8,A0,N8,A0,N8,G0,N4, F0,N8,F0,N8,E0,N8,E0,N8,D0,N8,D0,N8,C0,N4, G0,N8,G0,N8,F0,N4,E0,N8,E0,N8,D0,N4, G0,N8,G0,N8,F0,N8,F0,N8,E0,N8,E0,N8,D0,N4, C0,N8,E0,N8,G0,N4,A0,N8,C1,N8,G0,N4, F0,N8,F0,N8,E0,N8,E0,D0,N8,D0,N8,C0,N4,END,END};
void ss1() { int note=0,fre,dur,control; clock_t goal; while(song1[note]!=0) { fre=song1[note]; dur=song1[note+1]; if(kbhit()) break; if(fre) { outpurtb(0x43,0xb6); fre=(unsigned)(1193180L/fre); outportb(0x42,(char)(fre>>8)); outportb(0x42,(char)(fre>>8)); control=inportb(0x61); outportb(0x61,(control)|0x3); } goal=(clock_t)dur+clock(); while(goal>clock()); if(fre) outportb(0x61,control); goal=(clock_t)0; note=note+2; } }
void ss2() { int note=0,fre,dur,control; clock-t goal; while(song2[note]!=0) { fre=song2[note]; dur=song2[note+1]; if(kbhit()) break; if(fre) { outpurtb(0x43,0xb6); fre=(unsigned)(1193180L/fre); outportb(0x42,(char)(fre>>8)); outportb(0x42,(char)(fre>>8)); control=inportb(0x61); outportb(0x61,(control)|0x3); } goal=(clock-t)dur+clock(); while(goal>clock()); if(fre) outportb(0x61,control); goal=(clock-t)0; note=note+2; } } void ss() { int note=0,fre,dur,control; clock-t goal; while(song[note]!=0) { fre=song[note]; dur=song[note+1]; if(kbhit()) break; if(fre) { outpurtb(0x43,0xb6); fre=(unsigned)(1193180L/fre); outportb(0x42,(char)(fre>>8)); outportb(0x42,(char)(fre>>8)); control=inportb(0x61); outportb(0x61,(control)|0x3); } goal=(clock-t)dur+clock(); while(goal>clock()); if(fre) outportb(0x61,control); goal=(clock-t)0; note=note+2; } } main() { int n; printf("Enter input number(1-3):"); scanf("%d",&n); swith(n); { case 1:ss1(); break; case 2:ss2(); break; case 3:ss(); break; default:printf("input error \n"); } } 不过有错误!!