我需要制作一个C程序,让用户可以通过PC键盘播放歌曲和创作歌曲。我的老师对下面的代码以及从中产生的输出感到满意,但他说我还需要展示知识指针使用和函数调用引用以便传递主题。
我的计划是创建一个可以交换选项2中音符的功能(即数字键盘)。例如,Q通常表示C音符,W表示C sharp(C#)音符,但我希望用户能够在两个字母之间切换,使Q代表C#音符,W代表C音符。
所以问题是,从下面的代码开始,我如何为用户提供交换选项2中的音符,以及如何为此目的使用引用来调用?我认为我在这个编码中使用的选择结构(即switch..break)不适合,如果我想继续使用引用调用,但我实际上不知道如何开始。
#include <stdio.h>
#include <windows.h> /* The windows.h library define the beep() and sleep() functions. */
int main() /* The function main */
{
char ch;
do /* The loop do..while is used so that the program continuously prompt the user to make a choice. */
{
printf("\nMain Menu\n");
printf("_________\n\n");
printf("1- Play 'Twinkle Twinkle Little Star'.\n");
printf("2- Digital keyboard.\n");
printf("3- Exit this program.\n\n");
printf("Your choice >>> ");
scanf(" %c", &ch);
if (ch =='1') { /* The user is given an arithmetic choice from 1 until 3. */
printf("\nNow playing 'Twinkle Twinkle Little Star'");
fflush(stdout);
Beep (261.63,600);Beep (261.63,600);Beep (392.00,600);Beep (392.00,600);Beep (440.00,600);Beep (440.00,600);Beep (392.00,600);
Sleep(800);
Beep (349.23,600);Beep (349.23,600);Beep (329.63,600);Beep (329.63,600);Beep (293.66,600);Beep (293.66,600);Beep (261.63,600);
Sleep(800);
Beep (392.00,600);Beep (392.00,600);Beep (349.23,600);Beep (349.23,600);Beep (329.63,600);Beep (329.63,600);Beep (293.66,600);
Sleep(800);
Beep (392.00,600);Beep (392.00,600);Beep (349.23,600);Beep (349.23,600);Beep (329.63,600);Beep (329.63,600);Beep (293.66,600);
Sleep(800);
Beep (261.63,600);Beep (261.63,600);Beep (392.00,600);Beep (392.00,600);Beep (440.00,600);Beep (440.00,600);Beep (392.00,600);
Sleep(800);
Beep (349.23,600);Beep (349.23,600);Beep (329.63,600);Beep (329.63,600);Beep (293.66,600);Beep (293.66,600);Beep (261.63,600);
printf("\nChoose 1 to play again.");
}
else if (ch =='2') {
char m, filename[100]; /* The array declaration of variable that holds the name of the user's file. */
printf("\nThis is a digital keyboard.\n\n");
printf("Please enter any UPPERCASE letters and press 'Enter' to play some notes.\n");
printf("Please don't enter any other characters besides the representations.\n");
printf("Using other characters would make no sound.\n\n");
printf("Use number '1' to delay the notes for 0.5 seconds.\n");
printf("To return to main menu, please enter number '0'\n");
printf("Using numbers other than '0' and '1' would make no sound.\n\n");
printf("The notes you have played also will be saved in a file.\n");
printf("Please enter your file name:\n\n");
fflush(stdin);
gets(filename);
FILE *thefile;
thefile = fopen(filename,"w");
if (thefile == NULL)
{
printf("File fail to open\n");
}
printf("\nRepresentation:\n\n");
printf("Q -> C | W -> C# | E -> D | R -> D# | T -> E | Y -> F \n\n");
printf("U -> F# | I -> G | O -> G# | P -> A | A -> A# | S -> B \n\n");
printf("D -> C1 | F -> C1# | G -> D1 | H -> D1# | J -> E1 | K -> F1 \n\n");
printf("L -> F1# | Z -> G1 | X -> G1# | C -> A1 | V -> A1# | B -> B1 \n\n");
printf("\t\t\t N -> C2 | M -> C2# \n\n");
/* I plan to make the swapping at here */
do
{
scanf("%c",&m);
switch(m)
{
case 'Q' : Beep (261.63,600);break;
case 'W' : Beep (277.18,600);break;
case 'E' : Beep (293.66,600);break;
case 'R' : Beep (311.13,600);break;
case 'T' : Beep (329.63,600);break;
case 'Y' : Beep (349.23,600);break;
case 'U' : Beep (369.99,600);break;
case 'I' : Beep (392.00,600);break;
case 'O' : Beep (415.30,600);break;
case 'P' : Beep (440.00,600);break;
case 'A' : Beep (466.16,600);break;
case 'S' : Beep (493.88,600);break;
case 'D' : Beep (523.25,600);break;
case 'F' : Beep (554.37,600);break;
case 'G' : Beep (587.33,600);break;
case 'H' : Beep (622.25,600);break;
case 'J' : Beep (659.25,600);break;
case 'K' : Beep (698.46,600);break;
case 'L' : Beep (739.99,600);break;
case 'Z' : Beep (783.99,600);break;
case 'X' : Beep (830.61,600);break;
case 'C' : Beep (880.00,600);break;
case 'V' : Beep (932.33,600);break;
case 'B' : Beep (987.77,600);break;
case 'N' : Beep (1046.50,600);break;
case 'M' : Beep (1108.73,600);break;
case '1' : Sleep(500);break;
}
fputc(m, thefile); /* This will put all the character values that entered by the user into a file. */
}
while(m!='0'); /* Users can continue inserting characters until they insert zero. */
fclose(thefile);
}
else if (ch =='3')
break;
else
printf("Invalid character.\n"); /* This will prevent the user from entering irregular input. */
}
while(ch != '3'); /* A sentinel value of 3 allow the user to quit the program. */
return 0;
}
答案 0 :(得分:0)
我仍然需要通过引用来演示知识指针的使用和函数调用。
C没有按引用调用。据推测,您的老师希望您通过传递指向该数据的指针来演示向函数提供数据,但如果他真的称之为&#34;通过引用传递&#34;然后他很草率。 C提供了按值传递指针 - 具有类似的效果,但它并不完全相同。
我的计划是创建一个可以交换选项2中音符的功能(即数字键盘)。
这看起来既有点奇怪又可能很棘手。请允许我建议一个替代方案:创建一个播放歌曲的功能,并通过指向其中一个(或两个)阵列的指针接收要播放的音符。将第二首歌曲添加到您节目的曲目中(例如&#34; Mary有一只小羊羔&#34;),这样您就可以使用相同的功能根据该功能播放您选择的歌曲&#39;论证。
这种功能的原型可能如下所示:
void play_song(float *pitch, int *duration, int num_notes);
你可以将它与音高和音符阵列结合起来,例如:
float twinkle_pitch[TWINKLE_NUM_NOTES] =
{ 261.63, 261.63, 392.00, 392.00, /* etc */ };
int twinkle_duration[TWINKLE_NUM_NOTES] =
{ 600, 600, 600, 600, /* etc */ };
/* and similar for one or more other songs */
您的主程序将通过调用该功能播放选定的歌曲,例如
play_song(twinkle_pitch, twinkle_duration, TWINKLE_NUM_NOTES);
答案 1 :(得分:0)
在C中,所有函数参数都是“按值传递”。这意味着被调用函数在临时变量而不是原始变量中被赋予其参数值。
通过引用实现调用的唯一方法是使用指针作为函数的参数。
示例:
#include <stdio.h>
void call_by_value_inc(int formal_argument)
{
++formal_argument;
return;
}
void call_by_reference_inc(int *formal_argument)
{
++(*formal_argument);
return;
}
int main()
{
int actual_argument = 1;
call_by_value_inc(actual_argument);
/*after invoke call_by_value_inc,the actual_argument's value still be 1*/
printf("actual_argument %d\n", actual_argument);
call_by_reference_inc(&actual_argument);
/*after invoke call_by_value_inc,the actual_argument's value will be 2*/
printf("actual_argument %d\n", actual_argument);
}