zoukankan      html  css  js  c++  java
  • 几个常用输入输出格式说明

    1.    scanf()   这个函数有返回值,如果输入成功则返回输入的元素的个数
           例如:  scanf("%s %d", str,&d); 
                      如果输入成功返回2,否则不是2
    2.    strcmp() 这个函数用于字符比较,他的机理是将两个字符串按位相减
             如果结果为0则继续处理下一个字符,否则返回当前结果
             这样一直计算到非零差或两个串同时结束的地方的
               如:     str1 = "I love YOU";
                          str2 = "I love HER";
                           结果返回    'Y'-'H'
    3.    和strcmp()类似, memcmp()也是同样的机理,用法参看msdn
    4.    gets()   和   scanf("%s", ...) 的区别
                 gets()   在从文件输入的时候(stdin也看成文件),
                             读入以'\n'为结尾的字符串,并吃掉'\n'
                而scanf("%s",...)  则读入以空格为端点的串(空格不读入,也不吃掉)
                     例如:scanf("%s",str);  gets(str);
                             从终端输入aaaaaaaaaa[cr]
                             那么上述两条语句结束
                         str的内容先是aaaaaaaaaa
                                 后为'\n'   (好像是13)
    5.    一些格式简单的输入输出不提倡用cin cout
               因为他们的速度要比scanf printf 慢,在作时间比较紧的题目的时候比较郁闷

    6.   为了对付不同的编译器和操作系统,long long 的输出要用cout
                当然如果ojs允许的话  printf("%lld",...)      可以满足要求。
                一般架在linux系统上的编译器都支持, unsigned long long 使用 "%llu"
    7.   cin  cout 和 scanf printf 尽量不要混用,在VC中可能通不过编译
    8.   cout 和 printf也不要混用,cout首先输入一个buffer,然后再输出,而printf直接就输出了。
    9.   gets的返回值是输入字符串的指针,当无法从文件中读入字符串的时候返回NULL
    10-a.   有的题目(很少)告诉你它会给你一行数据,这些数据满足某些关系
            但是没有告诉这些数据的具体数目,此时可以使用strtok这个函数。
                  首先开一个字符数组buffer(其他名字也可以) 
                  然后用gets函数从标准输入读入一行字符
                  strtok函数的函数原形是 char * strtok(char *s1,const char * s2); 
                  s1表示要从那个字符串中找到一个token(token可以理解为一个语句块)     
                  s2表示以那些字母为语句块的分隔符。 
                  这个函数返回string s1的第一个token 
                  当s1 ==NULL 的时候表示接着上次找到的那个token的后面继续找另一个token
                   如果找不到token了,返回NULL
               举个例子:     #define MAXL someNumber 
                                  char buffer[MAXL], *ps; 
                                  gets(buffer);
                                  for(ps=strtok(buffer," \t");ps;ps=strtok(NULL," \t"))
                                   { //do sth. }

    10-b  如果分隔符为 “space”,除了使用strtok这个函数。 可以使用istringstream 流来控制

    11. 有的题目有前导空格,而这不是我们需要的
             那么可以这样
             scanf(" %...."); //只要在前面加个空格,那么它就会将输入流中的
              空格回车tab都吃掉,然后再读入,同理对于尾空格也适用
    ===========================================================

    /***
    *strtok.c - tokenize a string with given delimiters
    *
    *        Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
    *
    *Purpose:
    *        defines strtok() - breaks string into series of token
    *        via repeated calls.
    *
    *******************************************************************************/

    #include <cruntime.h>
    #include <string.h>
    #ifdef _MT
    #include <mtdll.h>
    #endif   /* _MT */

    /***
    *char *strtok(string, control) - tokenize string with delimiter in control
    *
    *Purpose:
    *        strtok considers the string to consist of a sequence of zero or more
    *        text tokens separated by spans of one or more control chars. the first
    *        call, with string specified, returns a pointer to the first char of the
    *        first token, and will write a null char into string immediately
    *        following the returned token. subsequent calls with zero for the first
    *        argument (string) will work thru the string until no tokens remain. the
    *        control string may be different from call to call. when no tokens remain
    *        in string a NULL pointer is returned. remember the control chars with a
    *        bit map, one bit per ascii char. the null char is always a control char.
    *
    *Entry:
    *        char *string - string to tokenize, or NULL to get next token
    *        char *control - string of characters to use as delimiters
    *
    *Exit:
    *        returns pointer to first token in string, or if string
    *        was NULL, to next token
    *        returns NULL when no more tokens remain.
    *
    *Uses:
    *
    *Exceptions:
    *
    *******************************************************************************/

    char * __cdecl strtok (char * string,const char * control)
    {
             unsigned char *str;
             const unsigned char *ctrl = control;

             unsigned char map[32];
             int count;

    #ifdef _MT
             _ptiddata ptd = _getptd();
    #else   /* _MT */
             static char *nextoken;    // tricky
    #endif   /* _MT */

             /* Clear control map */
             for (count = 0; count < 32; count++)
                     map[count] = 0;

             /* Set bits in delimiter table */
             do {
                     map[*ctrl >> 3] |= (1 << (*ctrl & 7));
             } while (*ctrl++);

             /* Initialize str. If string is NULL, set str to the saved
              * pointer (i.e., continue breaking tokens out of the string
              * from the last strtok call) */
             if (string)
                     str = string;
             else
    #ifdef _MT
                     str = ptd->_token;
    #else   /* _MT */
                     str = nextoken;
    #endif   /* _MT */

             /* Find beginning of token (skip over leading delimiters). Note that
              * there is no token iff this loop sets str to point to the terminal
              * null (*str == '\0') */
             while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
                     str++;

             string = str;

             /* Find the end of the token. If it is not the end of the string,
              * put a null there. */
             for ( ; *str ; str++ )
                     if ( map[*str >> 3] & (1 << (*str & 7)) ) {
                             *str++ = '\0';
                             break;
                     }

             /* Update nextoken (or the corresponding field in the per-thread data
              * structure */
    #ifdef _MT
             ptd->_token = str;
    #else   /* _MT */
             nextoken = str;
    #endif   /* _MT */

             /* Determine if a token has been found. */
             if ( string == str )
                     return NULL;
             else
                     return string;
    }
    用法参见:http://hi.baidu.com/patrolsun/blog/item/22c4c7588830f781800a18b2.html

  • 相关阅读:
    vue五十:Vue美团项目之商家详情-查看商品详情
    vue四十九:Vue美团项目之商家详情-tabbar状态切换和导航返回
    vue四十八:Vue美团项目之商家详情-左右联动之商品分类跟随商品列表滚动
    vue四十七:Vue美团项目之商家详情-左右联动之选中商品分类跳转到对应商品列表
    vue四十六:Vue美团项目之商家详情-商品滚动完成
    vue四十五:Vue美团项目之商家详情-商品分类滚动完成
    vue四十四:Vue美团项目之商家详情-导航栏和header布局
    vue四十三:Vue美团项目之首页-tabbar搭建
    vue四十二:Vue美团项目之首页-商家列表
    Ugly Number
  • 原文地址:https://www.cnblogs.com/yaozhongxiao/p/1491667.html
Copyright © 2011-2022 走看看