scanf()的唯一标志和编号 [英] scanf() only sign and number

查看:82
本文介绍了scanf()的唯一标志和编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要得到变量签署的和的的使用scanf()的。结果
有应该如何工作的:

 输入:
 + 10
 输出:
 OK,SIGN = +和数= 10

 

 输入:
 +10
 输出:
 失败!

 

 输入:
 10
 输出:
 失败!

 

 输入:
 一个
 输出:
 失败!

我试过这个解决方案,但它不为我工作。特别是对于输入: +10 的和的

 加='+';
 减去=' - '; 如果(scanf函数(%C%D,&安培;标志,和放大器;数)= 2 ||((SIGN =负)及!及(SIGN =加))||数量和LT;!0)
                {
                的printf(失败!);
                }
            否则{...}

感谢。


解决方案

scanf函数(%C%D,&安培;签署和放大器;数字)!= 2 做无法正常工作的格式不对的需要的的字符 INT 之间的空间。 A 0匹配或更多的白色空间,而不是一个单一的


所以,code需要寻找的签署空格的和的数量

 字符标志[2];
INT编号;
如果(scanf函数(%1 [+ - ] *%1 []%D标志,和放大器;!数)= 2){
  看跌期权(失败);
}

扫描并跳过可选的空白结果
%1 [+ - ]扫描并保存1 +或 - 结果
%* 1 []扫描并不会节省空间。结果
%D扫描白色空间,那么 INT


请注意:最好使用与fgets(),读行,然后用的sscanf()


更强大的解决方案 - 它使用与fgets()作为强大的解决方案不使用 scanf()的

 字符BUF [80];
  如果(与fgets(buf中,sizeof的BUF,标准输入)== NULL){
    卖出期权(EOF);
  }其他{
    INT N = 0;
    sscanf的(BUF,%* 1 [+ - ] *%1 []%* [0-9]%N,&安培; N);
    如果(N == 0){
      看跌期权(故障 - 转换不完整);
    }否则如果(BUF [N]!='\\ 0'){
      看跌期权(故障 - 额外的垃圾);
    }其他{
      字符标志;
      INT编号;
      sscanf的(BUF,%C%D,&安培;标志,&安培;号码);
      的printf(成功%C%d个\\ N,符号,数字);
    }
  }

%N保存扫描的字符的个数。

提示:追加%N INT N = 0; ... sscanf的(......%N 来的格式的尽头是一个简单的窍门1)测试,如果扫描是不完整的如果(N == 0) 2)测试尾随非空格如果(BUF [N]!='\\ 0')

注:没有检查溢出

I want to get variables sign and number with scanf().
There is how it should works:

 input:
 + 10
 output:
 OK, "sign = +" and "number = 10"

 

 input:
 +10
 output:
 Fail!

 

 input:
 10
 output:
 Fail!

 

 input:
 a
 output:
 Fail!

I've tried this solution, but it doesn't worked for me. Especially for inputs: +10 and a

 plus='+';
 minus='-';     

 if ( scanf(" %c %d", &sign, &number) != 2 || ((sign != minus) && (sign != plus)) || number < 0 ) 
                {
                printf("Fail!");
                } 
            else {...}

Thanks.

解决方案

scanf(" %c %d", &sign &number) != 2 does not work as the format does not require a space between the char and int. A " " matches 0 or more white-space, not a single ' '.


So code needs to look for sign, space and number.

char sign[2];
int number;
if (scanf(" %1[+-]%*1[ ]%d", sign, &number) != 2) {
  puts("Fail");
}

" " Scan and skip optional white-space
"%1[+-]" Scan and save 1 + or -
"%*1[ ]" Scan and do not save a space.
"%d" Scan white-spaces and then an int.


Note: Better to use fgets(), read the line and then use sscanf().


[Edit] More robust solution - it uses fgets() as robust solutions do not use scanf().

  char buf[80];
  if (fgets(buf, sizeof buf, stdin) == NULL) {
    puts("EOF");
  } else {
    int n = 0;
    sscanf(buf," %*1[+-]%*1[ ]%*[0-9] %n", &n);
    if (n == 0) {
      puts("Fail - conversion incomplete");
    } else if (buf[n] != '\0') {
      puts("Fail - Extra garbage");
    } else {
      char sign;
      int number;
      sscanf(buf," %c%d", &sign, &number);
      printf("Success %c %d\n",sign, number);
    }
  }

"%n" Saves the count of characters scanned.

Tip: Appending %n" to int n = 0; ... sscanf(..., "... %n" to the end of a format is an easy trick to 1) test if scanning was incomplete if (n == 0) and 2) test for trailing non-white-space if (buf[n] != '\0')

Note: No checks for overflow.

这篇关于scanf()的唯一标志和编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆