C语言播放器
发布网友
发布时间:2024-10-07 04:45
我来回答
共1个回答
热心网友
时间:2024-11-01 17:22
声明一下,纯属抄袭,并非原创,善用百度
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
void main()
{
void set(int *set1,int *set2,char *f,char *n,int *t,int rate,int num);
void music(int *set1,int *set2,int num);
int flag=0;
FILE *f1;
int t;
int rate;
char in[3];
int i;
int n;
char *frequency;
char *note;
int *time;
int *set1;
int *set2;
int menu;
int FileName[30];
while(1)
{
f1=NULL;
flag=0;
frequency=NULL;
note=NULL;
time=NULL;
set1=NULL;
set2=NULL;
i=n=0;
printf("1 播放程序自带的音乐music1\n");
printf("2 播放程序自带的音乐music2\n");
printf("3 通过输入文件名进行音乐的播放\n");
printf("4 退出\n");
while(1)
{
scanf("%d",&menu);
if(menu==1)
{
strcpy(FileName,"music1.txt");
break;
}
if(menu==2)
{
strcpy(FileName,"music2.txt");
break;
}
if(menu==3)
{
scanf("%s",FileName);
break;
}
if(menu==4)
exit(1);
}
printf("\n该文件的音乐编码如下:\n");
/*-------------------------------------*/
if((f1=fopen(FileName,"r"))==NULL)
{
printf("不能打开文件!\n");
exit(1);
}
/*-------------------------------------*/
fscanf(f1,"%d",&rate);
frequency=(char *)malloc(sizeof(char));
note=(char *)malloc(sizeof(char));
time=(int *)malloc(sizeof(int));
while(!feof(f1)&&flag!=1)
{
fscanf(f1,"%s%d",in,&t);
if(t!=-1)
{
printf("%s %d ",in,t);
frequency[i]=in[0];
note[i]=in[1];
time[i]=t;
i++;
n++;
frequency=(char *)realloc(frequency,(i+1)*sizeof(char));
note=(char *)realloc(note,(i+1)*sizeof(char));
time=(int *)realloc(time,(i+1)*sizeof(int));
}
else
flag=1;
}
fclose(f1);
for(i=0;i<n;i++)
{
switch(frequency[i])
{
case 'h':
switch(note[i])
{
case '1':
note[i]=523;
time[i]=time[i]*rate;
break;
case '2':
note[i]=587;
time[i]=time[i]*rate;
break;
case '3':
note[i]=659;
time[i]=time[i]*rate;
break;
case '4':
note[i]=698;
time[i]=time[i]*rate;
break;
case '5':
note[i]=784;
time[i]=time[i]*rate;
break;
case '6':
note[i]=880;
time[i]=time[i]*rate;
break;
case '7':
note[i]=988;
time[i]=time[i]*rate;
break;
}
break;
case 'm':
switch(note[i])
{
case '1':
note[i]=262;
time[i]=time[i]*rate;
break;
case '2':
note[i]=296;
time[i]=time[i]*rate;
break;
case '3':
note[i]=330;
time[i]=time[i]*rate;
break;
case '4':
note[i]=349;
time[i]=time[i]*rate;
break;
case '5':
note[i]=392;
time[i]=time[i]*rate;
break;
case '6':
note[i]=440;
time[i]=time[i]*rate;
break;
case '7':
note[i]=494;
time[i]=time[i]*rate;
break;
}
break;
case 'l':
switch(note[i])
{
case '1':
note[i]=131;
time[i]=time[i]*rate;
break;
case '2':
note[i]=147;
time[i]=time[i]*rate;
break;
case '3':
note[i]=165;
time[i]=time[i]*rate;
break;
case '4':
note[i]=175;
time[i]=time[i]*rate;
break;
case '5':
note[i]=196;
time[i]=time[i]*rate;
break;
case '6':
note[i]=220;
time[i]=time[i]*rate;
break;
case '7':
note[i]=247;
time[i]=time[i]*rate;
break;
}
break;
}
for(i=0;i<n;i++)
{
sound(note[i]);
delay(time[i]);
nosound();
}
free(frequency);
free(note);
free(time);
free(set1);
free(set2);
putchar('\n');
}
}
}