函数

作者: MinoyJet | 来源:发表于2017-04-27 17:22 被阅读0次

函数

1. 函数间可以互相调用,但是 main 函数是不能被其他函数调用的

2. 对于一个频繁使用的短小函数,在 C 和 C++ 中分别应用什么实现?

对于一个频繁使用的短小函数,在 C 中应用宏定义,在 C++ 中用内联函数。

3. 按照固定格式输出当前系统时间

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    time_t Time; /*定义 Time 为 time_t 类型*/
    struct tm *t; /*定义指针 t 为 tm 结构类型*/
    Time = time(NULL); /*将 time 函数返回值存到 Time 中*/
    t = localtime(&Time); /*调用 localtime 函数*/
    printf("Local time is: %s", asctime(t));/*调用 asctime 函数,以固定格式输出当前时间*/
    return 0;
}

4. 利用库函数实现字符匹配

输入:一个字符串和想要查找的字符
输出:该字符在字符串的位置和该字符开始后余下的字符串


#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char string[100]; 
    cin.getline(string, 100);  //最多输入 100 个字符 
    char ch;
    cin >> ch;
    char *str;
    str = strchr(string, ch);
    if (str)  //判断返回的指针是否为空 
        cout << str - string << endl << str << endl;
    else 
        cout << "no found" << endl;
    return 0; 
}

5. 利用库函数将字符串转换成整型

函数名:atol
包含在:<cstdlib>
函数原型:long atol(const char *nptr);

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    long n;
    char *str = "123456789";
    n = atol(str);
    cout << n << endl;
    return 0; 
}

6. 利用库函数实现小数分离

函数名:modf
包含在:<cmath>
函数原型:double modf(double num, double *i);

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double n;
    cin >> n;
    double num, i;  
    num = modf(n, &i);
    cout << n << " = " << num << " + " << i << endl;
    return 0; 
}

相关文章

  • Excel(三)

    AND函数 OR函数 NOT函数 IF函数 频率分析函数FREQUENCY

  • if、else if、for、while、repeat函数

    ①if函数 ②else if函数 ③for函数 ④while函数 ⑤repeat函数

  • strsplit、mapply、paste、match函数

    strsplit函数 mapply函数 strsplit函数 mapply函数 paste函数 match函数 第...

  • Oracle中常用函数(SQL)

    Oracle函授有以下几个分类:数字函数、字符函数、日期函数、转换函数、集合函数、分析函数 数字函数: 字符函数:...

  • MySQL函数

    字符函数 数字运算函数 比较运算符和函数 日期时间函数 信息函数 聚合函数 加密函数 流程函数

  • BI-SQL丨AND & OR & IN

    AND函数 & OR函数 & IN函数 AND函数、OR函数和IN函数都可以理解是WHERE函数的补充,当然也可以...

  • Python之函数

    课程大纲 函数定义 函数的参数 函数的返回值 高阶函数 函数作用域 递归函数 匿名函数 内置函数 函数式编程 将函...

  • 函数基本知识

    函数 函数的定义: def 函数名() 函数的调用:函数名() #不能将函数调用放在函数定义上方 函数的文档注...

  • 积分表——不定期更新

    基本初等函数包括: 常函数: 幂函数 指数函数 对数函数 三角函数 反三角函数 I、反函数Ⅱ、复合函数:初等函数(...

  • MySQL基本使用

    函数 常用函数 数学函数 字符串函数 日期函数

网友评论

      本文标题:函数

      本文链接:https://www.haomeiwen.com/subject/rmfnzttx.html