Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何以句点(.)结束程序_C - Fatal编程技术网

C 如何以句点(.)结束程序

C 如何以句点(.)结束程序,c,C,用户输入不同的字符:大小写字母和数字。输入以句点(.)结束。然后程序应计算并打印所有输入数字的总和,并忽略所有其他内容。程序不会以句点(.)结束 #包括 int main(){ int s=0,a; 字符n; printf(“Unesite znakove:”); 对于(;;){ scanf(“%c”和“&n”); 如果(n!='。){ a=(int)n; if(a>47&&astdin通常是行缓冲的 在输入'\n'之前,代码看不到任何输入。输入“123.”是不够的。代码需要最后输入:'\n'(

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

#包括
int main(){
int s=0,a;
字符n;
printf(“Unesite znakove:”);
对于(;;){
scanf(“%c”和“&n”);
如果(n!='。){
a=(int)n;

if(a>47&&a
stdin
通常是行缓冲的


在输入'\n'之前,代码看不到任何输入。输入“123.”是不够的。代码需要最后输入:'\n'(或关闭stdin)。

stdin以“煮熟”模式输入,这意味着在EOF或用户输入之前,程序实际上看不到任何内容

如果您希望数据可供程序使用,则需要在“原始”模式下逐个击键

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

注意:从以下位置刷取的代码:

#包括
#包括
#包括
#包括
#包括
结构termios-oldtermios;
int ttyraw(int fd)
{
/*按如下方式设置终端模式:
非常规模式-关闭或打开。
关闭信号生成(ISIG)
包括中断字符(BRKINT)。
关闭任何可能的输入预处理(IEXTEN)。
关闭回声模式。
禁用输入上的CR到NL映射。
禁用输入奇偶校验检测(INPCK)。
禁用输入(ISTRIP)上第八位的剥离PTCSETATTR(fd、TCSAFLUSH和newtermios)。
禁用流量控制(IXON)。
使用八位字符(CS8)。
禁用奇偶校验(PARENB)。
禁用任何依赖于实现的输出处理(OPOST)。
一次输入一个字节(最小值=1,时间=0)。
*/
新终端结构;
如果(tcgetattr(fd和oldtermios)<0)
返回(-1);
memcpy(newtermios、oldtermios、sizeof(struct termios));
newtermios.c|lflag&=~(ECHO | ICANON | IEXTEN | ISIG);
/*好的,为什么要使用IEXTEN?如果启用IEXTEN,则丢弃字符
已识别,但未传递给进程。此
字符导致输出暂停,直到另一个
接收到丢弃。作业控制的DSUSP字符,
删除任何特殊含义的下一个字符
以下字符、重印字符和一些
其他也属于这一类。
*/
newtermios.c|u iflag&=~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*如果输入字符的奇偶校验错误,则INPCK
选中。如果设置了此标志,则选中IGNPAR
查看是否应忽略具有奇偶校验错误的输入字节。
如果它不应该被忽略,那么PARMRK决定什么
进程将实际看到的字符序列。
当我们关闭IXON时,可以读取开始和停止字符。
*/
newtermios.c|cflag&=~(CSIZE | PARENB);
/*CSIZE是一个掩码,用于确定每个字节的位数。
PARENB启用输入奇偶校验和奇偶校验生成
关于输出。
*/
newtermios.c|u cflag |=CS8;
/*每个字符设置8位*/
newtermios.c_of lag&=~(OPOST);
/*这包括将选项卡扩展到空间*/
newtermios.c_cc[VMIN]=1;
newtermios.c_cc[vtcsettr(fd、TCSAFLUSH和newtermios)时间]=0;
/*你告诉我为什么会这样*/
if(tcsetattr(fd、TCSAFLUSH和newtermios)<0)
返回(-1);
返回(0);
}
int ttyreset(int fd)
{
if(tcsetattr(fd、TCSAFLUSH和oldtermios)<0)
返回(-1);
返回(0);
}
无效信号捕获(int sig)
{
ttyreset(0);
出口(0);
}
void main()
{
int i;
字符c;
/*捕捉最流行的信号*/
if((int)信号(SIGINT,sigcatch)<0)
{
佩罗(“信号”);
出口(1);
}
if((int)信号(SIGQUIT,sigcatch)<0)
{
佩罗(“信号”);
出口(1);
}
if((int)信号(SIGTERM,sigcatch)<0)
{
佩罗(“信号”);
出口(1);
}
/*在标准输入上设置原始模式*/
if(ttyraw(0)<0)
{
fprintf(stderr,“无法进入原始模式。\n”);
出口(1);
}
while((i=read(0,&c,1))==1)
{
如果((c&=255)==0177)/*ASCII删除*/
打破
printf(“%o\n\r”,c);
}
if(ttyreset(0)<0)
{
fprintf(stderr,“无法重置终端!\n”);
出口(-1);
}tcsetattr(fd、TCSAFLUSH和newtermios)
if(i<0)
{
fprintf(stderr,“读取错误”。\n”);
出口(-1);
}
出口(0);
}
主要标准是声明一个
struct termios
,使用
tcgetattr(fd,&oldtermios)
获取当前终端设置(为了安全起见,现在复制原始终端设置)将
oldtermios
复制到
newtermios
并修改所需模式的
newtermios
设置,然后使用
tcsetattr(fd、TCSAFLUSH和newtermios)
将终端修改为“原始”模式

完成后,确保调用
tcsetattr(fd、TCSAFLUSH和oldtermios);
将终端模式返回到“已烹调”


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

您可能应该在这里使用
while
循环,在这里使用
for
循环。这对我很好。我得到了这个输出。
Unesite znakove:123456.Suma je:21
。您得到的o/p是多少。@Pawan您在
之后按过任何键吗?
#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;
}
#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);
}