C:使用引用调用交换键盘音符

标签 c

我需要制作一个C程序,为用户提供通过PC键盘播放歌曲和创建歌曲的功能。我的老师对我下面的代码及其产生的输出非常满意,但他说我仍然需要演示知识指针使用和通过引用调用函数,以便通过主题。

我的计划是创建一个可以交换选项 2(即数字键盘)中的音符的功能。例如,Q 通常代表 C 音符,W 代表升 C (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;
}

最佳答案

I still need to demonstrate knowledge pointer use and function call by reference.

C 没有引用调用。想必你的老师希望你演示通过传递指向数据的指针来向函数提供数据,但如果他真的称之为“按引用传递”,那么他就很草率了。 C 提供按值传递指针——具有类似的效果,但并不完全相同。

My plan is to create a function that can swap the musical notes in Option 2 (i.e Digital keyboard).

这看起来有点奇怪,而且可能很棘手。请允许我提出一个替代方案:创建一个播放歌曲的函数,并通过指向其中一个(或两个)数组的指针接收要播放的音符。将第二首歌曲添加到程序的轨道中(例如“Mary Had a Little Lamb”),以便您可以使用相同的函数根据该函数的参数来播放您选择的这些歌曲。

此类函数的原型(prototype)可能如下所示:

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);

关于C:使用引用调用交换键盘音符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39250263/

相关文章:

c - 如何将链表拆分为两个列表

C++类成员函数转c函数

c - 为什么我的 Win32 应用程序没有启动?

python - 对 recvbuf 进行操作的 MPI_Sendrecv?

c - 如何在我的程序中使用终端命令的输出?

c - packetdrill 测试是否可移植?

c - sarq 和 shrq 的区别

c - 为什么 C 中的 double 是 8 字节对齐的?

c - Sqlite:为查询结果创建新表并保留原始模式

C ABI : is changing a void function to return an int a breaking change?