atoi函数的使用实例:【Ubuntu环境】

main.c:

 #include <stdio.h>
#include <stdlib.h>
extern int factorial(int f); //external function:如果写extern是显式的外部声明;不写也对,只是隐式的而已 int main(int argc, char ** argv)
{
int t;
if(argc < )
{
printf("The format of the input: %s number\n", argv[]);
return -;
}
else
{
t = atoi(argv[]);
printf("%d! is %d.\n", t, factorial(t));
}
return ;
}

factorial.c:

 #include <stdio.h>

 int factorial(int f)
{
if(f <= )
return ;
else
return factorial(f - ) * f;
}

编译运行:

Compile:
lxw@lxw-Aspire-4736Z:~/lxw0109/C++$ gcc -o factorial main.c factorial.c
Execute:
lxw@lxw-Aspire-4736Z:~/lxw0109/C++$ ./factorial 4
4! is 24.
lxw@lxw-Aspire-4736Z:~/lxw0109/C++$ ./factorial 5
5! is 120.

附:atoi函数的一种实现:

 int myAtoi(const char* str)
{
int sign = ,num = ;
assert(NULL != str);
while (*str == ' ')
{
str++;
}
if ('-' == *str)
{
sign = ;
str++;
}
while ((*str >= '') && (*str <= ''))
{
num = num * + (*str - ''); //就是这一行,将对应字符转化为数字
str++;
}
if(sign == )
return -num;
else
return num;
}

atoi函数的一种实现的更多相关文章

  1. (查找函数+atoi)判断与(注册函数+strcmp函数)判断两种方法

    loadrunner中接口判断的2中方法    如下: 1. ●查找函数web_reg_find() ● atoi():将字符串转换为整型值 作比较  > 0 Action() { //检查点函 ...

  2. JavaScript 函数的两种声明方式

    1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...

  3. JavaScript函数的4种调用方法详解

    在JavaScript中,函数是一等公民,函数在JavaScript中是一个数据类型,而非像C#或其他描述性语言那样仅仅作为一个模块来使用.函数有四种调用模式,分别是:函数调用形式.方法调用形式.构造 ...

  4. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  5. C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...

  6. swap函数的四种写法

    swap 函数的四种写法 (1)经典型 --- 嫁衣法 void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } ( ...

  7. 题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用

    #include<stdio.h> #include<stdlib.h> int sw(char *a){ ,c=; while(a[i]){ ') c=c*+a[i]-'; ...

  8. atoi()函数的实现

    atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...

  9. C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏

    C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...

随机推荐

  1. Linux操作系统下/etc/hosts文件配置方法

    1.关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) hosts文件是Linux系统中一个负 ...

  2. LeetCode459. Repeated Substring Pattern

    Description Given a non-empty string check if it can be constructed by taking a substring of it and ...

  3. linux上安装python3同时保留python2

    linux上安装python3同时保留python2?这个就要用到上篇说到的path变量了. 具体介绍及操作 这里我下载python3.6版本来进行介绍 django默认数据库为sqlite3,所以安 ...

  4. Win从环境变量开启MySQL之旅

    Win通过环境变量开启MySQL之旅 这篇文章主要介绍了Windows7下如何在命令行使用MySQL的相关资料,需要的朋友可以参考下 我在Win7下安装的MySQL版本是mysql-5.0.22-wi ...

  5. Hibernate命名查询

    hibernate命名的查询是通过一些有意义的名称来使用查询的方式.就类似于使用别名一样. Hibernate框架提供命名查询的概念,以便应用程序员不需要将查询分散到所有的java代码,进一步提高代码 ...

  6. unity3d面试题与参考答案

    1.C#程序题 1 2 3 4 5 6 7 8 9 10 11 private static void aaa(int x) { x = 10; }   private static void bbb ...

  7. point-position2修改版

    说明: 在共面直线测试中,由于计算误差等原因,共面条件判断不准,但计算结果依然正确. // point-position2.cpp : 定义控制台应用程序的入口点. #include "st ...

  8. ios - UILabel_长按复制

    1.添加长按的手势 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWith ...

  9. Laravel5.1 Middleware中间件(初级)

    中间件?什么鬼? 大家第一次接触这个词都会有这么个疑问,但它其实没那么神秘. 一句话就可以解释它:过滤HTTP请求专用机制. 为什么要使用中间件? 过滤HTTP请求是可以写在别的地方,比如说控制器中 ...

  10. std::condition_variable(3)复习

    #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...