Guest User

Untitled

a guest
Jan 11th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /*struct music
  6. {
  7.     char note[2];
  8.     int freq;
  9. };*/
  10.  
  11.  
  12. int findfrequency(char * note);
  13. void playMusic(char * music);
  14.  
  15.  
  16.  
  17. void main()
  18. {
  19.    
  20.     /*struct music array[15];*/
  21.    
  22.    
  23.    
  24.    
  25.     /*char * notes[15];
  26.     int freq[15];
  27.    
  28.    
  29.     notes[0]="D";
  30.     notes[1]="E";
  31.     notes[2]="F";
  32.     notes[3]="G";
  33.     notes[4]="A";
  34.     notes[5]="B";
  35.     notes[6]="c";
  36.     notes[7]="d";
  37.     notes[8]="e";
  38.     notes[9]="f";
  39.     notes[10]="g";
  40.     notes[11]="a";
  41.     notes[12]="b";
  42.     notes[13]="c'";
  43.     notes[14]="d'";
  44.    
  45.     freq[0]=294;
  46.     freq[1]=330;
  47.     freq[2]=370;
  48.     freq[3]=392;
  49.     freq[4]=440;
  50.     freq[5]=494;
  51.     freq[6]=554;
  52.     freq[7]=587;
  53.     freq[8]=659;
  54.     freq[9]=740;
  55.     freq[10]=784;
  56.     freq[11]=880;
  57.     freq[12]=988;
  58.     freq[13]=1109;
  59.     freq[14]=1175;
  60.    
  61.     int curFreq;
  62.     char * curNote;
  63.    
  64.     printf("Enter note: ");
  65.     scanf("%s", curNote);
  66.    
  67.     curFreq= findfrequency(curNote);
  68.    
  69.     if(curFreq==1)
  70.         printf("Invalid Note\n");
  71.    
  72.     else
  73.         printf("\n The frequency of that note is %d", curFreq);
  74.     */
  75.    
  76.     char * music1 = "DEFGABcd";
  77.     char * music2 = "D2E2F3GABcd";
  78.     char * music3 = "defgabc'd'";
  79.     printf("%s\n", music1);
  80.     playMusic(music1);
  81.     printf("%s\n", music2);
  82.     playMusic(music2);
  83.     printf("%s\n", music3);
  84.     playMusic(music3); 
  85.     getchar();
  86.  
  87.    
  88.    
  89.     getchar();
  90. }//end main
  91.  
  92.  
  93. int findfrequency(char *note) //part 2
  94. {
  95.     printf("%s", note);
  96.    
  97.     int freq[15];
  98.     int i;
  99.     char *notes[15];
  100.     freq[0]=294;
  101.     freq[1]=330;
  102.     freq[2]=370;
  103.     freq[3]=392;
  104.     freq[4]=440;
  105.     freq[5]=494;
  106.     freq[6]=554;
  107.     freq[7]=587;
  108.     freq[8]=659;
  109.     freq[9]=740;
  110.     freq[10]=784;
  111.     freq[11]=880;
  112.     freq[12]=988;
  113.     freq[13]=1109;
  114.     freq[14]=1175;
  115.    
  116.     notes[0]="D";
  117.     notes[1]="E";
  118.     notes[2]="F";
  119.     notes[3]="G";
  120.     notes[4]="A";
  121.     notes[5]="B";
  122.     notes[6]="c";
  123.     notes[7]="d";
  124.     notes[8]="e";
  125.     notes[9]="f";
  126.     notes[10]="g";
  127.     notes[11]="a";
  128.     notes[12]="b";
  129.     notes[13]="c'";
  130.     notes[14]="d'";
  131.    
  132.     for(i=0;i<15;i++)
  133.     {
  134.         int match;
  135.        
  136.         match= strcmp(note, notes[i]);
  137.        
  138.         if(match==0)
  139.         {
  140.             return freq[i];
  141.         }
  142.     }
  143.     return 1;
  144.    
  145. }//end find freq
  146.  
  147.  
  148. void playMusic(char * music) //part 3
  149. {
  150.     int freq;
  151.     int i=0;
  152.     int len = 0;
  153.     int j=0;
  154.     int compare;
  155.    
  156.    
  157.     char *nums[9];  //array of the numbers, i.e the duration
  158.     nums[0]="1";
  159.     nums[1]="2";
  160.     nums[2]="3";
  161.     nums[3]="4";
  162.     nums[4]="5";
  163.     nums[5]="6";
  164.     nums[6]="7";
  165.     nums[7]="8";
  166.     nums[8]="9";
  167.    
  168.  
  169.         while(i!=NULL)
  170.         {
  171.             while(j!=NULL)
  172.             {
  173.                 compare=strcmp(music+i, nums[j]);  //compare current music note or duration with the number array
  174.                
  175.                 if(compare==0)
  176.                 {
  177.                     printf("\t%s", nums[j]);  //if its a number then print that number
  178.                 }//end if
  179.                
  180.                 else
  181.                 {
  182.                     freq=findfrequency(music+i);    //otherwise call findfrequency() and print that
  183.                     printf("%d", freq);
  184.                 }//end else
  185.                
  186.                 j++;
  187.             }//end inner fo
  188.             i++;
  189.         }//end for
  190. }
Add Comment
Please, Sign In to add comment