如何用句点(。)结束程序

时间:2015-11-04 04:06:35

标签: c

用户输入不同的字符:大写和小写字母和数字。输入以句点(。)结束然后程序应计算并打印所有输入数字的总和,并忽略其他所有数字。程序不会以句点(。)结束

#include <stdio.h>

int main() {
   int s=0, a;
   char n; 

   printf("Unesite znakove: ");
   for ( ; ; ) {
      scanf ("%c", &n);

      if (n!='.') {
         a=(int)n;

         if (a>47 && a<58) {
            a=a-48;
            s+=a;
         }
         else continue;
      }
      else {
         break;
      }
   }

   printf ("\nSuma je: %d", s);

   return 0;
}

2 个答案:

答案 0 :(得分:2)

stdin通常是行缓冲的。

在输入'\n'之前,代码未看到任何输入。输入"123."是不够的。代码需要最终的 Enter '\n'(或stdin的关闭)。

答案 1 :(得分:0)

stdin正在'cooked'模式下输入,这意味着程序实际上看不到任何内容,直到EOF或用户输入为止。

如果您希望数据可供程序使用,按键击键,则终端需要处于“原始”模式

以下代码演示了如何将终端设置为“原始”模式

注意:从http://www.minek.com/files/unix_examples/raw.html

刷新代码
#include <stdio.h>
#include <signal.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>

struct termios oldtermios;

int ttyraw(int fd)
{
    /* Set terminal mode as follows:
       Noncanonical mode - turn off ICANON.
       Turn off signal-generation (ISIG)
        including BREAK character (BRKINT).
       Turn off any possible preprocessing of input (IEXTEN).
       Turn ECHO mode off.
       Disable CR-to-NL mapping on input.
       Disable input parity detection (INPCK).
       Disable stripptcsetattr(fd, TCSAFLUSH, &newtermios)ing of eighth bit on input (ISTRIP).
       Disable flow control (IXON).
       Use eight bit characters (CS8).
       Disable parity checking (PARENB).
       Disable any implementation-dependent output processing (OPOST).
       One byte at a time input (MIN=1, TIME=0).
    */
    struct termios newtermios;
    if(tcgetattr(fd, &oldtermios) < 0)
        return(-1);
    memcpy( newtermios, oldtermios, sizeof(struct termios) );

    newtermios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
    /* OK, why IEXTEN? If IEXTEN is on, the DISCARD character
       is recognized and is not passed to the process. This
       character causes output to be suspended until another
       DISCARD is received. The DSUSP character for job control,
       the LNEXT character that removes any special meaning of
       the following character, the REPRINT character, and some
       others are also in this category.
    */

    newtermios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
    /* If an input character arrives with the wrong parity, then INPCK
       is checked. If this flag is set, then IGNPAR is checked
       to see if input bytes with parity errors should be ignored.
       If it shouldn't be ignored, then PARMRK determines what
       character sequence the process will actually see.

       When we turn off IXON, the start and stop characters can be read.
    */

    newtermios.c_cflag &= ~(CSIZE | PARENB);
    /* CSIZE is a mask that determines the number of bits per byte.
       PARENB enables parity checking on input and parity generation
       on output.
    */

    newtermios.c_cflag |= CS8;
    /* Set 8 bits per character. */

    newtermios.c_oflag &= ~(OPOST);
    /* This includes things like expanding tabs to spaces. */

    newtermios.c_cc[VMIN] = 1;
    newtermios.c_cc[Vtcsetattr(fd, TCSAFLUSH, &newtermios)TIME] = 0;

    /* You tell me why TCSAFLUSH. */
    if(tcsetattr(fd, TCSAFLUSH, &newtermios) < 0)
        return(-1);
    return(0);
}


int ttyreset(int fd)
{
    if(tcsetattr(fd, TCSAFLUSH, &oldtermios) < 0)
        return(-1);

    return(0);
}

void sigcatch(int sig)
{
    ttyreset(0);
    exit(0);
}

void main()
{
    int i;
    char c;

    /* Catch the most popular signals. */
    if((int) signal(SIGINT,sigcatch) < 0)
    {
        perror("signal");
        exit(1);
    }
    if((int)signal(SIGQUIT,sigcatch) < 0)
    {
        perror("signal");
        exit(1);
    }
    if((int) signal(SIGTERM,sigcatch) < 0)
    {
        perror("signal");
        exit(1);
    }

    /* Set raw mode on stdin. */
    if(ttyraw(0) < 0)
    {
        fprintf(stderr,"Can't go to raw mode.\n");
        exit(1);
    }

    while( (i = read(0, &c, 1)) == 1)
    {
        if( (c &= 255) == 0177) /* ASCII DELETE */
            break;
        printf( "%o\n\r", c);
    }

    if(ttyreset(0) < 0)
    {
        fprintf(stderr, "Cannot reset terminal!\n");
        exit(-1);
    }tcsetattr(fd, TCSAFLUSH, &newtermios)

    if( i < 0)
    {
        fprintf(stderr,"Read error.\n");
        exit(-1);
    }

    exit(0);
}

主要标准是声明struct termios,使用tcgetattr(fd, &oldtermios)获取当前终端设置,(为安全起见,现在制作原始终端设置的副本。)复制oldtermiosnewtermios并修改所需模式的newtermios设置,然后使用tcsetattr(fd, TCSAFLUSH, &newtermios)将终端修改为“原始”模式。

完成后,请务必致电tcsetattr(fd, TCSAFLUSH, &oldtermios);将终端模式恢复为'cooked'

注意:signal()已过时/不可靠,请替换为sigaction()