⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.c

📁 一个C语言写的读入位置跟踪器数据的源程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    }

    return(intanswer);                          /* return the integer */
}

/*
    getfloatstring      -   Get float string

    Prototype in:       menu.h

    Parameters Passed:  floatstringbuf, pointer to buffer for the string

    Return Value:       floatstringbuf, if user inputs data OK
                        NULL ptr, if user selects ESC

    Remarks:            the routine allows the user to input numbers 0-9,
                        '.','-','+' and terminate with a CR.  If invalid
                        keys are depressed the BEL sound is echoed.
*/
char * getfloatstring(floatstringbuf)
char * floatstringbuf;
{
    char chr;
    char * floatbufptr = floatstringbuf;

    /*
        Get a string from the console
    */
    while (TRUE)
    {
        fflush(stdout);
        chr = getkey();

        /*
            Escape
        */
        if (chr == ESC)
            return(NULL);                   /* return with escape selected */

        /*
            Numbers
        */
        if ((chr >= 0x30) && (chr <= 0x39))
        {
            putchar(chr);                         /* echo character */
            *floatbufptr++ = chr;               /* store character */
            continue;
        }

        /*
            Backspace
        */
        if (chr == BS)
        {
            if (floatbufptr > floatstringbuf)       /* characters to runover? */
            {
                putchar(BS);                      /* backspace */
                putchar(SP);                      /* put up a space */
                putchar(BS);                      /* backspace again */
                *--floatbufptr = 0;             /* clear previous character */
                continue;
            }
            else
            {
                putchar(BEL);
                continue;
            }
        }

        /*
            Float Point Characters
        */
        if ((chr == '.') || (chr == '+') || ( chr == '-'))
        {
            *floatbufptr++ = chr;               /* store character */
            putchar(chr);                         /* echo */
            continue;
        }

        /*
            Carrage Return
        */
        if (chr == CR)
        {
            *floatbufptr = 0;                /* terminate string */
            if (strlen(floatstringbuf) == 0)    /* if they CR on nothing */
            {
                putchar(BEL);                     /* beepem */
                continue;                       /* ...just continue */
            }
            putchar(chr);                         /* echo the carriage ret */
            putchar(LF);                          /* send a line feed */
            break;
        }

        /*
            Garbage
        */
        putchar(BEL);                             /* beep */
    }

    return(floatstringbuf);                     /* return the integer */
}

/*
    getushort           -   Get Unsigned Short Number with Text Prompt

    Prototype in:       menu.h

    Parameters Passed:  void

    Return Value:       short  answ -   integer corresponding to the number
                                        input by the user or ESC_SEL if the
                                        user hit the ESC key

    Remarks:
*/
short getushort(prompt,min,max)
char * prompt;
short min;
short max;
{
    unsigned short invalid;
    short answ;

    do
    {
        invalid = FALSE;

        printf("\n\r%s ",prompt);
        answ = getnumber();
        if (answ == ESC_SEL)
            return(ESC_SEL);
        if ((answ < min) || (answ > max))
        {
            invalid = TRUE;
            printf("\n\r** ERROR ** illegal entry, please try again\n\r");
        }
    }
    while(invalid);

    return(answ);
}

/*
    getfloat            -   Get float Number with Text Prompt

    Prototype in:       menu.h

    Parameters Passed:  void

    Return Value:       double answ -  float corresponding to the number
                                       input by the user or ESC_SEL if the
                                       user hit the ESC key

    Remarks:
*/
double getfloat(prompt,min,max)
char * prompt;
double min;
double max;
{
    unsigned short invalid;
    double answ;
    char floatbuf[80];

    do
    {
        invalid = FALSE;

        printf("\n\r%s ",prompt);
        answ = atof(getfloatstring(floatbuf));
        if (answ == ESC_SEL)
            return(ESC_SEL);
        if ((answ < min) || (answ > max))
        {
            invalid = TRUE;
            printf("\n\r** ERROR ** illegal entry, please try again\n\r");
        }
    }
    while(invalid);

    return(answ);
}


/*
    hitkeycontinue      - Hit Key to Continue

    Prototype in:       menu.h

    Parameters Passed:  void

    Return Value:       void

    Remarks:            The User is prompted to hit any key and when a key
                        is hit control is returned to the calling routine.
*/
void hitkeycontinue()
{

    printf("\n\r....Hit Any Key to Continue\n\r");
    fflush(stdout);
    while (!ckkbhit());
    clearkey();
}

/*
    initconsole         - Initialize the Console

    Prototype in:       menu.h

    Parameters Passed:  void

    Return Value:       TRUE if initialized, else FALSE

    Remarks:            Routine only has an effect in non-UNIX environments

*/
int initconsole()
{
#ifdef UNIX
#ifdef UNIX_SGTTY
    /*
        Get the old terminal config and store it
    */
    if (ioctl(fileno(stdin),TIOCGETP,&oldstdin_sgttyb) >= 0)
    {
        /*
            Set the terminal to RAW mode (no ECHO and no CHR translation)
        */
        stdin_sgttyb.sg_flags = RAW;
        if (ioctl(fileno(stdin),TIOCSETP,&stdin_sgttyb) >= 0)
            return(TRUE);
    }
#endif
#ifdef UNIX_TERMIO
    /*
        Get the old terminal config and store it
    */
    if (ioctl(fileno(stdin),TCGETA,&oldstdin_termio) >= 0)
    {
        /*
            Set the terminal to RAW mode (no ECHO and no CHR translation)
        */
        stdin_termio.c_iflag = XOFF;
        stdin_termio.c_oflag = 0;
        stdin_termio.c_cflag = oldstdin_termio.c_cflag;
        stdin_termio.c_lflag = 0;
        stdin_termio.c_cc[VMIN] = 1;
        stdin_termio.c_cc[VTIME] = 2;
        if (ioctl(fileno(stdin),TCSETA,&stdin_termio) >= 0)
            return(TRUE);
    }
#endif

    printf("** ERROR ** could not set the Terminal to RAW mode\n\r");
    hitkeycontinue();

    /*
        Return indicating that the Terminal is NOT Configured
    */
    return(FALSE);

#else
    return(TRUE);
#endif

}


/*
    restoreconsole      - Restore the Console to its initial state

    Prototype in:       menu.h

    Parameters Passed:  void

    Return Value:       void

    Remarks:            Routine only has an effect in non-UNIX environments

*/
void restoreconsole()
{
#ifdef UNIX_SGTTY
    /*
        Restore the old terminal configuration
    */
    ioctl(fileno(stdin),TIOCSETP,&oldstdin_sgttyb);
    printf("\n\r");
#endif

#ifdef UNIX_TERMIO
    /*
        Restore the old terminal configuration
    */
    ioctl(fileno(stdin),TCSETA,&oldstdin_termio);
    printf("\n\r");
#endif
}



/*
    getfilename         Get a Filename from the User

    Prototype in:       menu.h

    Parameters Passed:  char * filenamebuf - pointer to the file name buffer

    Return Value:       char * filenamebuf if OK
                        NULL otherwise

    Remarks:

*/
char * getfilename(filenamebuf)
char * filenamebuf;
{
    char chr;
    char tempfilenamebuf[80];
    char * tempfilenamebufptr = tempfilenamebuf;

    strcpy (tempfilenamebuf,filenamebuf);

    printf("\n\r\n\rCurrent data file name is <%s>\n\r",tempfilenamebuf);
    printf("\n\rEnter the New Filename with PATH: ");

    /*
        Get a string from the console
    */
    while (TRUE)
    {
        fflush(stdout);
        chr = getkey();

        /*
            Escape
        */
        if (chr == ESC)
            return(filenamebuf);                   /* return with escape selected */

        /*
            Backspace
        */
        if (chr == BS)
        {
            if (tempfilenamebufptr > tempfilenamebuf)       /* characters to runover? */
            {
                putchar(BS);                      /* backspace */
                putchar(SP);                      /* put up a space */
                putchar(BS);                      /* backspace again */
                *--tempfilenamebufptr = 0;               /* clear previous character */
                continue;
            }
            else
            {
                putchar(BEL);
                continue;
            }
        }

        /*
            Carrage Return
        */
        if (chr == CR)
        {
            *tempfilenamebufptr = 0;             /* terminate string */
            if (strlen(tempfilenamebuf) == 0)    /* if they CR on nothing */
            {
                putchar(BEL);                    /* beepem */
                continue;                        /* ...just continue */
            }
            putchar(chr);                        /* echo the carriage ret */
            putchar(LF);                         /* send a line feed */
            break;
        }

        /*
            Numbers
        */
        putchar(chr);                         /* echo character */
        *tempfilenamebufptr++ = chr;               /* store character */
        continue;
    }

    /*
        Copy the Temp to the original
    */
    strcpy (filenamebuf,tempfilenamebuf);

    return(filenamebuf);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -