2015-11-04 65 views
1

用戶輸入不同的字符:大寫和小寫字母和數字。輸入以句點(。)結尾然後程序應計算並打印所有輸入數字的總和,並忽略其他所有內容。程序不會以句號(。)結尾如何結束帶句點(。)的程序

#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; 
} 
+0

你或許應該在這裏使用一個'while'循環,和'for'循環。 – itoctopus

+0

它對我很好。我得到了這個輸出。 'Unesite znakove:123456. Suma je:21'。什麼是你得到的o/p。 – Pawan

+0

@Pawan你有沒有按下任何鍵,比如d在''.''之後? – chux

回答

2

stdin通常是行緩衝。

代碼未看到任何輸入,直到輸入'\n'。輸入"123."是不夠的。代碼需要最後的輸入'\n'(或關閉stdin)。

0

標準輸入以'熟化'模式輸入,這意味着程序實際上沒有任何可見的內容,直到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)獲取當前終端設置,(爲了安全,現在做的副本原始終端設置)將oldtermios複製到newtermios並修改所需模式的newtermios設置,然後使用tcsetattr(fd, TCSAFLUSH, &newtermios)將終端修改爲「原始」模式。

完成後,一定要打電話tcsetattr(fd, TCSAFLUSH, &oldtermios);返回到「熟」終端模式

注:signal()是過時的/不可靠,更換sigaction()