如何写自己ISNUMBER()函数? [英] How to write own isnumber() function?

查看:150
本文介绍了如何写自己ISNUMBER()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C和我在想怎么写这个函数自己。我从命令行参数,因此存储argv数组中,我要决定是否或不是数字。什么是做到这一点的最简单的方法?

感谢您

 的#include<&stdio.h中GT;INT ISNUMBER(INT *参数)
{
    如果(*参数大于0&放大器;&放大器; *参数&所述; 128)
        返回1;
    返回0;
}INT主(INT ARGC,CHAR *的argv [])
{
    如果(的argc == 2)
        ISNUMBER(ARGV [1]);
    其他的printf(没有足够多的参数。);    返回0;
}


解决方案

阅读有关的与strtol(3)。你可以使用它作为

 布尔ISNUMBER(为const char * S){
   字符* E = NULL;
   (空)与strtol(S,急症,0);
   返回E = NULL&放大器;!&安培; * E ==(字符)0;
}

但不是非常有效的(例如,用于与数字的百万的字符串),因为无用的转换将作出

但事实上,你经常关心那个数字的值,所以你会叫与strtol 程序中的参数处理(的argv的参数)和关心的结果与strtol 是的实际值号。

您使用的事实,与strtol 可以更新(透过其第三个参数)一个指向解析字符串中的数字的结束。如果该结束指针不成为转换某种程度上未能字符串的结尾。

例如

  INT主(INT ARGC,字符** argv的){
   长NUM = 0;
   字符* ENDP = NULL;
   如果(的argc 2)
     {fprintf中(标准错误,丢失的程序参数\\ n);
       出口(EXIT_FAILURE); };
   NUM =与strtol(的argv [1],ENDP);
   如果(ENDP == NULL || * ENDP!=(char)的0)
     {fprintf中(标准错误,程序参数%s是差多少\\ n,ARGV [1]);
       出口(EXIT_FAILURE); };
   如果(NUM℃,|| NUM> = 128)
     {fprintf中(标准错误,数量%LD超出范围\\ n,NUM);
       出口(EXIT_FAILURE); };
   do_something_with_number(NUM);
   出口(EXIT_SUCCESS);
 }

I'm new to C and I'm thinking how to write this function myself. I take a parameter from command line, so it is stored in argv array and I want to decide whether it is or isn't number. What is the easiest way to do this?

Thank you

#include <stdio.h>

int isNumber(int *param)
{   
    if (*param > 0 && *param < 128)
        return 1;
    return 0;
} 

int main(int argc, char *argv[])
{
    if (argc == 2)
        isNumber(argv[1]);
    else printf("Not enought parameters.");

    return 0;
}

解决方案

Read about strtol(3). You could use it as

bool isnumber(const char*s) {
   char* e = NULL;
   (void) strtol(s, &e, 0);
   return e != NULL && *e == (char)0;
}

but that is not very efficient (e.g. for a string with a million of digits) since the useless conversion will be made.

But in fact, you often care about the value of that number, so you would call strtol in your program argument processing (of argv argument to main) and care about the result of strtol that is the actual value of the number.

You use the fact that strtol can update (thru its third argument) a pointer to the end of the number in the parsed string. If that end pointer does not become the end of the string the conversion somehow failed.

E.g.

int main (int argc, char**argv) {
   long num = 0;
   char* endp = NULL;
   if (argc < 2) 
     { fprintf(stderr, "missing program argument\n");
       exit (EXIT_FAILURE); }; 
   num = strtol (argv[1], endp);
   if (endp == NULL || *endp != (char)0)
     { fprintf(stderr, "program argument %s is bad number\n", argv[1]);
       exit (EXIT_FAILURE); }; 
   if (num<0 || num>=128)
     { fprintf(stderr, "number %ld is out of bounds.\n", num);
       exit(EXIT_FAILURE); };
   do_something_with_number (num);
   exit (EXIT_SUCCESS);
 } 

这篇关于如何写自己ISNUMBER()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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